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, extendIgnoreDataRule, setDataFieldValue, guid, initQueryFilter } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import styles from '../../CustomPages/HI/StepForm.css';
// import WFViewComponent from '../../Lib/WFViewComponent'
import XLSX from 'xlsx';
import { connect } from 'dva';
import moment from 'moment';
import config from "../../../config";

class CM036Edit extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isView: props.formCode === 'CM016_VIEW',
            data: null,
            changed: false,
        };
    };

    componentDidMount() {
        this.loadData();
    }
    onSave(nonotify) {
        const data = this.state.data;
        const uid = this.props.login.user.ID;
        const users = data && data.Nav_DrillRecord && data.Nav_DrillRecord.Nav_DrillEstimateUser && data.Nav_DrillRecord.Nav_DrillEstimateUser;
        const canSave = users && data.STATUS === 0 && users.find(it => it.USER_ID === uid) ? true : false;
        if (!canSave) {
            return;
        }
        if (this.state.changed) {
            this.props.dispatch({
                type: 'app/getDataByPost',
                payload: JSON.parse(JSON.stringify(data)),
                url: 'CM/DrillEstimate/FullUpdate',
                onComplete: (ret) => {
                    if (ret) {
                        this.setState({
                            changed: false
                        })
                        if (!nonotify) {
                            message.success("保存成功")
                            this.BtnClose();
                        }
                    }
                }
            });
        }
    }
    onNotify() {
        const { data, isView } = this.state;
        const uid = this.props.login.user.ID;
        const users = data && data.Nav_DrillRecord && data.Nav_DrillRecord.Nav_DrillEstimateUser && data.Nav_DrillRecord.Nav_DrillEstimateUser;
        const canNotify = !isView && data && data.STATUS === 0 && users && users.length > 0 && users[0].USER_ID == uid;
        const canSave = users && data.STATUS === 0 && users.find(it => it.USER_ID === uid) ? true : false;
        if (!canNotify) {
            return;
        }
        if (this.state.changed && canSave) {
            this.onSave(true)
        }
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: JSON.parse(JSON.stringify(data)),
            url: 'CM/DrillEstimate/Agree',
            onComplete: (ret) => {
                if (ret) {
                    message.success("通知成功")
                    this.BtnClose();
                }
            }
        });

    }
    onAgree() {
        const { data, isView } = this.state;
        const uid = this.props.login.user.ID;
        const users = data && data.Nav_DrillRecord && data.Nav_DrillRecord.Nav_DrillEstimateUser && data.Nav_DrillRecord.Nav_DrillEstimateUser;
        const canAgree = !isView && data && data.STATUS === 1 && users && users.find(it => it.USER_ID === uid && !it.AGREE);
        if (!canAgree) {
            return;
        }
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: JSON.parse(JSON.stringify(data)),
            url: 'CM/DrillEstimate/Agree',
            onComplete: (ret) => {
                if (ret) {
                    message.success("签到成功")
                    this.BtnClose();
                }
            }
        });

    }
    BtnClose = () => {
        if (typeof this.props.closeFn != "undefined")
            this.props.closeFn();
        else if (typeof this.props.data.closeTab == 'function') {
            this.props.data.closeTab(this.props.data.id)
        } else if (typeof this.props.onCancel === "function") {
            this.props.onCancel();
        }
    }
    loadData = () => {
        if (!this.props.record.ID) {
            return;
        }
        let json = initFilter(this.props.login.OrgId);
        extendRule(json, 'ID', 1, this.props.record.ID);
        extendInclude(json, 'Nav_DrillRecord');
        extendInclude(json, 'Nav_DrillRecord.Nav_ManageUser');
        extendInclude(json, 'Nav_DrillRecord.Nav_DrillEstimateUser');
        extendInclude(json, 'Nav_DrillRecord.Nav_DrillEstimateUser.Nav_User');
        extendInclude(json, 'Nav_DrillEstimateRecord');
        extendInclude(json, 'Nav_DrillEstimateRecord.Nav_Item');
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'CM/DrillEstimate/Get',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({
                        changed: false,
                        data: ret
                    })
                }
            }
        });
    }
    onTableBtnExport() {
        let TableWrap = document.getElementById('tableId' + this.props.record.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] || '';
    }
    fmtManagerUser(data) {
        if (!data || !data.Nav_DrillRecord || !data.Nav_DrillRecord.Nav_ManageUser)
            return '';
        return data.Nav_DrillRecord.Nav_ManageUser.NAME;
    }
    fmtEstimateUser(data) {
        if (!data || !data.Nav_DrillRecord || !data.Nav_DrillRecord.Nav_DrillEstimateUser)
            return '';
        let ret = [];
        for (let it of data.Nav_DrillRecord.Nav_DrillEstimateUser) {
            if (it.Nav_User && it.Nav_User.NAME) {
                ret.push(it.Nav_User.NAME);
            }
        }
        return ret.join('、');
    }


    render() {
        const { data, isView } = this.state;
        const uid = this.props.login.user.ID;
        const users = data && data.Nav_DrillRecord && data.Nav_DrillRecord.Nav_DrillEstimateUser && data.Nav_DrillRecord.Nav_DrillEstimateUser;
        const canNotify = !isView && data && data.STATUS === 0 && users && users.length > 0 && users[0].USER_ID == uid;
        const canAgree = !isView && data && data.STATUS === 1 && users && users.find(it => it.USER_ID === uid && !it.AGREE) && !canNotify;
        return <div>
            <div style={{ padding: '10px' }}>
                {
                    this.state.isView && <>
                        <ReactToPrint
                            trigger={() => <Button type={'primary'} icon={'printer'} >打印</Button>}
                            content={() => this.componentRef}
                        />
                        <Button style={{ marginLeft: '8px' }} onClick={() => this.onTableBtnExport()} icon="export" >导出</Button>
                    </>
                }
                {
                    canNotify && this.state.changed && <Button type={'primary'} style={{ marginLeft: '8px' }} onClick={() => this.onSave()} icon="save" >保存</Button>
                }
                {
                    canNotify && <Button style={{ marginLeft: '8px' }} onClick={() => this.onNotify()} icon="notification" >通知</Button>
                }
                {
                    canAgree && <Button style={{ marginLeft: '8px' }} onClick={() => this.onAgree()} icon="check" >签到</Button>
                }
            </div>


            {/* 应急演练效果评估 */}
            <div ref={el => (this.componentRef = el)} style={{ padding: '20px' }} id={'tableId' + this.props.record.ID}>
                {
                    this.state.isView && <h1 style={{ textAlign: 'center', margin: '15px' }}>应急演练效果评估</h1>
                }
                <table style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                    <tbody>
                        <tr>
                            <td colSpan={3} rowSpan={1}>演练主题</td>
                            <td colSpan={21} rowSpan={1}>{data && data.Nav_DrillRecord && data.Nav_DrillRecord.NAME}</td>
                        </tr>
                        <tr>
                            <td colSpan={3} rowSpan={1} >编号</td>
                            <td colSpan={3} rowSpan={1} >{data && data.CODE}</td>
                            <td colSpan={3} rowSpan={1} >演练时间</td>
                            <td colSpan={3} rowSpan={1} >{data && data.Nav_DrillRecord && data.Nav_DrillRecord.TIME}</td>
                            <td colSpan={3} rowSpan={1} >演练地点</td>
                            <td colSpan={3} rowSpan={1} >{data && data.Nav_DrillRecord && data.Nav_DrillRecord.PLACE}</td>
                            <td colSpan={3} rowSpan={1} >演练方式</td>
                            <td colSpan={3} rowSpan={1} >{data && data.Nav_DrillRecord && this.fmtEnum('CMDrillMethodEnum', data.Nav_DrillRecord.METHOD)}</td>
                        </tr>
                        <tr>
                            <td colSpan={3} rowSpan={1} >现场总指挥</td>
                            <td colSpan={3} rowSpan={1} >{this.fmtManagerUser(data)}</td>
                            <td colSpan={3} rowSpan={1} >评估人员</td>
                            <td colSpan={9} rowSpan={1} >{this.fmtEstimateUser(data)}</td>
                        </tr>
                        {
                            data && data.Nav_DrillEstimateRecord && data.Nav_DrillEstimateRecord.map((item, index) => {
                                return <>
                                    <tr key={item.ID}>
                                        <td colSpan={3} rowSpan={1} >{item.Nav_Item && item.Nav_Item.NAME}</td>
                                        <td colSpan={3} rowSpan={1}>
                                            <Select value={item.SCORE} disabled={!canNotify} onChange={value => {
                                                let data = this.state.data;
                                                data.Nav_DrillEstimateRecord[index].SCORE = value;
                                                this.setState({
                                                    data,
                                                    changed: true,
                                                });

                                            }} style={{ width: '100%' }}>
                                                <Select.Option value={1}>好</Select.Option>
                                                <Select.Option value={2}>较好</Select.Option>
                                                <Select.Option value={3}>一般</Select.Option>
                                                <Select.Option value={4}>差</Select.Option>
                                            </Select>
                                        </td>
                                        <td colSpan={3} rowSpan={1}>建议</td>
                                        <td colSpan={15} rowSpan={1}>
                                            <Input value={item.ADVISE} disabled={!canNotify} onChange={evt => {
                                                let data = this.state.data;
                                                data.Nav_DrillEstimateRecord[index].ADVISE = evt.target.value;
                                                this.setState({
                                                    data,
                                                    changed: true,
                                                });
                                            }} style={{ width: '100%' }} />
                                        </td>
                                    </tr>
                                </>
                            })
                        }

                        <tr>
                            <td colSpan={3} rowSpan={1}>其它改进方面</td>
                            <td colSpan={21} rowSpan={1}>
                                <Input value={data && data.OTHER_ADVISE} disabled={!canNotify} onChange={evt => {
                                    let data = this.state.data;
                                    data.OTHER_ADVISE = evt.target.value;
                                    this.setState({
                                        data,
                                        changed: true,
                                    });
                                }} style={{ width: '100%' }} />
                            </td>
                        </tr>
                        <tr>
                            <td colSpan={3} rowSpan={1}>审阅意见</td>
                            <td colSpan={21}>
                                {
                                    data && data.Nav_DrillRecord && data.Nav_DrillRecord.Nav_DrillEstimateUser &&
                                    data.Nav_DrillRecord.Nav_DrillEstimateUser.filter(it => it.AGREE).map((item, index) => {
                                        return item.Nav_User.AUTOGRAPH_ID ?
                                            <img width={'100px'} height={'100px'} src={config.serviceHost + 'PF/File/GetFile?id=' + item.Nav_User.AUTOGRAPH_ID} />
                                            :
                                            <img width={'100px'} height={'100px'} title={item.Nav_User.NAME} src={require("../../../../src/assets/logo.png")} />
                                    })
                                }
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    }

}

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