/*
 * Author: 黎永顺
 * name: 薪资核算重构-核算弹框
 * Description:
 * Date: 2023/10/9
 */
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 { batSaveAndAcct, batSaveBasic, salaryacctGetForm } from "../../../../apis/calculate";
import { calculateConditions } from "./condition";

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

const API = {
  "batSaveAndAcct": batSaveAndAcct,
  "acct": batSaveBasic
};

@inject("calculateStore")
@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.salaryacctGetForm(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) this.props.calculateStore.initCalcForm();
  }

  salaryacctGetForm = (props) => {
    const { calculateStore: { calculateForm } } = props;
    salaryacctGetForm().then(({ status, data }) => {
      if (status) {
        const { salarySobs } = data;
        this.setState({
          conditions: _.map(calculateConditions, item => ({
            ...item,
            items: _.map(item.items, o => {
              if (getKey(o) === "salarySobId") {
                return {
                  ...o,
                  options: _.map(salarySobs, g => ({ key: g.id.toString(), showname: g.name }))
                };
              }
              return { ...o };
            })
          }))
        }, () => calculateForm.initFormFields(this.state.conditions));
      }
    });
  };
  save = () => {
    const { calculateStore: { calculateForm }, type = "acct" } = this.props;
    calculateForm.validateForm().then(f => {
      if (f.isValid) {
        const { salarySobId, salaryMonthStr, ...payload } = calculateForm.getFormParams();
        this.setState({ loading: true });
        API[type]({ ...payload, salaryMonth: `${salaryMonthStr}-01`, salarySobIds: salarySobId.split(",") })
          .then(({ status, data, errormsg }) => {
            this.setState({ loading: false });
            if (status) {
              message.success(getLabel(30700, "操作成功"));
              this.props.onCancel("refresh", data);
            } else {
              message.error(errormsg);
            }
          }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { conditions, loading } = this.state;
    const { calculateStore: { calculateForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 480, height: 174 }} initLoadCss className="salary-acct-container"
        buttons={[
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(111, "确认")}</Button>
        ]}
      >
        <div className="calculate-dialog-layout">{getSearchs(calculateForm, conditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default Index;
