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 OG005ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            BtnAgreeDisplay: 'none',
            assessUser: ''
        };
    };

    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);
        }
    }
    fmtEnum(name,value){
        const enums = this.props.app.enums;
        if (!enums || !enums[name]) return '';
        return enums[name].enums[value] || ''; 
    }
    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);
        extendRule(json, 'ID', 1, this.props.data?.id);
        extendInclude(json, 'Nav_Post');
        extendInclude(json, 'Nav_SafeAssessItem');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/SafeAssessPlanInput/Get',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({ data: ret })
                    this.getAssessUser(ret.POST_ID)
                }
            }
        });
    }
    getAssessUser(postId) {
        let userjson = initFilter(this.props.login.OrgId, postId);
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'OG/OGSafeAssessPlanInput/GetAssessUser',
            payload: userjson,
            onComplete: (ret) => {
              if (ret) {
                var user = ''
                ret.Nav_User ? ret.Nav_User.map((t, idx) => {
                  user = user ? user + '  ' + t.NAME : t.NAME
                }) : ''
                this.setState({assessUser: user})
              }
            }
          })
    }
    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>
            </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', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                        <tr>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>编号</td>
                            <td colSpan={3} rowSpan={1} >{data.CODE}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>被考核岗位</td>
                            <td colSpan={3} rowSpan={1} >{data.Nav_Post ? data.Nav_Post.NAME : '' }</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>考核年度</td>
                            <td colSpan={3} rowSpan={1} >{data.ANNUAL}</td>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>考核周期</td>
                            <td colSpan={3} rowSpan={1} >{ this.fmtEnum('PTAssessmentCycleEnums',data.CYCLE) }</td>
                        </tr>
                        {/* <tr>
                            <td colSpan={3} rowSpan={1} className={styles.fontBold}>被考核人员</td>
                            <td colSpan={21} rowSpan={1} >{this.state.assessUser}</td>
                        </tr> */}
                        <tr>
                            <td colSpan={8} rowSpan={1} className={styles.fontBold}>考核项目</td>
                            <td colSpan={8} rowSpan={1} className={styles.fontBold}>标准分</td>
                            <td colSpan={8} rowSpan={1} className={styles.fontBold}>考核认定评分办法</td>
                        </tr>
                        {
                            data.Nav_SafeAssessItem ?  data.Nav_SafeAssessItem.map((item, index) => {
                                return <tr >
                                    <td colSpan={8} rowSpan={1} >{item.NAME}</td>
                                    <td colSpan={8} rowSpan={1} >{item.SCORE}</td>
                                    <td colSpan={8} rowSpan={1} >{item.METHOD}</td>
                                </tr>
                            }) : null
                        }
                    </table> : null
                }
            </div>
        </div>
    }

}

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