<template>
    <el-button @click="setAuditClick" v-if="permissions()">发送</el-button>
    <emr-audit-dialog :emr-id="emrInfo.id"/>
    <el-table :data="data"
              :height="getWindowSize.h / 1.1"
              :expand-row-keys="expandRow"
              @row-contextmenu="rowContextmenu"
              ref="elTableRef"
              row-key="rowId">
        <el-table-column type="expand">
            <template #header>
                <el-button text @click="addAudit">添加</el-button>
            </template>
            <template #default="{row}">
                <div style="width: 100%;padding: 10px">
                    <table style="width: 100%">
                        <tr v-if="!permissions()">
                            <td>
                                <el-button type="success" @click="rectification(row,1)">已整改</el-button>
                            </td>
                            <td>
                                <el-button type="warning" @click="rectification(row,2)">已知无需整改</el-button>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                审核人:{{ row.approverName }}
                            </td>
                            <td>
                                审核时间:{{ row.reviewTime }}
                            </td>
                        </tr>
                        <tr>
                            <td>
                                修改人:{{ row.modifyPersonName }}
                            </td>
                            <td>
                                修改时间:{{ row.modificationTime }}
                            </td>
                        </tr>
                        <tr>
                            <td>
                                状态： {{ row.remediationStatusName }}
                            </td>
                        </tr>
                    </table>
                </div>
            </template>
        </el-table-column>
        <el-table-column prop="name" label="项目" width="180">
            <template #default="{row}">
                <el-input v-model="row.name" :maxlength="100" v-if="row.disposition"/>
            </template>
        </el-table-column>
        <el-table-column prop="scoringCriteriaName" label="等级" width="40">
            <template #default="{row}">
                <select v-model="row.scoringCriteria" v-if="row.disposition">
                    <option v-for="item in scoringCriteriaList"
                            :key="item.code"
                            :value="item.code">
                        {{ item.name }}
                    </option>
                </select>
            </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注">
            <template #default="{row}" v-if="permissions()">
                <el-input v-model="row.remark" :maxlength="100"></el-input>
            </template>
        </el-table-column>
        <el-table-column prop="numberOfDefects" label="缺陷数">
            <template #default="{row}" v-if="permissions()">
                <el-input-number style="width: 40px"
                                 v-model="row.numberOfDefects"
                                 :min="1"
                                 :controls="false"/>
            </template>
        </el-table-column>
        <el-table-column type="selection" v-if="permissions()"/>
    </el-table>
    <right-click-menu v-if="!permissions()" :config="opt" :mouse-position="mousePosition"/>
</template>

<script setup name='EmrAudit'>
import {emrMitt} from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/emr-init";
import {getAuditMessages, rectifyMedicalRecords, setAudit} from "@/api/zhu-yuan-yi-sheng/emr-audit";
import {needRule} from "@/utils/public";
import {getWindowSize} from "@/utils/window-size";
import RightClickMenu from "@/components/menu-item/RightClickMenu.vue";
import {ref} from "vue";
import {stringIsBlank, stringNotBlank} from "@/utils/blank-utils";
import {xcMessage} from "@/utils/xiaochan-element-plus";
import EmrAuditDialog
    from "@/views/hospitalization/zhu-yuan-yi-sheng/electronic-medical-record/emr-editor/components/EmrAuditDialog.vue";
import {BizException, ExceptionEnum} from "@/utils/BizException";
import {uuid} from "@/utils/getUuid";
import {scoringCriteriaList} from "@/data";

const elTableRef = ref(null)

const emrInfo = ref({
    id: 0,
    code: "",
    createId: '',
    referPhysician: "",
    patNo: '',
    times: 0
})

const data = ref([])
const expandRow = ref([])

const permissions = () => {
    return needRule(56);
}

const setAuditClick = () => {
    let temp = {
        id: emrInfo.value.id,
        doctor: emrInfo.value.code === 'shoucibingchengjilu' ? emrInfo.value.referPhysician : emrInfo.value.createId,
        patNo: emrInfo.value.patNo,
        times: emrInfo.value.times,
        list: elTableRef.value.getSelectionRows()
    }
    setAudit(temp).then(() => {
        queryAudit(emrInfo.value)
    })
}

const mousePosition = ref()
const opt = [
    {
        name: '已整改',
        click: (data) => {
            rectification(data, 1)
        },
        validator: (data) => {
            return !!data.approver;
        }
    },
    {
        name: '已知无需整改',
        click: (data) => {
            rectification(data, 2)
        },
        validator: (data) => {
            return !!data.approver;
        }
    }, {
        name: '删除',
        click: (data) => {

        },
        validator: (data) => {
            return !!data.disposition;
        }
    }
]

const rectification = (data, flag) => {
    if (!data.approver) {
        return xcMessage.error('操作失败')
    } else {
        rectifyMedicalRecords(data.id, flag).then(() => {
            queryAudit(emrInfo.value)
        })
    }
}


const rowContextmenu = (row, column, event) => {
    event.preventDefault()
    mousePosition.value = {
        event,
        data: row,
        index: 0
    }
}

const queryAudit = (val) => {
    emrInfo.value = val
    getAuditMessages(emrInfo.value.id, emrInfo.value.code).then(async res => {
        expandRow.value = []
        data.value = res
        elTableRef.value.clearSelection()
        await nextTick()
        let tempList = []
        console.log(res)
        for (let i = 0, len = data.value.length; i < len; i++) {
            let item = data.value[i]
            item.rowId = i;
            if (stringNotBlank(item.approver)) {
                expandRow.value.push(i);
                elTableRef.value.toggleRowSelection(item);
                tempList.push(item);
            }
        }

        if (tempList.length > 0) {
            emrMitt.emit('setShowIframe', 5)
        }

        if (!permissions()) {
            data.value = tempList;
        }

    })

}

const addAudit = () => {
    let editor = emrMitt.emit('editor')
    if (stringIsBlank(editor?.documentData._id)) {
        BizException(ExceptionEnum.MESSAGE_ERROR, "请先选择病历。");
    }
    let tempData = {
        rowId: uuid(8, 10),
        name: '',
        scoringCriteria: 1,
        scoringCriteriaName: '轻',
        numberOfDefects: 1,
        remark: '',
        disposition: true,
    }
    data.value.push(tempData)
    elTableRef.value.toggleRowSelection(tempData);
}


onMounted(() => {
    emrMitt.on('audit', queryAudit)
})
</script>

<style scoped lang="scss">

</style>
