/*
  *  浮动薪酬
  *   薪资档案导入
  * @Author:    黎永顺
  * @Date:      2024/8/8
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaCheckbox, WeaDatePicker, WeaFormItem, WeaLocaleProvider } from "ecCom";
import ImportDialog from "../../../../components/importDialog";
import * as API from "../../../../apis/variableSalary";
import { convertToUrlString } from "../../../../util/url";

const getLabel = WeaLocaleProvider.getLabel;

class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      importDialog: {
        nextloading: false, link: "/api/bs/hrmsalary/variableSalary/downloadTemplate",
        importResult: {}, imageId: "", hasData: false, salaryMonth: "",
        previewUrl: "/api/bs/hrmsalary/variableSalary/preview"
      }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    const { importDialog } = this.state;
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { baseTableStore: { VSalryForm, VExtraSalryForm } } = nextProps;
      const payload = {
        ...VSalryForm.getFormParams(), ...VExtraSalryForm.getFormParams(), hasData: importDialog.hasData
      };
      this.setState({
        importDialog: {
          ...importDialog, salaryMonth: VExtraSalryForm.getFormParams().salaryMonth,
          link: `${importDialog.link}?${convertToUrlString(payload)}`
        }
      });
    } else {
      this.setState({
        importDialog: {
          nextloading: false, link: "/api/bs/hrmsalary/variableSalary/downloadTemplate", hasData: false,
          importResult: {}, imageId: "", previewUrl: "/api/bs/hrmsalary/variableSalary/preview", salaryMonth: ""
        }
      });
    }
  }

  handleImport = (payload) => {
    const { baseTableStore: { VExtraSalryForm } } = this.props;
    const { importDialog } = this.state;
    const { salaryMonth } = importDialog;
    const { taxAgentIds } = VExtraSalryForm.getFormParams();
    this.setState({ importDialog: { ...importDialog, nextloading: true } });
    API.importVariableSalary({
      ...payload, salaryMonth, taxAgentIds: _.isEmpty(taxAgentIds) ? [] : taxAgentIds.split(",")
    }).then(({ data, status }) => {
      this.setState({ importDialog: { ...importDialog, nextloading: false } });
      if (status) {
        this.setState({
          importDialog: { ...importDialog, ...payload, importResult: data }
        });
      }
    }).catch(() => this.setState({ importDialog: { ...importDialog, nextloading: false } }));
  };
  renderFormComponent = () => {
    const { baseTableStore: { VSalryForm, VExtraSalryForm } } = this.props;
    const { importDialog } = this.state;
    const { salaryMonth: month, hasData } = importDialog;
    return <div style={{ padding: "8px 16px", border: "1px solid #e5e5e5", margin: "4px 0" }}>
      <WeaFormItem label={getLabel(111, "薪资所属月")} labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}>
        <WeaDatePicker format="YYYY-MM" value={month}
                       onChange={val => {
                         const payload = {
                           ...VSalryForm.getFormParams(), ...VExtraSalryForm.getFormParams(),
                           salaryMonth: val, hasData
                         };
                         this.setState({
                           importDialog: {
                             ...importDialog, salaryMonth: val,
                             link: `/api/bs/hrmsalary/variableSalary/downloadTemplate?${convertToUrlString(payload)}`
                           }
                         });
                       }}/>
      </WeaFormItem>
    </div>;
  };

  render() {
    const { importDialog } = this.state;
    return (
      <ImportDialog
        {...this.props} {...importDialog}
        onResetImportResult={() => this.setState({
          importDialog: {
            ...importDialog, importResult: {}, imageId: "", link: "/api/bs/hrmsalary/variableSalary/downloadTemplate"
          }
        })}
        importParams={this.renderFormComponent()}
        exportDataDom={
          <WeaCheckbox
            value={importDialog.hasData ? "1" : "0"}
            content={getLabel(543208, "导出现有数据")}
            helpfulTip={getLabel(111, "提示：建议先导出现有最新数据，修改后再导入")}
            onChange={val => {
              const { baseTableStore: { VSalryForm, VExtraSalryForm } } = this.props;
              const { salaryMonth } = importDialog;
              const payload = {
                salaryMonth, ...VSalryForm.getFormParams(), ...VExtraSalryForm.getFormParams(),
                hasData: val === "1"
              };
              this.setState({
                importDialog: {
                  ...importDialog, hasData: val === "1",
                  link: `/api/bs/hrmsalary/variableSalary/downloadTemplate?${convertToUrlString(payload)}`
                }
              });
            }}
          />
        }
        nextCallback={imageId => this.setState({ importDialog: { ...importDialog, imageId } })}
        nextUplaodCallback={imageId => this.handleImport({ imageId })}
      />
    );
  }
}

export default Index;
