/*
 * Create:      柿子
 * CreateDate:  2023/04/23
 * Describe：   润乾报表
 * */
import React, { Component } from 'react';
import { Button, Modal } from 'antd';
import { Util } from 'tools';
import store from 'store';
import Reports from 'pages/raqSoft/reportsEmbed';

class RundryReport extends Component {
  constructor(props) {
    super(props);
    let userData = React.$getSessionData();
    this.state = {
      userData,
      categoryData: {},
      reportParam: {}, // 报表入参
      reportsKey: '', // 唯一标识
      reportResult:'',  //执行状态
      exportLoading: false
    }
  }

  componentDidMount() {
    let newCategory;
    if ('paras' in this.props && this.props.paras && this.props.paras.params) { // 侧菜单获取类别参数
      newCategory = this.props.paras.params;
    } else {
      if ('location' in this.props && this.props.location.state && this.props.location.state.params) { // 头菜单获取类别参数
        newCategory = this.props.location.state.params;
        React.$setSessionData('RundryReport', newCategory, false);
      } else { // 头菜单刷新界面时获取类别
        newCategory = React.$getSessionData('RundryReport', false);
      }
    }
    let categoryData = Util.getObjByUrlStr(newCategory);
    this.setState({ categoryData }, () => {
      this.getReportsKey();
    });
  }

  componentWillUnmount() {
    // 组件销毁前将静止让setState修改state的状态
    this.setState = (state, callback) => { return; }
  }

  getReportsKey = () => {
    const { userData, categoryData } = this.state;
    // 获取当前时间戳
    let timeStamp = new Date()?.getTime();
    let reportsKey = (categoryData?.key || 'rundryReport') + '_' + userData?.hospID + '_' + timeStamp;
    this.setState({ reportsKey })
  };

  // 开始执行
  handleExport = async () => {
    try {
      this.setState({ exportLoading: true ,reportResult:''});
      const { reportsKey } = this.state;
      let data = {
        params: [{
          key: reportsKey,
          reportsKey
        }]
      };
      let res = await React.$asyncPost(this, '11020009', data);
      this.setState({ exportLoading: false,reportResult:res?.result }, () => {
        // Modal.confirm({
        //   title: '数据导出中，完成后可在"导出记录"中下载',
        //   okText: '去下载',
        //   cancelText: '继续查询',
        //   onOk: this.handleDownload,
        //   onCancel: this.getReportsKey,
        // });
        // this.getReportsKey();
      });
    } catch (error) {
      this.setState({ exportLoading: false });
    }
  };

  // 去下载
  // handleDownload = () => {
  //   store.dispatch({
  //     type: 'downloadTask',
  //     value: {
  //       timeStamp: new Date().getTime(),
  //       visible: 'Y'
  //     }
  //   });
  //   this.getReportsKey();
  // }

  render() {
    const { categoryData, reportParam, reportsKey, exportLoading ,reportResult} = this.state;
    return (
      <div style={{ height: '100%', padding: '12px' }}>
        <div style={{ textAlign: 'right', paddingBottom: '12px' }}>
          <span style={{marginRight:'50px'}}>{reportResult ? reportResult : ''}</span>
          <Button loading={exportLoading} onClick={this.handleExport}>开始执行</Button>
        </div>
        <div style={{ height: '96%' }}>
          {categoryData && 'raqName' in categoryData && categoryData.raqName ? <Reports
            id={reportsKey}
            params={{ ...reportParam, reportsKey }}
            raqName={categoryData?.raqName || ''}
          /> : ''}
        </div>
      </div>
    )
  }
};

export default RundryReport;
