import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendIgnoreDataRule, extendInclude, endTask, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import { ExportToExcel } from '@woowalker/feui'
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import { message } from "antd/lib/index";
import styles from '../HI/StepForm.css';
import config from "../../../config.js";

//整改责任人 审批 专用了  10-13  

class BS042ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            loginid: this.props.login.user.ID,
            BtnAgreeDisplay: 'none',
            rowcount: 0,
            riskContentState: 15,
            DEALOPINION: '',
            isCheckerCheck: false,//是否 验收人 确认
            isDealUserCheck: false,//是否 整改责任人 确认
            isActualDealCheck: false,//整改落实人确定
            isOKLoad: false,
            isRejectLoad: false
        };
    };

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

    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }

    loadData = () => {
        // let json = initFilter(this.props.login.OrgId);
        // extendRule(json, 'ID', 1, this.props.data.id);
        // extendInclude(json, 'Nav_ListRiskSubmitContent');
        // extendInclude(json, 'Nav_Submit.Nav_Check');
        // extendInclude(json, 'Nav_ListRiskSubmitContent');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_Question');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_ListRiskQuestionReason');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_ListRiskQuestionReason.Nav_RiskReason');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_User');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_Department');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_DepartmentDeal');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_UserDeal');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_UserCheck');
        // extendInclude(json, 'Nav_ListRiskSubmitContent.Nav_UserActualDeal');
        // this.props.dispatch({
        //     type: 'app/getDataByPost',
        //     payload: json,
        //     url: 'BS/RiskSubmitNoticePerson/Get',
        //     onComplete: (ret) => {
        //         if (ret) {
        //             this.setState({   //设置setState全局变量
        //                 data: ret,      //将ret对象赋值给data, data供页面调用
        //                 rowcount: ret.Nav_ListRiskSubmitContent.length,

        //                 riskContentState: ret.Nav_ListRiskSubmitContent[0].RiskContentState,
        //                 isCheckerCheck: (this.state.loginid == ret.Nav_ListRiskSubmitContent[0].CHECK_USER_ID && ret.Nav_ListRiskSubmitContent[0].RiskContentState == 15) ? true : false,
        //                 isDealUserCheck: (this.state.loginid == ret.Nav_ListRiskSubmitContent[0].DEAL_USER_ID && ret.Nav_ListRiskSubmitContent[0].RiskContentState == 17) ? true : false,
        //                 isActualDealCheck: (this.state.loginid == ret.Nav_ListRiskSubmitContent[0].ACTUAL_DEAL_USER_ID && ret.Nav_ListRiskSubmitContent[0].RiskContentState == 32) ? true : false
        //             })
        //         }
        //     }
        // });


        //整改责任人 审批 专用了
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.data.id);
        if (this.props.data.tableKey != undefined && this.props.data.tableKey == "1") {
            extendRule(json, 'ISLOGIN', 1, 1);
        }
        // extendRule(json, 'RiskContentState', 1, 32);//整改落实人确认
        // extendRule(json, 'RiskContentState', 1, 32);//整改落实人确认
        // if () {

        // } 
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/BSRiskSubmitNoticePerson/GetDetailCheck',
            onComplete: (ret) => {
                if (ret) {
                    var isCheckerCheck = true//是否 验收人 确认
                    var isDealUserCheck = true//是否 整改责任人 确认
                    var isActualDealCheck = true
                    if (this.props.data.tableKey == undefined || this.props.data.tableKey != "1") {
                        isCheckerCheck = false
                        isDealUserCheck = false
                        isActualDealCheck = false
                    }

                    
                    if (isCheckerCheck)
                        isCheckerCheck = (this.state.loginid == ret.Nav_ListRiskSubmitContent[0].DEAL_USER_ID && ret.Nav_ListRiskSubmitContent[0].RiskContentState == 15) ? true : false//CHECK_USER_ID
                    if (isDealUserCheck)
                        isDealUserCheck = (this.state.loginid == ret.Nav_ListRiskSubmitContent[0].CHECK_USER_ID && ret.Nav_ListRiskSubmitContent[0].RiskContentState == 17) ? true : false//DEAL_USER_ID
                    if (isActualDealCheck)
                        isActualDealCheck = (this.state.loginid == ret.Nav_ListRiskSubmitContent[0].ACTUAL_DEAL_USER_ID && ret.Nav_ListRiskSubmitContent[0].RiskContentState == 32) ? true : false

                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                        rowcount: ret.Nav_ListRiskSubmitContent.length,
                        riskContentState: ret.Nav_ListRiskSubmitContent[0].RiskContentState,
                        isCheckerCheck: isCheckerCheck,
                        isDealUserCheck: isDealUserCheck,
                        isActualDealCheck: isActualDealCheck
                    })
                }
            }
        });

        //结束任务    修改为 整改责任人 和验收人 都要点击 确认  确认完成后   整改责任人 修改验收人
        // endTask(this.props.login.OrgId, this.props.data.TaskID, this.props.dispatch);

    }

    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")
    }
    //审批意见 改变
    DEALOPINIONCHANGE = (val) => {
        this.state.DEALOPINION = val
    }
    sureCheck = (val) => {
        debugger
        var tmpData = this.props.data
        if (val == 1 || val == 2) {
            //整改责任人 同意
            // Parameter4 审批结果  1同意 2驳回
            // Parameter3 驳回理由
            if (val == 2 && (this.state.DEALOPINION == undefined || this.state.DEALOPINION == null || this.state.DEALOPINION == '')) {
                // message.success('请填写驳回意见！')
                message.error('请填写驳回意见！')
                return false;
            }
            this.setState({
                isOKLoad: val == 1 ? true : false,
                isRejectLoad: val == 2 ? true : false,
            }, () => {
                let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID, this.state.rowcount, this.state.DEALOPINION, val);
                this.props.dispatch({
                    type: 'app/getDataByPost',
                    payload: json,
                    url: 'BS/BSRiskSubmit/UserCheckAndSend',
                    onComplete: (ret) => {
                        this.BtnClose()
                    }
                });
            })
        } else {
            //验收人 同意
            if (val == 20 && (this.state.DEALOPINION == undefined || this.state.DEALOPINION == null || this.state.DEALOPINION == '')) {
                // message.success('请填写驳回意见！')
                message.error('请填写驳回意见！')
                return false;
            }
            this.setState({
                // data: tmpData,
                isOKLoad: val == 20 ? false : true,
                isRejectLoad: val == 20 ? true : false,
            }, () => {
                let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID, val, this.state.DEALOPINION);
                this.props.dispatch({
                    type: 'app/getDataByPost',
                    payload: json,
                    url: 'BS/BSRiskSubmit/CheckerCheck',
                    onComplete: (ret) => {
                        this.BtnClose()
                    }
                });
            })

        }
    }

    sureCheckActualDeal = () => {
        let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID);
        // extendRule(json, 'ID', 1, this.props.data.id);
        // extendRule(json, "Parameter1", 1, this.props.data.TaskID)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/BSRiskSubmit/UpdateActualDealRecord',
            onComplete: (ret) => {
                this.BtnClose()
            }
        });
    }

    render() {
        const { data, riskContentState, isDealUserCheck, isCheckerCheck, isActualDealCheck } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tbody>
                        <tr>
                            <td><ReactToPrint trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>} content={() => this.componentRef} /></td>
                            <td><ExportToExcel fileName='隐患整改通知' tableId={'tableId' + this.props.data.id} pageStyle={"#tableId" + this.props.data.id + " { padding:0 40px;} img{width :120px}"} /></td>
                            {
                                //整改落实人确定
                                isActualDealCheck ? <td><Button type={'primary'} style={{ margin: "0 0 0 10px" }} icon={'save'} onClick={() => this.sureCheckActualDeal()} >确认</Button>  </td> : null
                            }
                        </tr>
                    </tbody>
                </table>
            </div>

            {/* 检查表制定 */}
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center', margin: '15px' }}>隐患整改通知</h1>
                {
                    //, paddingLeft: '50px'
                    data ? <div>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="发起时间">{data.CREATE_TIME}</Descriptions.Item>
                            <Descriptions.Item label="发起部门">{data.Nav_User?.Nav_Department?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="发起人">{data.Nav_User?.NAME}</Descriptions.Item>
                            <Descriptions.Item label="编码">{data.CODE}</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>
                    </div> : null
                }

                {
                    // data ? <table style={{ width: '100%', marginTop: '15px', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                    //     <tbody>
                    //         <tr>

                    data ? <table className={styles.PrintFormLight}>
                        <tbody>
                            <tr className={styles.PrintFormLightTh}>
                                {/* <td>名称</td> */}
                                <td>检查问题</td>
                                <td style={{ minWidth: '80px' }}>隐患等级</td>
                                <td>整改建议与措施</td>
                                <td style={{ minWidth: '110px' }}>隐患地点</td>
                                <td style={{ minWidth: '100px' }}>整改期限</td>
                                <td style={{ minWidth: '100px' }}>系统整改期限</td>
                                <td style={{ minWidth: '110px' }}>整改实施单位</td>
                                <td style={{ minWidth: '90px' }}>整改责任人</td>
                                <td style={{ minWidth: '70px' }}>验收人</td>
                                {(isActualDealCheck || data.Nav_ListRiskSubmitContent[0].Nav_UserActualDeal != null) ? <td style={{ minWidth: '90px' }}>整改落实人</td> : null}
                                <td style={{ minWidth: '70px' }}>附件</td>

                            </tr>
                            {data.Nav_ListRiskSubmitContent.map((item, i) => {
                                return <tr>
                                    {/* <td>{item.NAME}</td> */}
                                    {/* <td>{((item.ISBACK != null && item.ISBACK) ? "(返)" : "") + item.Nav_Question.DESCREPTION}</td> */}
                                    <td>{((item.ISBACK != null && item.ISBACK) ? "(返)" : "") + item.DESCREPTION}</td>
                                    <td>{this.props.app.enums.BSQuestionLevelEnum.enums[item.QUESTION_LEVEL]}</td>
                                    <td>{item.DEMAND}</td>
                                    <td>{item.ADDRESS}</td>
                                    <td>{new Date(item.LastDateUser).Format("yyyy-MM-dd")}</td>
                                    <td>{new Date(item.LastDateSystem).Format("yyyy-MM-dd")}</td>
                                    <td> {item.Nav_DepartmentDeal == null ? '' : item.Nav_DepartmentDeal.NAME}</td>
                                    {/* <td> {item.Nav_UserDeal == null ? '' : item.Nav_UserDeal.NAME} </td>
                                    <td> {item.Nav_UserCheck == null ? '' : item.Nav_UserCheck.NAME} </td> */}

                                    <td>{item.ISDEALUSERCHECK ? item.Nav_UserDeal?.NAME : (<a className={styles.red} > {item.Nav_UserDeal?.NAME} </a>)} </td>
                                    <td>{item.ISCHECKERCHECK ? item.Nav_UserCheck?.NAME : (<a className={styles.red} > {item.Nav_UserCheck?.NAME} </a>)} </td>

                                    {/* <td>{isActualDealCheck ? item.Nav_UserActualDeal?.NAME : (<a className={styles.red} > {item.Nav_UserActualDeal?.NAME} </a>)} </td> */}
                                    {(isActualDealCheck || data.Nav_ListRiskSubmitContent[0].Nav_UserActualDeal != null) ? (isActualDealCheck ? <td>{<a className={styles.red} > {item.Nav_UserActualDeal?.NAME} </a>}</td> : <td>{item.Nav_UserActualDeal?.NAME}</td>) : null}
                                
                                    <td>{
                                    item.Nav_Files && item.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>
                            })}

                        </tbody>
                    </table> : null
                }

                {
                    // 验收人验收
                    // (riskContentState == 15 || riskContentState == 17) ?
                    ((isCheckerCheck || isDealUserCheck) && !isActualDealCheck) ?
                        <table style={{ width: '100%', margin: '0,0,0,0', lineHeight: '35px' }}>
                            <tbody>
                                <tr><td colSpan={2}><br></br><hr></hr></td></tr>
                                <tr>
                                    <th style={{ float: 'right' }} >意见:</th>
                                    <td>
                                        <Input.TextArea style={{ width: '96%', margin: '0 0 0 15px' }} onChange={(evt) => this.DEALOPINIONCHANGE(evt.target.value)} />
                                    </td>
                                </tr>
                                <tr>
                                    <th></th>
                                    <td >
                                        <Button className={styles.agreedUser} type={'primary'} icon={'check'} loading={this.state.isOKLoad} onClick={() => this.sureCheck(riskContentState == 15 ? 1 : 10)} >同意</Button> {/*10 : 1 */}
                                        <Button className={styles.rejectedUser} type={'primary'} icon={'close'} loading={this.state.isRejectLoad} onClick={() => this.sureCheck(riskContentState == 15 ? 2 : 20)} >驳回</Button>   {/* 20 : 2 */}
                                    </td>
                                </tr>
                            </tbody>
                        </table> : null
                }

            </div>
        </div>
    }

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