<template>
	<a-card :bordered="false" style="margin-bottom: 10px" class="mb-2">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="姓名" name="userName">
						<a-input v-model:value="searchFormState.userName" placeholder="姓名查询" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="状态" name="status">
						<a-select v-model:value="searchFormState.status" placeholder="状态查询" :options="statusList">
						</a-select>
					</a-form-item>
				</a-col>

				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"

		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizActivateAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="detailRef.onOpen(record)" v-if="hasPerm('bizActivateDetail')">明细</a>
						<a-divider type="vertical" v-if="hasPerm(['bizActivateDetail','bizActivateEdit'],'and')" />
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizActivateEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizActivateEdit', 'bizActivateDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizActivate(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizActivateDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-tag
						:color="
							record.status === '1'
								? '#f50'
								: record.status === '2'
								  ? '#87d068'
								      : '#f50'
						"
					>
						{{ $TOOL.dictTypeData('activate_status', record.status) }}
					</a-tag>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<Detail ref="detailRef" @successful="tableRef.refresh()" />
</template>

<script setup name="bizactivate">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import Detail from './detail.vue'
	import bizActivateApi from '@/api/biz/bizActivateApi'
	import tool from '@/utils/tool'
	const tableRef = ref()
	const formRef = ref()
	const detailRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const searchFormRef = ref()
	let searchFormState = reactive({})
	const statusList = tool.dictList('activate_status')
	const columns = [
		{
			title: '姓名',
			dataIndex: 'userName',
			align: 'center',
		},
		{
			title: '糕点',
			dataIndex: 'accountBalance',
			align: 'center',
		},
		{
			title: '状态',
			dataIndex: 'status',
			align: 'center',
		},
		{
			title: '激活需要人数',
			dataIndex: 'successCount',
			align: 'center',
		},
		{
			title: '帮助激活人数',
			dataIndex: 'helpCount',
			align: 'center',
		},
		{
			title: '充值时间',
			dataIndex: 'createTime',
			align: 'center',
		},
	]
	// 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})


	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return bizActivateApi.bizActivatePage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteBizActivate = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizActivateApi.bizActivateDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizActivate = (params) => {
		bizActivateApi.bizActivateDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>
