<template>
	<a-card :bordered="false" class="xn-mb10">
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form"
				:model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item name="searchKey" label="名称关键词">
						<a-input v-model:value="searchFormState.searchKey" placeholder="请输入机构名称关键词"/>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-button type="primary" @click="tableRef.refresh(true)">
						<template #icon>
							<SearchOutlined/>
						</template>
						查询
					</a-button>
					<a-button class="snowy-button-left" @click="reset">
						<template #icon>
							<redo-outlined/>
						</template>
						重置
					</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('bizRechargePlanConfigAdd')">
						<template #icon>
							<plus-outlined/>
						</template>
						新增
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizRechargePlanConfigEdit')">编辑</a>
						<a-divider type="vertical"
								   v-if="hasPerm(['bizRechargePlanConfigEdit', 'bizRechargePlanConfigDelete'], 'and')"/>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizRechargePlanConfig(record)">
							<a-button type="link" danger size="small"
									  v-if="hasPerm('bizRechargePlanConfigDelete')">删除
							</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()"/>
</template>

<script setup name="rechargeplanconfig">
import {cloneDeep} from 'lodash-es'
import Form from './form.vue'
import bizRechargePlanConfigApi from '@/api/biz/bizRechargePlanConfigApi'
const searchFormRef = ref()
const searchFormState = ref({})
const tableRef = ref()
const formRef = ref()
const columns = [
	{
		title: '充值金额底线',
		dataIndex: 'rechargeAmountOne'
	},
	{
		title: '优惠券金额',
		dataIndex: 'couponAmount'
	},
	{
		title: '优惠券数量',
		dataIndex: 'couponNum'
	},
	{
		title: '账户余额增加金额',
		dataIndex: 'accountBalanceOne'
	},
	{
		title: '返点比例（代金券）',
		dataIndex: 'rebateRatioOne'
	},
]
// 操作栏通过权限判断是否显示
if (hasPerm(['bizRechargePlanConfigEdit', 'bizRechargePlanConfigDelete'])) {
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})
}
const loadData = (parameter) => {
	return bizRechargePlanConfigApi.bizRechargePlanConfigPage(parameter).then((data) => {
		return data
	})
}
// 重置
const reset = () => {
	searchFormRef.value.resetFields()
	tableRef.value.refresh(true)
}
// 删除
const deleteBizRechargePlanConfig = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	bizRechargePlanConfigApi.bizRechargePlanConfigDelete(params).then(() => {
		tableRef.value.refresh(true)
	})
}
// 批量删除
const deleteBatchBizRechargePlanConfig = (params) => {
	bizRechargePlanConfigApi.bizRechargePlanConfigDelete(params).then(() => {
		tableRef.value.clearRefreshSelected()
	})
}
</script>
