import { Button, Descriptions, Popconfirm, Row, Col, Form, Modal, 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';
import moment from 'moment';
import { message } from "antd/lib/index";
import FormPage from '../../../components/FormPage'
class OG008ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            BtnChargeAgreeDisplay: 'none',
            tmpData: {},
        };
    };

    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: 'OG/OGSafeAssessRecord/IdentityUpdate',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    onTableBtnChargeAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'OG/OGSafeAssessRecord/ChargeAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('已阅成功！');
                    this.setState({ BtnChargeAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }

    loadData = (dataId) => {
        let that = this;
        let json = initFilter(that.props.login.OrgId);
        extendRule(json, 'ID', 1, dataId);
        that.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/OGSafeAssessRecord/GetEdit',
            onComplete: (ret) => {
                if (ret) {
                    that.setState({ data: ret });
                    // if (ret.STATUS === 20) {
                    //     that.setState({ BtnAgreeDisplay: 'inline' })
                    // }else{
                    //     that.setState({ BtnAgreeDisplay: 'none' })
                    // }
                    // if(ret.STATUS === 30)
                    // {
                    //     that.setState({ BtnChargeAgreeDisplay: 'inline' })
                    // }
                    // else
                    // {
                    //     that.setState({ BtnChargeAgreeDisplay: 'none' })
                    // }
                    if (that.props.data.tableKey === "2" || that.props.data.tableKey === undefined) {
                        that.setState({ BtnAgreeDisplay: 'none' })
                    } else {
                        that.setState({ BtnAgreeDisplay: 'inline' })
                    }
                    //归档才显示打印和导出按钮
                    // if(ret.PRE_MEETING_STATUS==5){
                    //     this.setState({ isFinished: 'inline' })
                    // }
                }
            }
        });
    }

    onTableBtnExport() {
        const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;
        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;
        const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;
        return <div>
            <div style={{ padding: '10px' }}>
                <ReactToPrint
                    trigger={() => <Button style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 5 ? "inline" : "none" }} type={'default'} icon={'printer'} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button onClick={() => this.onTableBtnExport()} icon="export" style={{ marginLeft: '8px', display: data && data.IS_PUBLISH === 5 ? "inline" : "none" }}>导出</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}>
                <h1 style={{ textAlign: 'center' }}>安全生产责任制考核记录</h1>
                {
                    data ?
                        <div>
                            <Descriptions size="middle" bordered className={styles.description}>
                                <Descriptions.Item label="考核年度">{data?.ANNUAL}</Descriptions.Item>
                                <Descriptions.Item label="考核周期">{data?.CYCLE_NAME}</Descriptions.Item>
                                <Descriptions.Item label="考核时间">{data.TIME}</Descriptions.Item>
                                <Descriptions.Item label="考核部门">{data.Nav_Department ? data.Nav_Department.NAME : ''}</Descriptions.Item>
                            </Descriptions>
                        </div> : null
                }
                {
                    data ? <table style={{ width: '100%', textAlign: 'left' }} className={styles.PrintForm1}>
                        <tr>
                            <td width='150px' className={styles.fontBold}>序号</td>
                            <td className={styles.fontBold}>被考核人员</td>
                            <td className={styles.fontBold}>被考核岗位</td>
                            <td className={styles.fontBold}>安全生产指标考核项目</td>
                            <td className={styles.fontBold}>分数</td>
                            <td className={styles.fontBold}>考核认定评分办法</td>
                            <td className={styles.fontBold}>考核检查情况</td>
                            <td className={styles.fontBold}>改进措施</td>
                            <td className={styles.fontBold}>得分</td>
                        </tr>
                        {
                            data?.Nav_Details && data?.Nav_Details.map((item, i) => {
                                return <tr>
                                    <td>
                                        {i + 1}
                                    </td>
                                    <td>
                                        {item?.Nav_User?.NAME}
                                    </td>
                                    <td>
                                        {item.Nav_Post?.NAME}
                                    </td>
                                    <td>
                                        {
                                            item?.Nav_SafeAssessRecordItem && item?.Nav_SafeAssessRecordItem.map((item2, i) => {
                                                return <tr> {item2?.Nav_SafeAssessItem.NAME}</tr>
                                            })
                                        }
                                    </td>
                                    <td>
                                        {
                                            item?.Nav_SafeAssessRecordItem && item?.Nav_SafeAssessRecordItem.map((item2, i) => {
                                                return <tr>{item2?.Nav_SafeAssessItem.SCORE}</tr>
                                            })
                                        }
                                    </td>
                                    <td>
                                        {
                                            item?.Nav_SafeAssessRecordItem && item?.Nav_SafeAssessRecordItem.map((item2, i) => {
                                                return <tr> {item2.Nav_SafeAssessItem.METHOD}</tr>
                                            })
                                        }
                                    </td>
                                    <td>
                                        {
                                            item?.Nav_SafeAssessRecordItem && item?.Nav_SafeAssessRecordItem.map((item2, i) => {
                                                return <tr> {item2.RESULT_INSTRUCTIONS}</tr>
                                            })
                                        }
                                    </td>
                                    <td>
                                        {
                                            item?.Nav_SafeAssessRecordItem && item?.Nav_SafeAssessRecordItem.map((item2, i) => {
                                                return <tr> {item2.CORRECT_MEASURES}</tr>
                                            })
                                        }
                                    </td>
                                    <td>
                                        {
                                            item?.Nav_SafeAssessRecordItem && item?.Nav_SafeAssessRecordItem.map((item2, i) => {
                                                return <tr> {item2.SCORE}</tr>
                                            })
                                        }
                                    </td>
                                </tr>
                            })
                        }
                    </table> : null
                }
                {
                    data ?
                        <div>
                            <Descriptions size="middle" bordered className={styles.description}>
                                <Descriptions.Item label="被考核人员签名">
                                    <div>{
                                        data.Nav_Details && data.Nav_Details.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" }} 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" }} title={item.Nav_User?.NAME} />
                                            }
                                        })
                                    }</div>
                                </Descriptions.Item>
                            </Descriptions>
                        </div> : null
                }
            </div>
        </div>
    }

}

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