import { Button, Descriptions,Popconfirm, Row, Col, Form,Modal, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude,extendIgnoreDataRule, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import styles from '../HI/StepForm.css';
import config from "../../../config";
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import { message } from "antd/lib/index";
import FormPage from '../../../components/FormPage'
class PT017ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            tmpData: {},
        };
    };

    componentDidMount() {
        if (this.props.data?.id)
            this.loadData(this.props.data?.id);
    }

    componentWillReceiveProps(NextProps) {
        if (NextProps.data?.id && this.props.data?.id != NextProps.data?.id) {
            this.loadData(NextProps.data?.id);
        }
    }

    onTableBtnAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'HM/HMJobtaskIdentify/IdentityUpdate',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }

    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined"&&typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }

    loadData = (dataId) => {
        let that=this;
        let json = initFilter(that.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_User')
        extendInclude(json, 'Nav_Department')
        extendInclude(json, 'Nav_Formulates')
        extendInclude(json, 'Nav_Formulates.Nav_Formulate')
        extendInclude(json, 'Nav_PlanResources')
        extendInclude(json, 'Nav_PlanResources.Nav_Resources')
        extendInclude(json, 'Nav_Files')
        extendInclude(json, 'Nav_Files.Nav_ImgFile')
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File')
        extendInclude(json, 'Nav_PlanFiles')
        extendInclude(json, 'Nav_PlanFiles.Nav_ImgFile')
        extendInclude(json, 'Nav_PlanFiles.Nav_ImgFile.Nav_File')
        extendInclude(json, 'Nav_Goals')
        extendIgnoreDataRule(json)
       that.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'PT/IndicatorFormulation/Get',
            onComplete: (ret) => {
                if (ret) {
                    if(that.props.data && that.props.data.loadDataDeal)
                    {
                        that.props.data.loadDataDeal(1);
                    }
                    that.setState({ data: ret });
                    if (that.props.data.tableKey==="2" ||that.props.data.tableKey===undefined) {
                        that.setState({ BtnAgreeDisplay: 'none' })
                    }else{
                        that.setState({ BtnAgreeDisplay: 'inline' })
                    }
                    this.onClickApprove(false);
                    //归档才显示打印和导出按钮
                    // if(ret.PRE_MEETING_STATUS==5){
                    //     this.setState({ isFinished: 'inline' })
                    // }
                }
                else
                { message.error('请先尝试刷新，若仍然加载失败，请联系管理员排查!');}
            }
        });
    }

    onTableBtnExport() {
        let TableWrap = document.getElementById('tableId' + this.props.data.id);
        let Table = TableWrap.getElementsByTagName('table')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, "安全生产目标与指标制定表.xlsx")
    }

    onClickApprove=(appVisible)=>{
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true,key:guid(),param:this.props.data.param,appVisible:appVisible,BtnAgreeDisplay:'inline'},
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    render() {
        const { data } = this.state;
        // let aaa = Math.random();
        return <div>
            <div style={{ padding: '10px' }}>
            <Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px'}} icon="export">审批详情</Button>
                <ReactToPrint
                    trigger={() => <Button style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 5?"inline":"none" }} type={'default'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 5?"inline":"none" }}>导出</Button>
                {/* <Button type="primary" style={{marginLeft: '8px', display: this.state.BtnAgreeDisplay }} onClick={() => this.onTableBtnAgree()} icon="check" >确认</Button> */}
            </div>
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center', margin: '15px' }}>安全生产目标与指标制定表</h1>
                {
                     data ?
                     <div>
                        <Descriptions size="middle" bordered className={styles.description}>
                            <Descriptions.Item label="文件名称">{data.NAME}</Descriptions.Item>
                            <Descriptions.Item label="年度">{data.YEAR}</Descriptions.Item>
                            {/* .substring(0, 10) */}
                            <Descriptions.Item label="制定时间">{data.SET_TIME}</Descriptions.Item>
                            <Descriptions.Item label="制定人员">{data.Nav_User ? data.Nav_User.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="制定部门">{data.Nav_Department ? data.Nav_Department.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="制定要求">
                            {
                                data.Nav_Formulates && data.Nav_Formulates.map((item, i) => {
                                    return <tr><label> {item.Nav_Formulate?.NAME}</label></tr>
                                })
                            }
                            </Descriptions.Item>
                            <Descriptions.Item label="是否体现安全管理的要求">{data.IS_DEMAND == true?"是":"否"}</Descriptions.Item>
                            <Descriptions.Item label="只有伤亡指标">{data.IS_TARGET == true?"是":"否"}</Descriptions.Item>
                            <Descriptions.Item label="年度安全生产目标与指标附件">
                            {
                                data.Nav_Files && data.Nav_Files.map((item, i) => {
                                    return <a title={item.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} >{item.Nav_ImgFile.FILE_NAME} </a>
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="是否制定实施计划">{data.IS_PLAN == true?"是":"否"}</Descriptions.Item>
                            <Descriptions.Item label="制定实施计划考虑资源">
                            {
                                data.Nav_PlanResources && data.Nav_PlanResources.map((item, i) => {
                                    return <tr><label> {item.Nav_Resources?.NAME}</label></tr>
                                })
                            }
                            </Descriptions.Item>
                            <Descriptions.Item label="实施计划附件">
                            {
                                data.Nav_PlanFiles && data.Nav_PlanFiles.map((item, i) => {
                                    return <a title={item.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} >{item.Nav_ImgFile.FILE_NAME} </a>
                                })
                            }</Descriptions.Item>
                            {/* <Descriptions.Item label="修订原因">{data.REASON}</Descriptions.Item>
                            <Descriptions.Item label="修订概要">{data.PROFILE}</Descriptions.Item>
                            <Descriptions.Item label="修订版本号">{data.VERSION==0?"":data.VERSION}</Descriptions.Item> */}
                        </Descriptions>
                     </div>:null
                }
                {/* {
                     data ?
                     <div>
                        <Descriptions size="middle" bordered>
                            <Descriptions.Item label="修订原因">{data.REASON}</Descriptions.Item>
                            <Descriptions.Item label="修订概要">{data.PROFILE}</Descriptions.Item>
                            <Descriptions.Item label="修订版本号">{data.VERSION==0?"":data.VERSION}</Descriptions.Item>
                        </Descriptions>
                     </div>:null
                } */}
                {
                    data ?  <table style={{ width: '100%', textAlign: 'left'}} className={styles.PrintForm1}>
                    <tr>
                    <td width='150px' className={styles.fontBold}>序号</td>
                    <td className={styles.fontBold}>安全生产目标与指标</td>
                    </tr>
                    {
                                    data.Nav_Goals && data.Nav_Goals.map((item,i)=>{
                                        return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item.NAME}
                                            </td>
                                        </tr>
                                    })
                                }
                    </table>:null 
                }
                <FormPage {...this.state.tmpData} />
            </div>
        </div>
    }

}

export default connect(({ login }) => ({ login }))(PT017ShowPrint) 