<template>
    <div>
        <div v-show="checkPagePriv(pagePriv.privs, '人员列表')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="prList">人员列表</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '医生诊台')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="toDoctorCheck">医生诊台</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '保存')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="btnSave"
            :disabled="sumBtnDisabled('save')">保存</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '修改')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="btnEdit">修改</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '取消总检')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="btnDel">取消总检</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '审核')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="audit">审核</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '取消审核')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="unAudit">取消审核</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '新增建议')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="addSuggtion">新增建议</el-button>
        </div>
        <!-- 直接放在 建议旁边
        <div class="listBtn">
            <el-button type="primary" class="btnClass" @click="addDiagnosis">疾病诊断</el-button>
        </div>
        -->
        <div v-show="checkPagePriv(pagePriv.privs, '体检报告')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="report">体检报告</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '复查')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="reCheck">复查</el-button>
        </div>
        <div v-show="checkPagePriv(pagePriv.privs, '干预措施')" class="listBtn">
            <el-button type="primary" class="commonbutton" @click="intervene">干预措施</el-button>
        </div>


        <div v-show="sumDoctorCheck.sumDoctorCheckDialogVisible"
            :style="`z-index:3;border-radius: 10px;border: 1px solid #000;background-color:#FFF; position: absolute;top:70px;right:10px;width:300px;height: ${window.pageHeight - 42 - 5 - 30}px;opacity:1;`">

            <PatientRegisterList win="sumDoctorCheck" :winAbsolute="true" />

        </div>
        <!-- 抽屉方式，遮罩层不能操作 
        <el-drawer title="体检人员列表" :visible.sync="sumDoctorCheck.sumDoctorCheckDialogVisible" direction="rtl" 
          :wrapperClosable="false" :modal="false" :size="300">
          <PatientRegisterList :win="sumDoctorCheck" :winAbsolute="false"/>
        </el-drawer>
        -->
        <!-- 直接放在 建议旁边
        <el-drawer title="诊断列表" :visible.sync="sumDoctorCheck.sumDiagnosisVisible" direction="ltr"  :size="300"
          :wrapperClosable="false">
          <SumDiagnosis :patientRegisterId="sumDoctorCheck.sumPREdit.id" />
        </el-drawer>
        -->
    </div>
</template>
<script>

import { mapState } from "vuex";
import { getapi, postapi, putapi, deletapi } from "@/api/api";
import { getPagePriv, checkPagePriv, deepCopy, sumBtnDisabled } from "../../utlis/proFunc";

import PatientRegisterList from "../doctorCheck/PatientRegisterList.vue";
import SumDiagnosis from "./SumDiagnosis.vue";

export default {
    components: {
        PatientRegisterList,
        SumDiagnosis,
    },
    props: ["fnSetSuggestionFocus"],
    data() {
        return {
            pagePriv: {
                routeUrlorPageName: 'sumDoctorCheck', //当前页面归属路由或归属页面权限名称
                privs: [] // 页面权限
            },
            dialogVisible: false,
            tableData: [
                {
                    col: 'col',
                    details: [{ id: 1, name: '000' }, { id: 2, name: '001' }]
                }
            ],
        };
    },

    created() {
        //获取用户当前页面的权限
        let userPriv = window.sessionStorage.getItem('userPriv')
        if (userPriv) this.pagePriv.privs = deepCopy(getPagePriv(this.pagePriv.routeUrlorPageName))
    },

    //挂载完成
    mounted() {

    },

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

    methods: {

        checkPagePriv,

        prList() {
            this.sumDoctorCheck.sumDoctorCheckDialogVisible = true
        },

        //诊断列表
        addDiagnosis() {
            this.sumDoctorCheck.sumDiagnosisVisible = true
        },

        //to医生诊台
        toDoctorCheck() {
            // this.doctorCheck.prBase = Object.assign({},this.sumDoctorCheck.sumPREdit)
            // this.patientRegister.photo = this.doctorCheck.prBase.photo
            this.$router.push({ path: "/doctorCheck" });
        },

        // 按钮可用
        sumBtnDisabled(btnFlagName) {
            sumBtnDisabled(btnFlagName, this.dataTransOpts.tableS.patient_register)
        },

        //保存数据
        btnSave() {

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

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

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

        //修改结果
        btnEdit() {

            this.sumDoctorCheck.sumPREdit.completeFlag = '2'
        },

        //取消结果
        btnDel() {

            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 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 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() {

            if (!this.$peisAPI) {
                this.$message.info("此功能，需要在壳客户端才可运行！")
                return
            }
            ///3a0c990e-5756-2dc0-19d5-69a617fe4048
            let ReportCode = '0005';
            let token = window.sessionStorage.getItem('token');
            let user = window.sessionStorage.getItem('user');
            let toOutShell = {
                ReportCode, token,
                preViewCanPrint: this.sumDoctorCheck.sumPREdit.isAudit,
                Parameters: [
                    { Name: 'printer', Value: user },
                    { Name: 'LTS', Value: 'Y' }, //Y、N   N只看不能打印
                    { 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,
                diagnosisId: null,
                details: [
                    {
                        sumSuggestionHeaderId: id,
                        suggestionContent: '',
                    }
                ]
            }
            this.sumDoctorCheck.suggestionList.push(pojo);

            this.fnSetSuggestionFocus(id)
        },

        //复查
        reCheck() {
            console.log("复查")
        },

        //干预措施
        intervene() {
            console.log("干预措施")
        },

        //更新总检
        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: {

    },
};
</script>
<style scoped>
@import '../../assets/css/global_button.css';

.listBtn {
    margin-top: 10px;
    margin-left: 10px;
    text-align: center;
}
</style>