/*
  *  浮动薪酬
  * 薪资项目列表
  * @Author:    黎永顺
  * @Date:      2024/8/8
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaLocaleProvider, WeaTable } from "ecCom";
import { message, Modal } from "antd";
import * as API from "../../../../apis/variableSalary";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentDidMount() {
    this.getVariableSalaryItemList();
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.isQuery !== this.props.isQuery) this.setState({
      pageInfo: { ...this.state.pageInfo, current: 1 }
    }, () => this.getVariableSalaryItemList());
  }

  getVariableSalaryItemList = () => {
    const { baseTableStore: { VSalryForm } } = this.props;
    const { pageInfo } = this.state;
    const { username: itemName } = VSalryForm.getFormParams();
    this.setState({ loading: true });
    API.getVariableSalaryItemList({ ...pageInfo, itemName }).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { list: dataSource, columns, pageNum: current, pageSize, total } = data;
        this.setState({
            pageInfo: { ...pageInfo, current, pageSize, total }, dataSource,
            columns: [
              ..._.filter(columns, o => o.dataIndex !== "id"),
              {
                title: getLabel(111, "操作"), dataIndex: "oprate",
                render: (__, record) => (<React.Fragment>
                  <a href="javascript: void(0)" style={{ marginRight: 10 }}
                     onClick={() => this.handleEdit(record.id)}>{getLabel(111, "编辑")}</a>
                  {
                    record.canDelete && <a href="javascript: void(0)"
                                           onClick={() => this.handleDelete([record.id])}>{getLabel(111, "删除")}</a>
                  }
                </React.Fragment>)
              }
            ]
          }
        );
      }
    });
  };
  handleEdit = (id) => {
    API.getVariableSalaryItemDetail({ id }).then(({ status, data }) => {
      if (status) this.props.onEditSalaryItem(data);
    });
  };
  handleDelete = (itemIds) => {
    Modal.confirm({
      title: getLabel(111, "信息确认"),
      content: getLabel(111, "确认删除吗?"),
      onOk: () => {
        API.deleteVariableSalaryItem({ itemIds }).then(({ status, errormsg }) => {
          if (status) {
            message.success(getLabel(111, "删除成功"));
            this.getVariableSalaryItemList();
          } else {
            message.error(errormsg);
          }
        });
      }
    });
  };

  render() {
    const { columns, dataSource, loading, pageInfo } = 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.getVariableSalaryItemList());
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.getVariableSalaryItemList());
      }
    };
    return (
      <WeaTable columns={columns} dataSource={dataSource} loading={loading} bordered
                pagination={pagination} scroll={{ y: `calc(100vh - 170px)` }}/>
    );
  }
}

export default Index;
