import { Checkbox, message, Radio } from "antd/lib/index";
import { Button, Descriptions, Badge, Popconfirm, Row, Col, Form, Input, Select, Table } from 'antd';
import React from 'react';
import { initFilter, extendRule, extendInclude, extendIgnoreDataRule, setDataFieldValue, guid, initQueryFilter, showFiles, showApprove, showUserSign, showUsersSign } from "../../../utils/common";
import { ExportToExcel } from '@woowalker/feui'
import styles from '../HI/StepForm.css';
import ReactToPrint from "react-to-print";
import moment from 'moment';
import config from "../../../config.js";
import XLSX from 'xlsx';
import { connect } from 'dva';
import FormPage from '../../FormPage'

class SC030View extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: null,
            item: null,
            btndisplay: 'none',
            isTaskSign: false, //是否待办 审批
            JOINRESULT: 0, //10 参加  20 请假 
            NOTJOINREASON: '',//请假原因
            isHasOpinion: false,//会议意见库
            isMinutesDetail: false,//会议纪要落实
            listSign: []//会议签到
        }
    }

    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' + this.props.record.ID);
        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,
        });
    }

    //审批意见 改变
    NOTJOINREASONCHANGE = (val) => {
        this.state.NOTJOINREASON = val
    }
    //请假/参加
    sign = () => {
        let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID);
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SC/SCMtMeetingMinutes/MeetingMinutesSign',
            onComplete: (ret) => {
                if (ret) {
                    this.props.data.onCancel();
                }
            }
        });
    }

    //请假/参加
    sureCheck = (val) => {
        if (val && val == 20 && (this.state.NOTJOINREASON == undefined || this.state.NOTJOINREASON == null || this.state.NOTJOINREASON.length < 1)) {
            message.error("请填写请假备注！");
            return false
        }
        let json = initFilter(this.props.login.OrgId, this.props.data.id, null, null, null, this.props.data.TaskID, val, this.state.NOTJOINREASON);
        extendIgnoreDataRule(json)
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SC/SCMTMeeting/PAllCheck',
            onComplete: (ret) => {
                if (ret) {
                    this.props.data.onCancel();
                }
            }
        });
    }

    //加载数据(传过来的参数都在props里面，包括登录信息)
    loadData = (id) => {
        var taskID = this.props.data.TaskID
        var loginID = this.props.login.user.ID
        var orgId = this.props.login ? this.props.login.OrgId : '';   //登录后有存储登录信息
        let json = initFilter(orgId);
        extendRule(json, 'ID', 1, id);    //ID=‘’，字段，等于，条件值， 1代表=，2代表不等于，3小于，4小于等于，5大于，6大于等于，7以开始的Like,8以结束的Like，9包含 
        extendInclude(json, 'Nav_Meeting');
        extendInclude(json, 'Nav_ListPReview');
        extendInclude(json, 'Nav_ListPReview.Nav_User.Nav_Person.Nav_Post');
        extendInclude(json, 'Nav_ListPReview.Nav_User.Nav_Department');// extendInclude(json, 'Nav_ListPReview.Nav_User.Nav_Department.Nav_Parent');
        extendInclude(json, 'Nav_ListMinutesDetail.Nav_UserDeal');
        extendInclude(json, 'Nav_ListMinutesDetail.Nav_DepartmentDeal');
        extendInclude(json, 'Nav_Meeting.Nav_Department');
        extendInclude(json, 'Nav_Meeting.Nav_UserHost');
        extendInclude(json, 'Nav_Meeting.Nav_UserRecorder');
        extendInclude(json, 'Nav_ListPAskForLeave.Nav_User');
        extendInclude(json, 'Nav_ListPAbsence.Nav_User');
        extendInclude(json, 'Nav_ListPJoin.Nav_User.Nav_Department');
        extendInclude(json, 'Nav_ListPJoin.Nav_User.Nav_Person.Nav_Post');

        extendInclude(json, 'Nav_ListPJoin.Nav_User.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        extendInclude(json, 'Nav_Approve.Nav_ApproveDetails.Nav_ApproveUser.Nav_UserSignFiles.Nav_ImgFile.Nav_File');
        // extendInclude(json, 'Nav_ListContent.Nav_Content.Nav_ListContentDeal');//.Nav_ListContentDeal.CONTENTDETAIL
        // extendInclude(json, 'Nav_ListContent.Nav_ListContentDeal');//.CONTENT_DEAL_ID
        extendInclude(json, 'Nav_Files.Nav_ImgFile.Nav_File');
        extendIgnoreDataRule(json)
        let attendanceAry = [], leaveAry = [];
        let that = this;
        let btndisplay = 'none';
        this.props.dispatch({
            type: 'app/getDataByPost',
            payload: json,
            url: 'SC/SCMtMeetingMinutes/Get',
            onComplete: (ret) => {    //返回查询结果ret
                if (ret) {    //判断是否有记录
                    var isTaskSign = false
                    var isHasOpinion = false
                    var isMinutesDetail = false
                    if (this.props.data.tableKey != undefined && this.props.data.tableKey != "2") {
                        isTaskSign = true
                    }
                    if (ret.STATUS == 10 && taskID != undefined && taskID.length > 0) {
                        for (let i = 0; i < ret.Nav_ListPJoin.length; i++) {
                            if (ret.Nav_ListPJoin[i].USER_ID == loginID && ret.Nav_ListPJoin[i].ISRECORDRED == false) {
                                isTaskSign = true
                                break
                            }
                        }
                        if (!isTaskSign && ret.Nav_ListPReview != null && ret.Nav_ListPReview.length > 0) {
                            for (let i = 0; i < ret.Nav_ListPReview.length; i++) {
                                if (ret.Nav_ListPReview[i].USER_ID == loginID && ret.Nav_ListPReview[i].ISRECORDRED == false) {
                                    isTaskSign = true
                                    break
                                }
                            }
                        }
                    }
                    if (ret.Nav_ListContent != null && ret.Nav_ListContent.length > 0) {
                        for (let j = 0; j < ret.Nav_ListContent.length; j++) {
                            if (!isHasOpinion && ret.Nav_ListContent[j].Nav_Content.Nav_ListContentDeal != null && ret.Nav_ListContent[j].Nav_Content.Nav_ListContentDeal.length > 0) {
                                isHasOpinion = true
                            }
                        }
                    }
                    if (ret.Nav_ListMinutesDetail != null && ret.Nav_ListMinutesDetail.length > 0) {
                        isMinutesDetail = true
                    }

                    // var listSign = []
                    // if (ret.Nav_ListPJoin != undefined && ret.Nav_ListPJoin.length > 0) {
                    //     ret.Nav_ListPJoin.forEach(element => {
                    //         if (element.ISRECORDRED != undefined && element.ISRECORDRED == true) {
                    //             listSign.push(element)//element.Nav_User
                    //         }
                    //     });
                    // }

                    this.setState({   //设置setState全局变量
                        data: ret,      //将ret对象赋值给data, data供页面调用
                        isTaskSign: isTaskSign,// isTaskSign//有taskID 状态 审核完成  登陆者 属于参会人
                        isHasOpinion: isHasOpinion,//有会议意见
                        isMinutesDetail: isMinutesDetail,//会议纪要落实
                        // listSign: listSign//会议签到
                    })
                }
            }
        });
    }

    //渲染页面
    render() {
        const { data, isTaskSign, isMinutesDetail, isHasOpinion, listSign } = this.state;
        return <div>
            <div style={{ padding: '10px' }}>
                <table>
                    <tr>
                        <td><Button onClick={() => this.onClickApprove(true)} style={{ marginRight: '8px' }} icon="export">审批详情</Button></td>
                        <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>
                        {isTaskSign ? <td><Button className={styles.sign} 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 style={{ textAlign: 'center', margin: '15px' }}>会议纪要</h1>
                {
                    data ? <div>
                        <Descriptions title="" size="middle" bordered>
                            <Descriptions.Item label="会议主题" span={3}>{data.Nav_Meeting.THEME}</Descriptions.Item>
                            <Descriptions.Item label="会议编号">{data.Nav_Meeting.CODE}</Descriptions.Item>
                            {/* <Descriptions.Item label="会议时间">{data.Nav_Meeting.BEGIN_TIME}</Descriptions.Item> */}
                            <Descriptions.Item label="发起部门">{data.Nav_Meeting.Nav_Department.NAME}</Descriptions.Item>
                            <Descriptions.Item label="主持人">{data.Nav_Meeting.Nav_UserHost.NAME}</Descriptions.Item>
                            <Descriptions.Item label="记录人">{data.Nav_Meeting.Nav_UserRecorder.NAME}</Descriptions.Item>
                            <Descriptions.Item label="会议时间">{data.Nav_Meeting.BEGIN_TIME}</Descriptions.Item>
                            <Descriptions.Item label="会议地点">{data.Nav_Meeting.ADDRESS}</Descriptions.Item>
                            {
                                data.Nav_Meeting.MEETINGTYPE == 20 ?
                                    <Descriptions.Item label="审核意见" span={3} >{data.RESOLUTION}</Descriptions.Item>
                                    : (
                                        data.Nav_Meeting.MEETINGTYPE == 30 ?
                                            <Descriptions.Item label="评审意见" span={3} >{data.RESOLUTION}</Descriptions.Item>
                                            : (
                                                data.Nav_Meeting.MEETINGTYPE == 10 ?
                                                    <Descriptions.Item label="会议决议" span={3} >{data.RESOLUTION}</Descriptions.Item>
                                                    : null
                                            )
                                    )
                            }
                            <Descriptions.Item label="会议记录" span={3} >{data.MEETINGRECORD}</Descriptions.Item>

                            <Descriptions.Item label="请假人员" span={3}>{
                                data?.Nav_ListPAskForLeave.map((item, i) => {
                                    return (i > 0 ? "，" : "") + item.Nav_User.NAME
                                })
                            }</Descriptions.Item>
                            <Descriptions.Item label="缺席人员" span={3}>{data?.Nav_ListPAbsence.map((item, i) => {
                                return (i > 0 ? "，" : "") + item.Nav_User.NAME
                            })
                            }</Descriptions.Item>

                            <Descriptions.Item label="参会人员" span={3}>{data?.Nav_ListPJoin.map((item, i) => {
                                return (<a style={{ color: (item.ISRECORDRED ? "black" : "red") }}> {(i > 0 ? "，" : "") + item.Nav_User.NAME}</a>)
                            })
                            }</Descriptions.Item>

                            {/* <Descriptions.Item label="附件" span={3}>{data?.Nav_ListPJoin.map((item, i) => {
                                return (<a style={{ color: (item.ISRECORDRED ? "black" : "red") }}> {(i > 0 ? "，" : "") + item.Nav_User.NAME}</a>)
                            })
                            }</Descriptions.Item> */}

                            {/* extendInclude(json, 'Nav_Meeting.Nav_ListPReView.Nav_User'); */}
                            {
                                (data?.Nav_ListPReview && data.Nav_ListPReview.length > 0) ?
                                    <Descriptions.Item label="评审人员" span={3}>{
                                        data?.Nav_ListPReview?.map((item, i) => {
                                            //return (i > 0 ? "，" : "") + item.Nav_User.NAME
                                            // debugger
                                            return (<a style={{ color: (item.ISRECORDRED ? "black" : "red") }}> {(i > 0 ? "，" : "") + item.Nav_User.NAME}</a>)
                                        })
                                    }</Descriptions.Item>
                                    : null
                            }


                            <Descriptions.Item label="附件" span={3}>{
                                showFiles(data.Nav_Files, config.picServerHost, true)
                            }</Descriptions.Item>


                            {/* <Descriptions.Item label="会议签到" span={3}>{
                                showUsersSign(listSign, config.picServerHost, 75, 39)
                            }</Descriptions.Item> */}

                            {/* <Descriptions.Item label="会议决议" span={3}>{
                                <table  style={{ width: '100%', textAlign: 'center', borderTop: '1px solid #333', borderLeft: '1px solid #333' }} className={styles.PrintForm}>
                                    <tbody>
                                        <tr>

                                            <td>会议决议</td>
                                            <td>是否需要落实</td>
                                            <td>落实部门</td>
                                            <td>落实责任人</td>
                                            <td>责任人意见</td>
                                            <td>落实情况反馈</td>
                                            <td>落实效果</td>
                                        </tr>

                                        {data?.Nav_ListMinutesDetail.map((item, i) => {
                                            return <tr>
                                                <td>{item.RESOLUTION}</td>
                                                <td>{item.ISNEEDDEAL ? "是" : "否"}</td>
                                                <td>{item?.Nav_DepartmentDeal?.NAME}</td>
                                                <td>{item?.Nav_UserDeal?.NAME}</td>
                                                <td>{item.OPINION}</td>
                                                <td>{item.DEALBACK}</td>
                                                <td>{item.DEALEFFECT ? this.props.app.enums.DEALEFFECT.enums[item.DEALEFFECT] : ""}</td>
                                            </tr>
                                        })}

                                    </tbody>
                                </table>
                            }</Descriptions.Item> */}


                            <Descriptions.Item label="审阅意见" span={3}>{
                                showApprove(data.Nav_Approve?.Nav_ApproveDetails, this.props.app.enums.NodeApproveStatus.enums, config.picServerHost, 100, 65)
                            }</Descriptions.Item>

                        </Descriptions>
                    </div> : null
                }

                {/* 会议决议 */}
                {isMinutesDetail ?
                    <table className={styles.PrintFormLight} >
                        <tbody>
                            <tr className={styles.showPrintHead2} >
                                <td colSpan={7} style={{ textAlign: 'center' }}>会议决议</td>
                            </tr>
                            <tr className={styles.PrintFormLightTh}>
                                <td>会议决议</td>
                                <td>是否需要落实</td>
                                <td>落实部门</td> {/*部门/车间 班组人员归到车间 */}
                                <td>落实责任人</td>
                                <td>责任人意见</td>
                                <td>落实情况反馈</td>
                                <td>落实效果</td>
                            </tr>

                            {data?.Nav_ListMinutesDetail.map((item, i) => {
                                return <tr>
                                    <td>{item.RESOLUTION}</td>
                                    <td>{item.ISNEEDDEAL ? "是" : "否"}</td>
                                    <td>{item?.Nav_DepartmentDeal?.NAME}</td> {/*部门/车间 班组人员归到车间 */}
                                    <td>{item?.Nav_UserDeal?.NAME}</td>
                                    <td>{item.OPINION}</td>
                                    <td>{item.DEALBACK}</td>
                                    <td>{item.DEALEFFECT ? this.props.app.enums.DEALEFFECT.enums[item.DEALEFFECT] : ""}</td>
                                </tr>
                            })}
                        </tbody>
                    </table> : null
                }

                {/* 纪要意见 */}
                {/* 列表查看 不需要会议意见 */}

                {/* 会议签到表 */}
                {/*部门/车间 班组人员归到车间 */}
                {/* <td>{item.Nav_User.Nav_Department.DEPARTMENT_TYPE == 2 ? item.Nav_User.Nav_Department.Nav_Parent.NAME : item.Nav_User.Nav_Department.NAME}</td> */}
                {
                    data?.Nav_ListPJoin ?
                        <table className={styles.PrintFormLight} >
                            <tbody>
                                <tr className={styles.showPrintHead2} >
                                    <td colSpan={5} style={{ textAlign: 'center' }}>会议签到表</td>
                                </tr>
                                <tr className={styles.PrintFormLightTh}>
                                    <td>序号</td>
                                    <td>姓名</td>
                                    {/* <td>部门/车间/班组</td> */}
                                    <td>部门</td>
                                    <td>岗位</td>
                                    <td>电子签名</td>
                                </tr>
                                {data?.Nav_ListPJoin.map((item, i) => {
                                    return <tr>
                                        <td>{(i + 1)}</td>
                                        <td>{item.Nav_User.NAME}</td>
                                        <td>{item.Nav_User?.Nav_Department?.NAME}</td>
                                        <td>{item.Nav_User.Nav_Person?.Nav_Post?.NAME}</td>
                                        <td> {
                                            item.ISRECORDRED ?
                                                showUserSign(item.Nav_User, config.picServerHost, 100, 30)
                                                : null
                                        }</td>
                                    </tr>
                                })}

                                {/* <td>{item.Nav_User.Nav_Department.DEPARTMENT_TYPE == 2 ? item.Nav_User.Nav_Department.Nav_Parent.NAME : item.Nav_User.Nav_Department.NAME}</td> */}
                                {data?.Nav_ListPReview?.map((item, i) => {
                                    return <tr>
                                        <td>{(i + 1)}</td>
                                        <td>{item.Nav_User.Nav_Department.NAME}</td>
                                        <td>{item.Nav_User.NAME}</td>
                                        <td>{item.Nav_User.Nav_Person?.Nav_Post?.NAME}</td>
                                        <td> {
                                            item.ISRECORDRED ?
                                                showUserSign(item.Nav_User, config.picServerHost, 100, 30)
                                                : null
                                        }</td>
                                    </tr>
                                })}
                            </tbody>
                        </table> : null
                }

            </div>
        </div>
    }
}
export default connect(({ login, app }) => ({ login, app }))(SC030View) 