import { message } from "antd/lib/index";
import { Button, Descriptions, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendOrder, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import styles from '../HI/StepForm.css';
import moment from 'moment';
import config from '../../../config';
import XLSX from 'xlsx';
import { connect } from 'dva';
import FormPage from '../../../components/FormPage'
class HM101ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            tmpData: {},
            BtnSignDisplay: '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);
        }
    }
    loadData = (dataId) => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_LiableUser');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Detail.Nav_Users.Nav_User');
        extendInclude(json, 'Nav_Detail.Nav_Identifyings.Nav_Identifying');
        extendInclude(json, 'Nav_Detail.Nav_User');
        extendInclude(json, 'Nav_Detail.Nav_Area');
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'HM/HazardTask/Get',
            onComplete: (ret) => {
                if (ret) {
                    for (let i = 0; i < ret.Nav_Detail.length; i++) {
                        ret.Nav_Detail[i].Nav_Users.sort((a, b) => {
                            return a.ROW_NO > b.ROW_NO ? 1 : -1;
                        });
                    }
                    if (this.props.data && this.props.data.loadDataDeal) {
                        this.props.data.loadDataDeal(1);
                    }
                    this.onClickApprove(false);
                    this.setState({ data: ret });
                    this.props.dispatch({
                        type: 'app/getDataByPost',
                        url: 'HM/HazardTask/GetUserDealStatus',
                        payload: {
                            ID: this.props.data.id,
                        },
                        onComplete: (result) => {
                            if (result != null && this.props.data.TaskID != null) {
                                if (result) {
                                    this.setState({ BtnSignDisplay: 'inline' })
                                }
                            }
                        }
                    })
                }
            }
        });
    }
    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")
    }
    onPersonalAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'HM/HMHazardTask/UserSignin',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnSignDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    onClickApprove = (appVisible) => {
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true, key: guid(), appVisible: appVisible, BtnAgreeDisplay: 'inline' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }
    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <Button onClick={() => this.onClickApprove(true)} icon="export">审批详情</Button>
                <ReactToPrint
                    trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px' }}>导出</Button>
                <Button type={'primary'} style={{ marginLeft: '8px', display: this.state.BtnSignDisplay }} onClick={() => this.onPersonalAgree()} 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 ? <div>
                        <Descriptions size="middle" bordered>
                            {/* <Descriptions.Item label="编号">{data.CODE}</Descriptions.Item> */}
                            <Descriptions.Item label="层级">{this.props.app.enums.FMDepartmentType.enums[data.LEVEL]}</Descriptions.Item>
                            <Descriptions.Item label="制定人">{data.Nav_LiableUser ? data.Nav_LiableUser.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="执行时间">{moment(data.START_TIME).format('YYYY-MM-DD') + '~' + moment(data.END_TIME).format('YYYY-MM-DD')}</Descriptions.Item>
                            <Descriptions.Item label="辨识区域" span={3}>{
                                data.Nav_Detail && data.Nav_Detail.map((item, i) => {
                                    if (i == (data.Nav_Detail.length - 1)) {
                                        return item.Nav_Area ? item.Nav_Area.NAME : ''
                                    } else {
                                        return item.Nav_Area.NAME + '  '
                                    }
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="附件" span={3}>{
                                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>
                                })
                            }</Descriptions.Item>
                        </Descriptions>
                        <table style={{ width: '100%', textAlign: 'center' }} className={styles.PrintForm1}>
                            <tbody>
                                <tr>
                                    <td colSpan={10} rowSpan={1} className={styles.fontBold}>辨识区域</td>
                                    <td colSpan={10} rowSpan={1} className={styles.fontBold}>职业危害名称</td>
                                    <td colSpan={10} rowSpan={1} className={styles.fontBold}>辨识人员</td>
                                    <td colSpan={10} rowSpan={1} className={styles.fontBold}>记录人</td>
                                </tr>
                                {
                                    data.Nav_Detail && data.Nav_Detail.map((item, i) => {
                                        return <tr>
                                            <td colSpan={10} rowSpan={1}>{item.Nav_Area?.NAME}</td>
                                            <td colSpan={10} rowSpan={1}>{
                                                item.Nav_Identifyings && item.Nav_Identifyings.map((item1, i) => {
                                                    if (i == (item.Nav_Identifyings.length - 1)) {
                                                        return item1.Nav_Identifying.NAME
                                                    } else {
                                                        return item1.Nav_Identifying.NAME + '  '
                                                    }
                                                })
                                            }</td>
                                            <td colSpan={10} rowSpan={1}>
                                                {
                                                    item.Nav_Users && item.Nav_Users.map((item1, i) => {
                                                        if (i == (item1.Nav_User.length - 1)) {
                                                            if (item1.DEAL_STATUS == 0)
                                                                return <label style={{ color: 'red' }} title='未签到'> {item1.Nav_User.NAME}</label>
                                                            else
                                                                return item1.Nav_User.NAME
                                                        } else {
                                                            if (item1.DEAL_STATUS == 0)
                                                                return <label style={{ color: 'red' }} title='未签到'>{item1.Nav_User.NAME + '  '}</label>
                                                            else
                                                                return item1.Nav_User.NAME + '  '
                                                        }
                                                    })
                                                }
                                            </td>
                                            <td colSpan={10} rowSpan={1}>{item.Nav_User?.NAME}</td>
                                        </tr>
                                    })
                                }
                            </tbody>
                        </table>
                        <FormPage {...this.state.tmpData} />
                    </div> : null
                }
            </div>
        </div>
    }

}

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