import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule, setDataFieldValue, showApprove, guid, initQueryFilter, ShowDateTime } from '../../../utils/common';
import ReactToPrint from 'react-to-print';
import { ExportToExcel } from '@woowalker/feui'
import XLSX from 'xlsx'; import { connect } from 'dva';
import moment from 'moment';
import { message } from 'antd/lib/index';
import styles from '../HI/StepForm.css';
import config from '../../../config.js';
import FormPage from '../../FormPage'
class CM026ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            btndisplay: 'none',
            isCheck: false, //是否待办 审批
            span: null
        }
    }
    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);
        }
    }

    onTableBtnExport() {
        let TableWrap = document.getElementById('table1');
        let Table = TableWrap.getElementsByTagName('table1')[0];
        const wb = XLSX.utils.table_to_book(Table);
        XLSX.writeFile(wb, this.props.record.Nav_Department.NAME + '.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,
        });
    }

    sign = () => {
        let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID);
        extendRule(json, 'ID', 1, this.props.data.id);
        extendRule(json, "Parameter1", 1, this.props.data.TaskID)
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'CM/CMAbilityEvaluate/Check',
            onComplete: (ret) => {
                this.BtnClose()
            }
        });
    }
    loadData = (id) => {
        var orgId = this.props.login ? this.props.login.OrgId : '';
        let json = initFilter(orgId);
        extendRule(json, 'ID', 1, id);
        extendInclude(json, 'Nav_Users.Nav_User')
        extendInclude(json, 'Nav_ListProject.Nav_EvaluateProject')
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'CM/AbilityEvaluate/Get',
            onComplete: (ret) => {
                if (ret) {
                    ret.Nav_ListProject.sort(function (a, b) {
                        var x = a.Nav_EvaluateProject.CODE.toLowerCase();
                        var y = b.Nav_EvaluateProject.CODE.toLowerCase();
                        if (x < y) { return -1; }
                        if (x > y) { return 1; }
                        return 0;
                    });

                    let span = [];
                    let idx = 0;
                    for (let i = 0; i < ret.Nav_ListProject.length; i++) {
                        span[i] = 1;
                        if (i > 0) {
                            if (ret.Nav_ListProject[i].Nav_EvaluateProject.NAME === ret.Nav_ListProject[i - 1].Nav_EvaluateProject.NAME) {
                                span[idx]++;
                                span[i] = 0;
                            } else {
                                idx = i;
                            }
                        }
                    }


                    var isCheck = (this.props.data.tableKey != undefined && this.props.data.tableKey == "1") ? true : false
                    this.setState({
                        isCheck: isCheck,
                        data: ret,
                        span: span
                    })
                }
            }
        });
    }

    BtnClose = () => {
        if (typeof this.props.data.onCancel != "undefined" && typeof this.props.data.onCancel == 'function')
            this.props.data.onCancel();
    }

    render() {
        const { data, isCheck, span } = this.state;
        const enums = this.props.data.enums ? this.props.data.enums : this.props.app.enums;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tr>
                        <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>
                        {isCheck ? <td><Button type={'primary'} icon={'check'} onClick={() => this.sign()} >确认</Button>  </td> : null}
                    </tr>
                </table>
            </div>
            <FormPage {...this.state.tmpData} />
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px', paddingTop: '20px' }} id={'tableId' + this.props.data.id}>
                <h1 className={styles.showPrintHead}>应急能力评估表</h1>
                {
                    data ? <div style={{ width: '95%' }}>
                        <Descriptions title='' size='middle' bordered>
                            <Descriptions.Item label="名称">{data.NAME}</Descriptions.Item>
                            <Descriptions.Item column={16} label="创建时间">{data.CREATE_TIME}</Descriptions.Item>
                            <br></br>
                            {
                                data?.Nav_Users.length > 0 ?
                                    <Descriptions.Item column={24} label="评估人员">{
                                        data?.Nav_Users?.map((item, i) => {
                                            if (item.ISCHECK == true) {
                                                return <a style={{ color: "rgba(0, 0, 0, 0.65)" }}>{(i > 0 ? ',' : '') + item.Nav_User.NAME}</a>
                                            } else {
                                                return <a style={{ color: "red" }}>{(i > 0 ? ',' : '') + item.Nav_User.NAME}</a>
                                            }
                                        })
                                    }</Descriptions.Item>
                                    : null
                            }
                        </Descriptions>
                    </div> : null
                }
                {
                    (data?.Nav_ListProject && data?.Nav_ListProject.length > 0) ?
                        <table className={styles.PrintFormLight}>
                            <tbody>
                                <tr>
                                    <td colSpan={4} className={styles.showPrintHead2}>应急能力评估项目</td>
                                </tr>
                                <tr className={styles.PrintFormLightTh}>
                                    <td>评估项目</td>
                                    <td>评估内容</td>
                                    <td>评估情况</td>
                                    <td>备注</td>
                                </tr>
                                {data?.Nav_ListProject?.map((item, i) => {
                                    if (span[i] > 0) {
                                        return <tr>
                                            <td rowSpan={span[i]}>{item.Nav_EvaluateProject.NAME} </td>
                                            <td>{item.Nav_EvaluateProject.EVALUATE_CONTENT} </td>
                                            <td>{enums.CMEvaluateStatusEnum.enums[item.PLATFORM_TYPE]} </td>
                                            <td>{item.REMARK} </td>
                                        </tr>
                                    } else {
                                        return <tr>
                                            <td>{item.Nav_EvaluateProject.EVALUATE_CONTENT} </td>
                                            <td>{enums.CMEvaluateStatusEnum.enums[item.PLATFORM_TYPE]} </td>
                                            <td>{item.REMARK} </td>
                                        </tr>
                                    }

                                    // return <tr>
                                    //     <td>{item.Nav_EvaluateProject.NAME} </td>
                                    //     <td>{item.Nav_EvaluateProject.EVALUATE_CONTENT} </td>
                                    //     <td>{enums.CMEvaluateStatusEnum.enums[item.PLATFORM_TYPE]} </td>
                                    //     <td>{item.REMARK} </td>
                                    // </tr>
                                })}

                                <tr>
                                    <td style={{ backgroundColor: '#fafafa' }}>总体评估结论 </td>
                                    <td colSpan={3}>{data.OVERALL_EVALUATE_CONCLUSION} </td>
                                </tr>
                            </tbody>
                        </table>
                        : null
                }
            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(CM026ShowPrint) 
