/*
 * Author: 黎永顺
 * name: 个税对接-在线获取
 * Description:
 * Date: 2023/9/5
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTools } from "ecCom";
import { Button, message } from "antd";
import { getSearchs } from "../../../../util";
import { cumTaxPeriodCondition } from "../columns";
import { onlineRequest } from "../../../../apis/cumDeduct";
import { onlineActualAddUpAdvanceTax } from "../../../../apis/cumSituation";
import { postFetch } from "../../../../util/request";

const getLabel = WeaLocaleProvider.getLabel;
const getKey = WeaTools.getKey;
const APIFox = {
  online: onlineRequest,
  advance: onlineActualAddUpAdvanceTax
};

@inject("cumDeductStore")
@observer
class SalaryCumDeductChooseTaxPeriodDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false, conditions: []
    };
  }

  async componentWillReceiveProps(nextProps, nextContext) {
    const { cumDeductStore: { cumTaxPeriodForm, changeCumTaxPeriodForm } } = nextProps;
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { data } = await postFetch("/api/bs/hrmsalary/taxAgent/listAuth", { filterType: "ADMIN_DATA" });
      this.setState({
        conditions: _.map(cumTaxPeriodCondition, item => ({
          ...item, items: _.map(item.items, o => {
            if (getKey(o) === "taxAgentIds") {
              return {
                ...o, lable: getLabel(o.lanId, o.label),
                options: _.map(data, g => ({ key: String(g.id), showname: g.name }))
              };
            }
            return { ...o, lable: getLabel(o.lanId, o.label) };
          })
        }))
      });
      cumTaxPeriodForm.initFormFields(cumTaxPeriodCondition);
    }
    if (nextProps.visible !== this.props.visible && !nextProps.visible) {
      cumTaxPeriodForm.resetForm();
      changeCumTaxPeriodForm();
    }
  }

  save = () => {
    const { cumDeductStore: { cumTaxPeriodForm }, type } = this.props;
    cumTaxPeriodForm.validateForm().then(f => {
      const { declareMonth, taxAgentIds } = cumTaxPeriodForm.getFormParams();
      if (f.isValid) {
        this.setState({ loading: true });
        APIFox[type]({ declareMonth: declareMonth + "-01", taxAgentIds: taxAgentIds ? taxAgentIds.split(",") : [] })
          .then(({ status, errormsg }) => {
            this.setState({ loading: false });
            if (status) {
              message.success(getLabel(111, "获取成功！"));
              this.props.onCancel();
            } else {
              message.error(errormsg);
            }
          }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { loading, conditions } = this.state;
    const { cumDeductStore: { cumTaxPeriodForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} className="paymentDialog mulSelectDialog" initLoadCss
        style={{ width: 550 }}
        buttons={[<Button type="primary" loading={loading} onClick={this.save}>{getLabel(33703, "确认")}</Button>]}
        bottomLeft={getLabel(111, "点击保存后，稍后请点击【获取结果下载】下载获取结果。获取的数据将覆盖列表原本数据（有则覆盖无则新增）。")}
      >
        <div className="paymentDialogContent">
          {getSearchs(cumTaxPeriodForm, conditions, 1, false)}
        </div>
      </WeaDialog>
    );
  }
}

export default SalaryCumDeductChooseTaxPeriodDialog;
