<template>
    <div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass" @click="prList">查询</el-button>
        </div>
        <div class="listBtn">
            <el-button type="primary" class="btnClass" @click="toDoctorCheck">预览报告</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>
</template>
<script>

import { mapState } from "vuex";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { arrayExistObj, parseID } from "../../utlis/proFunc";

//import PatientRegisterList from "../doctorCheck/PatientRegisterList.vue";
export default {
    components: {
        //PatientRegisterList,
    },
    data() {
        return {
            
           
        };
    },

    created() { },

    //挂载完成
    mounted() {

    },

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

    methods: {
        prList() {
            this.sumDoctorCheck.sumDoctorCheckDialogVisible = true
        },
        //to医生诊台
        toDoctorCheck() {
            this.$router.push({ path: "/doctorCheck" });
        },

        //操作判断
        optGrant(optType) {
            let ret = ''
            if (!this.sumDoctorCheck.sumPREdit.id) return '请选择体检人员'
            if (this.sumDoctorCheck.sumPREdit.isLock == 'Y') return '人员已锁定，不可执行此操作'

            if (optType == 'save') {
                //if (!this.sumDoctorCheck.sumPREdit.summaryDoctor) return "请选择总检医生"
                if (this.sumDoctorCheck.sumPREdit.completeFlag == '3') return "该人员已总检，不可再执行此操作，如需操作请点【修改】"
                if (this.sumDoctorCheck.sumPREdit.isAudit == 'Y') return '该人员检查已审核，请先取消审核，方可执行此操作！'
            }
            if (optType == 'edit') {
                if (this.sumDoctorCheck.sumPREdit.completeFlag != '3') return "该人员尚未总检，无需执行此操作"
                if (this.sumDoctorCheck.sumPREdit.isAudit == 'Y') return '该人员检查已审核，请先取消审核，方可执行此操作！'
            }
            if (optType == 'del') {
                if (this.sumDoctorCheck.sumPREdit.completeFlag != '3') return "该人员尚未总检，无需执行此操作"
                if (this.sumDoctorCheck.sumPREdit.isAudit == 'Y') return '该人员检查已审核，请先取消审核，方可执行此操作！'
            }
            if (optType == 'audit') {
                if (this.sumDoctorCheck.sumPREdit.completeFlag != '3') return "该检查项目尚未保存，不可执行此操作"
                if (this.sumDoctorCheck.sumPREdit.isAudit == 'Y') return "该人员检查已审核，无需再执行此操作"
            }
            if (optType == 'unAudit') {
                if (this.sumDoctorCheck.sumPREdit.completeFlag != '3') return "该人员尚未总检，不可执行此操作"
                if (this.sumDoctorCheck.sumPREdit.isAudit == 'N') return "该人员检查尚未审核，无需执行此操作"
            }
            if (optType == 'report') {
                if (this.sumDoctorCheck.sumPREdit.completeFlag != '3') return "该人员检查尚未总检，不可执行此操作"
                if (this.sumDoctorCheck.sumPREdit.isAudit == 'N') return "该人员检查尚未审核，不可执行此操作"
            }

            return ret
        },

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

            //更新总检
            this.updateSumDoctorCheck();

            //更新综述       
            this.saveSummary();

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

        //修改结果
        edit() {
            let ret = this.optGrant('edit')
            if (ret) {
                alert(ret)
                return
            }
            this.sumDoctorCheck.sumPREdit.completeFlag = '2'
        },

        //取消结果
        del() {
            let body = {
                patientRegisterId: this.sumDoctorCheck.sumPREdit.id,
                //summaryDoctor: this.sumDoctorCheck.sumPREdit.summaryDoctor,
                //summaryDate: this.sumDoctorCheck.sumPREdit.summaryDate,
                completeFlag: '2'
            }
            console.log(`/api/app/patientregister/updatepatientregistersummarydoctor`, body)
            postapi(`/api/app/patientregister/updatepatientregistersummarydoctor`, body)
                .then((res) => {
                    console.log("del", res.data);
                    if (res.code != -1) {
                        this.sumDoctorCheck.sumPREdit.completeFlag = '2';
                        console.log(`/api/app/sumsummaryheader/deletesumsummarymany?PatientRegisterId=${this.sumDoctorCheck.sumPREdit.id}`)
                        return postapi(`/api/app/sumsummaryheader/deletesumsummarymany?PatientRegisterId=${this.sumDoctorCheck.sumPREdit.id}`)
                    }
                })
                .then((res) => {
                    console.log("deletesumsuggestionmany", res.data);
                    if (res.code != -1) {
                        console.log(`/api/app/sumsuggestionheader/deletesumsuggestionmany?PatientRegisterId=${this.sumDoctorCheck.sumPREdit.id}`)
                        return postapi(`/api/app/sumsuggestionheader/deletesumsuggestionmany?PatientRegisterId=${this.sumDoctorCheck.sumPREdit.id}`)
                    }
                })
                .then((res) => {
                    console.log("deletesumsuggestionmany", res.data);
                    if (res.code != -1) {
                        this.$message({ type: "success", message: `取消总检成功！` });
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `取消总检失败，原因：${err}` });
                });
        },

        //审核
        audit() {
            let ret = this.optGrant('audit')
            if (ret) {
                alert(ret)
                return
            }
            let body = {
                patientRegisterId: this.sumDoctorCheck.sumPREdit.id,
                //auditDoctor: "3fa85f64-5717-4562-b3fc-2c963f66afa6", 
                //auditDate: "string" 不传时，取当前时间
                isAudit: 'Y'
            }

            console.log(`/api/app/patientregister/updatepatientregisterauditordoctor`, body)
            postapi(`/api/app/patientregister/updatepatientregisterauditordoctor`, body)
                .then((res) => {
                    console.log("audit", res.data);
                    if (res.code != -1) {
                        this.sumDoctorCheck.sumPREdit.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
            }

            let body = {
                patientRegisterId: this.sumDoctorCheck.sumPREdit.id,
                //auditDoctor: "3fa85f64-5717-4562-b3fc-2c963f66afa6", 
                //auditDate: "string" 不传时，取当前时间
                isAudit: 'N'
            }

            console.log(`/api/app/patientregister/updatepatientregisterauditordoctor`, body)
            postapi(`/api/app/patientregister/updatepatientregisterauditordoctor`, body)
                .then((res) => {
                    console.log("unAudit", res.data);
                    if (res.code != -1) {
                        this.sumDoctorCheck.sumPREdit.isAudit = 'N';
                        this.$message({ type: "success", message: `取消总检审核成功` });
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `取消总检审核失败，原因：${err}` });
                });
        },

        //体检报告
        report() {
            let ret = this.optGrant('report')
            if (ret) {
                alert(ret)
                return
            }

            ///3a0c990e-5756-2dc0-19d5-69a617fe4048
            let ReportCode = '0005';
            let token = localStorage.getItem('token');
            let user = localStorage.getItem('user');
            let toOutShell = {
                ReportCode, token,
                preViewCanPrint: this.sumDoctorCheck.sumPREdit.isAudit,
                Parameters: [
                    { Name: 'printer', Value: user },
                    { Name: 'hisLog', Value: 'pic/hisLog.jpg' },
                    { Name: "peisReportFirstPage", Value: "pic/peisReportFirstPage.jpg" },
                    { Name: "peisReportPageHeader", Value: "pic/peisReportPageHeader.jpg" },
                    { Name: "peisReportPageFooter", Value: "pic/peisReportPageFooter.jpg" }
                ],
            };


            postapi(`/api/app/printreport/getpeisreport?PatientRegisterId=${this.sumDoctorCheck.sumPREdit.id}`)
                .then((res) => {
                    if (res.code != -1) {
                        toOutShell.ReportTable = res.data;
                        console.log('JSON.stringify(toOutShell)', JSON.stringify(toOutShell));
                        return this.$peisAPI.printPre(JSON.stringify(toOutShell));
                    }
                })
                .catch(err => {
                    this.$message.warning(err);
                });

        },

        //增加建议
        addSuggtion() {
            let id = String(new Date().getTime())
            let pojo = {
                id,
                patientRegisterId: this.sumDoctorCheck.sumPREdit.id,
                details: [
                    {
                        sumSuggestionHeaderId: id,
                        suggestionContent: '',
                    }
                ]
            }
            this.sumDoctorCheck.suggestionList.push(pojo);
        },

        //复查
        reCheck() {
            console.log("parseID('123456198007240000')", parseID('123456198007240000'))
            let ret = this.optGrant('reCheck')
            if (ret) {
                alert(ret)
                return
            }

        },

        //干预措施
        intervene() {
            let ret = this.optGrant('intervene')
            if (ret) {
                alert(ret)
                return
            }
        },

        //更新总检
        updateSumDoctorCheck() {
            let body = {
                patientRegisterId: this.sumDoctorCheck.sumPREdit.id,
                //summaryDoctor:this.sumDoctorCheck.sumPREdit.summaryDoctor,
                //summaryDate: this.sumDoctorCheck.sumPREdit.summaryDate,
                completeFlag: '3'
            }
            console.log(`/api/app/patientregister/updatepatientregistersummarydoctor`, body)
            postapi(`/api/app/patientregister/updatepatientregistersummarydoctor`, body)
                .then((res) => {
                    console.log("updateSumDoctorCheck", res.data);
                    if (res.code != -1) {
                        this.sumDoctorCheck.sumPREdit.completeFlag = '3';
                        this.$message({ type: "success", message: `总检保存成功！` });
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `总检保存失败，原因：${err}` });
                });

        },


        //更新综述
        saveSummary() {
            let body = []
            this.sumDoctorCheck.summaryList.forEach(item => {
                delete item.id;
                delete item.summaryFlag;
                delete item.displayOrder;

                item.details.forEach(e => {
                    delete e.sumSummaryHeaderId;
                    delete e.displayOrder;
                })
                body.push(item)
            });
            console.log(`/api/app/sumsummaryheader/createsumsummary`, body)
            if (body.length < 1) return
            postapi(`/api/app/sumsummaryheader/createsumsummary`, body)
                .then((res) => {
                    console.log("saveSummary", res.data);
                    if (res.code != -1) {
                        //this.sumDoctorCheck.sumPREdit.completeFlag = '1';
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `综述保存失败，原因：${err}` });
                });

        },


        //更新建议      
        saveSuggestion() {
            let body = []
            this.sumDoctorCheck.suggestionList.forEach(item => {
                delete item.id;
                delete item.suggestionFlag;
                delete item.displayOrder;

                item.details.forEach(e => {
                    delete e.sumSuggestionHeaderId;
                    delete e.displayOrder;
                })
                body.push(item)
            });
            console.log(`/api/app/sumsuggestionheader/createsumsuggestion`, body)
            if (body.length < 1) return
            postapi(`/api/app/sumsuggestionheader/createsumsuggestion`, body)
                .then((res) => {
                    console.log("saveSuggestion", res.data);
                    if (res.code != -1) {
                        //this.sumDoctorCheck.sumPREdit.completeFlag = '1';
                    }
                })
                .catch((err) => {
                    this.$message({ type: "error", message: `建议保存失败，原因：${err}` });
                });

        },

        addSummary() {
            if (!this.sumDoctorCheck.sumPREdit.id) {
                alert("请选择检查项目")
                return
            }
            this.doctorCheck.checkSummaryList.push({
                registerCheckId: this.sumDoctorCheck.sumPREdit.id,
                summary: '',
                summaryFlag: 'N',
            })
            this.doctorCheck.checkSuggestionList.push({
                registerCheckId: this.sumDoctorCheck.sumPREdit.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: 10px;
    /*
    text-align: center;
    */
}

.btnClass {
    width: 110px;
}
</style>