/*
  *  数据推送
  * 推送记录
  * @Author:    黎永顺
  * @Date:      2025/4/1
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { 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 },
      selectedRowKeys: []
    };
  }

  componentDidMount() {
    this.getPushRecordList();
  }

  componentWillUnmount() {
    this.setState({ selectedRowKeys: [] }, () => this.props.onChange("rowKey", []));
  }

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

  getPushRecordList = (props) => {
    const { pageInfo } = this.state, { query } = props || this.props;
    const payload = { ...pageInfo, ...query };
    this.setState({ loading: true });
    API.getPushRecordList(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,
          columns: [...columns, {
            title: getLabel(111, "操作"), dataIndex: "opts", width: 140, render: (__, record) => (<React.Fragment>
              <a href="javascript: void(0);" style={{ marginRight: 10 }}
                 onClick={_.debounce(() => this.props.onChange("push", record), 300)}>{getLabel(111, "推送")}</a>
              <a href="javascript: void(0);" style={{ marginRight: 10 }}
                 onClick={_.debounce(() => this.props.onChange("withdraw", record), 300)}>{getLabel(111, "撤回")}</a>
              <a href="javascript: void(0);"
                 onClick={() => this.props.onChange("view", record)}>{getLabel(111, "查看详情")}</a>
            </React.Fragment>)
          }]
        });
      }
    });
  };

  render() {
    const { columns, dataSource, loading, pageInfo, selectedRowKeys } = 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.getPushRecordList());
      },
      onChange: current => {
        this.setState({ pageInfo: { ...pageInfo, current } }, () => this.getPushRecordList());
      }
    };
    const rowSelection = {
      selectedRowKeys,
      onChange: v => this.setState({ selectedRowKeys: v }, () => this.props.onChange("rowKey", v))
    };
    return (<WeaTable rowKey="id" loading={loading} dataSource={dataSource} columns={columns} pagination={pagination}
                      rowSelection={rowSelection} scroll={{ y: `calc(100vh - 182px)` }}/>);
  }
}

export default Index;
