/*
  *  扣除名单确认
  * @Author:    黎永顺
  * @Date:      2025/3/26
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaButtonIcon, WeaDialog, WeaLocaleProvider, WeaTable, WeaTools } from "ecCom";
import DeductionListConfirmEmployeeDialog from "./deductionListConfirmEmployeeDialog";
import DeductionAmountEditDialog from "./deductionAmountEditDialog";
import { Button, Col, message, Modal, Row } from "antd";
import * as API from "../../../apis/declare";
import { calcPageNo } from "../../../util";

const { getLabel } = WeaLocaleProvider, { getUrlParams } = WeaTools;

class DeductionListConfirmDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      pageInfo: { current: 1, pageSize: 10, total: 0 }, loading: false, columns: [], dataSource: [], visible: false,
      selectedRowKeys: [], editDialog: { visible: false, record: {} }, loadingBtn: { confirm: false, feedback: false }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.getDeductionAmountList(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) this.setState({
      pageInfo: { current: 1, pageSize: 10, total: 0 }, loading: false, dataSource: [], selectedRowKeys: []
    });
  }

  getDeductionAmountList = (props) => {
    const { pageInfo } = this.state, { id: taxAgentId } = getUrlParams(), { year } = props || this.props;
    const payload = { ...pageInfo, taxAgentId, year };
    this.setState({ loading: true });
    API.getDeductionAmountList(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
        this.setState({
          dataSource, pageInfo: { ...pageInfo, current, pageSize, total },
          columns: [..._.map(columns, o => {
            if (o.dataIndex === "deductFlag") {
              return { ...o, render: v => (<span>{v === 1 ? getLabel(111, "是") : getLabel(111, "否")}</span>) };
            }
            return { ...o };
          }), {
            title: getLabel(111, "操作"), dataIndex: "action",
            render: (v, record) => (<a href="javascript:void(0)" onClick={() => this.setState({
              editDialog: { visible: true, record }
            })}>{getLabel(111, "编辑")}</a>)
          }]
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };
  handleDelete = () => {
    Modal.confirm({
      title: getLabel(111, "信息确认"),
      content: getLabel(111, "确认删除本条数据吗?"),
      onOk: () => {
        API.deleteDeductionAmount({ ids: this.state.selectedRowKeys }).then(({ status, errormsg }) => {
          if (status) {
            const { pageInfo } = this.state, { current, pageSize, total } = pageInfo;
            message.success(getLabel(111, "操作成功"));
            this.setState({
              selectedRowKeys: [],
              pageInfo: {
                ...pageInfo, current: calcPageNo(total, current, pageSize, this.state.selectedRowKeys.length)
              }
            }, () => this.getDeductionAmountList());
          } else {
            message.error(errormsg);
          }
        });
      }
    });
  };
  confirmDeductionAmount = () => {
    const { id: taxAgentId } = getUrlParams(), { year } = this.props;
    const payload = { taxAgentId, year };
    this.setState({ loadingBtn: { ...this.state.loadingBtn, confirm: true } });
    API.confirmDeductionAmount(payload).then(({ status, errormsg }) => {
      this.setState({ loadingBtn: { ...this.state.loadingBtn, confirm: false } });
      if (status) {
        message.success(getLabel(111, "操作成功"));
        this.getDeductionAmountList();
      } else {
        message.error(errormsg);
      }
    });
  };
  feedbackDeductionAmount = () => {
    const { id: taxAgentId } = getUrlParams(), { year } = this.props;
    const payload = { taxAgentId, year };
    this.setState({ loadingBtn: { ...this.state.loadingBtn, confirm: true } });
    API.feedbackDeductionAmount(payload).then(({ status, errormsg }) => {
      this.setState({ loadingBtn: { ...this.state.loadingBtn, confirm: false } });
      if (status) {
        message.success(getLabel(111, "操作成功"));
        this.getDeductionAmountList();
      } else {
        message.error(errormsg);
      }
    });
  };

  render() {
    const { loading, columns, dataSource, pageInfo, visible, selectedRowKeys, editDialog, loadingBtn } = this.state;
    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, pageSize }
        }, () => this.getDeductionAmountList());
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.getDeductionAmountList());
      }
    };
    const rowSelection = {
      selectedRowKeys, onChange: v => this.setState({ selectedRowKeys: v })
    };
    const height = this.refs.employeeRef ? this.refs.employeeRef.state.height : 400;
    return (
      <WeaDialog {...this.props} initLoadCss ref="confirmRef" className="confirmationDialog"
                 title={(<Row type="flex">
                   <Col span={12}>
                     <span className="title">{getLabel(111, "扣除名单确认")}</span>
                   </Col>
                   <Col span={12} className="title-right">
                     <Button type="primary" onClick={this.confirmDeductionAmount}
                             loading={loadingBtn.confirm}>{getLabel(111, "确认")}</Button>
                     <Button type="primary" onClick={this.feedbackDeductionAmount}
                             loading={loadingBtn.feedback}>{getLabel(111, "反馈")}</Button>
                   </Col>
                 </Row>)}
                 style={{
                   width: 1150, height: 490, minHeight: 200, minWidth: 380, maxHeight: "90%",
                   maxWidth: "90%", overflow: "hidden", transform: "translate(0px, 0px)"
                 }}>
        <div className="confirmationDialogContent">
          <div className="tableBtns">
            <WeaButtonIcon buttonType="add" type="primary" title={getLabel(111, "添加")}
                           onClick={() => this.setState({ visible: true })}/>
            <WeaButtonIcon buttonType="del" type="primary" title={getLabel(111, "删除")} onClick={this.handleDelete}
                           disabled={_.isEmpty(selectedRowKeys)}/>
          </div>
          <WeaTable columns={columns} dataSource={dataSource} loading={loading} pagination={pagination} rowKey="id"
                    rowSelection={rowSelection} scroll={{ y: `calc(${height}px - 113px)` }}/>
          <DeductionAmountEditDialog {...editDialog} onSuccess={this.getDeductionAmountList}
                                     onCancel={(callback) => this.setState({
                                       editDialog: { visible: false, record: {} }
                                     }, () => callback && callback())}/>
          <DeductionListConfirmEmployeeDialog visible={visible} onSuccess={this.getDeductionAmountList}
                                              year={this.props.year} payload={this.props.payload}
                                              onCancel={(callback) => this.setState({ visible: false }, () => callback && callback())}/>
        </div>
      </WeaDialog>
    );
  }
}

export default DeductionListConfirmDialog;
