/*
 * Author: 黎永顺
 * Description: 个税扣缴义务人-人员范围
 * Date: 2022-05-31 09:51:59
 * LastEditTime: 2022-06-28 17:43:30
 */
import React, { Component } from "react";
import { message, Modal } from "antd";
import { inject, observer } from "mobx-react";
import SlideTaxagentUser from "./slideTaxagentUser";
import { taxAgentRangeEdit } from "../../apis/taxAgent";
import "./index.less";

const personScopeWarrper = {};

@inject("taxAgentStore", "ledgerStore")
@observer
export default class PersonalScope extends Component {
  constructor(props) {
    super(props);
    this.state = {
      deleteLoading: false,
      queryLoading: false,
      submitLoading: false,
      targetTypeList: [],
      employeeStatus: [],
      dataSource: [],
      columns: [],
      pageObj: {
        current: 1,
        pageSize: 10,
        total: 0
      }
    };
  }

  componentDidMount() {
    this.getTaxAgentRangeForm();
    this.getTaxAgentRangeListInclude();
  }

  getTaxAgentRangeForm = () => {
    const { getTaxAgentRangeForm } = this.props.taxAgentStore;
    getTaxAgentRangeForm().then(({ status, data }) => {
      if (status) {
        const { employeeStatus, targetTypeList } = data;
        this.setState({ employeeStatus, targetTypeList });
      }
    });
  };
  getTaxAgentRangeListInclude = (pageModule = {}) => {
    const { taxAgentId } = this.props;
    const { getTaxAgentRangeListInclude } = this.props.taxAgentStore;
    this.setState({ queryLoading: true });
    getTaxAgentRangeListInclude({ ...pageModule, taxAgentId }).then(
      ({ status, data }) => {
        this.setState({ queryLoading: false });
        if (status && !_.isEmpty(data)) {
          const {
            columns,
            list: dataSource,
            pageNum: current,
            pageSize,
            total
          } = data;
          this.setState({
            columns,
            dataSource,
            pageObj: {
              current,
              pageSize,
              total
            }
          });
        }
      }
    );
  };

  taxAgentRangelistExt = (pageModule = {}) => {
    const { taxAgentId } = this.props;
    const { taxAgentRangelistExt } = this.props.taxAgentStore;
    this.setState({ queryLoading: true });
    taxAgentRangelistExt({ ...pageModule, taxAgentId }).then(
      ({ status, data }) => {
        this.setState({ queryLoading: false });
        if (status && !_.isEmpty(data)) {
          const {
            columns,
            list: dataSource,
            pageNum: current,
            pageSize,
            total
          } = data;
          this.setState({
            columns,
            dataSource,
            pageObj: {
              current,
              pageSize,
              total
            }
          });
        }
      }
    );
  };
  getTaxAgentRangeListExclude = (pageModule = {}) => {
    const { taxAgentId } = this.props;
    const { getTaxAgentRangeListExclude } = this.props.taxAgentStore;
    this.setState({ queryLoading: true });
    getTaxAgentRangeListExclude({ ...pageModule, taxAgentId }).then(
      ({ status, data }) => {
        this.setState({ queryLoading: false });
        if (status && !_.isEmpty(data)) {
          const {
            columns,
            list: dataSource,
            pageNum: current,
            pageSize,
            total
          } = data;
          this.setState({
            columns,
            dataSource,
            pageObj: {
              current,
              pageSize,
              total
            }
          });
        }
      }
    );
  };

  taxAgentRangeDelete = ({ ids, tab }) => {
    Modal.confirm({
      title: "信息确认",
      content: `确认删除该条数据吗?`,
      onOk: () => {
        return new Promise((resolve, reject) => {
          return tab === "2" ? this.taxAgentExtRangeDeleteComfirm(resolve, reject, ids) : this.taxAgentRangeDeleteComfirm(resolve, reject, ids, tab);
        }).catch(() => console.log("出错!"));
      }
    });
  };
  taxAgentExtRangeDeleteComfirm = (resolve, reject, ids) => {
    const { taxAgentRangeExtDelete } = this.props.taxAgentStore;
    const { pageObj } = this.state;
    message.destroy();
    message.loading("正在删除中...", 0);
    taxAgentRangeExtDelete(ids).then(({ status, errormsg }) => {
      message.destroy();
      resolve();
      if (status) {
        this.tagAgentRef.onSelectChange([]);
        message.success("删除成功");
        this.taxAgentRangelistExt({
          current: pageObj.current,
          pageSize: pageObj.pageSize
        });
      } else {
        message.error(errormsg || "删除失败");
      }
    }).catch(() => {
      message.destroy();
      reject();
    });
  };
  taxAgentRangeDeleteComfirm = (resolve, reject, ids, tab) => {
    const { taxAgentRangeDelete } = this.props.taxAgentStore;
    const { pageObj } = this.state;
    message.destroy();
    message.loading("正在删除中...", 0);
    taxAgentRangeDelete(ids).then(({ status, errormsg }) => {
      message.destroy();
      resolve();
      if (status) {
        this.tagAgentRef.onSelectChange([]);
        message.success("删除成功");
        tab == "1"
          ? this.getTaxAgentRangeListInclude({
            current: pageObj.current,
            pageSize: pageObj.pageSize
          })
          : this.getTaxAgentRangeListExclude({
            current: pageObj.current,
            pageSize: pageObj.pageSize
          });
      } else {
        message.error(errormsg || "删除失败");
      }
    }).catch(() => {
      message.destroy();
      reject();
    });
  };

  taxAgentRangeSave = (module) => {
    const { taxAgentId } = this.props;
    const { pageObj } = this.state;
    const { includeType, id } = module;
    const { taxAgentRangeSave, taxAgentRangeExtSave } = this.props.taxAgentStore;
    this.setState({ submitLoading: true });
    if (includeType === "2") {
      taxAgentRangeExtSave({ taxAgentId, ..._.pick(module, "targetIds") }).then(({ status, errormsg }) => {
        this.setState({ submitLoading: false });
        if (status) {
          this.tagAgentRef.closeModal();
          message.success("新增成功");
          this.taxAgentRangelistExt({ current: pageObj.current, pageSize: pageObj.pageSize });
        } else {
          message.error(errormsg || "新增失败");
        }
      });
    } else {
      const API= id ? taxAgentRangeEdit : taxAgentRangeSave;
      API({ ...module, taxAgentId }).then(({ status, errormsg }) => {
        this.setState({ submitLoading: false });
        if (status) {
          this.tagAgentRef.closeModal();
          message.success("操作成功");
          includeType == "1"
            ? this.getTaxAgentRangeListInclude({
              current: pageObj.current,
              pageSize: pageObj.pageSize
            })
            : this.getTaxAgentRangeListExclude({
              current: pageObj.current,
              pageSize: pageObj.pageSize
            });
        } else {
          message.error(errormsg);
        }
      });
    }
  };

  render() {
    const {
      dataSource,
      columns,
      pageObj,
      employeeStatus,
      targetTypeList,
      submitLoading,
      queryLoading
    } = this.state;
    const { hideIconInTax, showSalaryItemBtn } = this.props.taxAgentStore;
    return (
      <div style={personScopeWarrper} className="slideWrapper">
        <SlideTaxagentUser
          ref={(dom) => (this.tagAgentRef = dom)}
          {...this.props}
          dataSource={dataSource}
          columns={columns}
          pageObj={pageObj}
          hideIconInTax={hideIconInTax}
          showSalaryItemBtn={showSalaryItemBtn}
          submitLoading={submitLoading}
          queryLoading={queryLoading}
          setPageObj={(params) => {
            const { current, pageSize, tab } = params;
            this.setState(
              {
                pageObj: {
                  ...pageObj,
                  current,
                  pageSize
                }
              },
              () => {
                tab == "1"
                  ? this.getTaxAgentRangeListInclude({ ...pageObj, ...params })
                  : tab == "0" ? this.getTaxAgentRangeListExclude({ ...pageObj, ...params }) :
                    this.taxAgentRangelistExt({ ...pageObj, ...params });
              }
            );
          }}
          employeeStatus={employeeStatus}
          targetTypeList={targetTypeList}
          onDeleteTaxAgent={this.taxAgentRangeDelete}
          onTaxAgentSave={this.taxAgentRangeSave}
          onTaxAngetSearch={({ targetName, targetWorkcode, tab }) => {
            tab == "1"
              ? this.getTaxAgentRangeListInclude({ targetName, targetWorkcode })
              : tab == "0" ? this.getTaxAgentRangeListExclude({ targetName, targetWorkcode }) :
                this.taxAgentRangelistExt({ targetName, targetWorkcode });
          }}
          onChangeTab={(tab) => {
            this.tagAgentRef.onSelectChange([]);
            tab == "1"
              ? this.getTaxAgentRangeListInclude()
              : tab == "0" ? this.getTaxAgentRangeListExclude() : this.taxAgentRangelistExt();
          }}
        />
      </div>
    );
  }
}
