<template>
	<a-card :bordered="false" class="xn-mb10">
		<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="searchKey">
						<a-input v-model:value="searchFormState.searchKey" placeholder="请输入姓名或者手机号" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="充值时间" name="rechargeTime">
						<a-range-picker v-model:value="searchFormState.rechargeTime" value-format="YYYY-MM-DD HH:mm:ss" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @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>
		<s-table ref="tableRef" :columns="columns" :data="loadData" bordered :row-key="(record) => record.id">
			<template #bodyCell="{ column, record, index }">
				<template v-if="column.dataIndex === 'serial'">
					{{ index + 1 }}
				</template>
				<template v-if="column.dataIndex === 'isPay'">
					<a-tag v-if="record.isPay === '1'" color="blue">已支付</a-tag>
					<a-tag v-else-if="record.isPay === '2'" color="purple">已取消</a-tag>
					<a-tag v-else color="pink">待支付</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)">详情</a>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="rechargerecord">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import bizRechargeRecordApi from '@/api/biz/bizRechargeRecordApi'
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const columns = [
		{
			title: '序号',
			dataIndex: 'serial',
			width: 80,
			align: 'center',
		},
		{
			title: '姓名',
			dataIndex: 'name',
			align: 'center',
		},
		{
			title: '手机号',
			dataIndex: 'phone',
			align: 'center',
		},
		{
			title: '充值金额（元）',
			dataIndex: 'rechargeAmount',
			align: 'center',
		},
		{
			title: '充值时间',
			dataIndex: 'rechargeTime',
			align: 'center',
		},
		{
			title: '原账户糕点（个）',
			dataIndex: 'oldAccountBalance',
			align: 'center',
		},
		{
			title: '新账户糕点（个）',
			dataIndex: 'newAccountBalance',
			align: 'center',
		},
		{
			title: '是否支付',
			dataIndex: 'isPay',
			align: 'center',
		}
	]
	// 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		// rechargeTime范围查询条件重载
		if (searchFormParam.rechargeTime) {
			searchFormParam.startRechargeTime = searchFormParam.rechargeTime[0]
			searchFormParam.endRechargeTime = searchFormParam.rechargeTime[1]
			delete searchFormParam.rechargeTime
		}
		return bizRechargeRecordApi.bizRechargeRecordPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
</script>
