<template>
    <div class="mdt-dashboard">
        <mdt-steps :step="meetingStep" class="my-mdt-steps"/>
        <div class="mdt-dashboard-con">
            <!--会议召集编辑既往-->
            <dashboard :meetingInfo="meetingInfo"
            @refresh="refreshMdt"
            @changeStep="changeStep"
            :step="step"
            v-show="step < 3 || step == 4 || step > 5"/>
            <!--会议中-->
            <meeting-in
            @changeStep="changeStep"
            @openEmrDetail="e => $emit('openEmrDetail',e)"
            v-if="step == 3"
            ></meeting-in>
            <!--填写会议记录-->
            <!--小结填写-->
            <SpResizeContainer custom-class="mdt-resize-con" v-if="step == 5" :minL="900" rt="会议资料" :showL="false" :isShowL="showL">
                <!-- 填写会议参考资料 -->
                <referenceMaterial :mdtId="mdtId" slot="left"/>
                <!-- 填写小结记录 -->
                <div slot="center" style="height: 100%;position:relative">
                    <el-scrollbar tag="div" class="scroll">
                        <mdt-consult :healthCardNoCm="healthNum" @changeStep="changeStep" :mdtId="mdtId" @refresh="refreshMdt" @show="e => showDialog(e)"/>
                    </el-scrollbar>
                    <el-drawer
                        custom-class="lv-drawer"
                        :size="type == 'comment' ? '326px' : '173px'"
                        title="我是标题"
                        :visible.sync="dialogVisible"
                        append-to-body
                        :with-header="false">
                        <checkDocList :mdtId="mdtId" v-if="type == 'check'" @close="dialogVisible = false"/>
                        <comments v-if="type == 'comment'" @close="dialogVisible = false"/>
                    </el-drawer>
                </div>
            </SpResizeContainer>
            <div class="posi-fixed" v-if="!dialogVisible">
                <i class="icon-fixed iconfont iconyanjing" @click="showDialog('check')" v-if="(isPower == 2 || isPower == 3) && step == 5 && meetingInfo.mdtMeeting.eyeIcon"></i>
                <!-- <i class="icon-fixed el-icon-edit"  @click="showDialog('comment')"></i> -->
            </div>
            <!--准备会议资料查看-->
        </div>
    </div>
</template>
<script>
import { getMdtMeetingInfo,getInfoById,getMdtMember } from '@/request/mdt.js'
import SpResizeContainer from '../ResizeContainer/main'
import dashboard from './business/dashboard.vue'
import comments from './components/comments.vue'
import mdtConsult from './components/mdt-consult.vue'
import meetingIn from './business/meeting-in.vue'
import mdtSteps from './components/mdt-steps.vue'
import checkDocList from './components/checkDocList.vue'
import referenceMaterial from './business/reference-material.vue'
export default {
    name: 'SpMdtDashbord',
    componentName: 'SpMdtDashbord',
    props: {
        mdtId: String | Number,//会议id
        healthNum: String | Number,//患者健康卡号
        patientId: String | Number,//患者id
        doctorInfo: {//当前登录医生的信息
            type: Object,
            default: () => {
                return {
                    nicknames: '',
                    id: '',
                    username: ''
                }
            }
        },
        route: String,//路由，用于不同页面权限展示
        setting: String, //环境判断
        oldMdtId: [String,Number],//上一个会议ID
    },
    components: { SpResizeContainer,dashboard,comments,mdtConsult,meetingIn,mdtSteps,checkDocList,referenceMaterial },
    data() {
        return {
            isPower: 1,//权限
            type: 'check',
            dialogVisible: false,
            showL: false,
            meetingStep: 0,//会议结点
            step: 0,//当前医生结点
            meetingInfo: {
                mdtMeeting: {
                    meetingNodeStatus: 0,
                    meetingOpinionStatus: '',
                    eyeIcon: null
                },
                doctorList: []
            },
        }
    },
    provide(){
        return {
            topObj: this,
        }
    },
    computed: {
        //判断当前路由是全部患者还是mdt患者 || cm的我的患者，全部患者的路由不能有编辑权限
        isRoute(){
            return this.route == "/caseManage/myPatient" || this.route == "/patient/mdtPatient" ? true : false
        }
    },
    watch: {
        step(v) {
            // this.saveStepCom(v)
        },
        mdtId(v) {
            this.handleOption(1, { pointLoading: false })
        },
        patientId: {
            handler(v) {
                if(v) {
                    this.getMdtMeetingInfo()
                    // this.saveCurPatient(v)
                }
            },
        },
        healthNum(v) {
            // this.saveCurHealthNum(v)
        }
    },
    created() {
        this.refreshMdt('',true)
    },
    mounted() {
        this.handleOption(1, { pointLoading: false })
    },
    methods: {
        //子组件emit事件
        handleOption(t, d) {
            this.$emit('handle', t, d)
            if(t == 1){//loading

            } else if(t == 2){//保存上一个会议id

            } else if(t == 3){//保存会议id

            } else if(t == 4){//会议记录返回到列表

            } else if(t == 5){//会议Tumor Board备忘录
            } else if(t == 6) {//图片上传进度条

            } else if(t == 7) {//时间轴打印

            } else if(t == 8) {//上传进度条进度

            }
        },
        changeStep(e) {
            if((e.step < 3 || e.step == 4 || e.step == 6 || e.step == 5) && e.status){
                this.meetingStep = (e.step == 6 && !(this.isPower == 2 || this.isPower == 3)) ? 0 : e.step;
            }
            if(e.step ==5 && !(this.isPower == 2 || this.isPower == 3)) {
                this.step = 4;
                this.getMdtMeetingInfo()
            } else {
                this.step = e.step;
            }
        },
        async getMdtMeetingInfo(status) {
            const {code, data} = await getMdtMeetingInfo(this.patientId)
            this.handleOption(1, { pointLoading: false })
            if(code == 0) {
                if(data) {//会议可正常组建的情况
                    // this.saveDocPower(this.hasMdtAuthority(data.doctorList, this.doctorInfo.id))
                    this.meetingInfo = data;
                    this.isPower = data?.status || 5
                    // this.isPower = 2
                    // data.mdtMeeting.meetingNodeStatus = 3
                    if(data.mdtMeeting){
                        //需要emit出去
                        this.handleOption(3, data.mdtMeeting.id)
                        // this.saveMeetingId(data.mdtMeeting.id)
                        // this.saveMdtId(data.mdtMeeting.id)
                        this.meetingStep = data?.mdtMeeting?.meetingNodeStatus ? data.mdtMeeting.meetingNodeStatus : 0;
                        this.step = this.getCurDocNode(data?.mdtMeeting);
                    } else {
                        this.handleOption(3, '')
                        // this.saveMeetingId('')
                        // this.saveMdtId('')
                        this.step = 0;
                        this.meetingStep = 0;
                        this.meetingInfo.mdtMeeting = {
                            eyeIcon: 0
                        }
                    }
                } else {
                    //未组件团队没有任何数据的情况
                    this.step = 0;
                    this.meetingStep = 0;
                    this.isPower = 5
                    // await this.getTeamMember(status)
                }
                this.$forceUpdate()
            }
        },
        getCurDocNode(data) {
            if(data){
                if(data?.meetingNodeStatus == 1) {
                    return (this.isPower < 4 && this.isPower > 0 && this.isRoute) ? data?.meetingNodeStatus : 0;
                } else if(data?.meetingNodeStatus == 3){
                    return (this.isPower < 4 && this.isPower > 0 && this.isRoute) ? data?.meetingNodeStatus : 2;
                } else if(data?.meetingNodeStatus == 5){
                    if((this.isPower == 2 || this.isPower == 3) && this.isRoute){
                        return (data?.pdfReportUrl || data?.tboardPdfUrl) ? 4 : 5;
                    } else {
                        return 4
                    }
                } else if(data?.meetingNodeStatus == 7) {
                    return 6
                }else {
                    return data?.meetingNodeStatus
                }
            } else {
                return 0
            }
        },
        //重新获取会议信息，如果没有参数过来就根据当前患者查询最近一条，如果有id则是根据id查询对应的信息
        refreshMdt(e,status){
            if(status){
                this.handleOption(1, { pointLoading: true })
            }
            if(e){
                this.getInfoById(e,status)
            } else {
                this.getMdtMeetingInfo(status);
            }
        },
        //获取当前患者的团队，看是否在患者团队，非会议id查询情况下
        // async getTeamMember(status) {
        //     const res = await getMdtMember(this.patientId);
        //     if (res.code === 0) {
        //         this.saveDocPower(this.hasMdtAuthority(res.data, this.doctorInfo.id))
        //         this.meetingInfo.doctorList = res.data
        //     }
        // },
        //根据会议会议id查询会议详情情况下
        async getInfoById(id,status) {
            const {code, data, msg} = await getInfoById(id)
            this.handleOption(1, { pointLoading: false })
            if(code == 0) {
                this.isPower = data.status
                // this.saveDocPower(this.hasMdtAuthority(data.doctorList, this.doctorInfo.id))
                const meetingInfo = {
                    mdtMeeting: {
                        ...data
                    },
                    doctorList: data?.doctorList || []
                }
                this.meetingInfo = meetingInfo
                this.handleOption(3, meetingInfo?.mdtMeeting?.id)
                this.meetingStep = data?.meetingNodeStatus == 7 ? 5 : (data?.meetingNodeStatus ? data?.meetingNodeStatus : 0);
                this.step = this.getCurDocNode(meetingInfo?.mdtMeeting);
            } else {
                this.isPower = 5
                this.$message.error(msg)
            }
        },
        showDialog(type) {
            this.type = type;
            this.dialogVisible = true
        }
    }
}
</script>
<style lang="less" scoped>
// @import url('../../src/styles/new-mdt.less');
.mdt-dashboard {
    height: 100%;
    .posi-fixed {
        position: absolute;
        top: calc(50% - 84px);
        right: -24px;
        display: flex;
        flex-direction: column;
    }
    .icon-fixed {
        color: #fff;
        width: 28px;
        height: 28px;
        text-align: center;
        line-height: 28px;
        background: #1890ff;
        border-radius: 4px 0px 0px 4px;
        box-shadow: 0px 1px 4px 0px rgba(0,0,0,0.19); 
        cursor: pointer;
        margin-bottom: 10px;
    }
}
.my-mdt-steps {
    margin-bottom: 12px;
}
.mdt-dashboard-con {
    height: calc(100% - 84px);
    position: relative;
}
/deep/ .mdt-resize-con {
    .resize-left {
        background-color: #fff;
    }
    .resize-right {
        // background-color: #fff;
        .scroll {
            height: 100%;
        }
        .el-scrollbar__wrap {
            overflow-x: hidden;
        }
    }
    .change-icon {
        background: #1890ff!important;
        color: #fff!important;
        border-color: #1890ff!important;
    }
}
</style>