/*
 * Author: 黎永顺
 * name:工资单发放-重构页面补发工资单模板
 * Description:
 * Date: 2023/10/17
 */
import React, { Component } from "react";
import { toJS } from "mobx";
import { WeaButtonIcon, WeaLocaleProvider, WeaSearchGroup } from "ecCom";
import SalaryItemSettings from "../../../payroll/stepForm/salaryItemSettings";
import SalaryItems from "../payrollTempNormalSet/salaryItems";
import { getReplenishForm } from "../../../../apis/payroll";

const getLabel = WeaLocaleProvider.getLabel;

class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      replenishSalaryTemplateSalaryItemSet: [], salaryBillItemNameSet: {}
    };
  }

  componentDidMount() {
    const { payrollStore: { tmplDataSource } } = this.props;
    if (!toJS(tmplDataSource.replenishSalaryItemSetting)) {
      this.getReplenishForm();
    } else {
      this.setState({
        replenishSalaryTemplateSalaryItemSet: _.map(toJS(tmplDataSource.replenishSalaryItemSetting), o => ({
          ...o,
          items: _.map(o.items, it => ({ ...it, viewAttr: 1 }))
        }))
      });
    }
  }

  getReplenishForm = () => {
    const { payrollStore: { tmplDataSource, setTmplDataSource }, tmplId: id } = this.props;
    const salarySobId = toJS(tmplDataSource).salarySob;
    getReplenishForm({ salarySobId, id }).then(({ status, data }) => {
      if (status) {
        const { replenishSalaryTemplateSalaryItemSet, salaryBillItemNameSet } = data;
        this.setState({
          replenishSalaryTemplateSalaryItemSet, salaryBillItemNameSet
        }, () => {
          setTmplDataSource({
            ...toJS(tmplDataSource),
            replenishSalaryItemSetting: replenishSalaryTemplateSalaryItemSet
          });
        });
      }
    });
  };

  handleChangeSalaryItem = (resultSet) => {
    const { payrollStore: { setTmplDataSource, tmplDataSource } } = this.props;
    setTmplDataSource({ ...toJS(tmplDataSource), replenishSalaryItemSetting: resultSet });
    this.setState({ replenishSalaryTemplateSalaryItemSet: resultSet });
  };
  handleChangeSalaryItemShowNamesetting = (itemShowNamesetting) => {
    const { payrollStore: { setSalaryBillItemNameSetting, salaryBillItemNameSetting } } = this.props;
    setSalaryBillItemNameSetting(_.map(salaryBillItemNameSetting, it => {
      if (it.salaryBillType === 1) {
        return { ...it, salaryTemplateId: this.props.tmplId, itemShowNameSetting: itemShowNamesetting };
      }
      return { ...it, salaryTemplateId: this.props.tmplId };
    }));
  };

  render() {
    const { payrollStore: { tmplDataSource }, detail } = this.props;
    const { replenishSalaryTemplateSalaryItemSet, salaryBillItemNameSet } = this.state;
    return (
      <WeaSearchGroup
        title={
          <div className="salarySetTitle">
            <span>{getLabel(543593, "薪资项目设置")}</span>
            <WeaButtonIcon buttonType="add" type="primary" disabled={detail}
                           onClick={() => this.salaryItemSettingsRef.handleOpenModal(toJS(tmplDataSource).salarySob, getLabel(543594, "添加分类"))}/>
          </div>
        }
        items={[]} needTigger showGroup>
        {
          detail ? <SalaryItems dataSource={replenishSalaryTemplateSalaryItemSet}/> :
          <SalaryItemSettings
            ref={dom => this.salaryItemSettingsRef = dom}
            dataSource={replenishSalaryTemplateSalaryItemSet}
            onChangeSalaryItem={this.handleChangeSalaryItem}
            onChangeSalaryItemShowNamesetting={this.handleChangeSalaryItemShowNamesetting}
            salarySobId={toJS(tmplDataSource).salarySob}
            salaryTemplateId={this.props.tmplId || ""}
            isReplenish={true} salaryBillItemNameSet={salaryBillItemNameSet}
          />
        }
      </WeaSearchGroup>
    );
  }
}

export default Index;
