import { Button, Descriptions,Popconfirm, Row, Col, Form,Modal, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule,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'
import { T } from 'antd/lib/upload/utils';
class PT029ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            BtnChargeAgreeDisplay:'none',
            tmpData: {},
            isExsit:0,
        };
    };

    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: 'PT/PTMonitorAssessment/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: 'PT/PTMonitorAssessment/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);
        extendIgnoreDataRule(json)
        // extendInclude(json, 'Nav_User')
        // extendInclude(json, 'Nav_User.Nav_Person')
        // extendInclude(json, 'Nav_User.Nav_Person.Nav_Post')
        // extendInclude(json, 'Nav_Department')
        // extendInclude(json, 'Nav_Department.Nav_User')
        // extendInclude(json, 'Nav_DepartmentDetail')
        // extendInclude(json, 'Nav_DepartmentDetail.Nav_Department')
        // extendInclude(json, 'Nav_DepartmentDetail.Nav_User')
        // extendInclude(json, 'Nav_DepartmentDetail.Nav_DetailContent')
       that.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'PT/PTMonitorAssessment/GetEdit',
            onComplete: (ret) => {
                if (ret) {
                    if(that.props.data && that.props.data.loadDataDeal)
                    {
                        that.props.data.loadDataDeal(1);
                    }
                    let isExsit=ret?.Nav_Details.filter(t=>t.USER_ID!=null && t.USER_ID == this.props.login.userId).length;
                    that.setState({ data: ret,isExsit: isExsit});
                    if (ret.STATUS === 10) {
                        that.setState({ BtnAgreeDisplay: 'inline', BtnChargeAgreeDisplay: 'none' })
                    }
                    if(ret.STATUS === 20)
                    {
                        that.setState({ BtnAgreeDisplay: 'none',BtnChargeAgreeDisplay: 'none' })
                    }
                    if(ret.STATUS === 30)
                    {
                       if (that.props.data.tableKey==="2" ||that.props.data.tableKey===undefined) {
                        that.setState({ BtnAgreeDisplay: 'none',BtnChargeAgreeDisplay: 'none' })
                        }
                        else
                        {
                            that.setState({ BtnAgreeDisplay: 'none',BtnChargeAgreeDisplay: 'inline' })
                        }
                      
                    }
                    // if (that.props.data.tableKey==="2" ||that.props.data.tableKey===undefined) {
                    //     that.setState({ BtnAgreeDisplay: 'none' })
                    // }else{
                    //     that.setState({ BtnAgreeDisplay: 'inline' })
                    // }
                    this.onClickApprove(false);
                    //归档才显示打印和导出按钮
                    // if(ret.PRE_MEETING_STATUS==5){
                    //     this.setState({ isFinished: 'inline' })
                    // }
                }
                else
                { message.error('请先尝试刷新，若仍然加载失败，请联系管理员排查!');}
            }
        });
    }

    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")
    }

    onClickApprove=(appVisible)=>{
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true,key:guid(),param:this.props.data.param,appVisible:appVisible,BtnAgreeDisplay:'inline'},
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    render() {
        const { data ,isExsit} = this.state;
        const enums=this.props.data.enums ? this.props.data.enums :this.props.app.enums ;
        // let aaa = Math.random();
        return <div>
            <div style={{ padding: '10px' }}>
            <Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px'}} icon="export">审批详情</Button>
                <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>
                <Button type="primary" style={{marginLeft: '8px', display: this.state.BtnChargeAgreeDisplay }} onClick={() => this.onTableBtnChargeAgree()} icon="check" >已阅</Button>
            </div>
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center', margin: '15px' }}>监测与考核记录表</h1>
                {
                     data ?
                     <div>
                        <Descriptions size="middle" bordered className={styles.description}>
                            <Descriptions.Item label="监测时间">{data.SET_TIME}</Descriptions.Item>
                            <Descriptions.Item label="监测组织">{data.Nav_Department ? data.Nav_Department.NAME : ''}</Descriptions.Item>
                            {/* <Descriptions.Item label="监测人员">{data.Nav_User ? data.Nav_User.NAME : ''}</Descriptions.Item> */}
                            <Descriptions.Item label="监测人员">{
                                data.Nav_MonitorUser && data.Nav_MonitorUser.map((item, i) => {
                                    if (i == (data.Nav_MonitorUser.length - 1)) {
                                        if (item.DEAL_STATUS == 0)
                                            return <label style={{ color: 'red' }} title='未签到'> {item.Nav_User?.NAME}</label>
                                        else
                                            return item.Nav_User.NAME
                                    } else {
                                        if (item.DEAL_STATUS == 0)
                                            return <label style={{ color: 'red' }} title='未签到'>{item.Nav_User?.NAME + ','}</label>
                                        else
                                            return item.Nav_User.NAME + ','
                                    }
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="监测年度">{data?.YEAR}</Descriptions.Item>
                            {/* <Descriptions.Item label="监测周期">{data?.NAME}</Descriptions.Item> */}
                            <Descriptions.Item label="监测周期">{enums.PTAssessmentCycleEnums.enums[data?.ASSESSMENT_CYCLE]}</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>
                    <td className={styles.fontBold}>得分</td>
                    </tr>
                    {
                                    data?.Nav_Details && data?.Nav_Details.map((item,i)=>{
                                        if(data.STATUS === 30 && data.Nav_Department.USER_ID!=this.props.login.userId )
                                        {
                                            if(isExsit>0)
                                            {
                                                if(item.USER_ID ===this.props.login.userId)
                                            {
                                                return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item?.Nav_Department?.NAME}
                                            </td>
                                            <td>
                                                {item?.Nav_User?.NAME}
                                            </td>
                                            <td>
                                                {/* {item.Nav_User.Nav_Person.Nav_Post.NAME} */}
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2?.NAME}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr>{item2.SCORE}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.METHOD}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.CONTENT}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.MEASURES}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.RESULT_SCORE}</tr>
                                })
                            }
                                            </td>
                                        </tr>
                                            }
                                            }
                                            else
                                            {
                                                return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item?.Nav_Department?.NAME}
                                            </td>
                                            <td>
                                                {item?.Nav_User?.NAME}
                                            </td>
                                            <td>
                                                {/* {item.Nav_User.Nav_Person.Nav_Post.NAME} */}
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2?.NAME}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr>{item2.SCORE}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.METHOD}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.CONTENT}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.MEASURES}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.RESULT_SCORE}</tr>
                                })
                            }
                                            </td>
                                        </tr>
                                            }
                                        }
                                        else
                                        {
                                            return <tr>
                                            <td>
                                                {i+1}
                                            </td>
                                            <td>
                                                {item?.Nav_Department?.NAME}
                                            </td>
                                            <td>
                                                {item?.Nav_User?.NAME}
                                            </td>
                                            <td>
                                                {/* {item.Nav_User.Nav_Person.Nav_Post.NAME} */}
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2?.NAME}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr>{item2.SCORE}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.METHOD}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.CONTENT}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.MEASURES}</tr>
                                })
                            }
                                            </td>
                                            <td>
                                            {
                                item?.Nav_Contents && item?.Nav_Contents.map((item2, i) => {
                                    return <tr> {item2.RESULT_SCORE}</tr>
                                })
                            }
                                            </td>
                                        </tr>
                                        }
                                    })
                                }
                    </table>:null 
                }
                {
                     data ?
                     <div>
                        <Descriptions size="middle" bordered className={styles.description}>
                        <Descriptions.Item label="监测人员签名">
                            <div>{
                                    data.Nav_MonitorUser && data.Nav_MonitorUser.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",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 style={{ margin: "0 0 0 15px",width:'150px' }} title={item.Nav_User?.NAME} />
                                        }
                                    })
                        }</div>
                        </Descriptions.Item>
                        </Descriptions>
                     </div>:null
                }
                <FormPage {...this.state.tmpData} />
            </div>
        </div>
    }

}

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