import { message } from "antd/lib/index";
import { Button, Input, Descriptions, Select, Table, notification } from 'antd';
const { TextArea } = Input;
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 FO005ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            isFinished: 'none',
            isAudit: 'none',
            isSignin: 'none',
            DEALOPINION: '',
            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);
        }
    }
    //审批意见 改变
    DEALOPINIONCHANGE = (val) => {
        this.state.DEALOPINION = val
    }
    onChange = (value) => {
        this.setState({
            auditOpinion: value
        })
    }
    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_Users.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_ChargeUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Class');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_DepartmentPost');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'FO/CurrentClassRecord/Get',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({ data: ret })
                    if (ret.SHIFT_STATUS == 4) {
                        this.setState({ isFinished: 'inline' })
                    }
                    if (this.props.data.tableKey == "2" || this.props.data.tableKey == undefined) {
                        return
                    }
                    let isAllOK = true;
                    for (let i = 0; i < ret.Nav_Users.length; i++) {
                        if (ret.Nav_Users[i].USER_SHIFT_STATUS == 0) {
                            isAllOK = false;
                        }
                    }
                    if (!isAllOK && this.props.data.TaskID != null) {
                        this.setState({ isSignin: 'inline' })
                    }
                    if (isAllOK && this.props.data.tableKey == "1") {
                        this.setState({ isAudit: 'table-row' })
                    }
                } else { message.error('数据加载获取失败，请联系管理员排查!'); }
            }
        });
    }
    onChargeUserAgree(agree) {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'FO/FOCurrentClassRecord/ChargeUserAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
                CODE: agree,
                AUDIT_OPINION: this.state.DEALOPINION,
                DEPARTMENT_ID: this.state.data.DEPARTMENT_ID,
                CLASS_ID: this.state.data.CLASS_ID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('处理完成！');
                    this.setState({ isAudit: 'none' })
                    this.BtnClose();
                }
            }
        })
    }

    onTableBtnSignin() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'FO/FOCurrentClassRecord/UserSignin',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('签到成功！');
                    this.setState({ isSignin: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    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.isSignin }} onClick={() => this.onTableBtnSignin()} icon="check" >签到</Button>


            </div>


            {/* 岗位当班工作记录表 */}
            <div ref={el => (this.componentRef = el)} style={{ padding: isMobile ? '0px 0px 20px 0px' : '20px' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center' }}>岗位当班工作记录表</h1>
                {
                    data ?
                        <div>
                            <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={isMobile ? styles.description : styles.PrintForm}>
                                <tbody>
                                    <tr>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>记录日期</td>
                                        <td colSpan={3} rowSpan={1}>{data.RECORD_DATE}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>当班班次</td>
                                        <td colSpan={3} rowSpan={1}>{data.Nav_Class ? data.Nav_Class.NAME : ""}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>岗位</td>
                                        <td colSpan={3} rowSpan={1}>{data.Nav_DepartmentPost.NAME}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>当班人员</td>
                                        <td colSpan={3} rowSpan={1}>
                                            {
                                                data.Nav_Users && data.Nav_Users.map((item, i) => {
                                                    if (i == (data.Nav_Users.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={2} rowSpan={1} className={styles.fontBold}>本班运行情况</td>
                                        <td colSpan={2} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.CURRENT_CLASS_STATUS == 0 ? '正常' : '不正常' : null}</td>
                                        <td colSpan={2} 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={2} 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={2} rowSpan={1} className={styles.fontBold}>设备运行状况</td>
                                        <td colSpan={2} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.DEVICE_STATUS == 0 ? '正常' : '不正常' : null}</td>
                                        <td colSpan={2} 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={2} 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={2} rowSpan={1} className={styles.fontBold}>现场环境情况</td>
                                        <td colSpan={2} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.ENVIRONMENT_STATUS == 0 ? '正常' : '不正常' : null}</td>
                                        <td colSpan={2} 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={2} 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={2} rowSpan={1} className={styles.fontBold}>劳保用品使用情况</td>
                                        <td colSpan={2} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.SUPPLIES_STATUS == 0 ? '正常' : '不正常' : null}</td>
                                        {/* <td colSpan={2} rowSpan={1} className={styles.fontBold}>是否佩戴</td>
                                    <td colSpan={3} rowSpan={1}>{data.SHIFT_STATUS > 0 ? data.SUPPLIES_USED == 0 ? '否' : '是' : null}</td> */}
                                        <td colSpan={2} 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={2} 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={2} rowSpan={1} className={styles.fontBold}>设备工具材料使用存放情况</td>
                                        <td colSpan={8} rowSpan={1}>{data.DEVICE_STORAGE}</td>
                                        <td colSpan={2} rowSpan={1} className={styles.fontBold}>附件</td>
                                        <td colSpan={8} 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={2} rowSpan={1} className={styles.fontBold}>当班人员活动签名</td>
                                        <td colSpan={18} rowSpan={1}>{
                                            data.Nav_Users && data.Nav_Users.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={2} rowSpan={1} className={styles.fontBold}>班长签名</td>
                                        <td colSpan={18} rowSpan={1}>{
                                            data.SHIFT_STATUS == 4 ? <img width={'150px'} alt={data.Nav_ChargeUser.NAME} src={config.picServerHost + data.Nav_ChargeUser.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} /> : null
                                        }</td>
                                    </tr>
                                </tbody>
                            </table>
                            <table style={{ width: '100%', margin: '0,0,0,0', lineHeight: '35px' }}>
                                <tbody>
                                    <tr><td colSpan={2}><br></br><hr style={{ display: this.state.isAudit }} ></hr></td></tr>
                                    <tr>
                                        <th style={{ width: '90px', display: this.state.isAudit }}>意见:</th>
                                        <td>
                                            <Input.TextArea style={{ width: isMobile ? '330px' : '', margin: '0 0 0 15px', display: this.state.isAudit }} onChange={(evt) => this.DEALOPINIONCHANGE(evt.target.value)} />
                                        </td>
                                    </tr>
                                    <tr>
                                        <th></th>
                                        <td >
                                            <Button style={{ width: '120px', height: '38px', margin: '20px 0 20px 15px', display: this.state.isAudit }} type={'primary'} icon={'check'} onClick={() => this.onChargeUserAgree(1)} >同意</Button>
                                            <Button style={{ width: '120px', height: '38px', margin: '20px 0 20px 30px', backgroundColor: "#ED7D31", display: this.state.isAudit }} type="primary" htmlType="submit" icon="close" onClick={() => this.onChargeUserAgree(2)} >驳回</Button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div> : null
                }
            </div>
        </div>
    }

}

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