<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">{{ selectDictLabel(new_type, queryParams.newsType) }}</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="newsTitle">
                                <el-input v-model="queryParams.newsTitle" placeholder="请输入标题" clearable
                                    style="width: 180px" @keyup.enter="handleQuery" />
                            </el-form-item>
                            <el-form-item label="创建时间" prop="dateRange">
                                <div class="d-flex" style="width: 180px">
                                    <DateRange v-model="queryParams.dateRange"
                                        v-model:start-date="queryParams.startDate"
                                        v-model:end-date="queryParams.endDate"></DateRange>
                                </div>
                            </el-form-item>
                            <el-form-item label="状态" prop="newsStatus">
                                <el-select style="width: 140px" v-model="queryParams.newsStatus" clearable
                                    placeholder="请选择状态">
                                    <el-option v-for="item in news_status" :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.newsType" @change="handleQuery" :list="new_type" key-label="label" key-value="value" :isNum="false"></searchTabs>
                    </div> -->
                    <el-button type="primary"
                        @click="router.push({ path: queryParams.newsType == '14' ? 'news-input' : 'noticeannounceninput', query: { newsType: queryParams.newsType } })">新增{{
                            selectDictLabel(new_type, queryParams.newsType) }}</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 title="标题" align="center" field="newsTitle" :formatter="colNoData" width="400" />
                        <vxe-column title="发布时间" align="center" field="publishTime" :formatter="colNoData" />
                        <vxe-column title="来源" align="center" field="newsOrigin" :formatter="colNoData" />
                        <vxe-column title="浏览量" align="center" field="viewCount" />
                        <vxe-column title="类型" align="center" field="newsType">
                            <template #default="{ row }">
                                <dict-tag :options="new_type" :value="row.newsType" />
                            </template>
                        </vxe-column>
                        <vxe-column title="操作人" align="center" field="createByName" :formatter="colNoData" />
                        <vxe-column title="操作时间" align="center" field="createTime" :formatter="colNoData" />
                        <vxe-column title="状态" align="center" field="newsStatus">
                            <template #default="{ row }">
                                <dict-tag :options="news_status" :value="row.newsStatus" />
                            </template>
                        </vxe-column>
                        <vxe-column title="操作" width="250" align="center" fixed="right">
                            <template #default="{ row }">
                                <el-button type="text" size="small"
                                    @click="row.newsStatus === '1' ? unpublishNews(row) : publishNews(row)">
                                    {{ row.newsStatus === '1' ? '下架' : '上架' }}
                                </el-button>
                                <el-button type="text" size="small" @click="editNews(row)">编辑</el-button>
                                <el-button type="text" size="small" @click="deleteNews(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>
</template>

<script setup name="price-num-warn" lang="ts">
import { colNoData } from '@/utils/noData';
import { publishNews as publishNewsApi, unpublishNews as unpublishNewsApi, removeNews, fetchNewsList } from '@/api/dgtmedicine/news';
import { DateRange } from '@/views/models/index';
import { searchTabs } from '@/views/models';
const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { news_status, new_type } = toRefs<any>(proxy?.useDict('news_status', 'new_type'));
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const dataList = ref<any[]>([]);
const initFormData = {};
const data = reactive<any>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: '1',
        newsTitle: '',
        newsStatus: '',
        newsType: '14'
    },
    rules: {}
});

const { queryParams, form } = toRefs(data);
/** 查询会员信息列表 */
const getList = async () => {
    loading.value = true;
    const res = await fetchNewsList(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 editNews = (row) => {
    router.push({ path: row?.newsType == '14' ? 'news-input' : 'noticeannounceninput', query: { id: row.id, newsType: row?.newsType, newsStatus: row.newsStatus } });
};


/** 删除新闻 */
const deleteNews = async (row) => {
    ElMessageBox.confirm(`确认要删除 "${row.newsTitle}" 数据吗？`, '删除提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(async () => {
        const res = await removeNews([row.id]);
        if (res) {
            ElMessage.success('删除成功');
            getList();
        }
    });
};

/** 上架新闻 */
const publishNews = async (row) => {
    try {
        const res = await publishNewsApi(row.id); // Replace with your API call
        if (res) {
            ElMessage.success('上架成功');
            getList();
        }
    } catch (error) {
        ElMessage.error('下架失败');
    }
};

/** 下架新闻 */
const unpublishNews = async (row) => {
    try {
        const res = await unpublishNewsApi(row.id); // Replace with your API call
        if (res) {
            ElMessage.success('下架成功');
            getList();
        }
    } catch (error) {
        ElMessage.error('下架失败');
    }
};

onMounted(() => {
    queryParams.value.newsType = route.query?.newsType;
    getList();
});
</script>
