import { Button, Descriptions,Popconfirm, Row, Col, Form, 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 FO017ShowPrint 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: 'FO/FOCrucialLicenseJob/IdentityUpdateNew',
            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 = () => {
        let that=this;
        let json = initFilter(that.props.login.OrgId);
        extendRule(json, 'ID', 1, that.props.data.id);
        extendIgnoreDataRule(json)
        // extendInclude(json, 'Nav_CrucialLicensePerson');
        // extendInclude(json, 'Nav_CrucialLicensePerson.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_SafeConfirms');
        // extendInclude(json, 'Nav_SafeMeasures');
        // extendInclude(json, 'Nav_DealMeasures');
        that.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'FO/FOCrucialLicenseJob/FullGet',
            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);
                }
                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;
        const enums=this.props.data.enums ? this.props.data.enums :this.props.app.enums ;
        return <div>
            <div style={{ padding: '10px' }}>
            <Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon="export">审批详情</Button>
                <ReactToPrint
                    trigger={() => <Button type={'default'} icon={'printer'} style={{ marginLeft: '8px' }}>打印</Button>}
                    content={() => this.componentRef}
                    pageStyle=".printDIV { padding:0 40px;} img{width :120px}"
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px',display: data && data.FORM_STATUS === 2?"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} className="printDIV">
                <h1 style={{ textAlign: 'center', margin: '15px' }}>关键作业/许可作业工作票</h1>
                {
                     data ?
                     <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tbody>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>编号</td>
                                <td colSpan={3} rowSpan={1}>{data.CODE}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>作业名称</td>
                                <td colSpan={3} rowSpan={1}>{data.Nav_OperationStep.NAME}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>审批层级</td>
                                <td colSpan={3} rowSpan={1}>{enums.HMOperationTaskAuditEnum.enums[data.AUDIT_LEVEL]}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>作业地点</td>
                                <td colSpan={3} rowSpan={1}>{data.JOB_LOCATION}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>开始时间</td>
                                <td colSpan={3} rowSpan={1}>{data.JOB_DATE?data.JOB_DATE:null}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>结束时间</td>
                                <td colSpan={3} rowSpan={1}>{data.JOB_END_DATE?data.JOB_END_DATE:null}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>作业申请人</td>
                                <td colSpan={3} rowSpan={1}>{data.Nav_ApplyUser ? data.Nav_ApplyUser.NAME : ''}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>监护人</td>
                                <td colSpan={3} rowSpan={1}>{data.Nav_MonitorUser ? data.Nav_MonitorUser.NAME : ''}</td>
                            </tr>
                            <tr>
                                            <td colSpan={2} className={styles.fontBold}>作业人员</td>
                                            <td colSpan={3} className={styles.fontBold}>部门</td>
                                            <td colSpan={5} className={styles.fontBold}>证件</td>
                                        </tr>
                                    
                                    
                                        {
                                            data.Nav_CrucialLicensePerson && data.Nav_CrucialLicensePerson.map((item,i)=>{
                                                return <tr>
                                                    <td colSpan={2}>
                                                        {item.Nav_User?item.Nav_User.NAME:''}
                                                    </td>
                                                    <td colSpan={3}> 
                                                        {(item.Nav_User && item.Nav_User.Nav_Department)?item.Nav_User.Nav_Department.NAME:''}
                                                    </td>
                                                    <td colSpan={5}>
                                                        {item.Nav_Train?item.Nav_Train?.CERTIFICATE_NAME:item.JOB_DOCUMENT}
                                                    </td>
                                                </tr>
                                            })
                                        }
                                <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>作业原因</td>
                                <td colSpan={8} rowSpan={1}>{data.JobCause}</td>
                            </tr>
                        </tbody>
                     </table>:null
                }
                {
                     data ?
                     <table style={{ width: '100%', textAlign: 'center', marginTop:'10px',borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tbody>
                            
                            <tr>
                                            <td  width="100px" className={styles.fontBold}>序号</td>
                                            <td  colSpan={9} className={styles.fontBold}>作业前安全确认</td>
                                        </tr>
                                        {
                                            data.Nav_SafeConfirms && data.Nav_SafeConfirms.map((item,i)=>{
                                                return <tr>
                                                    <td width="100px">
                                                        {i+1}
                                                    </td>
                                                    <td colSpan={9}>
                                                        {item.NAME}
                                                    </td>
                                                </tr>
                                            })
                                        }
                        </tbody>
                     </table>:null
                }
                {
                     data ?
                     <table style={{ width: '100%', textAlign: 'center', marginTop:'10px',borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tbody>
                            
                                         <tr>
                                            <td width="100px" className={styles.fontBold}>序号</td>
                                            <td colSpan={9} className={styles.fontBold}>作业流程及安全措施</td>
                                        </tr>
                                        {
                                            data.Nav_SafeMeasures && data.Nav_SafeMeasures.map((item,i)=>{
                                                return <tr>
                                                    <td width="100px">
                                                        {i+1}
                                                    </td>
                                                    <td colSpan={9}>
                                                        {item.NAME}
                                                    </td>
                                                </tr>
                                            })
                                        }
                        </tbody>
                     </table>:null
                }
                {
                     data ?
                     <table style={{ width: '100%', textAlign: 'center', marginTop:'10px',borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tbody>
                                        <tr>
                                             <td width="100px" className={styles.fontBold}>序号</td>
                                            <td colSpan={9} className={styles.fontBold}>作业后处理措施</td>
                                        </tr>
                                        {
                                            data.Nav_DealMeasures && data.Nav_DealMeasures.map((item,i)=>{
                                                return <tr>
                                                   <td width="100px">
                                                        {i+1}
                                                    </td>
                                                    <td colSpan={9}>
                                                        {item.NAME}
                                                    </td>
                                                </tr>
                                            })
                                        }
                        </tbody>
                     </table>:null
                }
                {
                     data ?
                     <table style={{ width: '100%', textAlign: 'center', marginTop:'10px',borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tbody>
                                          <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>作业方案</td>
                                <td colSpan={8} rowSpan={1}>{
                                    data.Nav_PreOperSchFile && data.Nav_PreOperSchFile.map((item, i) => {
                                        return <a width={'20%'} title={item.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} >{item.Nav_ImgFile.FILE_NAME} </a>
                                    })
                                }</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>作业人员签名</td>
                                <td colSpan={8} rowSpan={1}>{
                                    data.Nav_CrucialLicensePerson && data.Nav_CrucialLicensePerson.map((item, i) => {
                                        if (item.DEAL_STATUS == 1) {
                                            if (item.Nav_User != null && item.Nav_User.Nav_UserSignFiles[0] != null)
                                                return <img style={{ margin: "0 0 0 15px", width:'150px'}} alt={item.Nav_User.NAME} src={config.picServerHost + item.Nav_User.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} />
                                            else
                                                return <img style={{ margin: "0 0 0 15px",width:'150px' }} title={item.Nav_User.NAME} />
                                        }
                                    })
                        }</td>
                            </tr>
                        </tbody>
                     </table>:null
                }
                 <FormPage {...this.state.tmpData} />
            </div>
        </div>
    }

}

export default connect(({ login,app }) => ({ login,app }))(FO017ShowPrint) 