import { Button, Descriptions } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendIgnoreDataRule, guid, showApprove, 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 FormPage from '../../../components/FormPage'
import styles from '../HI/StepForm.css';


class BS044ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
        };
    };

    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);
        if (this.props.data.SUBMIT_CONTENT_ID) {
            //隐患库 查看整改单  只取  SUBMIT_CONTENT_ID ，ID传值是框架需求所传值无实际意义
            extendRule(json, 'RISK_SUBMIT_CONTENT_ID', 1, this.props.data.SUBMIT_CONTENT_ID);
        } else {
            extendRule(json, 'ID', 1, this.props.data.id);
        }

        extendInclude(json, 'Nav_UserDeal.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        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_Files.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_FilesAfter.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_ApproveCheckAudit.Nav_ApproveDetails.Nav_ApproveUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            // url: 'BS/RiskSubmitContentDeal/Get',
            url: 'BS/BSRiskSubmitContentDeal/GetAll',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                    })
                }
            }
        });
    }

    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,
        });
    }
    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' }}>
                <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>
                        </tr>
                    </tbody>
                </table>
            </div>

            {/* 检查表制定 */}
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 className={styles.showPrintHead} >隐患整改记录表</h1>
                {
                    data ? <div>
                        <Descriptions title="" size="middle" bordered>

                            <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_UserDeal?.Nav_Department.NAME}</Descriptions.Item>
                            {/* <Descriptions.Item label="整改责任人">{data.Nav_UserDeal.NAME}</Descriptions.Item> */}
                            <Descriptions.Item label="整改责任人">{
                                (data.Nav_SubmitContent.RiskContentState == 36) ? <a style={{ color: "red" }}> {data.Nav_UserDeal.NAME}</a> : <a style={{ color: "rgb(0, 0, 0, 0.65)" }}> {data.Nav_UserDeal.NAME}</a>
                            }</Descriptions.Item>

                            <Descriptions.Item label="验收人">{
                                (15 <= data.Nav_SubmitContent.RiskContentState && data.Nav_SubmitContent.RiskContentState < 38) ? <a style={{ color: "red" }}> {data.Nav_UserCheck.NAME}</a> : <a style={{ color: "rgb(0, 0, 0, 0.65)" }}> {data.Nav_UserCheck.NAME}</a>
                            }
                            </Descriptions.Item>

                            <Descriptions.Item label="整改落实人">{
                                ((27 <= data.Nav_SubmitContent.RiskContentState && data.Nav_SubmitContent.RiskContentState < 36) || (data.Nav_SubmitContent.RiskContentState == 40 || data.Nav_SubmitContent.RiskContentState == 50 || data.Nav_SubmitContent.RiskContentState == 70)) ? <a style={{ color: "red" }}> {data.Nav_UserActualDeal.NAME}</a> : <a style={{ color: "rgb(0, 0, 0, 0.65)" }}> {data.Nav_UserActualDeal.NAME}</a>
                            }</Descriptions.Item>

                            <Descriptions.Item label="整改建议与措施">{data.Nav_SubmitContent.DEMAND}</Descriptions.Item>
                            <Descriptions.Item label="整改期限">{data.Nav_SubmitContent.LastDateUser?.split(' ')[0]}</Descriptions.Item>
                            <Descriptions.Item label="整改后情况描述">{data.DEALDESCRIBE}</Descriptions.Item>

                            {/* <Descriptions.Item label="编号">{data.CODE}</Descriptions.Item> */}
                            {/* <Descriptions.Item label="验收时间">{data.CHCECKTIME}</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.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>
                            {/* <Descriptions.Item label="完成情况">{(data.DEALSITUATION == 10 || data.DEALSITUATION == 30 || data.DEALSITUATION == 40) ? enums.DealSituation.enums[data.DEALSITUATION] : ''}</Descriptions.Item> */}
                            <Descriptions.Item label="完成情况">{enums.DealSituation.enums[data.DEALSITUATION]}</Descriptions.Item>
                            <Descriptions.Item label="验收意见">{data.DEALOPINION}</Descriptions.Item>

                            {/* 整改责任人签名 没这个流程 */}
                            {/* <Descriptions.Item label="整改责任人签名">{
                                data.ISDEALSIGN && data.Nav_UserDeal.Nav_UserSignFiles != null && data.Nav_UserDeal.Nav_UserSignFiles.map((item, i) => {
                                    debugger
                                    return <img width={'20%'} src={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} />
                                })
                            }</Descriptions.Item> */}

                            <Descriptions.Item label="整改责任人签名">{
                                data.Nav_SubmitContent.RiskContentState > 36 ? showUserSign(data.Nav_UserDeal, config.picServerHost) : null
                            }</Descriptions.Item>

                            <Descriptions.Item label="验收人签名">{
                                data.Nav_SubmitContent.RiskContentState > 37 ? showUserSign(data.Nav_UserCheck, config.picServerHost) : null
                                // data.ISCHECKSIGN && data.Nav_UserCheck.Nav_UserSignFiles != null && data.Nav_UserCheck.Nav_UserSignFiles.map((item, i) => {
                                //     return <img style={{ maxHeight: "200px", maxWidth: "200px" }} src={config.picServerHost + item.Nav_ImgFile.Nav_File.FILE_PATH} />
                                // })
                            }</Descriptions.Item>
                            {
                                data.Nav_ApplyDetail == null ? null :
                                    <Descriptions.Item label="延期整改信息" span={2} >{
                                        '延期理由：' + data.Nav_ApplyDetail.REASON + ' 原整改期限：' + data.Nav_ApplyDetail.LastDate + (data.Nav_ApplyDetail.TEMPDEMAND != null ? (" 临时整改建议与措施：" + data.Nav_ApplyDetail.TEMPDEMAND) : '')
                                    }</Descriptions.Item>
                            }
                            <Descriptions.Item label="审核意见">{
                                showApprove(data.Nav_ApproveCheckAudit?.Nav_ApproveDetails, this.props.app.enums.NodeApproveStatus.enums, config.picServerHost)
                            }</Descriptions.Item>
                        </Descriptions>
                    </div> : null
                }

                <FormPage {...this.state.tmpData} />
            </div>
        </div >
    }
}
export default connect(({ login, app }) => ({ login, app }))(BS044ShowPrint) 