<template>
    <div class="p-3">
        <div class="bg-fff flex1 ov-hd d-flex flex-cln" v-show="showSearch">
            <div class="pd3-16-16-0 border-bottom">
                <div class="f-s-20 c-333 f-w-7 mb-10">{{ selectDictLabel(new_type, queryParams.newsType)}}</div>
                <div class="d-flex">
                    <el-form class="flex1" ref="formRef" :model="queryParams" inline label-width="auto">
                        <el-form-item label="发布日期:" prop="dateTime">
                            <div class="d-flex w-230">
                                <DateRange v-model="queryParams.dateTime" v-model:start-date="queryParams.startDate" v-model:end-date="queryParams.endDate"></DateRange>
                            </div>
                        </el-form-item>
                        <el-form-item label="状态:" prop="validStatus">
                            <el-select v-model="queryParams.validStatus" placeholder="请选择状态" style="width: 200px" clearable>
                                <el-option label="有效" value="1"></el-option>
                                <el-option label="无效" value="0"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="关键字:" prop="keyword">
                            <el-input v-model="queryParams.keyword" placeholder="请输入关键字" style="width: 200px"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleQuery">查询</el-button>
                            <el-button @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                    <el-button type="primary" @click="router.push({ path: '/skill/group/public-input', query: { newsType: queryParams.newsType } })">新增{{ selectDictLabel(new_type, queryParams.newsType)}}</el-button>
                </div>
            </div>

            <div class="d-flex flex1 ov-hd flex-cln pd-12">
                <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="newsOrigin" :formatter="colNoData" />
                        <vxe-column title="浏览量" align="center" field="viewCount" />
                        <vxe-column title="状态" align="center" field="extraInfo.validStatus">
                            <template #default="{ row }">
                                <dict-tag :options="dm_valid_status" :value="row.extraInfo.validStatus || '0'" />
                            </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="news-list-cum" 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 { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { news_status, dm_valid_status, new_type} = toRefs<any>(proxy?.useDict('news_status', "dm_valid_status", 'new_type'));
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const formRef = ref<ElFormInstance>();
const dataList = ref<any[]>([]);
const initFormData = {};
const data = reactive<any>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: '1',
        newsType: '31'
    },
    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 = () => {
    formRef.value?.resetFields();
    handleQuery();
};

/** 编辑新闻 */
const editNews = (row) => {
    router.push({ path: `/skill/group/public-input`, 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('下架失败');
    }
};
const route = useRoute();
onMounted(() => {
    queryParams.value.newsType = route.query?.newsType;
    getList();
});
</script>
