import { message } from "antd/lib/index";
import { Button, Descriptions,Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, setDataFieldValue, guid, initQueryFilter,extendIgnoreDataRule } 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';
class FO021ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            enumData: null,
            BtnAgreeDisplay: 'none'
        };
    };

    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/FOJobActivityRecord/PersonalAgree',
            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 json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        // extendInclude(json, 'Nav_JobName.Nav_MonitorUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_JobNameOut.Nav_MonitorUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_OperationStep');
        // extendInclude(json, 'Nav_JobActivityPerson');
        // extendInclude(json, 'Nav_JobActivityPerson.Nav_User.Nav_Department');
        // extendInclude(json, 'Nav_JobActivityPerson.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_JobActivityPerson.Nav_RelatedUser.Nav_Signs.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_Details.Nav_Files.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_Flow.Nav_Files.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_Measure.Nav_Files.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'FO/FOJobActivityRecord/GetEdit',
            onComplete: (ret) => {
                if (ret) {
                    if(this.props.data && this.props.data.loadDataDeal)
                    {
                        this.props.data.loadDataDeal(1);
                    }
                    this.setState({ data: ret })
                    if (this.props.data.tableKey=="2" ||this.props.data.tableKey==undefined) {
                        this.setState({ BtnAgreeDisplay: 'none' })
                    }else{
                        this.setState({ BtnAgreeDisplay: 'inline' })
                    }
                //     if (ret.ID) {
                //         this.props.dispatch({
                //             type: 'app/getDataByPost',
                //             url: 'FO/FOPreShiftMeetingRecord/GetPreShiftMeetingRecord',
                //             payload: {
                //                 ID: dataId,
                //             },
                //             onComplete: (result) => {
                //                 if (result != null) {
                //                     if (result) {
                //                         this.setState({ BtnAgreeDisplay: 'inline' })
                //                     }else{
                //                         this.setState({ BtnAgreeDisplay: 'none' })
                //                     }
                //                 }
                //             }
                //         })
                //     }
                }
                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")
    }

    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <ReactToPrint
                    trigger={() => <Button type={'default'} icon={'printer'} style={{ marginLeft: '8px',display: data && data.FORM_STATUS === 2?"inline":"none" }} >打印</Button>}
                    content={() => this.componentRef}
                    pageStyle=".printDIV { padding:0 40px;} img{width :120px}"
                />
                <Button style={{ marginLeft: '8px',display: data && data.FORM_STATUS === 2?"inline":"none" }} onClick={() => this.onTableBtnExport()} icon="export" >导出</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.Nav_OperationStep?data.Nav_OperationStep.NAME:''}</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={8} rowSpan={1}>{data.JOB_DATE?moment(data.JOB_DATE).format('YYYY-MM-DD'):null +"~"+data.JOB_END_DATE?moment(data.JOB_END_DATE).format('YYYY-MM-DD'):null}</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.IS_OUTSOURCE == false &&data.Nav_JobActivityPerson && data.Nav_JobActivityPerson.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.JOB_DOCUMENT}
                                                    </td>
                                                </tr>
                                            })
                                        }
                                        {
                                             data.IS_OUTSOURCE == true &&data.Nav_JobActivityPerson && data.Nav_JobActivityPerson.map((item,i)=>{
                                                return <tr>
                                                    <td colSpan={2}>
                                                        {item.Nav_RelatedUser?item.Nav_RelatedUser.NAME:''}
                                                    </td>
                                                    <td colSpan={3}>
                                                        {''}
                                                    </td>
                                                    <td colSpan={5}>
                                                        {item.JOB_DOCUMENT}
                                                    </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={7} className={styles.fontBold}>作业前安全确认</td>
                                            <td width="100px" className={styles.fontBold}>是否确认</td>
                                            <td colSpan={2} className={styles.fontBold}>附件</td>
                                        </tr>
                                    
                                    
                                        {
                                            data.Nav_Details && data.Nav_Details.map((item,i)=>{
                                                return <tr>
                                                    <td width="100px">
                                                        {i+1}
                                                    </td>
                                                    <td colSpan={7}>
                                                        {item.SafeConfirmsStr}
                                                    </td>
                                                    <td width="100px">
                                                        {item.IS_CONFIRM?"是":"否"}
                                                    </td>
                                                    <td colSpan={2}>
                                                    {
                                item.Nav_Files && item.Nav_Files.map((t, i) => {
                                    return <a width={'20%'} title={t.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + t.Nav_ImgFile.Nav_File.FILE_PATH} >{t.Nav_ImgFile.FILE_NAME} <br></br></a>
                                })
                            }
                                                    </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={7} className={styles.fontBold}>作业流程及安全措施</td>
                                            <td width="100px" className={styles.fontBold}>是否确认</td>
                                            <td colSpan={2} className={styles.fontBold}>附件</td>
                                        </tr>
                                        {
                                            data.Nav_Flow && data.Nav_Flow.map((item,i)=>{
                                                return <tr>
                                                    <td width="100px">
                                                        {i+1}
                                                    </td>
                                                    <td colSpan={7}>
                                                        {item.SafeMeasuresStr}
                                                    </td>
                                                    <td width="100px">
                                                        {item.IS_CONFIRM?"是":"否"}
                                                    </td>
                                                    <td colSpan={2}>
                                                    {
                                item.Nav_Files && item.Nav_Files.map((t, i) => {
                                    return <a width={'20%'} title={t.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + t.Nav_ImgFile.Nav_File.FILE_PATH} >{t.Nav_ImgFile.FILE_NAME}<br></br> </a> 
                                })
                            }
                                                    </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={7} className={styles.fontBold}>作业后处理措施</td>
                                            <td width="100px" className={styles.fontBold}>是否确认</td>
                                            <td colSpan={2} className={styles.fontBold}>附件</td>
                                        </tr>
                                    
                                    
                                        {
                                            data.Nav_Measure && data.Nav_Measure.map((item,i)=>{
                                                return <tr>
                                                    <td width="100px">
                                                        {i+1}
                                                    </td>
                                                    <td colSpan={7}>
                                                        {item.DealMeasuresStr}
                                                    </td>
                                                    <td width="100px">
                                                        {item.IS_CONFIRM?"是":"否"}
                                                    </td>
                                                    <td colSpan={2}>
                                                    {
                                item.Nav_Files && item.Nav_Files.map((t, i) => {
                                    return <a width={'20%'} title={t.Nav_ImgFile.FILE_NAME} target='_blank' href={config.picServerHost + t.Nav_ImgFile.Nav_File.FILE_PATH} >{t.Nav_ImgFile.FILE_NAME}<br></br> </a>
                                })
                            }
                                                    </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} className={styles.fontBold}>作业人员签名</td>
                                            <td colSpan={8} className={styles.fontBold}>{
                                 data.IS_OUTSOURCE ==false&&data.Nav_JobActivityPerson && data.Nav_JobActivityPerson.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 8px",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 8px",width:'150px'  }} title={item.Nav_User.NAME} />
                                        }
                                    })}
                                    {
                                 data.IS_OUTSOURCE ==true&&data.Nav_JobActivityPerson && data.Nav_JobActivityPerson.map((item, i) => {
                                        if (item.DEAL_STATUS == 1) {
                                            if (item.Nav_RelatedUser != null && item.Nav_RelatedUser.Nav_Signs[0] != null)
                                                return <img style={{ margin: "0 0 0 8px",width:'150px' }} alt={item.Nav_RelatedUser.NAME} src={config.picServerHost + item.Nav_RelatedUser.Nav_Signs[0].Nav_ImgFile.Nav_File.FILE_PATH} />
                                            else
                                                return <img style={{ margin: "0 0 0 8px",width:'150px'  }} title={item.Nav_RelatedUser.NAME} />
                                        }
                                    })}
                                    </td>    
                                 </tr>
                                 <tr>
                                            <td colSpan={2} className={styles.fontBold}>已采取有效的风险控制措施</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.IS_EFFECTIVE === 1 ?"是" :"否"}</td>
                                            <td colSpan={2} className={styles.fontBold}>描述内容</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.EFFECTIVE}</td>        
                                 </tr>
                                 <tr>
                                            <td colSpan={2} className={styles.fontBold}>作业人员未存在违规行为</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.IS_EXIST === 1 ?"是" :"否"}</td>
                                            <td colSpan={2} className={styles.fontBold}>描述内容</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.EXIST}</td>        
                                 </tr>
                                 <tr>
                                            <td colSpan={2} className={styles.fontBold}>作业人员习惯合适</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.IS_SUITABLE === 1 ?"是" :"否"}</td>
                                            <td colSpan={2} className={styles.fontBold}>描述内容</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.SUITABLE}</td>        
                                 </tr> 
                                 <tr>
                                            <td colSpan={2} className={styles.fontBold}>作业人员无需再培训</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.IS_NEED === 1 ?"是" :"否"}</td>
                                            <td colSpan={2} className={styles.fontBold}>描述内容</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.NEED}</td>        
                                 </tr>
                                 <tr>
                                            <td colSpan={2} className={styles.fontBold}>当前操作规程无需改进</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.IS_REQUIRES === 1 ?"是" :"否"}</td>
                                            <td colSpan={2} className={styles.fontBold}>描述内容</td>
                                            <td colSpan={3} className={styles.fontBold}>{data.REQUIRES}</td>        
                                 </tr>
                                 <tr>
                                            <td colSpan={2} className={styles.fontBold}>监护人签字</td>
                                            <td colSpan={8} className={styles.fontBold}>{
                                    (data.IS_OUTSOURCE ==false&&data.Nav_JobName?.Nav_MonitorUser && data.Nav_JobName?.Nav_MonitorUser?.Nav_UserSignFiles[0])?
                                    <img style={{ margin: "0 0 0 15px", width:'150px'}} alt={data.Nav_JobName?.Nav_MonitorUser?.NAME} src={config.picServerHost + data.Nav_JobName?.Nav_MonitorUser?.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} />
                                    :data.Nav_JobName?.Nav_MonitorUser?.NAME
                                }{
                                    (data.IS_OUTSOURCE ==true&&data.Nav_JobNameOut?.Nav_MonitorUser && data.Nav_JobNameOut?.Nav_MonitorUser?.Nav_UserSignFiles[0])?
                                    <img style={{ margin: "0 0 0 15px", width:'150px'}} alt={data.Nav_JobNameOut?.Nav_MonitorUser?.NAME} src={config.picServerHost + data.Nav_JobNameOut?.Nav_MonitorUser?.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} />
                                    :data.Nav_JobNameOut?.Nav_MonitorUser?.NAME
                                }</td>      
                                 </tr>
                        </tbody>
                     </table>:null
                }
            </div>
        </div>
    }

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