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 } 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';
class FOChangeShfitRecord extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            enumData: null,
            BtnAgreeDisplay: 'none',
            isFinished: 'none',
            isMobile: false
        };
    };

    componentDidMount() {
        if (window.navigator.userAgent.indexOf("Windows") < 1) {
            this.setState({ isMobile: true })
        }
        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/FOChangeShiftRecord/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_Class');
        extendInclude(json, 'Nav_PreUsers.Nav_User');
        extendInclude(json, 'Nav_AfterUsers.Nav_User');
        extendInclude(json, 'Nav_PreUsers.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_AfterUsers.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'FO/ChangeShiftRecord/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.SHIFT_STATUS >= 3) {
                        this.setState({ isFinished: 'inline' })
                    }
                    //if (ret.ID && (ret.SHIFT_STATUS == 0 || ret.SHIFT_STATUS == 1 || ret.SHIFT_STATUS == 2)) {
                    // this.props.dispatch({
                    //     type: 'app/getDataByPost',
                    //     url: 'FO/FOChangeShiftRecord/GetChangeShiftRecord',
                    //     payload: {
                    //         ID: dataId,
                    //     },
                    //     onComplete: (result) => {
                    //         if (result != null) {
                    //             if (result) {
                    //                 this.setState({ BtnAgreeDisplay: '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, this.props.record.Nav_Department.NAME + ".xlsx")
    }
    render() {
        const { data, isMobile } = this.state;
        return <div>
            <div style={{ marginTop: '10px' }}>
                <ReactToPrint
                    trigger={() => <Button type={'default'} icon={'printer'} style={{ marginLeft: '20px', display: this.state.isFinished }}>打印</Button>}
                    content={() => this.componentRef}
                    pageStyle=".printDIV { padding:0 40px;} img{width :120px}"
                />
                <Button style={{ marginLeft: '20px', display: this.state.isFinished }} onClick={() => this.onTableBtnExport()} icon="export" >导出</Button>
                <Button style={{ marginLeft: isMobile ? '0' : '20px', 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" }}>
                <h1 style={{ textAlign: 'center' }}>岗位交接班记录表</h1>
                {
                    data ? <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={isMobile ? styles.description : styles.PrintForm}>
                        <tbody>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>班次</td>
                                <td colSpan={3} rowSpan={1}>{data.Nav_Class ? data.Nav_Class.NAME : ''}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>时间</td>
                                <td colSpan={6} rowSpan={1}>{data.CHANGE_SHIFT_TIME.substring(0, 16)}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>地点</td>
                                <td colSpan={6} rowSpan={1}>{data.ADDRESS}</td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>本班运行情况</td>
                                <td colSpan={3} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.CURRENT_CLASS_STATUS == 0 ? '正常' : '不正常' : null}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>存在问题</td>
                                <td colSpan={6} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.CURRENT_CLASS_STATUS == 0 ? '无' : data.CURRENT_CLASS_QUESTION : null}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>处理情况</td>
                                <td colSpan={6} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.CURRENT_CLASS_STATUS == 0 ? '无' : data.CURRENT_CLASS_MEASURE : null}</td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>设备运行状况</td>
                                <td colSpan={3} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.DEVICE_STATUS == 0 ? '正常' : '不正常' : null}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>存在问题</td>
                                <td colSpan={6} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.DEVICE_STATUS == 0 ? '无' : data.DEVICE_QUESTION : null}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>处理情况</td>
                                <td colSpan={6} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.DEVICE_STATUS == 0 ? '无' : data.DEVICE_MEASURE : null}</td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>现场环境情况</td>
                                <td colSpan={3} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.ENVIRONMENT_STATUS == 0 ? '正常' : '不正常' : null}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>存在问题</td>
                                <td colSpan={6} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.ENVIRONMENT_STATUS == 0 ? '无' : data.ENVIRONMENT_QUESTION : null}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>处理情况</td>
                                <td colSpan={6} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.ENVIRONMENT_STATUS == 0 ? '无' : data.ENVIRONMENT_MEASURE : null}</td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>劳保用品使用情况</td>
                                <td colSpan={3} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.SUPPLIES_STATUS == 0 ? '正常' : '不正常' : null}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>存在问题</td>
                                <td colSpan={6} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.SUPPLIES_STATUS == 0 ? '无' : data.SUPPLIES_QUESTION : null}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>处理情况</td>
                                <td colSpan={6} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.SUPPLIES_STATUS == 0 ? '无' : data.SUPPLIES_MEASURE : null}</td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>设备工具材料使用存放情况</td>
                                <td colSpan={9} rowSpan={1}>{data.DEVICE_STORAGE}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>附件</td>
                                <td colSpan={9} rowSpan={1}>{
                                    data.Nav_Files && data.Nav_Files.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={3} rowSpan={1} className={styles.fontBold}>交班人员</td>
                                <td colSpan={21} rowSpan={1}>
                                    {
                                        data.Nav_PreUsers && data.Nav_PreUsers.map((item, i) => {
                                            if (i == (data.Nav_PreUsers.length - 1)) {
                                                if (item.USER_SHIFT_STATUS == 0)
                                                    return <label style={{ color: 'red' }} title='未签到'> {item.Nav_User.NAME}</label>
                                                else
                                                    return item.Nav_User.NAME
                                            } else {
                                                if (item.USER_SHIFT_STATUS == 0)
                                                    return <label style={{ color: 'red' }} title='未签到'>{item.Nav_User.NAME + '  '}</label>
                                                else
                                                    return item.Nav_User.NAME + '  '
                                            }
                                        })
                                    }
                                </td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>交班人员</td>
                                <td colSpan={21} rowSpan={1}>{
                                    data.Nav_PreUsers && data.Nav_PreUsers.map((item, i) => {
                                        if (item.USER_SHIFT_STATUS == 1) {
                                            {
                                                if (item.Nav_User != null && item.Nav_User.Nav_UserSignFiles[0] != null)
                                                    return <img 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 width={'150px'} title={item.Nav_User.NAME} />
                                            }
                                        }
                                    })
                                }</td>
                            </tr>

                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>上班运行记录</td>
                                <td colSpan={9} rowSpan={1}>{data.LAST_CLASS_RECORD_STATUS == 0 ? '完整' : '不完整'}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>存在的问题</td>
                                <td colSpan={9} rowSpan={1}>{data.LAST_CLASS_RECORD_QUESTION == undefined ? "无" : data.LAST_CLASS_RECORD_QUESTION}</td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>上班工作完成情况</td>
                                <td colSpan={9} rowSpan={1}>{data.LAST_CLASS_FINISH_STATUS == 0 ? '完成' : '未完成'}</td>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>存在的问题</td>
                                <td colSpan={9} rowSpan={1}>{data.LAST_CLASS_FINISH_QUESTION == undefined ? "无" : data.LAST_CLASS_FINISH_QUESTION}</td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>接班人员</td>
                                <td colSpan={21} rowSpan={1}>{
                                    data.Nav_AfterUsers && data.Nav_AfterUsers.map((item, i) => {
                                        if (i == (data.Nav_AfterUsers.length - 1)) {
                                            if (item.USER_SHIFT_STATUS == 0)
                                                return <label style={{ color: 'red' }} title='未签到'> {item.Nav_User.NAME}</label>
                                            else
                                                return item.Nav_User.NAME
                                        } else {
                                            if (item.USER_SHIFT_STATUS == 0)
                                                return <label style={{ color: 'red' }} title='未签到'>{item.Nav_User.NAME + '  '}</label>
                                            else
                                                return item.Nav_User.NAME + '  '
                                        }
                                    })
                                }</td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} className={styles.fontBold}>接班人员</td>
                                <td colSpan={21} rowSpan={1}>{
                                    data.Nav_AfterUsers && data.Nav_AfterUsers.map((item, i) => {
                                        if (item.USER_SHIFT_STATUS == 1) {
                                            if (item.Nav_User != null && item.Nav_User.Nav_UserSignFiles[0] != null)
                                                return <img 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 width={'150px'} title={item.Nav_User.NAME} />
                                        }
                                    })
                                }</td>
                            </tr>
                        </tbody>
                    </table> : null
                }
            </div>
        </div>
    }
}
export default connect(({ login }) => ({ login }))(FOChangeShfitRecord) 