<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="trainingName">
                                <el-input v-model="queryParams.trainingName" placeholder="搜会议名称" clearable style="width: 160px" @keyup.enter="handleQuery" />
                            </el-form-item>
                            <el-form-item label="培训方式" prop="joinType">
                                <el-select v-model="queryParams.joinType" placeholder="请选择培训方式" clearable style="width: 160px" @change="handleQuery">
                                    <el-option v-for="item in dm_training_join_type" :key="item.value" :label="item.label" :value="item.value" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="是否颁发证书" prop="certFlag">
                                <el-select v-model="queryParams.certFlag" placeholder="请选择是否颁发证书" clearable style="width: 160px" @change="handleQuery">
                                    <el-option v-for="item in yes_no" :key="item.value" :label="item.label" :value="item.value" />
                                </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: 'meeting-add' })">新增会议</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 field="trainingName" title="会议名称" :formatter="colNoData" />
                        <vxe-column field="trainingTime" min-width="200" title="培训时间">
                            <template #default="{ row }">{{ row.trainingStart  }}~{{ row.trainingEnd }}</template>
                        </vxe-column>
                        <vxe-column field="joinType" title="培训方式" width="80">
                            <template #default="{ row }">
                                {{ selectDictLabel(dm_training_join_type, row.joinType) }}
                            </template>
                        </vxe-column>
                        <!-- <vxe-column field="trainingLocation" title="培训地点" align="center" min-width="150" /> -->
                        <vxe-column field="certFlag" title="是否颁发证书" width="80">
                            <template #default="{ row }">
                                {{ selectDictLabel(yes_no, row.certFlag) }}
                            </template>
                        </vxe-column>
                        <vxe-column field="joinCount" title="报名人数" width="80" />
                        <vxe-column field="signCount" title="签到人数" width="80" />
                        <vxe-column field="certCount" title="领取证书人数" width="80" />
                        <vxe-column title="创建人" align="center" field="createByName" width="80" :formatter="colNoData" />
                        <vxe-column title="创建时间" align="center" field="createTime" min-width="100" :formatter="colNoData" />
                        <vxe-column field="certFlag" title="会议状态" width="80">
                            <template #default="{ row }">
                                <DictTag :options="dm_training_status" :value="row?.trainingStatus"></DictTag>
                            </template>
                        </vxe-column>
                        <vxe-column title="操作" width="300" fixed="right">
                            <template #default="{ row }">
                                <el-button v-if="['3'].includes(row?.trainingStatus)" type="primary" text @click="trainingOn(row)">上架</el-button>
                                <span></span>
                                <el-button v-if="!['3'].includes(row?.trainingStatus)" type="danger" text @click="trainingOff(row)">下架</el-button>
                                <span></span>
                                <el-button v-if="['3'].includes(row?.trainingStatus)" type="primary" @click="editRow(row)" text>编辑</el-button>
                                <span></span>
                                <el-button v-if="['1', '0'].includes(row?.trainingStatus)" @click="trainingSignIn(row)" style="color: #0079fe;" text>签到二维码</el-button>
                                <span></span>
                                <el-button @click="router.push({ path: 'meeting-detail', query: { id: row?.id } })" style="color: #0079fe;" text>详情</el-button>
                                <span></span>
                                <el-button text type="danger" @click="deleteRow(row)">删除</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>
    <SignInCode v-if="showSignIn" v-model:show="showSignIn" :info="rowInfo" :dict="{ dm_training_join_type }"></SignInCode>
</template>

<script setup name="meeting" lang="ts">
import { colNoData, colNoUnm } from '@/utils/noData';
import { searchTabs } from '@/views/models';
import { trainingList, queryTrainingCount, trainingDelete, publishTraining, unpublishTraining } from '@/api/training';
import { SignInCode } from '../models';

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { dm_training_join_type, yes_no, dm_training_cert, dm_training_status } = toRefs<any>(proxy?.useDict('dm_training_join_type', 'yes_no', 'dm_training_cert', 'dm_training_status'));
const loading = ref(true);
const showSearch = ref(true);
const showSignIn = ref(false);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const dataList = ref<any[]>([]);
const initFormData = {};
const rowInfo = ref<any>({});
const data = reactive<any>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10
    },
    rules: {}
});

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

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};
// 上架会议
const trainingOn = async (row: any) => {
    ElMessageBox.confirm(`确认要上架该会议吗？`, '上架提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async () => {
        const res = await publishTraining(row?.id);
        if (res) {
            ElMessage.success('上架成功');
            getList();
        }
    });
};
// 下架会议
const trainingOff = async (row: any) => {
    ElMessageBox.confirm(`确认要下架该会议吗？`, '下架提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async () => {
        const res = await unpublishTraining(row?.id);
        if (res) {
            ElMessage.success('下架成功');
            getList();
        }
    });
};
/** 编辑会议 */
const editRow = (row) => {
    router.push({ path: 'meeting-add', query: { id: row.id } });
};

/** 删除会议 */
const deleteRow = async (row: any) => {
    ElMessageBox.confirm(`确认要删除该个会议全部记录吗？`, '删除提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async () => {
        const res = await trainingDelete(row?.id);
        if (res) {
            ElMessage.success('删除成功');
            getList();
        }
    });
};
// 获取会议统计数量
const tabs = ref([]);
const getMeetingCount = async () => {
    const res = await queryTrainingCount();
    if (res?.code === 200) {
        tabs.value = res.data;
    }
};
// 打开签到二维码
const trainingSignIn = (row: any) => {
    rowInfo.value = { ...row };
    showSignIn.value = true;
};
onMounted(() => {
    getMeetingCount();
    getList();
});
</script>
