<template>
    <div class="p-3">
        <div class="bg-fff flex1 ov-hd d-flex flex-cln" v-show="showSearch">
            <div class="pd-16 border-bottom">
                <div class="f-s-20 c-333 f-w-7 mb-10">云药专家</div>
                <div class="d-flex">
                    <div class="flex1 ov-hd d-flex j-ed">
                        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="auto">
                            <el-form-item label="姓名" prop="name">
                                <el-input v-model="queryParams.name" placeholder="搜姓名" clearable style="width: 160px" @keyup.enter="handleQuery" />
                            </el-form-item>
                            <el-form-item label="单位" prop="workUnit">
                                <el-input v-model="queryParams.workUnit" placeholder="搜单位关键字" clearable style="width: 160px" @keyup.enter="handleQuery" />
                            </el-form-item>
                            <el-form-item label="专家组" prop="queryPersonType">
                                <el-select style="width: 160px" v-model="queryParams.queryPersonType" clearable placeholder="请选择专家组">
                                    <el-option v-for="item in dm_person_type" :key="item.value" :label="item.label" :value="item.value" @change="handleQuery" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="专业品种" prop="variety">
                                <el-input v-model="queryParams.variety" placeholder="搜专业品种" clearable style="width: 160px" @keyup.enter="handleQuery" />
                            </el-form-item>
                            <el-form-item label="前端显示" prop="putawayFlag">
                                <el-select style="width: 140px" v-model="queryParams.putawayFlag" clearable placeholder="请选择状态">
                                    <el-option label="显示" value="1" />
                                    <el-option label="不显示" value="0" />
                                </el-select>
                            </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>
                    </div>
                </div>
            </div>

            <div class="d-flex flex1 ov-hd flex-cln pd-16">
                <div class="d-flex j-sb mb-16">
                    <div>
                        <searchTabs v-model="queryParams.status" @change="handleQuery" :list="tabs" key-label="name" key-count="num" key-value="type"></searchTabs>
                    </div>
                    <el-button type="primary" @click="router.push({ path: 'authority-input' })">新增专家信息</el-button>
                </div>
                <div class="flex1 ov-hd">
                    <vxe-table :loading="loading" border :data="dataList" min-height="0" max-height="100%">
                        <vxe-column title="序号" align="center" type="seq" width="60" />
                        <vxe-column title="姓名" align="center" field="name" :formatter="colNoData" />
                        <vxe-column title="性别" width="70" align="center">
                            <template #default="{ row }">
                                {{ selectDictLabel(sys_sex_type, row?.sex) }}
                            </template>
                        </vxe-column>
                        <vxe-column title="拟加入专家组" min-width="160">
                            <template #default="{ row }">
                                <DictTag :options="dm_person_type" :value="row?.personType"></DictTag>
                            </template>
                        </vxe-column>
                        <vxe-column title="单位" field="workUnit" :formatter="colNoData" />
                        <vxe-column title="职称/职务" field="post" :formatter="colNoData" />
                        <vxe-column title="专业品种" field="variety" :formatter="colNoData" />
                        <vxe-column title="岗位状态" width="90">
                            <template #default="{ row }">
                                <DictTag :options="dm_position_status" :value="row?.status"></DictTag>
                            </template>
                        </vxe-column>
                        <vxe-column title="被抽取次数" width="100" field="chooseCount" :formatter="colNoData" />
                        <vxe-column title="前端显示" width="90">
                            <template #default="{ row }">
                                <el-switch v-model="row.putawayFlag" active-value="1" inactive-value="0" @change="changeRowPutaway($event, row)"></el-switch>
                            </template>
                        </vxe-column>
                        <vxe-column title="操作" width="210" align="center" fixed="right">
                            <template #default="{ row }">
                                <el-button type="primary" @click="editRow(row)" text style="width: 40px">编辑</el-button>
                                <el-button type="primary" @click="gotodetailsPage(row)" text style="width: 40px">详情</el-button>
                                <el-button text type="danger" @click="deleteRow(row)" style="width: 40px">删除</el-button>
                            </template>
                        </vxe-column>
                    </vxe-table>
                </div>
                <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
            </div>
        </div>
    </div>
</template>

<script setup name="authority-info" lang="ts">
import { colNoData } from '@/utils/noData';
import { DateRange } from '@/views/models/index';
import { searchTabs } from '@/views/models';
import { expertList, expertPersonCount, expertPersonDelete, expertPersonOff, expertPersonOn } from '@/api/authority';
const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_sex_type, dm_educational_type, dm_person_type, dm_position_status } = toRefs<any>(proxy?.useDict('sys_sex_type', 'dm_educational_type', 'dm_person_type', 'dm_position_status'));
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const dataList = ref<any[]>([]);
const initFormData = {};
const data = reactive<any>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: ''
    },
    rules: {}
});

const { queryParams, form } = toRefs(data);
/** 查询会员信息列表 */
const getList = async () => {
    loading.value = true;
    const res = await expertList(queryParams.value);
    dataList.value = res.rows;
    total.value = res.total;
    loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};
// /** 切换前端显示状态 */
const changeRowPutaway = async (value, row) => {
    try {
        if (+value) {
            await expertPersonOn(row.id);
            ElMessage.success('上架成功');
        } else {
            await expertPersonOff(row.id);
            ElMessage.success('下架成功');
        }
        getList();
    } catch (error) {
        ElMessage.error('操作失败');
    }
};
/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};

/** 编辑专家 */
const editRow = (row) => {
    router.push({ path: 'authority-input', query: { id: row.id } });
};
/** 详情页面 */
const gotodetailsPage =(row)=>{
    router.push({ path: 'exportdetail', query: { id: row.id } });
}
/** 删除专家 */
const deleteRow = async (row) => {
    ElMessageBox.confirm(`确认要删除专家为"${row.name}"的数据吗？`, '删除提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async () => {
        const res = await expertPersonDelete(row.id);
        if (res) {
            ElMessage.success('删除成功');
            getList();
        }
    });
};
// 获取专家统计数量
const tabs = ref([]);
const getExpertPersonCount = async () => {
    const res = await expertPersonCount();
    if (res?.code === 200) {
        tabs.value = res.data
    }
};
onMounted(() => {
    getExpertPersonCount();
    getList();
});
</script>
