/*
 * Author: 黎永顺
 * name: 个税申报重构-列表
 * Description:
 * Date: 2023/10/12
 */
import React, { Component } from "react";
import { WeaLocaleProvider, WeaTable } from "ecCom";
import { Dropdown, Menu, message, Modal } from "antd";
import { getDeclareList, withDrawTaxDeclaration } from "../../../../apis/declare";
import { sysConfCodeRule, sysinfo } from "../../../../apis/ruleconfig";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentDidMount() {
    this.getDeclareList(this.props);
    this.sysConfCodeRule();
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.isRefresh !== this.props.isRefresh) this.getDeclareList(nextProps);
  }

  sysConfCodeRule = () => {
    sysConfCodeRule({ code: "WITHDRAW_TAX_DECLARATION" }).then(({ status, data }) => {
      if (status && data === "1") this.setState({ showWithDrawBtn: data === "1" });
    });
  };
  getDeclareList = async (props) => {
    const { data: sysData } = await sysinfo();
    const { pageInfo } = this.state, { queryParams } = props;
    const { dateRange, ...extra } = queryParams;
    const [fromSalaryMonth, endSalaryMonth] = dateRange || [];
    const params = { fromSalaryMonth: fromSalaryMonth + "-01", endSalaryMonth: endSalaryMonth + "-01", ...extra };
    const payload = { ...pageInfo, ...params };
    this.setState({ loading: true });
    getDeclareList(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        let { columns, list: dataSource, pageNum, pageSize, total } = data;
        sysData["TAX_DECLARATION_DATE_TYPE"] === "1" && (columns = _.filter(columns, o => o.dataIndex !== "salaryMonth"));
        this.setState({
          dataSource, pageInfo: { ...pageInfo, pageNum, pageSize, total },
          columns: _.map(columns, o => {
            const { dataIndex } = o;
            let width = "";
            switch (dataIndex) {
              case "taxAgentName":
              case "operateTime":
                width = "15%";
                break;
              case "description":
                width = "20%";
                break;
              default:
                width = "10%";
                break;
            }
            return { ...o, width };
          })
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };
  taxdeclarationDelete = (taxDeclarationId) => {
    withDrawTaxDeclaration({ taxDeclarationId }).then(({ status, errormsg }) => {
      if (status) {
        message.success(getLabel(505793, "撤回成功"));
        this.getDeclareList(this.props);
      } else {
        message.error(errormsg);
      }
    });
  };

  render() {
    const { loading, dataSource, columns, pageInfo, showWithDrawBtn } = 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.getDeclareList(this.props));
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.getDeclareList(this.props));
      }
    };
    return (
      <WeaTable
        rowKey="id"
        dataSource={dataSource} loading={loading}
        pagination={pagination} columns={[
        ...columns,
        {
          dataIndex: "operate", title: getLabel(30585, "操作"),
          width: 170, render: (__, record) => {
            const { id } = record;
            return <React.Fragment>
              <a
                href={`${window.ecologyContentPath || ""}/spa/hrmSalary/static/index.html#/main/hrmSalary/generateDeclarationDetail?id=${id}`}
                target="_blank"
              >
                {getLabel(83110, "查看详情")}
              </a>
              {
                !showWithDrawBtn && <a href="javascript:void(0)" style={{ marginLeft: 10 }}
                                       onClick={() => this.props.onFilterLog("log", record.id)}>{getLabel(545781, "操作日志")}</a>
              }
              {
                showWithDrawBtn &&
                <a
                  href="javascript:void(0);" style={{ marginLeft: 10 }}
                  onClick={() => {
                    Modal.confirm({
                      title: getLabel(131329, "信息确认"),
                      content: getLabel(543848, "确认撤回该条数据吗?"),
                      onOk: () => this.taxdeclarationDelete(id)
                    });
                  }}
                >
                  {getLabel(32025, "撤回")}
                </a>
              }
              {
                showWithDrawBtn && <Dropdown overlay={
                  <Menu>
                    <Menu.Item>
                      <a href="javascript:void(0)"
                         onClick={() => this.props.onFilterLog("log", record.id)}>{getLabel(545781, "操作日志")}</a>
                    </Menu.Item>
                  </Menu>
                }>
                  <a href="javascript:void(0)" style={{ marginLeft: 10 }}><i className="icon-coms-more"/></a>
                </Dropdown>
              }
            </React.Fragment>;
          }
        }
      ]}
      />
    );
  }
}

export default Index;
