<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="是否显示" prop="isShow">
          <el-select
              v-model="queryParams.isShow"
              class="w-[260px]"
              clearable
          >
            <el-option label="全部" value="" />
            <el-option
                v-for="(item, index) in dictData.Integer"
                :key="index"
                :label="item.name"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="新闻标题" prop="title">
          <el-input class="w-[260px]"  v-model="queryParams.title" />
        </el-form-item>
        <el-form-item label="副标题" prop="subhead">
          <el-input class="w-[260px]" v-model="queryParams.subhead" />
        </el-form-item>
        <el-form-item label="新闻分类" prop="cateId">
          <el-select
              v-model="queryParams.cateId"
              class="flex-1"
              clearable
              placeholder="请选择新闻分类"
          >
            <el-option v-for="item in newsCateList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <div>
        <el-button v-perms="['news:add']" type="primary" @click="handleAdd()">
          <template #icon>
            <icon name="el-icon-Plus" />
          </template>
          新增
        </el-button>
      </div>
      <el-table
          class="mt-4"
          size="large"
          v-loading="pager.loading"
          :data="pager.lists"
      >
        <el-table-column label="新闻标题" prop="title" width="200px" />
        <el-table-column label="副标题" prop="subhead" min-width="100" />
        <el-table-column label="创建时间" prop="createTime" min-width="100" />
        <el-table-column label="更新时间" prop="updateTime" min-width="100" />
        <el-table-column label="是否显示" prop="isShow" min-width="100">
          <template #default="{ row }">
            <dict-value :options="dictData.Integer" :value="row.isShow" />
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" width="50" min-width="50" />

        <el-table-column label="新闻主图" prop="newsImg" min-width="100" >
          <template #default="{ row }">
            <image-contain
                :width="100"
                :height="80"
                :src="row.newsImg"
                :preview-src-list="[row.newsImg]"
                preview-teleported
                hide-on-click-modal
            />
          </template>
        </el-table-column>
        <el-table-column label="新闻分类" prop="cateName" min-width="100" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button
                v-perms="['news:edit']"
                type="primary"
                link
                @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
                v-perms="['news:del']"
                type="danger"
                link
                @click="handleDelete(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
        v-if="showEdit"
        ref="editRef"
        @success="getLists"
        @close="showEdit = false"
    />
  </div>
</template>
<script lang="ts" setup name="news">
import { newsDelete, newsLists } from '@/api/news'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {useDictData} from "@/hooks/useDictOptions";
import {listAll} from "@/api/newscate";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
  isShow: '',
  sort: '',
  title: '',
  subhead: '',
  newsImg: '',
  content: '',
  cateId: '',
})
const newsCateList = ref<any[]>([])
const { dictData } = useDictData<{
  Integer: any[]
}>(['Integer'])


const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: newsLists,
  params: queryParams
})



const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await newsDelete({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

const getNewsCateList  = async () => {
  const res: any = await listAll()
  newsCateList.value = res || []
}


getLists()
getNewsCateList()
</script>
