import { message } from "antd/lib/index";
import { Button, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, 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 OG001ReportView extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            assessUser: '',
            isReviewed: false
        };
    };

    componentDidMount() {
        this.loadData(this.props.data?.id);
    }

    componentWillReceiveProps(NextProps) {
        if (NextProps.data?.id && this.props.data?.id != NextProps.data?.id) {
            this.loadData(NextProps.data?.id);
        }
    }
    // 暂时隐藏安全生成责任制后续流程
    // onTableBtnAgree() {
    //     let json = {
    //         ORG_ID: this.props.login.OrgId,
    //         TaskID: this.props.data.TaskID,
    //     };
    //     this.props.dispatch({
    //         type: 'app/getDataByPost',
    //         url: 'OG/OGSafePdtSigned/Agree',
    //         payload: json,
    //         onComplete: (ret) => {
    //             if (ret) {
    //                 message.success('审阅成功！');
    //                 this.BtnClose();
    //             }
    //         }
    //     })
    // }
    onTableBtnAgree() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'HM/HMNoticeRelease/PersonalAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('确认成功！');
                    this.setState({ BtnAgreeDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }
    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }
    loadData = (dataId) => {
        let json = initFilter(this.props.login.OrgId);
        if (dataId == "") {
            extendRule(json, 'Nav_SafePdtSigned.ANNUAL', 1, new Date().getFullYear())
        } else {
            extendRule(json, 'SAFE_PDT_SIGNED_ID', 1, dataId)
        }
        extendInclude(json, 'Nav_User.Nav_Department');
        extendInclude(json, 'Nav_Post');
        extendInclude(json, 'Nav_ChargeUser');
        extendInclude(json, 'Nav_SafePdtSigned.Nav_StandardCreate');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/SafePdtSignedPost/OrderPaged',
            onComplete: (ret) => {
                if (ret && ret.length > 0) {
                    if (this.props.data.tableKey == "2" || this.props.data.tableKey == undefined) {
                        this.setState({ isReviewed: false })
                    } else {
                        this.setState({ isReviewed: true })
                    }
                    this.setState({
                        data: ret,
                    })
                }
            }
        });
    }
    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 } = this.state;
        return <div>
            <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.isReviewed && (
                    <Button type={'primary'} style={{ marginLeft: '8px' }} onClick={() => this.onTableBtnAgree()} icon="check" >审阅</Button>
                )}
            </div>

            {/* 安全生产责任制签订 */}
            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id} style={{ padding: '20px' }}>
                <h1 style={{ textAlign: 'center' }}>安全生产责任制签订</h1>
                {
                    data ? <table style={{ width: '100%', textAlign: 'center', border: '1px solid #333' }} className={styles.PrintForm}>
                        <tr>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>年度</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>责任制名称</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>岗位</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>部门</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>签订人员</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>签订状态</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>签订日期</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>负责人</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>负责人签订状态</td>
                            <td colSpan={4} rowSpan={1} className={styles.fontBold}>查看</td>
                        </tr>
                        {
                            data.map((d, idx) => {
                                return <tr>
                                    <td colSpan={4} rowSpan={1}>{d.Nav_SafePdtSigned.ANNUAL}</td>
                                    <td colSpan={4} rowSpan={1}>{d.Nav_SafePdtSigned.Nav_StandardCreate.NAME}</td>
                                    <td colSpan={4} rowSpan={1}>{d.Nav_Post.NAME}</td>
                                    <td colSpan={4} rowSpan={1}>{d.Nav_User.Nav_Department.NAME}</td>
                                    <td colSpan={4} rowSpan={1}>{d.Nav_User.NAME}</td>
                                    <td colSpan={4} rowSpan={1}>{d.DEAL_STATUS == 0 ? <label style={{ color: 'red' }} title='未签订'>未签订</label> : "已签订"}</td>
                                    <td colSpan={4} rowSpan={1}>{d.SIGNED_TIME}</td>
                                    <td colSpan={4} rowSpan={1}>{d.Nav_ChargeUser.NAME}</td>
                                    <td colSpan={4} rowSpan={1}>{d.CHARGE_DEAL_STATUS == 0 ? <label style={{ color: 'red' }} title='未签订'>未签订</label> : "已签订"}</td>
                                    <td colSpan={4} rowSpan={1}>{d.DEAL_STATUS == 0 ? <Button icon={'eye'} onClick={this.getSqlsByFormId} ></Button> : null}</td>
                                </tr>
                            })
                        }
                    </table> : null
                }
            </div>
        </div>
    }

}

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