/*
  *  薪资项目管理
  * 表格
  * @Author:    黎永顺
  * @Date:      2024/8/16
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaCheckbox, WeaLocaleProvider, WeaTable } from "ecCom";
import { Dropdown, Menu, Spin } from "antd";
import * as API from "../../apis/item";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentDidMount() {
    this.getItemList(this.props);
  }

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

  getItemList = (props) => {
    const { name } = props;
    const { pageInfo } = this.state;
    const payload = { name, ...pageInfo };
    this.setState({ loading: true });
    API.getItemList(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 === "useDefault" || o.dataIndex === "hideDefault" || o.dataIndex === "useInEmployeeSalary") {
              return { ...o, width: 80, render: text => <WeaCheckbox value={String(text)} disabled display="switch"/> };
            }
            return { ...o };
          })
        });
      }
    });
  };

  render() {
    const { dataSource, columns, pageInfo, loading } = this.state;
    const { selectedRowKeys, onChange, onDropMenuClick, showOperateBtn } = this.props;
    const pagination = {
      ...pageInfo,
      showTotal: total => `${getLabel(111, "共")} ${total} ${getLabel(111, "条")}`,
      showQuickJumper: true,
      showSizeChanger: true,
      pageSizeOptions: ["10", "20", "50", "100"],
      onShowSizeChange: (current, pageSize) => {
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize }
        }, () => this.getItemList(this.props));
      },
      onChange: current => {
        this.setState({ pageInfo: { ...pageInfo, current } }, () => this.getItemList(this.props));
      }
    };
    const rowSelection = {
      selectedRowKeys, onChange,
      getCheckboxProps: record => ({ disabled: !record.canDelete })
    };
    return (<Spin spinning={loading && pageInfo.total === 0}>
      <WeaTable rowKey="id" dataSource={dataSource} rowSelection={rowSelection} pagination={pagination}
                loading={loading} scroll={{ y: `calc(100vh - 180px)` }}
                columns={[
                  ...columns, {
                    dataIndex: "opt", title: getLabel(111, "操作"), width: 140,
                    render: (__, record) => (<React.Fragment>
                      <a style={{ marginRight: 8 }} onClick={() => onDropMenuClick("edit", record.id)}
                         href="javascript:void(0);">{showOperateBtn ? getLabel(111, "编辑") : getLabel(111, "查看")}</a>
                      {
                        showOperateBtn && record.canDelete &&
                        <a style={{ marginRight: 8 }} href="javascript:void(0);"
                           onClick={() => onDropMenuClick("delete", record.id)}>{getLabel(111, "删除")}</a>
                      }
                      <Dropdown overlay={
                        <Menu>
                          <Menu.Item>
                            <a href="javascript:void(0);"
                               onClick={() => onDropMenuClick("log", record.id)}>{getLabel(545781, "操作日志")}</a>
                          </Menu.Item>
                        </Menu>
                      }>
                        <a href="javascript:void(0)"><i className="icon-coms-more"/></a>
                      </Dropdown>
                    </React.Fragment>)
                  }
                ]}/>
    </Spin>);
  }
}

export default SalaryItemsTable;
