/*
 * Author: 黎永顺
 * name:工资单发放-重构页面正常核算工资单模板
 * Description:
 * Date: 2023/10/17
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { toJS } from "mobx";
import { WeaButtonIcon, WeaLocaleProvider, WeaSearchGroup, WeaTools } from "ecCom";
import { getPayrollItemList, getPayrollShowForm } from "../../../../apis/payroll";
import { tempNormalSetConditions } from "../conditions";
import { payrollTempNormalSetForm } from "./formRender";
import SalaryItemSettings from "../../../payroll/stepForm/salaryItemSettings";
import SalaryItems from "./salaryItems";

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

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

  componentDidMount() {
    this.getPayrollShowForm();
  }

  componentWillUnmount() {
    const { payrollStore: { initPayrollTempNormalForm } } = this.props;
    initPayrollTempNormalForm();
  }

  getPayrollShowForm = () => {
    const {
      detail, tmplId: id, payrollStore: { payrollTempNormalForm, tmplDataSource, setTmplDataSource }
    } = this.props;
    getPayrollShowForm({ id }).then(async ({ status, data }) => {
      if (status) {
        const { salaryTemplateShowSet, salaryTemplateSalaryItemSet: salaryItemSet, salaryBillItemNameSet } = data;
        const { data: result } = salaryTemplateShowSet;
        const fieldsEchoData = { ...result, ...toJS(tmplDataSource) };
        if (!id && !fieldsEchoData.salaryItemSetting) this.getPayrollItemList();
        if (id && !fieldsEchoData.salaryItemSetting) {
          setTmplDataSource({ salaryItemSetting: salaryItemSet, ...fieldsEchoData });
          this.setState({ salaryItemSet });
        }
        if (fieldsEchoData.salaryItemSetting) {
          setTmplDataSource({
            salaryItemSetting: _.map(toJS(fieldsEchoData.salaryItemSetting), o => ({
              ...o,
              items: _.map(o.items, it => ({ ...it, viewAttr: 1 }))
            })), ...fieldsEchoData
          });
          this.setState({
            salaryItemSet: _.map(toJS(fieldsEchoData.salaryItemSetting), o => ({
              ...o,
              items: _.map(o.items, it => ({ ...it, viewAttr: 1 }))
            }))
          });
        }
        this.setState({
          conditions: _.map(tempNormalSetConditions, it => {
            if (it.title === "themeSet") {
              return {
                ...it, title: getLabel(543587, "主题及其他设置"),
                items: _.map(it.items, o => {
                  if (getKey(o) === "textContentPosition") {
                    return {
                      ...o, label: getLabel(o.lanId, o.label), viewAttr: !detail ? o.viewAttr : 1,
                      options: [
                        { key: "1", showname: getLabel(542697, "薪资项目前") },
                        { key: "2", showname: getLabel(542698, "薪资项目后") }
                      ]
                    };
                  } else if (getKey(o) === "background") {
                    return {
                      ...o, viewAttr: !detail ? o.viewAttr : 1, title: getLabel(20001, "上传图片")
                    };
                  }
                  return { ...o, viewAttr: !detail ? o.viewAttr : 1, label: getLabel(o.lanId, o.label) };
                })
              };
            }
          }),
          salaryBillItemNameSet
        }, () => {
          payrollTempNormalForm.initFormFields(this.state.conditions);
          //字段回显
          const tempBaseSetDomkeys = _.reduce(tempNormalSetConditions, (pre, cur) => ([...pre, ..._.map(cur.items, o => getKey(o))]), []);
          _.map(tempBaseSetDomkeys, it => {
            switch (it) {
              case "salaryItemZeroStatus":
              case "salaryItemNullStatus":
                payrollTempNormalForm.updateFields({ [it]: fieldsEchoData[it] ? "1" : "0" });
                break;
              default:
                payrollTempNormalForm.updateFields({ [it]: fieldsEchoData[it] ? fieldsEchoData[it].toString() : "" });
                break;
            }
          });
        });
      }
    });
  };
  getPayrollItemList = () => {
    const { salaryItemSet } = this.state;
    const { payrollStore: { tmplDataSource, setTmplDataSource } } = this.props;
    getPayrollItemList({ salarySobId: toJS(tmplDataSource).salarySob }).then(({ status, data }) => {
      if (status && _.isEmpty(salaryItemSet)) {
        this.setState({ salaryItemSet: data }, () => {
          setTmplDataSource({ ...toJS(tmplDataSource), salaryItemSetting: data });
        });
      }
    });
  };
  handleChange = (params) => {
    const { payrollStore: { payrollTempNormalForm, setTmplDataSource, tmplDataSource } } = this.props;
    const key = _.keys(params)[0];
    if (key === "background") {
      const background = payrollTempNormalForm.getFormDatas().background.valueObj[0].acclink;
      setTmplDataSource({ ...toJS(tmplDataSource), background });
    }
  };
  handleInsertVar = (key, themeVar) => {
    const { payrollStore: { payrollTempNormalForm, setTmplDataSource, tmplDataSource } } = this.props;
    const theme = payrollTempNormalForm.getFormParams().theme;
    if (key === "background") {
      setTmplDataSource({ ...toJS(tmplDataSource), background: themeVar });
    } else {
      payrollTempNormalForm.updateFields({ [key]: `${theme}${themeVar}` });
    }
  };
  handleChangeSalaryItem = (resultSet) => {
    const { payrollStore: { setTmplDataSource, tmplDataSource } } = this.props;
    setTmplDataSource({ ...toJS(tmplDataSource), salaryItemSetting: resultSet });
    this.setState({ salaryItemSet: resultSet });
  };
  handleChangeSalaryItemShowNamesetting = (itemShowNamesetting) => {
    const { payrollStore: { setSalaryBillItemNameSetting, salaryBillItemNameSetting } } = this.props;
    setSalaryBillItemNameSetting(_.map(salaryBillItemNameSetting, it => {
      if (it.salaryBillType === 0) {
        return { ...it, salaryTemplateId: this.props.tmplId, itemShowNameSetting: itemShowNamesetting };
      }
      return { ...it, salaryTemplateId: this.props.tmplId };
    }));
  };

  render() {
    const { conditions, salaryBillItemNameSet, salaryItemSet } = this.state;
    const { detail, payrollStore: { payrollTempNormalForm, tmplDataSource } } = this.props;
    return (
      <React.Fragment>
        {!_.isEmpty(conditions) && payrollTempNormalSetForm(payrollTempNormalForm, conditions, toJS(tmplDataSource).background, this.handleChange, this.handleInsertVar)}
        <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={salaryItemSet}/> : <SalaryItemSettings
              ref={dom => this.salaryItemSettingsRef = dom}
              dataSource={salaryItemSet} salaryTemplateId={this.props.tmplId || ""}
              onChangeSalaryItem={this.handleChangeSalaryItem}
              onChangeSalaryItemShowNamesetting={this.handleChangeSalaryItemShowNamesetting}
              salarySobId={toJS(tmplDataSource).salarySob}
              isReplenish={false} salaryBillItemNameSet={salaryBillItemNameSet}
            />
          }
        </WeaSearchGroup>
      </React.Fragment>
    );
  }
}

export default Index;
