import { Button, Descriptions, Row, Col, Form, Input, Select, Table, Upload } from 'antd';
import { message } from "antd/lib/index";
import React from 'react';
import moment from 'moment';
import { initFilter, extendRule, extendInclude, extendOrder, 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';
import FormPage from '../../../components/FormPage'
class HM061ShowPrint 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_Request');
        extendInclude(json, 'Nav_Identifyings.Nav_Identifying');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Detail.Nav_Users.Nav_User');
        extendInclude(json, 'Nav_Detail.Nav_User');
        extendInclude(json, 'Nav_Detail.Nav_Area');
        extendInclude(json, 'Nav_Areas.Nav_Area');
        extendInclude(json, 'Nav_LiableUser');
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'HM/RiskTask/Get',
            onComplete: (ret) => {
                if (ret) {
                    if (this.props.data && this.props.data.loadDataDeal) {
                        this.props.data.loadDataDeal(1);
                    }
                    this.onClickApprove(false);
                    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;
                        });
                    }
                    this.setState({ data: ret })
                    this.props.dispatch({
                        type: 'app/getDataByPost',
                        url: 'HM/HMRiskTask/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/HMRiskTask/UserSignin',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnSignDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    renderLevel(level) {
        let str = '';
        if (level == undefined) {
            return str;
        }
        if (level.indexOf('0') >= 0) {
            str += '部门 ';
        }
        if (level.indexOf('1') >= 0) {
            str += '车间 ';
        }
        if (level.indexOf('2') >= 0) {
            str += '班组 ';
        }
        if (level.indexOf('3') >= 0) {
            str += '公司 ';
        }
        return str;
    }
    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="层级">{this.renderLevel(data.LEVEL)}</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="辨识区域">{
                                data.Nav_Areas && data.Nav_Areas.map((item, i) => {
                                    if (i == (data.Nav_Areas.length - 1)) {
                                        return item.Nav_Area ? item.Nav_Area.NAME : ''
                                    } else {
                                        return item.Nav_Area.NAME + '  '
                                    }
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="制定人">{data.Nav_LiableUser ? data.Nav_LiableUser.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="辨识要求">{data.Nav_Request ? data.Nav_Request.NAME : ''}</Descriptions.Item>
                            <Descriptions.Item label="辨识对象">{
                                data.Nav_Identifyings && data.Nav_Identifyings.map((item, i) => {
                                    if (i == (data.Nav_Identifyings.length - 1)) {
                                        return item.Nav_Identifying.NAME
                                    } else {
                                        return item.Nav_Identifying.NAME + '  '
                                    }
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="附件">{
                                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}>
                            <tr>
                                <td className={styles.fontBold}>辨识区域</td>
                                <td className={styles.fontBold}>层级</td>
                                <td className={styles.fontBold}>辨识人员</td>
                                <td className={styles.fontBold}>记录人</td>
                            </tr>
                            {
                                data.Nav_Detail && data.Nav_Detail.map((item, i) => {
                                    return <tr>
                                        <td>{item.Nav_Area?.NAME}</td>
                                        <td>{this.props.app.enums.FMDepartmentType.enums[item.LEVEL]}</td>
                                        <td>
                                            {
                                                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>{item.Nav_User?.NAME}</td>
                                    </tr>
                                })
                            }
                        </table>
                        <FormPage {...this.state.tmpData} />
                    </div> : null
                }
            </div>
        </div>
    }

}

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