/*
 * Author: 黎永顺
 * name: 个税申报重构- 申报
 * Description:
 * Date: 2023/10/12
 */
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 { getTaxAgentSelectListAsAdmin } from "../../../../apis/taxAgent";
import { batSaveDeclare } from "../../../../apis/declare";
import { declareConditions } from "./condition";
import * as API from "../../../../apis/ruleconfig";

const getKey = WeaTools.getKey;
const getLabel = WeaLocaleProvider.getLabel;

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

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

  getTaxAgentSelectListAsAdmin = async (props) => {
    const { data: sysinfo } = await API.sysinfo();
    const { declareStore: { declareForm } } = props;
    getTaxAgentSelectListAsAdmin().then(({ status, data }) => {
      if (status) {
        this.setState({
          conditions: _.map(declareConditions, item => ({
            ...item,
            items: _.map(item.items, o => {
              if (getKey(o) === "taxAgentId") {
                return {
                  ...o, options: _.map(data, g => ({ key: g.id, showname: g.content }))
                  // helpfulTitle: getLabel(563420, "提示：可选择单个个税扣缴义务人进行申报，若不选择，则批量对管理下的所有个税扣缴义务人进行申报；")
                };
              } else if (getKey(o) === "salaryMonthStr") {
                return {
                  ...o,
                  label: sysinfo["TAX_DECLARATION_DATE_TYPE"] === "1" ? getLabel(111, "税款所属期") : getLabel(111, "薪资所属月")
                };
              }
              return { ...o };
            })
          }))
        }, () => declareForm.initFormFields(this.state.conditions));
      }
    });
  };
  save = () => {
    const { declareStore: { declareForm } } = this.props;
    declareForm.validateForm().then(f => {
      if (f.isValid) {
        const { taxAgentId, ...payload } = declareForm.getFormParams();
        this.setState({ loading: true });
        batSaveDeclare({
          ...payload, taxCycle: `${payload.salaryMonthStr}-01`, salaryDate: `${payload.salaryMonthStr}-01`,
          taxAgentIds: taxAgentId.split(",")
        }).then(({ status, errormsg }) => {
          this.setState({ loading: false });
          if (status) {
            message.success(getLabel(30700, "操作成功"));
            this.props.onCancel("refresh");
          } else {
            message.error(errormsg);
          }
        }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { conditions, loading } = this.state;
    const { declareStore: { declareForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 500, height: 174 }} initLoadCss className="salary-declare-container"
        buttons={[
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(543618, "生成申报表")}</Button>
        ]}
      >
        <div className="declare-dialog-layout">{getSearchs(declareForm, conditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default Index;
