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';

class OG020Edit extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            view: props.formCode == 'OG020_SHOWPRINT',
            related: [],
        };
    };
    componentDidMount() {
        if (window.navigator.userAgent.indexOf("Windows") < 1) {
            this.setState({ isMobile: true })
        }
        if (!this.state.view)
            this.loadRelated();
        this.loadData();
    }

    componentWillReceiveProps(NextProps) {
        if (NextProps.data?.id && this.props.data?.id != NextProps.data?.id) {
            this.loadData(NextProps.data?.id);
        }
    }
    loadRelated = () => {
        let json = initFilter(this.props.login.OrgId);
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/Related/Entities',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({ related: ret })
                }
            }
        });
    }
    loadData = (dataId) => {
        let json = initFilter(this.props.login.OrgId);
        if (this.props.data.id) {
            extendRule(json, 'ID', 1, this.props.data.id);
        }
        extendInclude(json, 'Nav_Related');
        extendInclude(json, 'Nav_Details.Nav_Item');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/RelatedEstimate/Get',
            onComplete: (ret) => {
                if (ret) {
                    if (!this.props.data.id) {
                        ret.RELATED_ID = undefined;
                    }
                    ret.Nav_Details.sort((a, b) => { return a.NUM - b.NUM })

                    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")
    }
    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();
    }
    onSave = () => {
        if (this.state.view) {
            message.error("当前无法保存");
            return;
        }
        if (!this.state.data.RELATED_ID) {
            message.error("请选择相关方");
            return;
        }
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: this.state.data,
            url: 'OG/OGRelatedEstimate/FullUpdate',
            onComplete: (ret) => {
                if (ret) {
                    message.success('保存成功')
                    this.loadRelated();
                    this.BtnClose();
                }
            }
        });
    }
    render() {
        const { data } = this.state;
        const OGEvaluationResultEnum = this.props.app.enums && this.props.app.enums.OGEvaluationResultEnum && this.props.app.enums.OGEvaluationResultEnum.options;
        return <div>
            <div style={{ padding: '10px' }}>
                {
                    this.state.view && <>
                        <ReactToPrint
                            trigger={() => <Button type={'primary'} icon={'printer'} >打印</Button>}
                            content={() => this.componentRef}
                        />
                        <Button style={{ marginLeft: '8px' }} onClick={() => this.onTableBtnExport()} icon="export" >导出</Button>
                    </>
                }
                {
                    !this.state.view &&
                    <Button type={'primary'} style={{ marginLeft: '8px' }} onClick={() => this.onSave()} icon="export" >保存</Button>
                }
            </div>

            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id}>
                <h1 style={{ textAlign: 'center' }}>相关方安全表现评估表</h1>
                <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                    <tbody>
                        <tr>
                            <td colSpan={4} rowSpan={1} >相关方名称</td>
                            <td colSpan={16} rowSpan={1} >评估项目</td>
                            <td colSpan={4} rowSpan={1} >评估结论</td>
                        </tr>
                        {
                            data && data.Nav_Details && data.Nav_Details.map((it, index) => {
                                return <tr key={it.ID}>
                                    {
                                        index == 0 &&
                                        <td colSpan={4} rowSpan={data.Nav_Details.length}>{
                                            this.state.view ?
                                                data.Nav_Related && data.Nav_Related.NAME
                                                :
                                                <Select style={{ width: '100%' }} value={data.RELATED_ID} onChange={
                                                    (v) => {
                                                        let data = this.state.data;
                                                        data.RELATED_ID = v;
                                                        this.setState({
                                                            data
                                                        })
                                                    }
                                                }>
                                                    {
                                                        this.state.related && this.state.related.map((it) => {
                                                            return <Select.Option value={it.ID}>{it.NAME}</Select.Option>
                                                        })
                                                    }
                                                </Select>
                                        }</td>
                                    }
                                    <td colSpan={16} rowSpan={1}>{it.Nav_Item.NAME}</td>
                                    <td colSpan={4} rowSpan={1} >
                                        {
                                            this.state.view ?
                                                this.fmtEnum('OGEvaluationResultEnum', it.RESULT_TYPE)
                                                :
                                                <Select value={it.RESULT_TYPE}
                                                    onChange={
                                                        (v) => {
                                                            let data = this.state.data;
                                                            data.Nav_Details[index].RESULT_TYPE = v;
                                                            this.setState({ data });
                                                        }
                                                    }
                                                >
                                                    {
                                                        OGEvaluationResultEnum && OGEvaluationResultEnum.map(o => {
                                                            return <Select.Option key={o.value} value={o.value}>{o.label}</Select.Option>
                                                        })
                                                    }
                                                </Select>
                                        }
                                    </td>
                                </tr>
                            })
                        }
                    </tbody>
                </table>
            </div>
        </div>
    }

}

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