/*
 * Author: 黎永顺
 * name: 个税申报-异常、失败详情
 * Description:
 * Date: 2023/8/18
 */
import React, { Component } from "react";
import { WeaDialog, WeaInputSearch, WeaLocaleProvider, WeaReqTop, WeaTable } from "ecCom";
import { Button, Col, Row } from "antd";
import {
  taxdeclarationEmployeeList4Fail,
  taxdeclarationEmployeeList4NotDeclare,
  taxdeclarationEmployeeList4NoValue
} from "../../../apis/declare";
import { convertToUrlString, getQueryString } from "../../../util/url";

const { getLabel } = WeaLocaleProvider;
const APIFox = {
  list4NotDeclare: taxdeclarationEmployeeList4NotDeclare,
  list4NoValue: taxdeclarationEmployeeList4NoValue,
  list4Fail: taxdeclarationEmployeeList4Fail
};

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

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { type } = nextProps;
      this.setState({
        selectedKey: _.head(type).key
      }, () => this.queryList());
    }
  }

  queryList = () => {
    const taxDeclareRecordId = getQueryString("id");
    const { keyword, pageInfo, selectedKey } = this.state;
    const payload = {
      ...pageInfo, keyword, taxDeclareRecordId
    };
    this.setState({ loading: true });
    APIFox[selectedKey](payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
        this.setState({
          dataSource, pageInfo: { ...pageInfo, current, pageSize, total }, columns
        });
      } else {

      }
    }).catch(() => this.setState({ loading: false }));
  };
  errorExport = () => {
    let exportUrl = "";
    const { selectedKey, keyword } = this.state;
    const payload = {
      taxDeclareRecordId: getQueryString("id"), keyword
    };
    if (selectedKey === "list4NotDeclare") {
      exportUrl = "/api/bs/hrmsalary/taxdeclaration/employee/export4NotDeclare";
    } else if (selectedKey === "list4NoValue") {
      exportUrl = "/api/bs/hrmsalary/taxdeclaration/employee/export4NoValue";
    }
    window.open(`${window.ecologyContentPath || ""}${exportUrl}?${convertToUrlString(payload)}`, "_blank");
  };
  failExport = () => {
    const payload = {
      taxDeclareRecordId: getQueryString("id")
    };
    window.open(`${window.ecologyContentPath || ""}/api/bs/hrmsalary/taxdeclaration/employee/export4Fail?${convertToUrlString(payload)}`, "_blank");
  };

  render() {
    const { type } = this.props;
    const { loading, columns, dataSource, pageInfo, selectedKey, keyword } = 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.queryList());
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.queryList());
      }
    };
    return (
      <WeaDialog
        {...this.props}
        scalable hasScroll className="declareResultDialog" initLoadCss
        title={
          type.length === 1 ?
            (<Row className="declareResultDialogTitle" type="flex">
              <Col span={12} className="declareResultDialogTitle-left">
                <span className="title">{this.props.title}</span>
              </Col>
              <Col span={12} className="declareResultDialogTitle-right">
                <Button type="primary" onClick={this.failExport}>{getLabel(17416, "导出")}</Button>
              </Col>
            </Row>) :
            <WeaReqTop
              title={this.props.title} buttonSpace={10} tabDatas={type} selectedKey={selectedKey}
              buttons={[
                <Button type="primary" onClick={this.errorExport}>{getLabel(17416, "导出")}</Button>
              ]}
              onChange={key => this.setState({ selectedKey: key }, () => this.queryList())}
            />
        }
        style={{
          width: 1150,
          height: 606.6,
          minHeight: 200,
          minWidth: 380,
          maxHeight: "90%",
          maxWidth: "90%",
          overflow: "hidden",
          transform: "translate(0px, 0px)"
        }}
      >
        <div className="declareResultDialogContent">
          {
            type.length !== 1 &&
            < div className="declareNoSearchBox">
              <WeaInputSearch
                style={{ width: 200 }} placeholder={getLabel(111, "请输入姓名/工号/身份证号")}
                value={keyword} onChange={val => this.setState({ keyword: val })}
                onSearch={this.queryList}
              />
            </div>
          }
          <WeaTable
            columns={columns} dataSource={dataSource}
            loading={loading} className="declareTable"
            pagination={pagination}
          />
        </div>
      </WeaDialog>
    );
  }
}

export default DeclareResultDialog;
