<template>
    <div class="app-container">
        <el-card>
            <template #header>
                <div class="card-header">
                    <span>工作台</span> <span class="subHeaderTitle">- 系统通知</span>
                </div>
            </template>
            <el-row :gutter="10" class="my_row">
                <el-col :span="12">
                    <el-form :model="queryParams" ref="queryRef" :inline="true" class="searchInputForm">
                        <el-form-item label="">
                            <el-input v-model="queryParams.noticeTitle" placeholder="请输入供应商/媒体名称" :prefix-icon="Search"
                                style="width: 400px;" />
                        </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="12" style="text-align: right;">
                    <el-button type="primary" class="primaryBtn" @click="handleQuery">查询</el-button>
                    <el-button type="primary" class="primaryBtn" @click="resetQuery">重置</el-button>
                    <el-button type="primary" class="primaryBtn" @click="handleExport">导出</el-button>
                </el-col>
            </el-row>

            <el-table v-loading="loading" :data="noticeListData" height="calc(100vh - 306px)">
                <el-table-column label="序号" align="center" width="80">
                    <template #default="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column label="通知类型" align="center" prop="businessType" min-width="120">
                    <template #default="scope">
                        <span v-if="scope.row.businessType == 1">供应商</span>
                        <span v-if="scope.row.businessType == 2">媒体库</span>
                    </template>
                </el-table-column>
                <el-table-column label="通知内容" align="left" prop="noticeTitle" min-width="300" />
                <el-table-column label="通知时间" align="center" prop="createTime" min-width="210">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="130" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button link type="primary" @click="handleUpdate(scope.row)">去处理</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                @pagination="getNoticePageList" />

        </el-card>

    </div>
</template>

<script setup name="Post">
import { onMounted, ref } from 'vue';
import { Search } from '@element-plus/icons-vue'
import { noticeList, exportNotice } from "@/api/notice"
import { useRouter } from 'vue-router'
import { useBackgroundStore } from '@/store/modules/background'
import otherbg from '@/assets/images/otherbg.png'
const bgStore = useBackgroundStore()

const router = useRouter()
const { proxy } = getCurrentInstance()

// 列表数据
const noticeListData = ref([])
const loading = ref(true)
const total = ref(0)

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeType: 1,
        status: 0,
        noticeTitle: undefined,
    }
})

const { queryParams } = toRefs(data)

/** 查询通知列表 */
const getNoticePageList = () => {
    loading.value = false
    noticeList(queryParams.value).then(res => {
        noticeListData.value = res.rows
        total.value = res.total
        loading.value = false
    })
}

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1
    getNoticePageList()
}

/** 重置按钮操作 */
function resetQuery() {
    queryParams.value = {
        pageNum: 1,
        pageSize: 10,
        noticeType: 1,
        status: 0,
        noticeTitle: undefined,
    }
    handleQuery()
}

/** 去处理 */
const handleUpdate = (row) => {
    if (row.businessType == 1) router.push('/supplier/formPage/' + row.businessId)
    if (row.businessType == 2) router.push('/mediaLibrary/formPage/' + row.businessId)
}
/** 导出按钮操作 */
const handleExport = () => {
    exportNotice(queryParams.value).then(res => {
        // 通过a标签打开新页面下载文件
        const a = document.createElement('a')
        a.href = URL.createObjectURL(res)
        // a标签里有download属性可以自定义文件名
        const downLoadName = '通知信息导出_' + Date.now() + '.xlsx'
        a.setAttribute('download', downLoadName)
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
    })
}

// 初始化
onMounted(() => {
    bgStore.setBgImage(otherbg)
    getNoticePageList()
}); 
</script>
<style lang='scss'>
.el-form-item__content {
    display: block;
}
</style>