import { Button, Descriptions } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, guid, showApprove, extendIgnoreDataRule } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import { ExportToExcel } from '@woowalker/feui'
import XLSX from 'xlsx';
import { connect } from 'dva';
import styles from '../HI/StepForm.css';
import config from "../../../config.js";
import FormPage from '../../../components/FormPage'


class BS046ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            tmpData: {},
            loginid: this.props.login.user.ID,
            isCheck: true,
        };
    };

    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 = () => {
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendInclude(json, 'Nav_SubmitContent');
        extendInclude(json, 'Nav_User');
        extendInclude(json, 'Nav_DepartmentApply');
        extendInclude(json, 'Nav_ListApplyDetail.Nav_SubmitContent');
        extendInclude(json, 'Nav_Approve.Nav_ApproveDetails.Nav_ApproveUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/BSRiskSubmitDelayApply/GetAll',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                        // loginid: this.props.login.user.ID,
                        isCheck: (ret.OPERARTETYPE == 40 && ret.Nav_ListApplyDetail.length > 0 && ret.Nav_ListApplyDetail[0].CHCEKTIME == null && this.state.loginid == ret.Nav_ListApplyDetail[0].Nav_SubmitContent.CHECK_USER_ID) ? true : false
                    })
                }
            }
        });

        //结束任务
        // 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")
    }

    onClickApprove = (appVisible) => {
        const newtmpData = {
            data: { id: this.props.data.id, isShow: true, key: guid(), param: this.props.data.param, appVisible: appVisible, BtnAgreeDisplay: 'none' },
            formCode: "PF123",
        }
        this.setState({
            tmpData: newtmpData,
        });
    }
    sureCheck = () => {
        var detailID = ''
        for (var i = 0; i < this.state.data.Nav_ListApplyDetail.length; i++) {
            detailID += ',' + this.state.data.Nav_ListApplyDetail[i].ID
        }

        let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID, detailID);
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'BS/BSRiskSubmitDelayApply/CheckDetail',
            onComplete: (ret) => {
                this.props.data.onCancel();
            }
        });
    }

    render() {
        const { data } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                {/* 
                <Button onClick={() => this.onClickApprove()} style={{ marginRight: '8px' }} icon="export">审批详情</Button>
                <ReactToPrint trigger={() => <Button type={'default'} icon={'printer'} >打印</Button>} content={() => this.componentRef} />
                <ExportToExcel fileName='延期整改申请' tableId={'tableId' + this.props.data.id} /> */}

                <table>
                    <tbody>
                        <tr>
                            <td><Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon="export">审批详情</Button></td>
                            <td><ReactToPrint trigger={() => <Button type={'default'} 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>
                            {
                                //验收人 确认
                                this.state.isCheck ? <td><Button type={'primary'} icon={'save'} onClick={() => this.sureCheck()} >确认</Button>  </td> : null
                            }
                            {/* <td><Button type={'primary'} icon={'save'} onClick={() => this.sureCheck()} >确认</Button>  </td> */}
                        </tr>
                    </tbody>
                </table>
            </div>

            <FormPage {...this.state.tmpData} />
            {/* 检查表制定 */}
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.data.id}>
                {/* <h1 style={{ textAlign: 'center', margin: '15px' }}>延期整改申请</h1> */}
                <h1 className={styles.showPrintHead} >延期整改申请</h1>
                {
                    //, paddingLeft: '50px'
                    data ? <div>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="编码" >{data.CODE}</Descriptions.Item>
                            <Descriptions.Item span={2} label="延期整改名称" >{data.NAME}</Descriptions.Item>
                            <Descriptions.Item label="申请人">{data.Nav_User.NAME}</Descriptions.Item>
                            <Descriptions.Item label="申请部门" >{data.Nav_DepartmentApply.NAME}</Descriptions.Item>
                            <Descriptions.Item label="问题等级">{this.props.app.enums.BSQuestionLevelEnum.enums[data.QUESTION_LEVEL]}</Descriptions.Item>

                            {/* <Descriptions.Item label="隐患内容">{data.Nav_SubmitContent.NAME}</Descriptions.Item> */}
                            {/* <Descriptions.Item label="隐患地址">{data.Nav_SubmitContent.ADDRESS}</Descriptions.Item> */}
                            {/* <Descriptions.Item label="检查问题">{data.Nav_Question.DESCREPTION}</Descriptions.Item> */}
                            {/* <Descriptions.Item label="整改建议与措施">{data.DEMAND}</Descriptions.Item>
                            <Descriptions.Item label="临时整改建议与措施">{data.TEMPDEMAND}</Descriptions.Item>
                            <Descriptions.Item label="延期理由">{data.REASON}</Descriptions.Item>
                            <Descriptions.Item label="申请延期期限">{data.LastDate}</Descriptions.Item>
                            <Descriptions.Item label="延期后整改期限">{data.LastDateApply}</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>临时整改建议与措施</td>
                                <td>原整改期限</td>
                                <td>延期后整改期限</td>
                            </tr>
                            {data.Nav_ListApplyDetail.map((item, i) => {
                                return <tr>
                                    <td>{item.Nav_SubmitContent.DESCREPTION}</td>
                                    <td>{item.REASON}</td>
                                    <td>{item.TEMPDEMAND}</td>
                                    <td>{item.LastDate?.split(' ')[0]}</td>
                                    <td>{item.LastDateApply?.split(' ')[0]}</td>
                                </tr>
                            })}

                            <tr>
                                <td>审核意见
                                </td>
                                <td colSpan={4} >
                                    {
                                        showApprove(data.Nav_Approve?.Nav_ApproveDetails, this.props.app.enums.NodeApproveStatus.enums, config.picServerHost)
                                    }
                                </td>
                            </tr>
                        </tbody>
                    </table> : null
                }
            </div>
        </div >
    }
}
export default connect(({ login, app }) => ({ login, app }))(BS046ShowPrint) 