<template>
    <headerTop @search="searchFn" @reset="resetFn" @export="exportFn" @add="addFn"></headerTop>
    <addCom ref="addComRef"></addCom>
    <el-card shadow="always" class="content_body_row">
        <el-table :data="tableListInfo.list" border style="width: 100%" show-overflow-tooltip
            :max-height="'calc(100vh - 72px - 48px - 65px - 12px - 60px - 48px)'"
            :height="'calc(100vh - 72px - 48px - 65px - 12px - 60px  - 48px)'">

            <el-table-column type="index" width="72" label="序号" />
            <el-table-column prop="topicName" label="标题" min-width="100" />
            <el-table-column prop="v_category" label="分类" min-width="120" />
            <el-table-column prop="creatBy" label="发布人" min-width="120" />
            <el-table-column prop="viewNum" label="查看人数" min-width="220" />
            <el-table-column label="操作" width="210" align="center">

                <template #default="scope">
                    <el-button type="primary" size="small" @click.prevent="showRowFn(scope.row)">
                        查看
                    </el-button>
                    <el-button type="primary" size="small" @click.prevent="editRowFn(scope.row)">
                        编辑
                    </el-button>
                    <el-button type="danger" size="small" @click.prevent="deleteRowFn(scope.row)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="paination_out">
            <Pagination :currentPage="paginationInfo.pageNum" :pageSize="paginationInfo.pageSize" @sendPage="getPageFn"></Pagination>
        </div>
    </el-card>
</template>

<script lang="ts" setup>
import headerTop from "./com/headerTop.vue"
import addCom from "./com/addCom.vue"
import { ElConfirmBeforeOpert } from "utils/elementCom"
import { apiBmTopicInfoList,apiBmTopicInfoExport,apiDeleteBmTopicInfoById ,} from "http/api/config"
import {exportBlob} from "utils/index"
import moment from "moment";
import {ElMessage} from "element-plus"
 

const addComRef = ref()
const paginationInfo = reactive({
    pageNum: 1,
    pageSize: 15
})
let total = ref(0)

let tableListInfo = reactive({
    list: []
})

const showRowFn = (row: any) => {
    addComRef.value.show(row.id)
    
}


const editRowFn = (row: any) => {
    addComRef.value.edit(row.id)

}

const deleteItem = async (row: any) => {
    const itemID = row.id
    const index = tableListInfo.list.findIndex((ele: any) => ele.id == itemID)
    const res = await apiDeleteBmTopicInfoById(itemID)
    let tipMessage = "删除成功"

    tableListInfo.list.splice(index, 1)

    ElMessage({
        type: 'success',
        message: tipMessage
    })

}
const deleteRowFn = (row: any) => {
    ElConfirmBeforeOpert(
        '操作确认',
        '是否确定删除数据？',
        deleteItem,
        '确定',
        '取消',
        row
    )

}
let searchParams: any = {
    carName: ''
}
const searchFn = (params: any) => {
    searchParams = params
    console.log("searchParams", params)
    searchParams.beginTime = moment(params.dataRange[0]).format('yyyy-MM-DD')
    searchParams.endTime = moment(params.dataRange[1]).format('yyyy-MM-DD')
    initTableList()
}

const resetFn = (params: any) => {
    searchParams = params
    console.log("searchParams", params)
    searchParams.beginTime = moment(params.dataRange[0]).format('yyyy-MM-DD')
    searchParams.endTime = moment(params.dataRange[1]).format('yyyy-MM-DD')
    initTableList()
}


const exportFn = async() => {
    const res = await apiBmTopicInfoExport({})
    exportBlob(res)

}

const addFn = () => {
    addComRef.value.open({
        a: "111"
    })
}

const getPageFn = (val: any) => {
    paginationInfo.pageNum = val.pageNum
    paginationInfo.pageSize = val.pageSize
    initTableList()
}

const initTableList = async () => {
    const params = {
        ...searchParams,
        ...paginationInfo
    }
    const res: any = await apiBmTopicInfoList(params)
    console.log("apiBmTopicInfoList", res)
    tableListInfo.list = res.rows.map((ele: any) => {
        return {
            ...ele,
            status: ele.status == 1 ? true : false
        }
    })
    // tableListInfo.list = 
    total.value = res.total
}
onMounted(() => {
    initTableList()
})

</script>

<style scoped lang="scss"></style>
