/*
 * Author: 黎永顺
 * name: 统计维度管理列表
 * Description:
 * Date: 2023/4/11
 */
import React, { Component } from "react";
import { WeaLocaleProvider, WeaTable } from "ecCom";
import { message, Modal } from "antd";
import { dimensionDelete, dimensionList } from "../../../apis/statistics";
import DimensionExtensionAttrsDialog from "./dimensionExtensionAttrsDialog";
import "../index.less";

const { getLabel } = WeaLocaleProvider;

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

  componentDidMount() {
    this.dimensionList();
  }

  dimensionList = (extra = {}) => {
    const { pageInfo } = this.state;
    this.setState({ loading: true });
    dimensionList({ ...pageInfo, ...extra }).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { pageNum: current, pageSize, total, list: dataSource } = data;
        this.setState({
          dataSource,
          pageInfo: {
            ...pageInfo,
            current, pageSize, total
          }
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };
  dimensionDelete = (payload) => {
    Modal.confirm({
      title: getLabel(111, "信息确认"),
      content: getLabel(111, "确认要删除吗?"),
      onOk: () => {
        dimensionDelete(payload).then(({ status, errormsg }) => {
          if (status) {
            message.success(getLabel(111, "删除成功"));
            this.dimensionList();
          } else {
            message.error(errormsg || getLabel(111, "删除失败"));
          }
        });
      }
    });
  };

  render() {
    const { dataSource, loading, pageInfo, visible } = this.state;
    const { onEdit } = 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.dimensionList());
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.dimensionList());
      }
    };
    const columns = [
      { dataIndex: "dimName", title: getLabel(111, "统计维度") },
      { dataIndex: "remark", title: getLabel(111, "描述") },
      { dataIndex: "dimType", title: getLabel(111, "维度类型") },
      {
        dataIndex: "operate", title: getLabel(111, "操作"),
        render: (_, record) => {
          return (
            <span className="space10">
              {
                record.canEdit &&
                <a href="javascript: void(0);" onClick={() => onEdit(record.id)}>{getLabel(111, "编辑")}</a>
              }
              {
                record.canDelete &&
                <a href="javascript: void(0);"
                   onClick={() => this.dimensionDelete([record.id])}>{getLabel(111, "删除")}</a>
              }
              {
                record.dimName === getLabel(30042, "人员") && record.dimType === getLabel(111, "定性") &&
                <a href="javascript: void(0);"
                   onClick={() => this.setState({ visible: true })}>{getLabel(111, "扩展属性")}</a>
              }
            </span>
          );
        }
      }
    ];
    return (
      <React.Fragment>
        <WeaTable
          rowKey="id"
          className="dimensionTableWrapper"
          dataSource={dataSource}
          pagination={pagination}
          loading={loading}
          columns={columns}
        />
        <DimensionExtensionAttrsDialog visible={visible} onCancel={() => this.setState({ visible: false })}/>
      </React.Fragment>
    );
  }
}

export default DimensionTable;
