/*
 * Author: 黎永顺
 * name: 补差
 * Description:
 * Date: 2022/12/16
 */
import React, { Component } from "react";
import RegTop from "./regTop";
import { message, Modal } from "antd";
import { WeaNewScroll } from "ecCom";
import { getQueryString } from "../../../../util/url";
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";

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 billMonth = getQueryString("billMonth");
    const paymentOrganization = getQueryString("paymentOrganization");
    const payload = { ids, 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(",") : [];
    const billMonth = getQueryString("billMonth");
    const paymentOrganization = getQueryString("paymentOrganization");
    switch (key) {
      case "delete":
        Modal.confirm({
          title: "信息确认",
          content: "确定删除数据吗？",
          onOk: () => this.delBalance()
        });
        break;
      case "import":
        this.setState({
          importDiffModal: {
            ...importDiffModal, visible: true,
            importparams: { billMonth: getQueryString("billMonth") }
          }
        });
        break;
      case "export":
        const url = `${window.location.origin}/api/bs/hrmsalary/welfare/balance/export?billMonth=${billMonth}&paymentOrganization=${paymentOrganization}`;
        window.open(url, "_self");
        break;
      case "search":
        this.diffListRef.recessionList({ userName: name, workcode, departmentIds, subCompanyIds, current: 1 });
        break;
      case "add":
        this.setState({
          addPersonalDialog: {
            ...addPersonalDialog, visible: true, paymentOrganization, billMonth
          }
        });
        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 billMonth = getQueryString("billMonth");
    const { selectKey, importDiffModal, returnEditPersonSlide, addPersonalDialog } = this.state;
    return (
      <div className="differenceWrapper">
        <RegTop
          type="difference"
          ref={dom => this.regTopRef = dom}
          billMonth={billMonth}
          onChange={this.handleChangeOpt}
          selectKey={selectKey}
        />
        <div className="tableWrapper">
          <WeaNewScroll height="100%">
            <RegList
              type="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())}/>
          </WeaNewScroll>
        </div>
      </div>
    );
  }
}

export default MakeupDifference;
