/*
 * Author: 黎永顺
 * name: 退差组件
 * Description:
 * Date: 2022/11/22
 */
import React, { Component } from "react";
import { message, Modal } from "antd";
import RegTop from "./regTop";
import RegList from "./regList";
import RegAddEmployee from "./regAddEmployee";
import RegEditDetial from "./regEditDetial";
import { getQueryString } from "../../../../util/url";
import { calcPageNo } from "../../../../util";
import * as API from "../../../../apis/standingBook";
import "./index.less";

class Regression extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectKey: [],
      returnPersonModal: {
        title: "添加退差人员",
        visible: false
      },
      returnEditPersonSlide: {
        title: "",
        editId: "",
        visible: false
      },
      loading: { save: false }
    };
    this.regEmmployeeRef = null;
    this.regListRef = null;
    this.regTopRef = null;
  }

  delRecession = () => {
    const { selectKey } = this.state;
    API.delRecession(selectKey).then(({ status, errormsg }) => {
      if (status) {
        message.success("删除成功");
        const current = calcPageNo(this.regListRef.state.pageInfo.total, this.regListRef.state.pageInfo.current, 10, selectKey.length);
        this.regListRef.recessionList({ current });
        this.regListRef.handleResetSelectRowKeys([]);
        this.setState({ selectKey: [] });
      } else {
        message.error(errormsg || "删除失败");
      }
    });
  };
  handleSave = (params) => {
    const { loading } = this.state;
    const billMonth = getQueryString("billMonth");
    const paymentOrganization = getQueryString("paymentOrganization");
    const payload = {
      ...params,
      billMonth,
      paymentOrganization
    };
    this.setState({ loading: { ...loading, save: true } });
    API.saveRecession(payload).then(({ status, errormsg }) => {
      this.setState({ loading: { ...loading, save: false } });
      if (status) {
        message.success("操作成功。若退差月无核算明细，账单月则无法获取退差数据。");
        this.handleCloseModal();
      } else {
        message.error(errormsg || "操作失败");
      }
    });
  };
  handleChangeOpt = (key) => {
    const { returnPersonModal } = 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 creator = Number(getQueryString("creator"));
    const paymentOrganization = getQueryString("paymentOrganization");
    switch (key) {
      case "add":
        this.setState({ returnPersonModal: { ...returnPersonModal, visible: true } });
        break;
      case "delete":
        Modal.confirm({
          title: "信息确认",
          content: "删除人员本账单月将不包含该人员的退差数据!",
          onOk: () => {
            this.delRecession();
          },
          onCancel: () => {
          }
        });
        break;
      case "export":
        const url = `${window.location.origin}/api/bs/hrmsalary/welfare/recession/export?creator=${creator}&billMonth=${billMonth}&paymentOrganization=${paymentOrganization}`;
        window.open(url, "_self");
        break;
      case "search":
        this.regListRef.recessionList({ userName: name, workcode, departmentIds, subCompanyIds, current: 1 });
        break;
      default:
        break;
    }
  };
  handleCloseModal = (refreshList = false) => {
    const { returnPersonModal, returnEditPersonSlide } = this.state;
    this.setState({
      returnPersonModal: { ...returnPersonModal, visible: false, title: "添加退差人员" },
      returnEditPersonSlide: { ...returnEditPersonSlide, visible: false, title: "", editId: "" }
    }, () => {
      this.regEmmployeeRef.handleReset();
      refreshList && this.regListRef.recessionList();
    });
  };
  handleEdit = (record) => {
    const { userName, id: editId } = record;
    const { returnEditPersonSlide } = this.state;
    this.setState({
      returnEditPersonSlide: { ...returnEditPersonSlide, visible: true, title: userName, editId }
    });
  };

  render() {
    const billMonth = getQueryString("billMonth");
    const { returnPersonModal, returnEditPersonSlide, selectKey, loading } = this.state;
    return (
      <div className="regressionWrapper">
        <RegTop
          type="regression"
          ref={dom => this.regTopRef = dom}
          billMonth={billMonth}
          onChange={this.handleChangeOpt}
          selectKey={selectKey}
        />
        <div className="tableWrapper">
          {/*<WeaNewScroll height="100%">*/}
          <RegList
            type="regression"
            ref={dom => this.regListRef = dom}
            visible={returnPersonModal.visible}
            onChangeRowkey={(selectKey) => this.setState({ selectKey })}
            onEdit={this.handleEdit}
          />
          {/*编辑弹框*/}
          <RegEditDetial {...returnEditPersonSlide} onCancel={this.handleCloseModal}/>
          {/* 弹框 */}
          <RegAddEmployee
            ref={dom => this.regEmmployeeRef = dom}
            {...returnPersonModal}
            loading={loading}
            onCancel={this.handleCloseModal}
            onSave={this.handleSave}
          />
          {/*</WeaNewScroll>*/}
        </div>
      </div>
    );
  }
}

export default Regression;
