<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="title">
          <el-input class="w-[280px]" v-model="queryParams.title" />
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-select
              v-model="queryParams.isShow"
              class="w-[280px]"
              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="linkAddress">
          <el-input class="w-[280px]" v-model="queryParams.linkAddress" />
        </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="['banner: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" min-width="100" show-overflow-tooltip >
          <template #default="{ row }">
            <el-link @click.stop="handlePreview(row.img)" :underline="false">
              {{ row.title }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="banner图" prop="img" min-width="100">
          <template #default="{ row }">
            <file-item :uri="row.img" file-size="80px" type="video"></file-item>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="100" />
        <el-table-column label="更新时间" prop="updateTime" min-width="100" />
        <el-table-column label="排序编号" prop="sort" 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="linkAddress" min-width="100" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button
                v-perms="['banner:edit']"
                type="primary"
                link
                @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
                v-perms="['banner: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"
        :dict-data="dictData"
        @success="getLists"
        @close="showEdit = false"
    />
    <preview v-model="showPreview" :url="previewUrl" type="video" />
  </div>
</template>
<script lang="ts" setup name="banner">
import { bannerDelete, bannerLists } from '@/api/banner'
import { useDictData } from '@/hooks/useDictOptions'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import Preview from '@/components/material/preview.vue'
import FileItem from "@/components/material/file.vue";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
  title: '',
  sort: null,
  isShow: null,
  linkAddress: '',
})

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

const previewUrl = ref('')
const showPreview = ref(false)
const { dictData } = useDictData<{
  Integer: any[]
}>(['Integer'])


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 bannerDelete({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

const handlePreview = (img: string) => {
  previewUrl.value = img
  console.log(img)
  showPreview.value = true
}

getLists()
</script>
