<template>
	<a-drawer :title="title" :width="850" :open="visible" :destroy-on-close="true" @close="onClose">
		<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="rechargeFormRef.onOpen(null, serviceCustomerId, scAccountId)" v-if="hasPerm('bizServiceCustomerFlowRecharge')">
							<template #icon><plus-outlined /></template>
							充值
						</a-button>
						<a-button type="primary" @click="consumeFormRef.onOpen(null, serviceCustomerId, scAccountId)" v-if="hasPerm('bizServiceCustomerFlowConsume')">
							<template #icon><minus-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 === 'action'">
						<a-space>
							<a @click="cancelRef.onOpen(record, serviceCustomerId, scAccountId)" v-if="hasPerm('bizServiceCustomerFlowCancel')">取消</a>
						</a-space>
					</template>
				</template>
			</s-table>
		</a-card>
	</a-drawer>

	<RechargeForm ref="rechargeFormRef" @successful="tableRef.refresh()" />
	<ConsumeForm ref="consumeFormRef" @successful="tableRef.refresh()" />
	<CancelForm ref="cancelRef" @successful="tableRef.refresh()" />
</template>

<script setup name="bizservicecustomeraccount">
	import { cloneDeep } from 'lodash-es'
	import RechargeForm from './flowRechargeForm.vue'
	import ConsumeForm from './flowConsumeForm.vue'
	import CancelForm from './flowCancelForm.vue'	
	import bizServiceCustomerFlowApi from '@/api/biz/bizServiceCustomerFlowApi'
	import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
	import {Modal} from 'ant-design-vue';
	import {createVNode} from 'vue';
	
	const submitLoading = ref(false)
	const toolConfig = { refresh: true, height: false, columnSetting: false, striped: false }
	// 默认是关闭状态
	const visible = ref(false)
	const searchFormState = ref({})
	const tableRef = ref()
	const rechargeFormRef = ref()
	const consumeFormRef = ref()
	const cancelRef = ref()
	const recordData = ref()
	const title = ref()
	const serviceCustomerId = ref()
	const scAccountId = ref()
	
	const columns = [
		{
			title: '序号',
			width: 50,
			dataIndex: 'serial',
			align:'center'
		},
		{
			title: '操作类型',
			width: 100,
			dataIndex: 'operateType',
			align:'center'
		},
		{
			title: '操作前余额',
			width: 150,
			dataIndex: 'operateAmountBegin',
			align:'center'
		},
		{
			title: '操作金额',
			width: 150,
			dataIndex: 'operateAmount',
			align:'center'
		},
		{
			title: '操作后余额',
			width: 150,
			dataIndex: 'operateAmountAfter',
			align:'center'
		},
		{
			title: '操作时间',
			width: 180,
			dataIndex: 'createTime',
			align: 'center'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizServiceCustomerFlowCancel'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 100
		})
	}
	// 打开抽屉
	const onOpen = (record, scId) => {
		recordData.value = record
		title.value = "【" + record.loginAccount + "】-账户流水管理"
		searchFormState.value = {
			serviceCustomerId: scId,
			scAccountId: record.id
		}
		serviceCustomerId.value = scId
		scAccountId.value = record.id
		visible.value = true
	}
	const loadData = (parameter) => {
		return bizServiceCustomerFlowApi.bizServiceCustomerFlowPage(Object.assign(parameter, searchFormState.value)).then((data) => {
			return data
		})
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}
	// 删除
	const deleteConfig = (record) => {
		Modal.confirm({
			title: '确定删除该数据吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				let params = [
					{
						id: record.id
					}
				]

				bizServiceCustomerFlowApi
					.bizServiceCustomerFlowDelete(params)
					.then(() => {
						tableRef.value.refresh(true)
					})
					.finally(() => {
						submitLoading.value = false
					})
			},
			onCancel() {}
		})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
