<template>
    <div style="margin-left: 10px;">
        <div class="listBtn">
            <el-button type="primary" class="btnClass" @click="prList">人员列表</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass"  @click="save">保存结果</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass"  @click="edit">修改结果</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass"  @click="del">删除结果</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass" @click="delItem">删除项目</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass" >历次结果</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass" >体检报告</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass" >生成小结</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass"  @click="toSumDoctorCheck">总检</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass"  @click="audit">审核</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass"  @click="unAudit">取消审核</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass" >排队</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass"  @click="addSummary">新增小结</el-button>
        </div>
        <!-- 人员档案列表 -->
        <el-dialog title="人员档案列表" :visible.sync="doctorCheck.doctorCheckDialogVisible" width="1000px" >
            <PatientRegisterList win="doctorCheck"/>
        </el-dialog>
    </div>
</template>
<script>
import { mapState, mapActions } from "vuex";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { arrayExistObj } from "../../utlis/proFunc";

import PatientRegisterList from "./PatientRegisterList.vue";

export default {
    components: {        
        PatientRegisterList,       
    },
    data() {
        return {
            
        };
    },

    created() { },

    //挂载完成
    mounted() {
       
    },

    computed: {
        ...mapState(["dict", "patientRegister", "doctorCheck"]),
    },

    methods: {
        ...mapActions(["getCustomerOrgGroup"]),

        prList(){
            console.log('this.doctorCheck.doctorCheckDialogVisible',this.doctorCheck.doctorCheckDialogVisible)
            this.doctorCheck.doctorCheckDialogVisible = true 
            console.log('this.doctorCheck.doctorCheckDialogVisible2',this.doctorCheck.doctorCheckDialogVisible) 
        },

        //操作判断
        optGrant(optType){    
            let ret = ''        
            if (!this.doctorCheck.RegisterCheckEdit.id) return '请选择组合项目'
            if (this.doctorCheck.RegisterCheckEdit.isLock == 'Y') return '组合项目已锁定，不可执行此操作'
            if (this.doctorCheck.RegisterCheckEdit.completeFlag == '2') return '组合项目已弃检，不可执行此操作'
            if(optType == 'save'){
                if (!this.doctorCheck.RegisterCheckEdit.checkDoctorId) return "请选择检查医生"
                if (this.doctorCheck.RegisterCheckEdit.completeFlag == '1') return "该项目项目已保存，不可再执行此操作，如需操作请点【修改结果】"
                if (this.doctorCheck.RegisterCheckEdit.isAudit == 'Y') return '组合项目已审核，请先取消审核，方可执行此操作！'
            }
            if(optType == 'edit'){
                if (this.doctorCheck.RegisterCheckEdit.completeFlag == '0') return "该检查项目尚未保存，无需执行此操作"
                if (this.doctorCheck.RegisterCheckEdit.isAudit == 'Y') return '组合项目已审核，请先取消审核，方可执行此操作！'
            }
            if(optType == 'del'){
                if (this.doctorCheck.RegisterCheckEdit.completeFlag == '0') return "该检查项目尚未保存，无需执行此操作"
                if (this.doctorCheck.RegisterCheckEdit.isAudit == 'Y') return '组合项目已审核，请先取消审核，方可执行此操作！'
            }
            if(optType == 'audit'){
                if (this.doctorCheck.RegisterCheckEdit.completeFlag == '0') return "该检查项目尚未保存，无需执行此操作"
                if (this.doctorCheck.RegisterCheckEdit.isAudit == 'Y') return "该检查项目已审核，无需再执行此操作"
            }
            if(optType == 'unAudit'){
                if (this.doctorCheck.RegisterCheckEdit.completeFlag == '0') return "该检查项目尚未保存，无需执行此操作"
                if (this.doctorCheck.RegisterCheckEdit.isAudit == 'N') return "该检查项目尚未审核，无需执行此操作"
            }
            if(optType == 'delItem'){
                if (this.doctorCheck.RegisterCheckEdit.completeFlag == '1') return "该项目项目已保存，不可再执行此操作，如需操作请点【修改结果】"
                if (this.doctorCheck.RegisterCheckEdit.isAudit == 'Y') return "该检查项目已审核，请先取消审核"
            }
            
            return ret
        },

        //保存数据
        save() {
            let ret = this.optGrant('save')
            if(ret){
                alert(ret)
                return
            }            

            //更新明细
            this.updateCheckItemList();

            //更新检查项目与医生
            this.updateDoctorCheck();

            //更新小结        
            this.saveCheckSummary();

            //更新建议      
            this.saveCheckSuggestion();
        },

        //修改结果
        edit(){
            let ret = this.optGrant('edit')
            if(ret){
                alert(ret)
                return
            } 
            this.doctorCheck.RegisterCheckEdit.completeFlag = '0'
        },

        //删除结果
        del(){
            let ret = this.optGrant('del')
            if(ret){
                alert(ret)
                return
            }            
            console.log(`/api/app/registercheck/updatecomplete?RegisterCheckId=${this.doctorCheck.RegisterCheckEdit.id}&CompleteFlag=0`)
            postapi(`/api/app/registercheck/updatecomplete?RegisterCheckId=${this.doctorCheck.RegisterCheckEdit.id}&CompleteFlag=0`)
                .then((res) => {
                    console.log("del", res.data);
                    if (res.code != -1) {
                        this.doctorCheck.RegisterCheckEdit.completeFlag = '0';
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `组合项目删除失败，原因：${err}` });
                });
        },

        //删除明细项目
        delItem(){
            let ret = this.optGrant('delItem')
            if(ret){
                alert(ret)
                return
            }
            let lfind =  arrayExistObj(this.doctorCheck.CheckItemList,'itemId',this.doctorCheck.checkItem.itemId)
            if(lfind == -1) return
            
            console.log(`/api/app/registercheckitem/deleteregistercheckitem?RegisterCheckId=${this.doctorCheck.checkItem.registerCheckId}&ItemId=${this.doctorCheck.checkItem.itemId}`)
            postapi(`/api/app/registercheckitem/deleteregistercheckitem?RegisterCheckId=${this.doctorCheck.checkItem.registerCheckId}&ItemId=${this.doctorCheck.checkItem.itemId}`)
                .then((res) => {
                    console.log("delItem", res.data);
                    if (res.code != -1) {
                        this.doctorCheck.CheckItemList.splice(lfind,1);
                        this.doctorCheck.checkItem.itemId = ''
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `项目明细删除失败，原因：${err}` });
                });
        },

        //审核
        audit(){
            let ret = this.optGrant('audit')
            if(ret){
                alert(ret)
                return
            }
            let body ={
                registerCheckId: this.doctorCheck.RegisterCheckEdit.id,
                //auditorUserId: "3fa85f64-5717-4562-b3fc-2c963f66afa6", 
                //auditTime: "string" 不传时，取当前时间
            }

            console.log(`/api/app/registercheck/updateauditordoctor`,body)
            postapi(`/api/app/registercheck/updateauditordoctor`,body)
                .then((res) => {
                    console.log("audit", res.data);
                    if (res.code != -1) {
                        this.doctorCheck.RegisterCheckEdit.isAudit = 'Y';
                        this.$message({ type: "success", message: `组合项目审核成功` });
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `组合项目审核失败，原因：${err}` });
                });
        },

        //取消审核
        unAudit(){
            let ret = this.optGrant('unAudit')
            if(ret){
                alert(ret)
                return
            }            
            
            console.log(`/api/app/registercheck/updateisaudit?RegisterCheckId=${this.doctorCheck.RegisterCheckEdit.id}&IsAudit=N`)
            postapi(`/api/app/registercheck/updateisaudit?RegisterCheckId=${this.doctorCheck.RegisterCheckEdit.id}&IsAudit=N`)
                .then((res) => {
                    console.log("unAudit", res.data);
                    if (res.code != -1) {
                        this.doctorCheck.RegisterCheckEdit.isAudit = 'N';
                        this.$message({ type: "success", message: `组合项目取消审核成功` });
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `组合项目取消审核失败，原因：${err}` });
                });
        },

        //总检
        toSumDoctorCheck(){
            this.$router.push({ path: "/sumDoctorCheck" });
        },

        //更新检查项目与医生
        updateDoctorCheck() {
            let body = {
                registerCheckId: this.doctorCheck.RegisterCheckEdit.id,
                checkDoctorId: this.doctorCheck.RegisterCheckEdit.checkDoctorId,
                checkDate: this.doctorCheck.RegisterCheckEdit.checkDate
            }
            console.log(`/api/app/registercheck/updatecheckdoctor`, body)
            postapi(`/api/app/registercheck/updatecheckdoctor`, body)
                .then((res) => {
                    console.log("updateCheckItemList", res.data);
                    if (res.code != -1) {
                        this.doctorCheck.RegisterCheckEdit.completeFlag = '1';
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `项目明细保存失败，原因：${err}` });
                });

        },

        //更新明细
        updateCheckItemList() {
            let body = []
            for (let i = 0; i < this.doctorCheck.checkItemList.length; i++) {
                body.push({
                    registerCheckId: this.doctorCheck.checkItemList[i].registerCheckId,
                    itemId: this.doctorCheck.checkItemList[i].itemId,
                    result: this.doctorCheck.checkItemList[i].result,
                    checkDoctorName: this.doctorCheck.RegisterCheckEdit.checkDoctorId,
                    checkDate: this.doctorCheck.RegisterCheckEdit.checkDate,
                })
            }
            console.log(`/api/app/registercheckitem/updateregistercheckitemmany`, body)
            postapi(`/api/app/registercheckitem/updateregistercheckitemmany`, body)
                .then((res) => {
                    console.log("updateCheckItemList", res.data);
                    if (res.code != -1) {
                        //
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `项目明细保存失败，原因：${err}` });
                });
        },

        //更新小结        
        saveCheckSummary() {
            let body = []
            this.doctorCheck.checkSummaryList.forEach(item => {
                body.push({
                    registerCheckId: item.registerCheckId,
                    summary: item.summary,
                    summaryFlag: item.summaryFlag,
                })
            });

            console.log(`/api/app/registerchecksummary/createregisterchecksummarymany`, body)
            postapi(`/api/app/registerchecksummary/createregisterchecksummarymany`, body)
                .then((res) => {
                    console.log("saveCheckSummary", res.data);
                    if (res.code != -1) {
                        //this.doctorCheck.RegisterCheckEdit.completeFlag = '1';
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `项目明细保存失败，原因：${err}` });
                });

        },

        //更新建议      
        saveCheckSuggestion() {
            let body = []
            this.doctorCheck.checkSuggestionList.forEach(item => {
                body.push({
                    registerCheckId: item.registerCheckId,
                    suggestion: item.suggestion
                })
            });

            console.log(`/api/app/registerchecksuggestion/createregisterchecksuggestionmany`, body)
            postapi(`/api/app/registerchecksuggestion/createregisterchecksuggestionmany`, body)
                .then((res) => {
                    console.log("saveCheckSuggestion", res.data);
                    if (res.code != -1) {
                        //this.doctorCheck.RegisterCheckEdit.completeFlag = '1';
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `项目明细保存失败，原因：${err}` });
                });

        },

        addSummary(){
            if (!this.doctorCheck.RegisterCheckEdit.id) {
                alert("请选择检查项目")
                return
            }
            this.doctorCheck.checkSummaryList.push({
                registerCheckId: this.doctorCheck.RegisterCheckEdit.id,
                summary: '',
                summaryFlag: 'N',
            })
            this.doctorCheck.checkSuggestionList.push({
                registerCheckId: this.doctorCheck.RegisterCheckEdit.id,
                suggestion: '',                
            })
        },
    },

    //监听事件()
    watch: {
        //1级单位值改变，分组改变
        "patientRegister.query.CustomerOrgParentId"(newVal, oldVal) {
            console.log(
                "watch patientRegister.query.CustomerOrgParentId newVal：",
                newVal,
                " oldVal：",
                oldVal
            );
            if (newVal != oldVal && newVal !== this.dict.personOrgId) {
                this.getCustomerOrgGroup(newVal);
            }
        },
    },
};
</script>
<style scoped>
.listBtn {
    margin-top: 5px;
    text-align: center;
}
.btnClass{
  width:110px;
}
</style>