/*
 * Author: 黎永顺
 * name: 统计报表-分享记录
 * Description:
 * Date: 2023/9/19
 */
import React, { Component } from "react";
import { WeaLocaleProvider, WeaSlideModal, WeaTable } from "ecCom";
import { message, Modal } from "antd";
import { salaryStatisticsPushcancelAll, salaryStatisticsPushGetTable } from "../../../apis/statistics";
import SalaryStatisticsDetailShareLogDialog from "./salaryStatisticsDetailShareLogDialog";

const { getLabel } = WeaLocaleProvider;

class SalaryStatisticsDetailShareRecord extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false, dataSource: [], columns: [],
      pageInfo: { current: 1, pageSize: 10, total: 0 },
      shareLogDialog: {
        visible: false, id: ""
      }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.salaryStatisticsPushGetTable();
  }

  salaryStatisticsPushGetTable = () => {
    this.setState({ loading: true });
    salaryStatisticsPushGetTable({ ...this.state.pageInfo })
      .then(({ status, data }) => {
        this.setState({ loading: false });
        if (status) {
          const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
          this.setState({
            columns, dataSource,
            pageInfo: { ...this.state.pageInfo, current, pageSize, total }
          });
        }
      }).catch(() => this.setState({ loading: false }));
  };
  handleWithdrawAll = (id) => {
    Modal.confirm({
      title: getLabel(131329, "信息确认"),
      content: getLabel(111, "确定要全部撤回吗？"),
      onOk: () => {
        salaryStatisticsPushcancelAll({ id }).then(({ status, errormsg }) => {
          if (status) {
            message.success(getLabel(30700, "操作成功！"));
            this.salaryStatisticsPushGetTable();
          } else {
            message.error(errormsg);
          }
        });
      }
    });
  };

  render() {
    const { dataSource, columns, loading, pageInfo, shareLogDialog } = this.state;
    const pagination = {
      ...pageInfo,
      showTotal: total => `${getLabel(18609, "共")} ${total} ${getLabel(18256, "条")}`,
      showQuickJumper: true, showSizeChanger: true,
      pageSizeOptions: ["10", "20", "50", "100"],
      onShowSizeChange: (current, pageSize) => {
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize }
        }, () => {
          this.salaryStatisticsPushGetTable();
        });
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => {
          this.salaryStatisticsPushGetTable();
        });
      }
    };
    return (
      <WeaSlideModal
        {...this.props} className="shareRecordWrapper"
        direction="right" top={0} width={800} height={100}
        measureT="%" measureX="px" measureY="%"
        title={<div className="titleDialog">
          <div className="titleCol">
            <div className="titleLeftBox">
              <div className="titleIcon"><i className="icon-coms-fa"/></div>
              <div className="title">{getLabel(111, "分享记录")}</div>
            </div>
          </div>
          <div></div>
        </div>}
        content={(
          <WeaTable
            rowKey="id" dataSource={dataSource} pagination={pagination} loading={loading}
            columns={[
              ...columns,
              {
                dataIndex: "operate",
                width: 150,
                title: getLabel(30585, "操作"),
                render: (__, record) => (
                  <React.Fragment>
                    <a href="javascript:void(0);" style={{ marginRight: 10 }}
                       onClick={() => this.setState({
                         shareLogDialog: { visible: true, id: record.id }
                       })}>{getLabel(111, "查询详情")}</a>
                    <a href="javascript:void(0);"
                       onClick={() => this.handleWithdrawAll(record.id)}>{getLabel(111, "全部撤回")}</a>
                    <SalaryStatisticsDetailShareLogDialog
                      {...shareLogDialog}
                      onGetTable={() => this.salaryStatisticsPushGetTable()}
                      onCancel={() => this.setState({
                        shareLogDialog: { visible: false, id: "" }
                      })}
                    />
                  </React.Fragment>
                )
              }
            ]}
          />
        )}/>
    );
  }
}

export default SalaryStatisticsDetailShareRecord;
