/*
 * Author: 黎永顺
 * name: 个税申报-异常、失败详情
 * Description:
 * Date: 2023/8/18
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTable } from "ecCom";
import { Button, message } from "antd";
import { deptFillCondition, taxFillColumns, taxFillCondition } from "./constants";
import { getSearchs } from "../../../util";
import { saveDepartmentCodeAndCheck } from "../../../apis/taxAgent";

const { getLabel } = WeaLocaleProvider;

@inject("taxAgentStore")
@observer
class TaxFilingInfoDialofg extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedRowKeys: [], checkLoading: false
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (!_.isEmpty(nextProps.taxAgentTaxReturnCheckFormDTO) &&
      Object.prototype.toString.call(nextProps.taxAgentTaxReturnCheckFormDTO) === "[object Object]"
    ) {
      const { taxAgentTaxReturnCheckFormDTO, taxAgentStore: { taxfillInfoForm, deptfillInfoForm } } = nextProps;
      taxfillInfoForm.initFormFields(taxFillCondition);
      deptfillInfoForm.initFormFields(deptFillCondition);
      const fields = _.map(taxFillCondition[0].items, it => {
        return it.domkey[0];
      });
      fields.map(item => {
        taxfillInfoForm.updateFields({
          [item]: item !== "divideFiling" ? (taxAgentTaxReturnCheckFormDTO[item] || "") : (taxAgentTaxReturnCheckFormDTO[item] === "ON" ? getLabel(538048, "是") : getLabel(30587, "否"))
        });
      });
    }
    if (nextProps.visible !== this.props.visible && !nextProps.visible) {
      const { taxAgentStore: { setTaxfillInfoForm, initDeptfillInfoForm } } = nextProps;
      setTaxfillInfoForm();
      initDeptfillInfoForm();
    }
  }

  handleSaveOrKnow = () => {
    const { taxAgentStore: { taxfillInfoForm, deptfillInfoForm }, checkPayload } = this.props;
    if (taxfillInfoForm.getFormParams().divideFiling === getLabel(538048, "是")) {
      deptfillInfoForm.validateForm().then(f => {
        if (f.isValid) {
          this.setState({ checkLoading: true });
          saveDepartmentCodeAndCheck(_.omitBy({ ...checkPayload, ...deptfillInfoForm.getFormParams() }, val => _.isNil(val)))
            .then(({ status, errormsg }) => {
              this.setState({ checkLoading: false });
              if (status) {
                message.success(getLabel(30700, "操作成功！"));
                this.props.onCancel(true);
              } else {
                message.error(errormsg);
              }
            }).catch(() => this.setState({ checkLoading: false }));
        } else {
          f.showErrors();
        }
      });
    } else {
      this.props.onCancel();
    }
  };

  render() {
    const { selectedRowKeys, checkLoading } = this.state;
    const { taxAgentStore: { taxfillInfoForm, deptfillInfoForm }, taxAgentTaxReturnCheckFormDTO, loading } = this.props;
    const rowSelection = {
      type: "radio",
      selectedRowKeys,
      onChange: selectedRowKeys => this.setState({ selectedRowKeys })
    };
    return (
      <WeaDialog
        {...this.props}
        hasScroll className="taxfillingDialog" initLoadCss
        buttons={
          Object.prototype.toString.call(taxAgentTaxReturnCheckFormDTO) === "[object Object]" ?
            [
              <Button type="primary" onClick={this.handleSaveOrKnow} loading={checkLoading}>
                {taxfillInfoForm.getFormParams().divideFiling === getLabel(538048, "是") ? getLabel(537558, "保存") : getLabel(545147, "知道了")}
              </Button>
            ] : [
              <Button loading={loading} type="primary"
                      onClick={() => !_.isEmpty(selectedRowKeys) && this.props.onSubmit(selectedRowKeys[0])}>{getLabel(725, "提交")}</Button>
            ]
        }
        style={{
          width: 850,
          height: 480,
          minHeight: 200,
          minWidth: 380,
          maxHeight: "50%",
          maxWidth: "50%",
          overflow: "hidden",
          transform: "translate(0px, 0px)"
        }}
      >
        <div className="taxfillingDialogContent">
          {
            Object.prototype.toString.call(taxAgentTaxReturnCheckFormDTO) === "[object Object]" ?
              <React.Fragment>
                {getSearchs(taxfillInfoForm, taxFillCondition, 1)}
                {taxfillInfoForm.getFormParams().divideFiling === getLabel(538048, "是") && getSearchs(deptfillInfoForm, deptFillCondition, 1)}
              </React.Fragment> :
              <WeaTable
                rowKey="index" dataSource={taxAgentTaxReturnCheckFormDTO}
                columns={_.map(taxFillColumns, o => ({
                  dataIndex: o.dataIndex,
                  width: 200,
                  title: getLabel(o.titleId, o.title)
                }))}
                scroll={{ x: 1200 }} rowSelection={rowSelection}
              />
          }
        </div>
      </WeaDialog>
    );
  }
}

export default TaxFilingInfoDialofg;
