/*
  * 其他免税扣除设置
  *
  * @Author:    黎永顺
  * @Date:      2024/12/24
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaButtonIcon, WeaDialog, WeaLocaleProvider, WeaTable } from "ecCom";
import { message, Modal, Spin } from "antd";
import { postFetch } from "../../../util/request";
import DetailSettingsDialog from "./detailSettingsDialog";

const getLabel = WeaLocaleProvider.getLabel;

class TaxSetDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataSource: [], columns: [], loading: false, pageInfo: { current: 1, pageSize: 10, total: 0 },
      selectedRowKeys: [], detailSettingsDialog: { visible: false, dataType: "", mainId: "", id: "", record: {} }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      this.setState({
        detailSettingsDialog: { ...this.state.detailSettingsDialog, dataType: nextProps.dataType, mainId: nextProps.id }
      }, () => this.getList(nextProps));
    } else if (nextProps.visible !== this.props.visible && !nextProps.visible) {
      this.setState({
        dataSource: [],
        columns: [],
        loading: false,
        pageInfo: { current: 1, pageSize: 10, total: 0 },
        selectedRowKeys: [],
        detailSettingsDialog: { visible: false }
      });
    }
  }

  getList = (props) => {
    const { id, dataType, viewParams = {} } = props || this.props, { pageInfo, detailSettingsDialog } = this.state;
    this.setState({ loading: true });
    postFetch(`/api/bs/hrmsalary/otherDeduction/${dataType}List`, { ...pageInfo, ...viewParams, id })
      .then(({ status, data }) => {
        this.setState({ loading: false });
        if (status) {
          const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
          this.setState({
            dataSource, pageInfo: { current, pageSize, total }, columns: _.map(columns, o => ({
              ...o,
              width: o.dataIndex === "operate" && 120,
              render: (text, record) => o.dataIndex === "operate" ? (<div className="space_div">
                <a href="javascript:void(0);" onClick={() => this.setState({
                  detailSettingsDialog: { ...detailSettingsDialog, visible: true, id: record.id, record }
                })}>{getLabel(111, "编辑")}</a>
                <a href="javascript:void(0);"
                   onClick={() => this.handleDelete([record.id])}>{getLabel(111, "删除")}</a>
              </div>) : (<span>{text}</span>)
            }))
          });
        }
      });
  };
  handleDelete = (ids = []) => {
    const { selectedRowKeys } = this.state, { dataType, id: mainId } = this.props;
    Modal.confirm({
      title: getLabel(131329, "信息确认"),
      content: getLabel(388758, "确认要删除吗?"),
      onOk: () => {
        postFetch(`/api/bs/hrmsalary/otherDeduction/delete${_.upperFirst(dataType)}`, { ids, mainId })
          .then(({ status, errormsg }) => {
            if (status) {
              message.success(getLabel(502230, "删除成功！"));
              this.setState({ selectedRowKeys: _.difference(selectedRowKeys, ids) }, () => this.getList());
            } else {
              message.error(errormsg);
            }
          });
      }
    });
  };

  render() {
    const { pageInfo, dataSource, loading, columns, selectedRowKeys, detailSettingsDialog } = this.state;
    const { viewParams } = this.props;
    const pagination = {
      ...pageInfo,
      showTotal: total => `${getLabel(18609, "共")} ${total} ${getLabel(18256, "条")}`,
      showQuickJumper: true,
      showSizeChanger: true,
      pageSizeOptions: ["10", "20", "50", "100"],
      onShowSizeChange: (current, pageSize) => {
        this.setState({ pageInfo: { ...pageInfo, current: 1, pageSize } }, () => this.getList());
      },
      onChange: current => {
        this.setState({ pageInfo: { ...pageInfo, current } }, () => this.getList());
      }
    };
    const rowSelection = {
      selectedRowKeys, onChange: (selectedRowKeys) => this.setState({ selectedRowKeys })
    };
    return (<WeaDialog {...this.props} initLoadCss className="sys-salary-wrapper" ref={dom => this.taxSetRef = dom}
                       title={`${getLabel(111, "附表明细")}（${this.props.label}）`}
                       style={{
                         width: "60vw", height: 600, minHeight: 200, minWidth: 380, maxHeight: "90%",
                         maxWidth: "90%", overflow: "hidden", transform: "translate(0px, 0px)"
                       }}>
      <div className="sys-item-table-box">
        <Spin spinning={loading && pageInfo.total === 0}>
          {
            _.isEmpty(viewParams) && <div className="sys-item-table-opts">
              <WeaButtonIcon buttonType="add" type="primary" title={getLabel(111, "添加")}
                             onClick={() => this.setState({
                               detailSettingsDialog: { ...detailSettingsDialog, visible: true }
                             })}/>
              <WeaButtonIcon buttonType="del" type="primary" title={getLabel(111, "删除")}
                             disabled={_.isEmpty(selectedRowKeys)} onClick={() => {
                if (_.isEmpty(selectedRowKeys)) {
                  message.warning(getLabel(111, "请选择要删除的数据！"));
                  return;
                }
                this.handleDelete(selectedRowKeys);
              }}/>
            </div>
          }
          <WeaTable columns={!_.isEmpty(viewParams) ? _.filter(columns, o => o.dataIndex !== "operate") : columns}
                    dataSource={dataSource} pagination={pagination} bordered
                    rowSelection={rowSelection} loading={loading} rowKey="id"
                    scroll={{ y: this.taxSetRef ? this.taxSetRef.state.height - 164 : 600 }}/>
          <DetailSettingsDialog {...detailSettingsDialog} onCancel={(callback) => this.setState({
            detailSettingsDialog: { ...detailSettingsDialog, visible: false, id: "" }
          }, () => callback && callback())} onSuccess={this.getList}/>
        </Spin>
      </div>
    </WeaDialog>);
  }
}

export default TaxSetDialog;
