<template>
	<div>
		<el-card class="!border-none mb-4" shadow="never">
			<el-form class="mb-[-16px]" :model="queryParams" inline>
				<el-form-item class="w-[180px]" label="类型" prop="type">
					<el-select v-model="queryParams.type" clearable placeholder="请选择类型">
						<el-option label="全部" value=""></el-option>
						<el-option v-for="(item, index) in dictData.activity_zone" :key="index" :label="item.remark"
							:value="item.value" />
					</el-select>
				</el-form-item>
				<el-form-item label="表单名称" prop="title">
					<el-input class="w-[280px]" v-model="queryParams.title" clearable placeholder="请输入表单名称" />
				</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" v-loading="pager.loading" shadow="never">
			<el-button v-perms="['activity_zone/add']" type="primary" @click="handleAdd">
				<template #icon>
					<icon name="el-icon-Plus" />
				</template>
				新增
			</el-button>
			<div class="mt-4">
				<el-table :data="pager.lists" @selection-change="handleSelectionChange">
					<el-table-column label="ID" prop="id" show-overflow-tooltip />
					<el-table-column label="类型" prop="type">
						<template #default="{ row }">
							<div v-for="(item, index) in dictData.activity_zone">
								<div v-if="item.value == row.type">{{item.remark}}</div>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="标题" prop="title" show-overflow-tooltip />
					<el-table-column label="商品分类" prop="cate_names" show-overflow-tooltip />
					<el-table-column label="备注" prop="remark" show-overflow-tooltip />

					<el-table-column label="操作" width="180" fixed="right">
						<template #default="{ row }">
							<router-link :to="{
							        path: 'activity_zone_details',
							        query: {
							            id: row.id
							        }
							    }">
								商品列表
							</router-link>
							<el-button v-perms="['activity_zone/export']" type="primary" link
								@click="handleExport(row)">
								导出
							</el-button>
							<el-button v-perms="['activity_zone/delete']" type="danger" link
								@click="handleDelete(row.id)">
								删除
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="flex mt-4 justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
	</div>
</template>

<script lang="ts" setup name="activityZoneLists">
	import { usePaging } from '@/hooks/usePaging'
	import { useDictData } from '@/hooks/useDictOptions'
	import { apiActivityZoneFormLists, apiActivityZoneFormDelete, apiActivityZoneFormAdd, apiActivityZoneFormExport } from '@/api/activity_zone_form'
	import { timeFormat } from '@/utils/util'
	import feedback from '@/utils/feedback'
	import productPop from './component/productPop.vue'
	import EditPopup from './edit.vue'

	const showProduct = ref(false) // 选择商品列表是否显示
	const showRemark = ref(false)
	const showEdit = ref(false)
	const editRef = shallowRef<InstanceType<typeof EditPopup>>()


	// 查询条件
	const queryParams = reactive({
		type: '',
		title: '',
	})

	// 选中数据
	const selectData = ref<any[]>([])

	// 表格选择后回调事件
	const handleSelectionChange = (val : any[]) => {
		selectData.value = val.map(({ id }) => id)
	}

	// 获取字典数据
	const { dictData } = useDictData('activity_zone')

	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: apiActivityZoneFormLists,
		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?.setFormData(data)
	}

	// 删除
	const handleDelete = async (id : number | any[]) => {
		await feedback.confirm('确定要删除？')
		await apiActivityZoneFormDelete({ id })
		getLists()
	}

	const handleExport = (row : any) => {
		showRemark.value = false
		apiActivityZoneFormExport({
			id: row.id
		}).then((res) => {
			window.open(res.url, '_blank')
			ElMessage.success('导出成功')
		})
	}

	getLists()
</script>