<template>
    <page-layer>
        <template #header>
            <el-select v-model="select" @change="query">
                <el-option v-for="item in selectList"
                           :key="item.code"
                           :label="item.name"
                           :value="item.code"/>
            </el-select>
            <el-button @click="querySelectClick" type="primary">查询</el-button>
        </template>

        <template #main="{size}">
            <xc-table :local-data="suggestionList"
                      :final-height="size.height - 50"
                      @row-click="rowClick"
            >
                <el-table-column prop="linkName" label="环节名称">
                    <template #default="{row}">
                        <el-input v-model="row.linkName" v-if="row.$edit" maxlength="20"/>
                    </template>
                </el-table-column>

                <el-table-column prop="name" label="名称">
                    <template #default="{row}">
                        <el-input v-model="row.name" v-if="row.$edit" maxlength="100"/>
                    </template>
                </el-table-column>

                <el-table-column prop="emrCode" label="病历编码">
                    <template #default="{row}">
                        <el-input v-model="row.emrCode" v-if="row.$edit" maxlength="100"/>
                    </template>
                </el-table-column>

                <el-table-column prop="scoringCriteriaName" label="评分">
                    <template #default="{row}">
                        <el-select v-model="row.scoringCriteria"
                                   v-if="row.$edit">
                            <el-option v-for="item in scoringCriteriaList"
                                       :key="item.code"
                                       :label="item.name"
                                       :value="item.code"/>
                        </el-select>
                        <span v-else>
                            {{ getScoringCriteriaName(row.scoringCriteria) }}
                        </span>
                    </template>
                </el-table-column>

                <el-table-column>
                    <template #header>
                        <el-button text type="primary" @click="newClick">新增</el-button>
                    </template>
                    <template #default="{row,$index}">
                        <el-button v-if="row.id" @click="reviseClick(row)" type="warning">修改</el-button>
                        <el-button v-else @click="addToClick(row)" type="primary">添加</el-button>
                        <el-button @click="delClick(row,$index)" type="danger">删除</el-button>
                    </template>
                </el-table-column>

            </xc-table>
        </template>

    </page-layer>
</template>

<script setup name='EmrSuggestion'>
import {addTo, deleteById, getLinkQuality, getLinkSelect, revise} from "@/api/emr/emr-suggestion";
import PageLayer from "@/layout/PageLayer.vue";
import XcTable from "@/components/xiao-chan/xc-table/XcTable.vue";
import {scoringCriteriaList} from "@/data";
import {stringIsBlank} from "@/utils/blank-utils";
import {BizException, ExceptionEnum} from "@/utils/BizException";

const selectList = ref({})

const select = ref('')
const suggestionList = ref([])

const query = () => {
    getLinkQuality(select.value).then(res => {
        suggestionList.value = res
    })
}

let tempData = {
    $edit: false
}
const rowClick = (val) => {
    tempData.$edit = false
    val.$edit = true
    tempData = val
}

const reviseClick = (val) => {
    verifyTheData(val)
    revise(val)
}

const newClick = () => {
    let temp = {
        id: null,
        name: '',
        emrCode: select.value,
        scoringCriteria: 1,
        linkName: suggestionList.value[0].linkName
    }
    suggestionList.value.push(temp)
    rowClick(temp)
}

const addToClick = (val) => {
    verifyTheData(val)
    addTo(val).then(() => {
        query()
    })
}

const delClick = (row, index) => {
    if (row.id) {
        deleteById(row).then(() => {
            query()
        })
    } else {
        suggestionList.value.splice(index, 1)
    }

}

const verifyTheData = (val) => {
    if (stringIsBlank(val.name)) {
        BizException(ExceptionEnum.MESSAGE_ERROR, '名称，不能为空');
    }
    if (stringIsBlank(val.emrCode)) {
        BizException(ExceptionEnum.MESSAGE_ERROR, '病历编码，不能为空');
    }
    if (stringIsBlank(val.linkName)) {
        BizException(ExceptionEnum.MESSAGE_ERROR, '病历编码，不能为空');
    }
}


function getScoringCriteriaName(val) {
    if (val == null) return "";
    switch (val) {
        case 1:
            return "轻";
        case 2:
            return "中";
        case 3:
            return "重";
        default:
            return "";
    }
}

const querySelectClick = () => {
    getLinkSelect().then(res => {
        selectList.value = res
    })
}


onMounted(() => {
    querySelectClick()
})

</script>

<style scoped lang="scss">

</style>
