import { Button, Input, Descriptions, message } from 'antd';
const { TextArea } = Input;
import React from 'react';
import { initFilter, extendIgnoreDataRule, extendRule, extendInclude, showUserSign } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import { ExportToExcel } from '@woowalker/feui'
import XLSX from 'xlsx';
import { connect } from 'dva';
import config from "../../../config.js";
import styles from '../HI/StepForm.css';


class BS044Check extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            DEALSITUATION: 0,
            DEALOPINION: '',
            loginid: this.props.login.user.ID,
            isDealUserCheck: false,//是否 整改责任人 确认
            isCheckerCheck: 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);
        }
    }
    //审批意见 改变
    DEALOPINIONCHANGE = (val) => {
        this.state.DEALOPINION = val
    }

    //完成情况 改变	
    DEALSITUATIONCHANGE = (val) => {
        this.state.DEALSITUATION = val
    }
    sureCheck = (val) => {
        //驳回 10
        if (val == 10) {
            this.setState({
                isRejectLoad: true,
                isOKLoad: false,
            })
        } else {
            //同意 40
            this.setState({
                isOKLoad: true,
                isRejectLoad: false
            })
        }

        if (val && val == 10 && (this.state.DEALOPINION == undefined || this.state.DEALOPINION == null || this.state.DEALOPINION.length < 1)) {
            message.error("请填写驳回的验收意见！");
            this.setState({
                isRejectLoad: false,
                isOKLoad: false,
            })
            return false
        }

        // let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID, this.state.DEALSITUATION, this.state.DEALOPINION);
        let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID, val, this.state.DEALOPINION);
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/BSRiskSubmit/CheckDeal',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({
                        isRejectLoad: false,
                        isOKLoad: false,
                    })
                    this.props.data.onCancel();
                }
            }
        });
    }

    loadData = () => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.data.id);

        extendInclude(json, 'Nav_UserDeal.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_UserDeal.Nav_UserSignFiles.Nav_User');
        extendInclude(json, 'Nav_UserCheck.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_UserActualDeal');
        extendInclude(json, 'Nav_SubmitNoticePerson');
        extendInclude(json, 'Nav_UserDeal.Nav_Department');
        extendInclude(json, 'Nav_SubmitContent.Nav_Question');
        // extendInclude(json, 'Nav_ListUserSign.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_ListCheckUserSign.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_FilesAfter.Nav_ImgFile.Nav_File');


        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/RiskSubmitContentDeal/Get',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                        DEALSITUATION: ret.DEALSITUATION,
                        isDealUserCheck: (this.state.loginid == ret.Nav_SubmitContent.DEAL_USER_ID && ret.Nav_SubmitContent.RiskContentState == 36) ? true : false,
                        isCheckerCheck: (this.state.loginid == ret.Nav_SubmitContent.CHECK_USER_ID && ret.Nav_SubmitContent.RiskContentState == 37) ? true : false,
                    })
                    // this.DEALOPINIONCHANGE(ret.DEALOPINION) 
                }
            }
        });
    }

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

    render() {
        const { data, isDealUserCheck, isCheckerCheck, isOKLoad, isRejectLoad } = this.state;
        const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tbody>
                        <tr>
                            <td><ReactToPrint trigger={() => <Button icon={'printer'} >打印</Button>} content={() => this.componentRef} pageStyle={"#tableId" + this.props.data.id + " { padding:0 40px;} img{width :120px}"} /></td>
                            <td><ExportToExcel fileName='隐患整改记录表' tableId={'tableId' + this.props.data.id} /></td>
                        </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>
                {
                    data ? <div >
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="编号">{data.CODE}</Descriptions.Item>
                            <Descriptions.Item label="整改截止时间">{data.ENDDATE}</Descriptions.Item>
                            <Descriptions.Item label="整改落实人">{data.Nav_UserActualDeal.NAME}</Descriptions.Item>
                            <Descriptions.Item label="整改部门">{data.Nav_UserDeal.Nav_Department.NAME}</Descriptions.Item>
                            <Descriptions.Item label="整改责任人">{data.Nav_SubmitContent.RiskContentState < 37 ? <a className={styles.red} >{data.Nav_UserDeal.NAME}</a> : data.Nav_UserDeal.NAME}</Descriptions.Item>
                            <Descriptions.Item label="验收人">{data.Nav_SubmitContent.RiskContentState < 38 ? <a className={styles.red} >{data.Nav_UserCheck.NAME}</a> : data.Nav_UserCheck.NAME}</Descriptions.Item>
                            <Descriptions.Item label="验收时间">{data.CHCECKTIME}</Descriptions.Item>
                            <Descriptions.Item label="通知单编号">{data.Nav_SubmitNoticePerson.CODE}</Descriptions.Item>
                            <Descriptions.Item label="检查问题">{data.Nav_SubmitContent != null ? data.Nav_SubmitContent.DESCREPTION : ""}</Descriptions.Item>
                            <Descriptions.Item label="整改建议与措施" >{data.Nav_SubmitContent.DEMAND}</Descriptions.Item>
                            <Descriptions.Item label="整改后情况描述">{data.DEALDESCRIBE}</Descriptions.Item>
                            <Descriptions.Item label="隐患地点">{data.Nav_SubmitContent.ADDRESS}</Descriptions.Item>
                            <Descriptions.Item label="备注">{data.REMARK}</Descriptions.Item>
                            {/* <hr></hr> */}
                            <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.Item label="整改情况描述">{data.DEALDESCRIBE}</Descriptions.Item> */}
                            <Descriptions.Item label="附件（整改后）">{
                                data.Nav_FilesAfter && data.Nav_FilesAfter.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>
                            {/* <hr></hr> */}
                            <Descriptions.Item label="整改责任人签名">{data.Nav_SubmitContent.RiskContentState > 36 ? showUserSign(data.Nav_UserDeal, config.picServerHost) : null}</Descriptions.Item>
                            <Descriptions.Item label="验收人签名">{data.Nav_SubmitContent.RiskContentState >= 38 ? showUserSign(data.Nav_UserCheck, config.picServerHost) : null}</Descriptions.Item>
                        </Descriptions>
                        {(isDealUserCheck || isCheckerCheck) ?
                            <table style={{ width: '100%', margin: '0,0,0,0', lineHeight: '35px' }}>
                                <tbody>
                                    <tr><td colSpan={2}><br></br><hr></hr></td></tr>
                                    <tr>
                                        {data.Nav_SubmitContent.RiskContentState == 36 ? <th style={{ float: "right" }} >审核意见:</th> : <th style={{ float: "right" }} >验收意见:</th>}
                                        <td>
                                            {/* placeholder='如驳回，请详细描述整改情况' */}
                                            <Input.TextArea style={{ width: '96%', margin: '0 0 0 15px' }} onChange={(evt) => this.DEALOPINIONCHANGE(evt.target.value)} />   {/* defaultValue={data.DEALOPINION} */}
                                        </td>
                                    </tr>
                                    <tr>
                                        <th></th>
                                        <td >
                                            <Button style={{ width: '120px', height: '38px', margin: '20px 0 20px 15px' }} type={'primary'} icon={'check'} loading={isOKLoad} onClick={() => this.sureCheck(40)} >同意</Button>
                                            {/* 默认未完成 */}
                                            <Button style={{ width: '120px', height: '38px', margin: '20px 0 20px 30px', backgroundColor: "#ED7D31", borderColor: "#ED7D31" }} loading={isRejectLoad} type="primary" htmlType="submit" icon="close" size="50px" onClick={() => this.sureCheck(10)} >驳回</Button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            : null
                        }

                    </div> : null
                }

            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(BS044Check) 