/*
  *  数据推送列表
  *
  * @Author:    黎永顺
  * @Date:      2024/11/19
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaCheckbox, WeaLocaleProvider, WeaTable } from "ecCom";
import * as API from "../../../../apis/datapush";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentDidMount() {
    this.getPushSettingList();
  }

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

  getPushSettingList = (props) => {
    const { pageInfo } = this.state, { query } = props || this.props;
    const payload = { ...pageInfo, ...query };
    this.setState({ loading: true });
    API.getPushSettingList(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize, total },
          dataSource: _.map(dataSource, o => ({
            ...o, salarySobs: _.map(o.salarySobs, k => k.name).join(","),
            salarySobIds: _.map(o.salarySobs, k => k.id).join(",")
          })),
          columns: [..._.map(columns, o => {
            if (o.dataIndex === "able") return {
              ...o, render: v => (<WeaCheckbox value={String(v)} disabled display="switch"/>)
            };
            return { ...o };
          }), {
            title: getLabel(111, "操作"), dataIndex: "opts", width: 120, render: (__, record) => (<React.Fragment>
              <a href="javascript: void(0);" style={{ marginRight: 10 }}
                 onClick={() => this.props.onChange("edit", record)}>{getLabel(111, "编辑")}</a>
              <a href="javascript: void(0);" style={{ marginRight: 10 }}
                 onClick={() => this.props.onChange("del", record.id)}>{getLabel(111, "删除")}</a>
            </React.Fragment>)
          }]
        });
      }
    });
  };

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

export default Index;
