/*
  *  数据采集-其他免税扣除
  * 明细设置弹框
  * @Author:    黎永顺
  * @Date:      2024/12/24
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaHelpfulTip, WeaLocaleProvider, WeaTools } from "ecCom";
import { WeaSwitch } from "comsMobx";
import FormInfo from "../../../components/FormInfo";
import { taxDetailSettingsConditions } from "./columns";
import { Button, message } from "antd";
import { getDomkes, toDecimal_n } from "../../../util";
import { postFetch } from "../../../util/request";
import { getIncomeCategoryList } from "../../../apis/ledger"; //获取薪资类型
import cs from "classnames";

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

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

  async componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { data } = await getIncomeCategoryList();
      this.setState({
        conditions: _.map(taxDetailSettingsConditions[nextProps.dataType], item => ({
          ...item, items: _.map(item.items, o => ({
            ...o, options: getKey(o) === "voucherTypeName" ?
              [{ key: "MONTH", showname: "月度" }, { key: "YEAR", showname: "年度" }] :
              getKey(o) === "incomeCategory" ? _.map(data, it => ({
                key: it.value.toString(),
                showname: <div className="reportTypeName-box">
                  <span>{it.defaultLabel}</span>
                  <span
                    className={cs("reportTypeName", { "danger": it.reportTypeName === getLabel(111, "非居民所得") })}>
                      {it.reportTypeName}
                    </span>
                </div>
              })) : []
          }))
        }))
      }, () => {
        this.props.otherDeductStore.settingsForm.initFormFields(this.state.conditions);
        if (nextProps.id) _.map(getDomkes(this.state.conditions), domkey => {
          this.props.otherDeductStore.settingsForm.updateFields({ [domkey]: { value: nextProps.record[domkey].toString() } });
        });
      });
    } else if (nextProps.visible !== this.props.visible && !nextProps.visible) {
      this.props.otherDeductStore.initSettingsForm();
    }
  }

  save = () => {
    const { otherDeductStore: { settingsForm }, mainId, id, dataType } = this.props;
    settingsForm.validateForm().then(f => {
      if (f.isValid) {
        let { deductionMonth, incomeCategory, ...payload } = settingsForm.getFormParams();
        deductionMonth && (payload = { ...payload, deductionMonth: deductionMonth + "-01" });
        this.setState({ loading: true });
        postFetch(`/api/bs/hrmsalary/otherDeduction/save${_.upperFirst(dataType)}`, {
          ...payload, mainId, id,
          incomeCategory: Number(incomeCategory)
        }).then(({ status, errormsg }) => {
          this.setState({ loading: false });
          if (status) {
            message.success(getLabel(111, "操作成功！"));
            this.props.onCancel(this.props.onSuccess);
          } else {
            message.error(errormsg);
          }
        });
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { otherDeductStore: { settingsForm } } = this.props, { loading, conditions } = this.state;
    const itemRender = {
      freeAmount: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ freeAmount: { value: toDecimal_n(v, 2) } })}/>);
      },
      identificationNumber: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }} form={form} formParams={formParams}/>);
      },
      effectiveDate: (field, textAreaProps, form, formParams) => {
        return (<div className="cust">
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }} form={form} formParams={formParams}/>
          <div className="cus_helpful">
            <WeaHelpfulTip title={getLabel(field.helpfulTipLanId, field.helpfulTip)}/>
          </div>
        </div>);
      },
      donateDate: (field, textAreaProps, form, formParams) => {
        return (<div className="cust">
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }} form={form} formParams={formParams}/>
          <div className="cus_helpful">
            <WeaHelpfulTip title={getLabel(field.helpfulTipLanId, field.helpfulTip)}/>
          </div>
        </div>);
      },
      yearPremium: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ yearPremium: { value: toDecimal_n(v, 2) } })}/>);
      },
      monthPremium: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ monthPremium: { value: toDecimal_n(v, 2) } })}/>);
      },
      currentDeduction: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ currentDeduction: { value: toDecimal_n(v, 2) } })}/>);
      },
      donateAmount: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ donateAmount: { value: toDecimal_n(v, 2) } })}/>);
      },
      deductionProportion: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ deductionProportion: { value: toDecimal_n(v, 2) } })}/>);
      },
      actualDeduction: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ actualDeduction: { value: toDecimal_n(v, 2) } })}/>);
      },
      derateAmount: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ derateAmount: { value: toDecimal_n(v, 2) } })}/>);
      },
      otherDeduction: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ otherDeduction: { value: toDecimal_n(v, 2) } })}/>);
      },
      payAmount: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ payAmount: { value: toDecimal_n(v, 2) } })}/>);
      }
    };
    if (_.reduce(conditions, (pre, cur) => (pre += cur.items.length), 0) * 47 + 33 === 33) return null;
    return (
      <WeaDialog
        {...this.props} initLoadCss title={getLabel(111, "明细设置")}
        style={{ width: 480, height: _.reduce(conditions, (pre, cur) => (pre += cur.items.length), 0) * 47 + 33 }}
        buttons={[
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(111, "确定")}</Button>,
          <Button type="ghost" onClick={() => this.props.onCancel()}>{getLabel(111, "取消")}</Button>
        ]}
      >
        <FormInfo className="form-dialog-layout" center={false} itemRender={itemRender}
                  form={settingsForm} formFields={conditions}/>
      </WeaDialog>
    );
  }
}

export default DetailSettingsDialog;
