/*
 * Author: 黎永顺
 * name: 补差
 * Description:
 * Date: 2022/12/16
 */
import React, { Component } from "react";
import RegTop from "./regTop";
import { message, Modal } from "antd";
import * as API from "../../../../apis/standingBook";
import { calcPageNo } from "../../../../util";
import RegList from "./regList";
import RegEditDetial from "./regEditDetial";
import AddCompensationPersonnelDialog from "./addCompensationPersonnelDialog";
import StandingBookCalcImportDialog from "./standingBookCalcImportDialog";
import "./index.less";
import { convertToUrlString } from "../../../../util/url";

class MakeupDifference extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectKey: [], loading: { save: false },
      importDiffModal: {
        visible: false,
        fieldUrl: "getBalanceWelfareList",
        tmpUrl: "exportSiaccountWelfarebalanceimporttemplatetetemplate",
        cacheUrl: "cacheBalanceWelfareList",
        importUrl: "importBalanceInsuranceDetail",
        importparams: {}
      },
      returnEditPersonSlide: {
        title: "", editId: "", visible: false
      },
      addPersonalDialog: {
        visible: false, paymentOrganization: "", billMonth: ""
      }
    };
    this.diffListRef = null;
    this.regTopRef = null;
  }

  delBalance = () => {
    const { selectKey: ids } = this.state;
    const payload = { ids, ..._.pick(this.props, ["billMonth", "paymentOrganization"]) };
    API.delBalance(payload).then(({ status, errormsg }) => {
      if (status) {
        message.success("删除成功");
        const current = calcPageNo(this.diffListRef.state.pageInfo.total, this.diffListRef.state.pageInfo.current, 10, ids.length);
        this.diffListRef.recessionList({ current });
        this.diffListRef.handleResetSelectRowKeys([]);
        this.setState({ selectKey: [] });
      } else {
        message.error(errormsg || "删除失败");
      }
    });
  };
  handleChangeOpt = (key) => {
    const { importDiffModal, addPersonalDialog } = this.state;
    const name = this.regTopRef.state.name;
    const workcode = this.regTopRef.state.workcode;
    const departmentIds = this.regTopRef.state.departmentIds ? this.regTopRef.state.departmentIds.split(",") : [];
    const subCompanyIds = this.regTopRef.state.subCompanyIds ? this.regTopRef.state.subCompanyIds.split(",") : [];
    switch (key) {
      case "delete":
        Modal.confirm({
          title: "信息确认",
          content: "确定删除数据吗？",
          onOk: () => this.delBalance()
        });
        break;
      case "import":
        this.setState({
          importDiffModal: {
            ...importDiffModal, visible: true,
            importparams: { ..._.pick(this.props, ["billMonth"]) }
          }
        });
        break;
      case "export":
        const url = `/api/bs/hrmsalary/welfare/balance/export?${convertToUrlString(_.pick(this.props, ["billMonth", "paymentOrganization"]))}`;
        window.open(url, "_blank");
        break;
      case "search":
        this.diffListRef.recessionList({ userName: name, workcode, departmentIds, subCompanyIds, current: 1 });
        break;
      case "add":
        this.setState({
          addPersonalDialog: {
            ...addPersonalDialog, ..._.pick(this.props, ["billMonth", "paymentOrganization"]), visible: true
          }
        });
        break;
      default:
        break;
    }
  };
  handleEdit = (record) => {
    const { userName, id: editId } = record;
    const { returnEditPersonSlide } = this.state;
    this.setState({
      returnEditPersonSlide: { ...returnEditPersonSlide, visible: true, title: userName, editId }
    });
  };
  handleCloseModal = (refreshList = false) => {
    const { returnEditPersonSlide } = this.state;
    this.setState({
      returnEditPersonSlide: { ...returnEditPersonSlide, visible: false, title: "", editId: "" }
    }, () => {
      refreshList && this.diffListRef.recessionList();
    });
  };

  render() {
    const { selectKey, importDiffModal, returnEditPersonSlide, addPersonalDialog } = this.state;
    return (
      <div className="differenceWrapper">
        <RegTop
          {..._.pick(this.props, ["billMonth", "type"])}
          regtopType="difference"
          ref={dom => this.regTopRef = dom}
          onChange={this.handleChangeOpt}
          selectKey={selectKey}
        />
        <div className="tableWrapper">
          <RegList
            {..._.pick(this.props, ["billMonth", "paymentOrganization", "creator", "type"])}
            regType="difference"
            ref={dom => this.diffListRef = dom}
            onChangeRowkey={(selectKey) => this.setState({ selectKey })}
            onEdit={this.handleEdit}
          />
          {/*编辑弹框*/}
          <RegEditDetial {...returnEditPersonSlide} onCancel={this.handleCloseModal}/>
          {/*添加补差人员*/}
          <AddCompensationPersonnelDialog {...addPersonalDialog}
                                          onCancel={(isRefresh) => this.setState({
                                            addPersonalDialog: {
                                              ...addPersonalDialog,
                                              visible: false
                                            }
                                          }, () => isRefresh && this.diffListRef.recessionList())}
          />
          {/*导入补差*/}
          <StandingBookCalcImportDialog {...importDiffModal}
                                        onCancel={(isInit) => this.setState({
                                          importDiffModal: { ...importDiffModal, visible: false }
                                        }, () => isInit && this.diffListRef.recessionList())}/>
        </div>
      </div>
    );
  }
}

export default MakeupDifference;
