<!-- QA列表 -->
<template>
    <div class="table-box">
        <div style="padding-bottom: 16px">
            <el-button type="primary" @click="handleAdd"> 添加 </el-button>
        </div>
        <ProTable
            ref="proTableRef"
            :formData="dataStore.formData"
            :columns="dataStore.columns"
            :request-api="getQAListApi"
            :init-param="dataStore.initParam"
        >
            <template #sort="scope">
                <el-input v-model="scope.row.sort" @blur="handleBlur(scope.row)" @input="handleInput(scope.row)"></el-input>
            </template>
            <template #image="scope">
                <el-image :src="scope.row.image ? h + scope.row.image : ''" style="width: 60px; height: 60px" />
            </template>

            <template #operation="scope">
                <el-button size="small" type="primary" @click="handleToDetails(scope.row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="getQADel(scope.row.id)">删除</el-button>
            </template>
        </ProTable>
    </div>
</template>

<script setup lang="ts" name="QAListIndex">
import ProTable from "@/components/ProTable/index.vue";
import { h } from "@/utils/url";
import { useMsg } from "@/hooks/useMsg";
import { integerRexg } from "@/utils/regexp/index";
import { messageBox } from "@/utils/messageBox";
//列表接口
import { getQAListApi, getQASortApi, getQAListDelApi } from "@/api/modules/QAList";
//深拷贝方法
import { cloneDeep } from "lodash-es";
//表格和搜索條件
import { RULE_FORM, FORM_DATA, COLUMNS } from "./constant/index";
// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTableRef = ref<any>(null);
const $router = useRouter();
// 数据源
const dataStore = reactive<any>({
    columns: COLUMNS, //列表配置项
    initParam: cloneDeep(RULE_FORM), // 初始化搜索条件|重置搜索条件
    ruleForm: cloneDeep(RULE_FORM), // 搜索參數
    formData: FORM_DATA //搜索配置项
});
//详情
const handleToDetails = async (id: any) => {
    $router.push({
        path: "/admin/QAManagement/list/edit",
        query: {
            id,
            title: "编辑问答"
        }
    });
};
//添加
const handleAdd = async () => {
    $router.push({
        path: "/admin/QAManagement/list/edit",
        query: {
            title: "添加问答"
        }
    });
};
//排序
const getQASort = async (row: any) => {
    const result = await getQASortApi({ id: row.id, sort: row.sort });
    if (result?.code === 0) {
        useMsg("success", result?.msg);
        proTableRef?.value?.getTableList();
    }
};
//删除
const getQADel = async (id: any) => {
    messageBox("你确定要删除?", async () => {
        const result = await getQAListDelApi(id);
        if (result?.code === 0) {
            const { msg } = result;
            useMsg("success", msg);
            proTableRef?.value?.getTableList();
        }
    });
};
//排序input框失焦
const handleBlur = (row: any) => {
    getQASort(row);
};
//排序input输入
const handleInput = (row: any) => {
    row.sort = integerRexg(row.sort);
};
</script>

<style scoped></style>
