/*
 * Author: 黎永顺
 * name: 同步到薪资账套
 * Description:
 * Date: 2023/8/31
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider } from "ecCom";
import { Button, message } from "antd";
import { getSearchs } from "../../util";
import { getSalarySobBySalaryItem, syncSalaryItemToSalarySobItem } from "../../apis/item";
import { salarySetConditions } from "./columns";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentWillReceiveProps(nextProps, nextContext) {
    const { salaryItemStore: { salarySetform } } = nextProps;
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.getSalarySobBySalaryItem(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) salarySetform.resetForm();
  }

  getSalarySobBySalaryItem = (props) => {
    const { id, salaryItemStore: { salarySetform } } = props;
    getSalarySobBySalaryItem({ id }).then(({ status, data }) => {
      if (status) {
        this.setState({
          conditions: _.map(salarySetConditions, item => {
            return {
              ...item,
              items: _.map(item.items, o => ({
                ...o,
                options: _.map(data, it => ({ key: it.id, showname: it.content }))
              }))
            };
          })
        }, () => {
          salarySetform.initFormFields(this.state.conditions);
        });
      }
    });
  };
  save = () => {
    const { salaryItemStore: { salarySetform }, id: salaryItemId } = this.props;
    salarySetform.validateForm().then(f => {
      if (f.isValid) {
        const { salarySobIds } = salarySetform.getFormParams();
        this.setState({ loading: true });
        syncSalaryItemToSalarySobItem({ salaryItemId, salarySobIds: salarySobIds.split(",") })
          .then(({ status, errormsg }) => {
            this.setState({ loading: false });
            if (status) {
              message.success(getLabel(38462, "同步成功！"));
              this.props.onCancel();
            } else {
              message.error(errormsg || getLabel(81556, "同步失败！"));
            }
          }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { salaryItemStore: { salarySetform } } = this.props;
    const { conditions } = this.state;
    return (
      <WeaDialog
        {...this.props} className="salarySetDialog" 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={this.state.loading}>{getLabel(537558, "确定")}</Button>]}
      >
        <div className="salarySetDialogContent"> {getSearchs(salarySetform, conditions, 1, false)} </div>
      </WeaDialog>
    );
  }
}

export default SyncToSalaryAccountSetDialog;
