<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="6">
					<a-form-item label="门店" name="orgId">
						<a-tree-select
							v-model:value="searchFormState.orgId"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择门店"
							allow-clear
							tree-default-expand-all
							:tree-data="treeData"
							:field-names="{
									children: 'children',
									label: 'name',
									value: 'id'
								}"
							selectable="false"
							tree-line
						/>
					</a-form-item>
				</a-col>-->
				<a-col :span="6">
					<a-form-item name="status" label="状态">
						<a-select v-model:value="searchFormState.status" placeholder="请选择">
							<a-select-option v-for="item in statusData" :key="item.value" :value="item.value">{{
								item.label
							}}</a-select-option>
						</a-select>
					</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 style="margin-left: 8px" @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, index }">
				<template v-if="column.dataIndex === 'serial'">
					{{ index + 1 }}
				</template>
				<template v-if="column.dataIndex === 'rebateRatio'">
					{{ record.rebateRatio + ' %' }}
				</template>
				<template v-if="column.dataIndex === 'rebateUserRatio'">
					{{ record.rebateUserRatio + ' %' }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-switch
						:loading="loading"
						:checked="record.status === 'ENABLE'"
						checked-children="启用"
						un-checked-children="停用"
						@change="editStatus(record)"
						v-if="hasPerm('bizUserUpdataStatus')"
					/>
					<span v-else>{{ $TOOL.dictTypeData('COMMON_STATUS', record.status) }}</span>
				</template>
				<template v-if="column.dataIndex === 'isAccount'">
					<a-tag
						:color="
							record.isAccount === '0'
								? '#87d068'
								: record.isAccount === '1'
								  ? '#f50'
								      : '#f50'
						"
					>
						{{ $TOOL.dictTypeData('is_destroy', record.isAccount) }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a
							@click="detailRef.onOpen(record)"
							v-if="hasPerm('bizCakeAdd') && record.status === 'DISABLED'"
						>蛋糕券</a>
						<a-divider
							type="vertical"
							v-if="
								hasPerm(['bizCakeAdd', 'bizRechargePlanConfigEdit'], 'and') &&
								record.status === 'DISABLED'
							"
						/>
						<a
							@click="formRef.onOpen(record)"
							v-if="hasPerm('bizRechargePlanConfigEdit') && record.status === 'DISABLED'"
							>编辑</a
						>
						<a-divider
							type="vertical"
							v-if="
								hasPerm(['bizRechargePlanConfigEdit', 'bizRechargePlanConfigDelete'], 'and') &&
								record.status === 'DISABLED'
							"
						/>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizRechargePlanConfig(record)">
							<a-button
								type="link"
								danger
								size="small"
								v-if="hasPerm('bizRechargePlanConfigDelete') && record.status === 'DISABLED'"
								>删除
							</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
	<Detail ref="detailRef" @successful="tableRef.refresh()" />
</template>

<script setup name="rechargeplanconfig">
	import Form from './form.vue'
	import bizRechargePlanConfigApi from '@/api/biz/bizRechargePlanConfigApi'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	import tool from '@/utils/tool'
	import { ref } from 'vue'
	import Detail from './detail.vue'

	const searchFormRef = ref()
	const searchFormState = ref({})
	const tableRef = ref()
	const formRef = ref()
	const detailRef = ref()
	const loading = ref(false)
	const statusData = tool.dictList('COMMON_STATUS')
	const treeData = ref([])
	const columns = [
		{
			title: '序号',
			dataIndex: 'serial',
			width: 80,
			align: 'center',
		},
		/*{
			title: '门店',
			dataIndex: 'orgName',
			align: 'center',
		},*/
		{
			title: '充值金额及以上（元）',
			dataIndex: 'rechargeAmount',
			align: 'center',
		},
		/*{
			title: '蛋糕券大小(寸)',
			dataIndex: 'couponAmount',
			align: 'center',
		},
		{
			title: '蛋糕券数量',
			dataIndex: 'couponNum',
			align: 'center',
		},*/
		{
			title: '账户到账糕点（个）',
			dataIndex: 'accountBalance',
			align: 'center',
		},
		{
			title: '是否立即到账',
			dataIndex: 'isAccount',
			align: 'center',
		},
		/*{
			title: '店员返点比例',
			dataIndex: 'rebateRatio',
			align: 'center',
		},
		{
			title: '用户返点比例',
			dataIndex: 'rebateUserRatio',
			align: 'center',
		},*/
		{
			title: '状态',
			dataIndex: 'status',
			align: 'center',
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizRechargePlanConfigEdit', 'bizRechargePlanConfigDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 200
		})
	}
	const loadData = (parameter) => {
		return bizRechargePlanConfigApi
			.bizRechargePlanConfigPage(Object.assign(parameter, searchFormState.value))
			.then((data) => {
				return data
			})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}

	// 获取机构树并加入顶级
	bizOrgApi.orgTreeSelector().then((res) => {
		treeData.value = res
	})
	// 删除
	const deleteBizRechargePlanConfig = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizRechargePlanConfigApi.bizRechargePlanConfigDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 修改状态
	const editStatus = (record) => {
		loading.value = true
		if (record.status === 'ENABLE') {
			bizRechargePlanConfigApi
				.bizRechargePlanConfigDisabled({ id: record.id })
				.then(() => {
					tableRef.value.refresh()
				})
				.finally(() => {
					loading.value = false
				})
		} else {
			bizRechargePlanConfigApi
				.bizRechargePlanConfigEnable({ id: record.id })
				.then(() => {
					tableRef.value.refresh()
				})
				.finally(() => {
					loading.value = false
				})
		}
	}
</script>
