/*
 * Author: 黎永顺
 * name: 个税申报表详情-tab动态添加
 * Description:
 * Date: 2023/12/29
 */
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/index";
import * as API from "../../../apis/declare";
import { commonEnumList } from "../../../apis/ruleconfig";
import { taxTabConditions } from "./constants";
import { getQueryString } from "../../../util/url";

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

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

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

  getTabForm = (props) => {
    const payload = {
      enumClass: "com.engine.salary.enums.salarysob.IncomeCategoryEnum"
    };
    commonEnumList(payload).then(({ status, data }) => {
      if (status) {
        this.setState({
          conditions: _.map(taxTabConditions, item => ({
            ...item, items: _.map(item.items, o => {
              if (getKey(o) === "incomeCategory") {
                return {
                  ...o, label: getLabel(o.lanId, o.label),
                  options: _.map(data, it => ({ key: it.enum, showname: it.defaultLabel }))
                };
              }
              return { ...o };
            })
          }))
        }, () => props.declareStore.tabDecForm.initFormFields(this.state.conditions));
      }
    });
  };
  save = () => {
    const { declareStore: { tabDecForm } } = this.props;
    tabDecForm.validateForm().then(f => {
      if (f.isValid) {
        const payload = tabDecForm.getFormParams();
        this.setState({ loading: true });
        API.addTaxDeclaration({ ...payload, taxDeclareRecordId: Number(getQueryString("id")) })
          .then(({ status, data, errormsg }) => {
            this.setState({ loading: false });
            if (status) {
              message.success(getLabel(30700, "操作成功"));
              this.props.onCancel(true);
            } else {
              message.error(errormsg);
            }
          }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { conditions, loading } = this.state;
    const { declareStore: { tabDecForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 480, height: 80 }} initLoadCss title={getLabel(1421, "新增")}
        buttons={[
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(826, "确定")}</Button>
        ]}
      >
        <div className="calculate-dialog-layout">{getSearchs(tabDecForm, conditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default TabEditDialog;
