/*
  * 推送记录
  * 查看详情
  * @Author:    黎永顺
  * @Date:      2025/4/2
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaInputSearch, WeaLocaleProvider, WeaSlideModal, WeaTable, WeaTop } from "ecCom";
import { getPushRecordDetail } from "../../../../apis/datapush";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.getPushRecordDetail(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) this.setState({
      query: { name: "" }, pageInfo: { current: 1, pageSize: 10, total: 0 }
    });
  }

  getPushRecordDetail = (props) => {
    const { recordId } = props || this.props, { pageInfo, query } = this.state;
    const payload = { ...query, ...pageInfo, recordId };
    this.setState({ loading: true });
    getPushRecordDetail(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
        this.setState({ columns, dataSource, pageInfo: { current, pageSize, total } });
      }
    });
  };

  render() {
    const { query, loading, dataSource, columns, 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.getPushRecordDetail());
      },
      onChange: current => {
        this.setState({ pageInfo: { ...pageInfo, current } }, () => this.getPushRecordDetail());
      }
    };
    return (<WeaSlideModal
      {...this.props} className="pushDetailDialog"
      title={<WeaTop title={getLabel(111, "推送详情")} icon={<i className="icon-coms-fa"/>} iconBgcolor="#F14A2D"
                     buttons={[<WeaInputSearch value={query.name} onSearch={() => this.setState({
                       pageInfo: { ...pageInfo, current: 1 }
                     }, () => this.getPushRecordDetail())}
                                               onChange={v => this.setState({ query: { ...query, name: v } })}/>]}/>
      }
      direction="right" top={0} width={800} height={100}
      measureT="%" measureX="px" measureY="%"
      content={<div className="pushDetail_content">
        <WeaTable loading={loading} dataSource={dataSource} columns={columns} pagination={pagination}
                  scroll={{ y: `calc(100vh - 182px)` }}/>
      </div>}
    />);
  }
}

export default PushDetailDialog;
