<!-- 医生列表 -->
<template>
    <div class="doctor-list">
        <el-card class="!border-none" shadow="never">
            <el-form class="mb-[-16px]" :model="formData" inline>
                <el-form-item class="w-[280px]" label="医生账号">
                    <el-input
                        v-model="formData.account"
                        placeholder="请输入医生账号"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item class="w-[280px]" label="医生名称">
                    <el-input
                        v-model="formData.name"
                        placeholder="请输入医生名称"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        
        <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
            <el-button v-perms="['auth.admin/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增医生
            </el-button>
            
            <div class="mt-4">
                <el-table :data="pager.lists" size="large">
                    <el-table-column label="ID" prop="id" min-width="60" />
                    <el-table-column label="头像" min-width="100">
                        <template #default="{ row }">
                            <el-avatar :size="50" :src="row.avatar"></el-avatar>
                        </template>
                    </el-table-column>
                    <el-table-column label="账号" prop="account" min-width="120" />
                    <el-table-column label="姓名" prop="name" min-width="100" />
                    <el-table-column label="手机号" prop="phone" min-width="120" />
                    <el-table-column label="创建时间" prop="create_time" min-width="180" />
                    <el-table-column label="最近登录时间" prop="login_time" min-width="180" />
                    <el-table-column label="最近登录IP" prop="login_ip" min-width="120" />
                    <el-table-column label="状态" min-width="100" v-perms="['auth.admin/edit']">
                        <template #default="{ row }">
                            <el-switch
                                v-model="row.disable"
                                :active-value="0"
                                :inactive-value="1"
                                @change="changeStatus(row)"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['auth.admin/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['auth.admin/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="doctorList">
import { adminDelete, adminEdit, adminLists } from '@/api/perms/admin'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'

import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()

// 表单数据 - 固定 role_id=1 查询医生；数据范围与医助列表一致，仅看自己权限内成员
const formData = reactive({
    account: '',
    name: '',
    role_id: 1,
    apply_data_scope: 1
})

const showEdit = ref(false)

const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: adminLists,
    params: formData
})

// 修改状态
const changeStatus = (data: any) => {
    adminEdit({
        id: data.id,
        avatar: data.avatar,
        account: data.account,
        name: data.name,
        role_id: data.role_id,
        jobs_id: data.jobs_id,
        dept_id: data.dept_id,
        disable: data.disable,
        multipoint_login: data.multipoint_login
    }).finally(() => {
        getLists()
    })
}

// 新增医生
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑医生
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除医生
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除该医生吗？')
    await adminDelete({ id })
    getLists()
}

onMounted(() => {
    getLists()
})
</script>

<style scoped lang="scss">
.doctor-list {
    padding: 20px;
}
</style>
