import { message } from "antd/lib/index";
import { Button, Descriptions, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, setDataFieldValue, guid, initQueryFilter, onBeforeSaveHandleRecordLog } from "../../../utils/common";
import ReactToPrint from "react-to-print";
import config from "../../../config";
import { connect } from 'dva';
import moment from 'moment';

class OG001ShowPrint extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            enumData: null,
            BtnUserDisplay: 'none',
            BtnChargeUserDisplay: 'none',
            data: 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);
        }
    }


    onTableBtnUser() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'OG/OGSafePdtSigned/UserAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('签订成功！');
                    this.setState({ BtnUserDisplay: 'none' })
                    this.BtnClose();
                }
            }
        })
    }

    onTableBtnChargeUser() {
        this.props.dispatch({
            type: 'app/getDataByPost',
            url: 'OG/OGSafePdtSigned/ChargeUserAgree',
            payload: {
                ID: this.props.data.id,
                TaskID: this.props.data.TaskID,
            },
            onComplete: (ret) => {
                if (ret) {
                    message.success('签订成功！');
                    this.setState({ BtnChargeUserDisplay: '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);
        extendRule(json, 'ID', 1, dataId);
        extendInclude(json, 'Nav_SafePdtSignedPost.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_SafePdtSignedPost.Nav_ChargeUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_StandardCreate');
        json.IgnoreDataRule = true;
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'OG/OGSafePdtSigned/FullGet',
            onComplete: (ret) => {
                if (ret) {
                    this.setState({ data: ret })
                    let findChargeUser = ret.Nav_SafePdtSignedPost.find(item => item.CHARGE_USER_ID == this.props.login.user.ID && item.CHARGE_DEAL_STATUS == 0);
                    if (findChargeUser != undefined) {
                        this.setState({ BtnChargeUserDisplay: 'inline' })
                    } else {
                        let finduser = ret.Nav_SafePdtSignedPost.find(item => item.USER_ID == this.props.login.user.ID && item.DEAL_STATUS == 0);
                        if (finduser != undefined) {
                            this.setState({ BtnUserDisplay: 'inline' })
                        }
                    }
                }
            }
        });
    }

    render() {
        const { data } = this.state;
        return <div>
            <div style={{ marginTop: '10px' }}>
                <ReactToPrint
                    trigger={() => <Button type={'default'} icon={'printer'} style={{ marginLeft: '20px', display: this.state.isFinished }} >打印</Button>}
                    content={() => this.componentRef}
                />
                <Button type={'primary'} style={{ marginLeft: '20px', display: this.state.BtnUserDisplay }} onClick={() => this.onTableBtnUser()} icon="check" >签订</Button>
                <Button type={'primary'} style={{ marginLeft: '20px', display: this.state.BtnChargeUserDisplay }} onClick={() => this.onTableBtnChargeUser()} icon="check" >主管签订</Button>

            </div>



            <div ref={el => (this.componentRef = el)} id={'tableId' + this.props.data.id} style={{ padding: '20px' }}>

                <h1 style={{ textAlign: 'center' }}>{data ? data.Nav_StandardCreate.NAME : ''}</h1>
                <div style={{ fontSize: '1.5em', textIndent: '2em' }}>
                    <div dangerouslySetInnerHTML={{ __html: data ? data.FILE_CONTENT : '' }}></div>
                </div>
                {
                    data ? <table style={{ width: '100%', marginTop: '8px', textAlign: 'left' }}>
                        <tbody>
                            <tr>
                                <td colSpan={3} rowSpan={1} >负责人:{
                                    data.Nav_SafePdtSignedPost && data.Nav_SafePdtSignedPost.map((item, i) => {
                                        if (i == 0) {
                                            if (item.CHARGE_DEAL_STATUS == 1) {
                                                if (item.Nav_ChargeUser != null && item.Nav_ChargeUser.Nav_UserSignFiles[0] != null)
                                                    return <span><img width={'150px'} alt={item.Nav_ChargeUser.NAME} src={config.picServerHost + item.Nav_ChargeUser.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} /> <span>{new Date(item.CHARGE_SIGNED_TIME).Format("yyyy-MM-dd")}</span></span>
                                                else
                                                    return <span><img width={'150px'} alt={item.Nav_ChargeUser.NAME} /> <span>{new Date(item.CHARGE_SIGNED_TIME).Format("yyyy-MM-dd")}</span></span>
                                            }
                                        }
                                    })
                                }
                                </td>
                            </tr>
                            <tr>
                                <td colSpan={3} rowSpan={1} >签订人员:
                                    {
                                        data.Nav_SafePdtSignedPost && data.Nav_SafePdtSignedPost.map((item, i) => {
                                            if (item.DEAL_STATUS == 1) {
                                                if (item.Nav_User != null && item.Nav_User.Nav_UserSignFiles[0] != null)
                                                    return <span><img width={'150px'} alt={item.Nav_User.NAME} src={config.picServerHost + item.Nav_User.Nav_UserSignFiles[0].Nav_ImgFile.Nav_File.FILE_PATH} /> <span>{new Date(item.SIGNED_TIME).Format("yyyy-MM-dd")}</span></span>
                                                else
                                                    return <span><img width={'150px'} alt={item.Nav_User.NAME} /> <span>{new Date(item.SIGNED_TIME).Format("yyyy-MM-dd")}</span></span>
                                            }
                                        })
                                    }
                                </td>
                            </tr>
                        </tbody>
                    </table> : null
                }

            </div>
        </div>
    }

}
export default connect(({ login }) => ({ login }))(OG001ShowPrint) 