/*
 * Author: 黎永顺
 * name:工资单发放-重构页面-工资单模板复制
 * Description:
 * Date: 2023/10/13
 */
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 { copyConditions } from "../conditions";
import { duplicatePayroll, getPayrollTemplateLedgerList } from "../../../../apis/payroll";

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

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

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.getPayrollTemplateLedgerList(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) nextProps.payrollStore.initPayrollCopyForm();
  }

  getPayrollTemplateLedgerList = (props) => {
    getPayrollTemplateLedgerList().then(({ status, data }) => {
      if (status) {
        this.setState({
          conditions: _.map(copyConditions, item => {
            return {
              ...item, items: _.map(item.items, o => {
                if (getKey(o) === "salarySobId") {
                  return {
                    ...o, label: getLabel(o.lanId, o.label),
                    options: _.map(data, d => ({ key: d.id, showname: d.content }))
                  };
                } else {
                  return { ...o, label: getLabel(o.lanId, o.label) };
                }
              })
            };
          })
        }, () => {
          props.payrollStore.payrollCopyForm.initFormFields(this.state.conditions);
          props.payrollStore.payrollCopyForm.updateFields({ salarySobId: { value: props.salarySobId } });
        });
      }
    });
  };

  save = () => {
    const { payrollStore: { payrollCopyForm }, copyId: id } = this.props;
    payrollCopyForm.validateForm().then(f => {
      if (f.isValid) {
        const payload = payrollCopyForm.getFormParams();
        this.setState({ loading: true });
        duplicatePayroll({ id, ...payload }).then(({ status, errormsg }) => {
          this.setState({ loading: false });
          if (status) {
            message.success(getLabel(30700, "操作成功！"));
            this.props.onCancel("refresh");
          } else {
            message.error(errormsg);
          }
        });
      } else {
        f.showErrors();
      }
    }).catch(() => this.setState({ loading: false }));
  };

  render() {
    const { loading, conditions } = this.state;
    const { payrollStore: { payrollCopyForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 480, height: 127 }} initLoadCss
        buttons={[
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(537558, "保存")}</Button>
        ]}
      >
        <div className="payroll-dialog-layout">{getSearchs(payrollCopyForm, conditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default Index;
