/*
  *  扣除名单确认
  * 人员添加列表
  * @Author:    黎永顺
  * @Date:      2025/3/26
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaDialog, WeaLocaleProvider, WeaTable, WeaTools } from "ecCom";
import { Button, message } from "antd";
import * as API from "../../../apis/declare";

const { getLabel } = WeaLocaleProvider, { getUrlParams } = WeaTools;
const columns = [{ title: getLabel(111, "姓名"), dataIndex: "employeeName" }, {
  title: getLabel(111, "分部"),
  dataIndex: "subCompanyName"
}, { title: getLabel(111, "部门"), dataIndex: "departmentName" }, {
  title: getLabel(111, "个税扣缴义务人"),
  dataIndex: "taxAgentName"
}, { title: getLabel(111, "工号"), dataIndex: "jobNum" }, { title: getLabel(111, "手机号码"), dataIndex: "mobile" }];

class DeductionListConfirmEmployeeDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      pageInfo: { current: 1, pageSize: 10, total: 0 }, loading: false, dataSource: [], selectedRowKeys: [],
      saveloading: false
    };
  }

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

  employeedeclareList = (props) => {
    const { pageInfo } = this.state, { payload } = props || this.props;
    this.setState({ loading: true });
    API.employeedeclareList({ ...payload, ...pageInfo }).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { pageInfo: result } = data;
        const { list: dataSource, pageNum: current, pageSize, total } = result;
        this.setState({ dataSource, pageInfo: { ...pageInfo, current, pageSize, total } });
      }
    }).catch(() => this.setState({ loading: false }));
  };
  save = () => {
    const { selectedRowKeys: employeeDeclareIds } = this.state, { id: taxAgentId } = getUrlParams(), { year } = this.props;
    const payload = { taxAgentId, year, employeeDeclareIds };
    this.setState({ saveloading: true });
    API.addDeductionAmount(payload).then(({ status, errormsg }) => {
      this.setState({ saveloading: false });
      if (status) {
        message.success(getLabel(111, "操作成功"));
        this.props.onCancel(this.props.onSuccess);
      } else {
        message.error(errormsg);
      }
    });
  };

  render() {
    const { loading, dataSource, pageInfo, selectedRowKeys, saveloading } = 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.employeedeclareList());
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.employeedeclareList());
      }
    };
    const rowSelection = {
      selectedRowKeys, onChange: v => this.setState({ selectedRowKeys: v })
    };
    const height = this.refs.employeeRef ? this.refs.employeeRef.state.height : 400;
    return (<WeaDialog {...this.props} hasScroll initLoadCss title={getLabel(111, "选择人员")} ref="employeeRef"
                       buttons={[<Button type="primary" disabled={_.isEmpty(selectedRowKeys)} loading={saveloading}
                                         onClick={this.save}>{getLabel(111, "确认")}</Button>,
                         <Button type="ghost" onClick={() => this.props.onCancel()}>{getLabel(111, "取消")}</Button>]}
                       style={{
                         width: 1150, height: 490, minHeight: 200, minWidth: 380, maxHeight: "90%", maxWidth: "90%",
                         overflow: "hidden", transform: "translate(0px, 0px)"
                       }}>
      <div className="confirmationDialogContent">
        <WeaTable columns={columns} dataSource={dataSource} loading={loading} pagination={pagination} rowKey="id"
                  rowSelection={rowSelection} scroll={{ y: `calc(${height}px - 113px)` }}/>
      </div>
    </WeaDialog>);
  }
}

export default DeductionListConfirmEmployeeDialog;
