<!-- 规则列表 -- 表格 -->
<template>
    <div class="layout-pd">
        <el-row>
            <!--操作-->
            <el-col :xs="24">
                <el-card class="mt8" shadow="hover">
                    <el-form :model="Data.Filter" @submit.stop.prevent>
                        <el-form-item prop="name" style="width: 100%">
                            <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
                                <el-form-item label="模板名称">
                                    <el-input v-model="Data.time1" placeholder="请输入模板名称" clearable></el-input>
                                </el-form-item>
                            </el-col>
                        </el-form-item>
                    </el-form>

                    <hr>

                    <!-- 按钮 -->
                    <el-row justify="space-between" class="submit-button">
                        <el-row>
                            <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
                            <el-button type="primary" icon="ele-RefreshRight" @click="onReset"> 重置 </el-button>
                        </el-row>
                        <el-row>
                            <el-button type="primary" icon="ele-RefreshRight" @click="addTemplate"> 添加 </el-button>
                        </el-row>
                    </el-row>
                </el-card>
            </el-col>
            <!--表格-->
            <el-col :xs="24">
                <el-card style="height: 70vh" class="my-fill mt8" shadow="hover">
                    <el-table ref="multipleTableRef" v-loading="Data.loading" stripe :data="Data.tableModel"
                        row-key="id" style="width: 100%">
                        <el-table-column v-for="column in Data.dynamicColumns" :key="column.prop" :prop="column.prop"
                            :label="column.label" />
                    </el-table>
                    <div class="my-flex my-flex-end" style="margin-top: 20px">
                        <el-pagination v-model:currentPage="Data.pageInput.currentPage"
                            v-model:page-size="Data.pageInput.pageSize" :total="Data.total"
                            :page-sizes="[5, 10, 20, 50, 100]" small background @size-change="onSizeChange"
                            @current-change="onCurrentChange" layout="total, sizes, prev, pager, next, jumper" />
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <EditDialog ref="editDialogRef" />

    </div>

</template>

<script setup lang="ts" name="">
import { defineAsyncComponent, onMounted, reactive, ref, watch } from "vue";
import { pushTemplateModel_SearchFilter, pushTemplateModel, PageInputPushTemplateFilterModel } from "/@/api/admin/AlarmService/pushTemplateDto";
import { pushTemplateApi } from "/@/api/admin/AlarmService/pushTemplateApi";

/**页面对象 */
const Data = reactive({
    time1: '',
    /** 选择框列表 */
    statusList: [] as any,
    supplierList: [] as any,
    /**加载显示 */
    loading: false,
    /**条件查询模块 */
    Filter: {
        /** 模板名称 */
        templateName: ""
    } as pushTemplateModel_SearchFilter,
    /**表格信息 */
    tableModel: [] as pushTemplateModel,
    tableData: [] as any,
    /**动态表头 */
    dynamicColumns: [
        { prop: 'templateName', label: '模板名称' },
        { prop: 'templateType', label: '模板类型' },
        { prop: 'templateContent', label: '模板内容' },

    ],
    /**分页总数 */
    total: 0,
    /**分页标识 */
    pageInput: {
        currentPage: 1,
        pageSize: 10,
    } as PageInputPushTemplateFilterModel,
})

/**引入组件*/
const EditDialog = defineAsyncComponent(() => import('./components/pushTemplate.vue'))
const editDialogRef = ref()

onMounted(() => {
    init()
})

// 条件查询
const init = async () => {
    Data.loading = true
    const res: any = await new pushTemplateApi().getPage({ ...Data.pageInput, filter: Data.Filter })
    console.log(res)
    Data.tableModel = res?.data ?? []
    // Data.total = 100   // 接口中没有总的数据量
    Data.loading = false
    dataChange()
}

const onQuery = () => {
    init()
    console.log("查询")
}

/**重置查询条件 */
const resetQuery = () => {
    Data.Filter.templateName = ''
}
/**重置 */
const onReset = () => {
    resetQuery()
    init()
}

/** 
 * 添加模板
*/
const addTemplate = (() => {
    editDialogRef.value.openDialog()
    init()
})

/**
* 页条变化
* @param val
*/
const onSizeChange = (val: number) => {
    Data.pageInput.pageSize = val
    init()
}

/**
 * 页数 变化
 * @param val
 */
const onCurrentChange = (val: number) => {
    Data.pageInput.currentPage = val
    init()
}

/** 数据转换 */
const dataChange = (() => {
    // Data.tableData = []
    // // 推送方式
    // let index = 0
    Data.tableModel.forEach((item: any) => {
        console.log(item)


        if (item.templateType == 'wx') {
            item.templateType = "微信"
        } else if (item.templateType == 'email') {
            item.templateType = "电子邮箱"
        }

        // if(item.templateName != null){
        //     Data.tableData.push(item)
        // }

    })
})



</script>

<style scoped lang="scss">
@import '/@/theme/tableStyle.scss';
</style>