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,extendIgnoreDataRule, 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';
class FO008ShowPrint 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/FOPreOperSch/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_User');
        extendInclude(json, 'Nav_Department');
        extendInclude(json, 'Nav_PreOperSchUser.Nav_RealDepartmentPost');
        extendInclude(json, 'Nav_PreOperSchUser.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_PreOperSchFile.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'FO/PreOperSch/Get',
            onComplete: (ret) => {
                if (ret) {
                    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)} id={'tableId' + this.props.data.id} style={{ padding: '20px' }} 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.FILE_NAME}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>发起时间</td>
                                <td colSpan={3} rowSpan={1}>{data.LANUNCH_TIME?moment(data.LANUNCH_TIME).format('YYYY-MM-DD'):null}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>发起部门</td>
                                <td colSpan={3} rowSpan={1}>{data.Nav_Department ? data.Nav_Department.NAME : ''}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>发起人员</td>
                                <td colSpan={3} rowSpan={1}>{data.Nav_User ? data.Nav_User.NAME : ''}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>参与人员</td>
                                <td colSpan={8} rowSpan={1}>{
                                data.Nav_PreOperSchUser && data.Nav_PreOperSchUser.map((item, i) => {
                                    if (i == (data.Nav_PreOperSchUser.length - 1)) {
                                        if (item.DEAL_STATUS == 0)
                                            return <label style={{ color: 'red' }} title='未签到'> {item.Nav_User.NAME}</label>
                                        else
                                            return item.Nav_User.NAME
                                    } else {
                                        if (item.DEAL_STATUS == 0)
                                            return <label style={{ color: 'red' }} title='未签到'>{item.Nav_User.NAME + '  '}</label>
                                        else
                                            return item.Nav_User.NAME + '  '
                                    }
                                })
                            }</td>
                            </tr>
                            <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.CONTENT}</td>
                            </tr>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>电子签名</td>
                                <td colSpan={8} rowSpan={1} className={styles.fontBold}>{
                                    data.Nav_PreOperSchUser && data.Nav_PreOperSchUser.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
                }
            </div>
        </div>
    }

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