import { message } from "antd/lib/index";
import { Button, Popconfirm, Row, Col, Checkbox, Radio, Form, Input, Select, Table, Upload, Icon, PageHeader, Modal } 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 '../../CustomPages/HI/StepForm.css';
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import config from "../../../config";
class SCShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            TYPE: this.props.data.TYPE,
            LIST: this.props.data.LIST,
            PRINT: null,
        };
    };
    componentDidMount() {
        if (window.navigator.userAgent.indexOf("Windows") < 1) {
            this.setState({ isMobile: true })
        }
        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();
    }

    fmtEnum(name, value) {
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || '';
    }
    loadData = (dataId) => {
        if (dataId == "")
            return;
        let json = initFilter(this.props.login.OrgId, '', null, null, null);
        if (this.props.data.isView) {
            extendRule(json, 'ID', 1, dataId);
        } else {
            extendRule(json, 'Nav_Versions.ID', 9, dataId);
        }
        json.OrgType = 2;
        extendInclude(json, 'Nav_Post');
        extendInclude(json, 'Nav_Versions');
        extendInclude(json, 'Nav_Versions.Nav_User');
        extendInclude(json, 'Nav_Versions.Nav_Department');
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SC/System/Get',
            onComplete: (ret) => {
                if (!ret)
                    return;
                ret.Nav_Versions.sort((a, b) => { return a.VERSION - b.VERSION });
                if (this.props.formCode == "SC005_VIEW") {
                    ret.Nav_Versions = ret.Nav_Versions.filter(t => t.STATUS == 2 || t.STATUS == 3);
                }
                this.setState({
                    data: ret,
                    TYPE: ret.TYPE,
                    PRINT: this.state.LIST ? null : ret.Nav_Versions[ret.Nav_Versions.length - 1],
                })
            }
        });
    }
    onTableBtnExport() {
        let TableWrap = document.getElementById('tableId' + this.props.data.id);
        let Table = TableWrap.getElementsByTagName('table')[0];
        const wb = XLSX.utils.table_to_book(Table);
        let name = this.fmtName();
        if (this.state.data && this.state.data.CODE)
            name += '-' + this.state.data.CODE;
        name += '.xlsx';
        XLSX.writeFile(wb, name)
    }
    fmtName() {
        let fix = this.state.PRINT && this.state.PRINT.IS_ANNUL ? '废除' : '修订';
        if (this.state.TYPE === 0) {
            return `制度${fix}记录表`
        } else if (this.state.TYPE === 1) {
            return `安全生产责任制${fix}记录表`
        } else if (this.state.TYPE === 2) {
            return `岗位安全操作规程和指导书${fix}记录表`
        }
    }
    fmtTime(time) {
        time = time || '';
        return time.substr(0, 10);
    }
    render() {
        const { data } = this.state;
        const ver = data && data.Nav_Versions.find(it => it.STATUS == 1 || it.FILE_STATUS == 1)
        return <div>
            {
                this.state.PRINT && <div style={{ padding: '10px' }}>
                    <ReactToPrint
                        trigger={() => <Button type={'primary'} icon={'printer'} >打印</Button>}
                        content={() => this.componentRef}
                    />
                    <Button style={{ marginLeft: '8px' }} onClick={() => this.onTableBtnExport()} icon="export" >导出</Button>
                    {
                        this.state.LIST && <Button style={{ marginLeft: '8px' }} onClick={() => { this.setState({ PRINT: null }) }} >返回</Button>
                    }
                </div>

            }
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.data.id}>
                {
                    this.state.PRINT && <h1 style={{ textAlign: 'center', margin: '15px' }}>{this.fmtName()}</h1>
                }
                <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                    {
                        !this.state.PRINT && this.state.LIST && <tbody>
                            <tr>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>编号</td>
                                <td colSpan={10} rowSpan={1}>{data ? data.CODE : null}</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>名称</td>
                                <td colSpan={10} rowSpan={1}>{data ? data.NAME : null}</td>
                            </tr>
                            <tr>
                                <td colSpan={1} rowSpan={1} className={styles.fontBold}>序号</td>
                                <td colSpan={1} rowSpan={1} className={styles.fontBold}>版本号</td>
                                <td colSpan={this.state.TYPE === 1 ? 3 : 4} rowSpan={1} className={styles.fontBold}>修订/废除原因</td>
                                <td colSpan={this.state.TYPE === 1 ? 3 : 4} rowSpan={1} className={styles.fontBold}>修订概要</td>
                                {
                                    this.state.TYPE === 1 &&
                                    <td colSpan={2} rowSpan={1} className={styles.fontBold}>责任制说明</td>
                                }
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>编制单位</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>编制/废除时间</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>颁布时间</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>录入人员</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>文件状态</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>文件附件</td>
                                <td colSpan={2} rowSpan={1} className={styles.fontBold}>查看</td>
                            </tr>
                            {
                                data && data.Nav_Versions && data.Nav_Versions.map((it, idx) => {
                                    return <tr key={it.ID}>
                                        <td colSpan={1} rowSpan={1} >{idx + 1}</td>
                                        <td colSpan={1} rowSpan={1} >{`${it.VERSION}.0`}</td>
                                        <td colSpan={this.state.TYPE === 1 ? 3 : 4} rowSpan={1} >{it.VERSION !== 1 ? it.REASON : null}</td>
                                        <td colSpan={this.state.TYPE === 1 ? 3 : 4} rowSpan={1} >{it.VERSION !== 1 ? it.SUMMARY : null}</td>
                                        {
                                            this.state.TYPE === 1 &&
                                            <td colSpan={2} rowSpan={1} >{it.NOTE}</td>
                                        }
                                        <td colSpan={2} rowSpan={1} >{it.Nav_Department.NAME}</td>
                                        <td colSpan={2} rowSpan={1} >{this.fmtTime(it.EDIT_TIME)}</td>
                                        <td colSpan={2} rowSpan={1} >{this.fmtTime(it.PUBLISH_TIME)}</td>
                                        <td colSpan={2} rowSpan={1}>{it.Nav_User.NAME}</td>
                                        <td colSpan={2} rowSpan={1} >{this.fmtEnum('SCSystemFileStatus', it.FILE_STATUS)}</td>
                                        <td colSpan={2} rowSpan={1} >{
                                            it.Nav_Files && it.Nav_Files.map((file, i) => {
                                                return <a width={'20%'} title={file.Nav_File.FILE_NAME} target='_blank' href={config.serviceHost + 'PF/File/GetFile?id=' + file.FILE_ID} >{file.Nav_File.FILE_NAME} </a>
                                            })
                                        }</td>
                                        <td colSpan={2} rowSpan={1}>{
                                            idx !== 0 && <Button onClick={() => {
                                                this.setState({ PRINT: it })
                                            }} icon="eye" ></Button>
                                        }</td>
                                    </tr>
                                })
                            }
                        </tbody>
                    }
                    {
                        this.state.PRINT && <tbody>
                            <tr>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起时间</td>
                                <td colSpan={4} rowSpan={1}>{this.fmtTime(this.state.PRINT.TIME)}</td>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起部门</td>
                                <td colSpan={4} rowSpan={1}>{this.state.PRINT.Nav_Department && this.state.PRINT.Nav_Department.NAME}</td>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>发起人员</td>
                                <td colSpan={4} rowSpan={1}>{this.state.PRINT.Nav_User && this.state.PRINT.Nav_User.NAME}</td>
                            </tr>
                            <tr>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>{this.state.PRINT.IS_ANNUL ? '废除时间' : '修订时间'}</td>
                                <td colSpan={4} rowSpan={1}>{this.fmtTime(this.state.PRINT.TIME)}</td>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>名称</td>
                                <td colSpan={4} rowSpan={1}>{data && data.NAME}</td>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>编号</td>
                                <td colSpan={4} rowSpan={1}>{data && data.CODE}</td>
                            </tr>
                            <tr>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>{this.state.PRINT.IS_ANNUL ? '废除原因' : '修订原因'}</td>
                                <td colSpan={20} rowSpan={1}>{this.state.PRINT.REASON}</td>
                            </tr>
                            {
                                !this.state.PRINT.IS_ANNUL && <tr>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>制定要求</td>
                                    <td colSpan={20} rowSpan={1}>{this.state.PRINT.REQUIRE}</td>
                                </tr>
                            }

                            {
                                !this.state.PRINT.IS_ANNUL && <tr>
                                    <td colSpan={4} rowSpan={1} className={styles.fontBold}>修订概要</td>
                                    <td colSpan={20} rowSpan={1}>{this.state.PRINT.SUMMARY}</td>
                                </tr>
                            }

                            <tr>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>文件附件</td>
                                <td colSpan={20} rowSpan={1}>{
                                    this.state.PRINT.Nav_Files && this.state.PRINT.Nav_Files.map((file, i) => {
                                        return <a width={'20%'} title={file.Nav_File.FILE_NAME} target='_blank' href={config.serviceHost + 'PF/File/GetFile?id=' + file.FILE_ID} >{file.Nav_File.FILE_NAME} </a>
                                    })
                                }</td>
                            </tr>
                            <tr>
                                <td colSpan={4} rowSpan={1} className={styles.fontBold}>审核意见</td>
                                <td colSpan={20} rowSpan={1}>
                                </td>
                            </tr>
                        </tbody>
                    }
                </table>
            </div>
        </div>
    }
}

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