<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="疾病大类" prop="pid">
                <el-tree-select v-model="queryParams.pid" :data="diseaseOptions" style="width: 180px;"
                :props="{ value: 'id', label: 'name', children: 'children' }" value-key="id" placeholder="请选择"
                check-strictly />
            </el-form-item>
          
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['disease:disease:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="info" plain icon="Sort" @click="toggleExpandAll">展开/折叠</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-if="refreshTable" v-loading="loading" :data="diseaseList" row-key="id"
            :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <el-table-column label="名称" prop="name" />
            <el-table-column label="描述" align="center" prop="desc" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['disease:disease:edit']">修改</el-button>
                    <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)"
                        v-hasPermi="['disease:disease:add']">新增</el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['disease:disease:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加或修改疾病对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="diseaseRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="描述" prop="desc">
                    <el-input v-model="form.desc" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="" prop="pid">
                    <el-tree-select v-model="form.pid" :data="diseaseOptions"
                        :props="{ value: 'id', label: 'name', children: 'children' }" value-key="id" placeholder="请选择"
                        check-strictly />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Disease">
import { listDisease, getDisease, delDisease, addDisease, updateDisease } from "@/api/biosafety/disease"

const { proxy } = getCurrentInstance()

const diseaseList = ref([])
const diseaseOptions = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const title = ref("")
const isExpandAll = ref(true)
const refreshTable = ref(true)


const data = reactive({
    form: {},
    queryParams: {
        name: null,
        desc: null,
        pid: null
    },
    rules: {
    }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询疾病列表 */
function getList() {
    loading.value = true
    listDisease(queryParams.value).then(response => {

        diseaseList.value = proxy.handleTree(response.data, "id", "pid")
        loading.value = false
    })
}

/** 查询疾病下拉树结构 */
function getTreeselect() {
    listDisease().then(response => {
        diseaseOptions.value = response.data.filter(item => item.pid === 0) 
    })
}

// 取消按钮
function cancel() {
    open.value = false
    reset()
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        name: null,
        desc: null,
        pid: null
    }
    proxy.resetForm("diseaseRef")
}

/** 搜索按钮操作 */
function handleQuery() {
    getList()
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef")
    handleQuery()
}

/** 新增按钮操作 */
function handleAdd(row) {
    reset()
    getTreeselect()
    if (row != null && row.id) {
        form.value.pid = row.id
    } else {
        form.value.pid = 0
    }
    open.value = true
    title.value = "添加疾病"
}

/** 展开/折叠操作 */
function toggleExpandAll() {
    refreshTable.value = false
    isExpandAll.value = !isExpandAll.value
    nextTick(() => {
        refreshTable.value = true
    })
}

/** 修改按钮操作 */
async function handleUpdate(row) {
    reset()
    await getTreeselect()
    if (row != null) {
        form.value.pid = row.pid
    }
    getDisease(row.id).then(response => {
        form.value = response.data
        open.value = true
        title.value = "修改疾病"
    })
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["diseaseRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateDisease(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功")
                    open.value = false
                    getList()
                })
            } else {
                addDisease(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功")
                    open.value = false
                    getList()
                })
            }
        }
    })
}

/** 删除按钮操作 */
function handleDelete(row) {
    proxy.$modal.confirm('是否确认删除疾病' + row.name + '的数据项？').then(function () {
        return delDisease(row.id)
    }).then(() => {
        getList()
        proxy.$modal.msgSuccess("删除成功")
    }).catch(() => { })
}
getTreeselect()
getList()
</script>
  
