<template>
	<a-drawer title="客户账号管理" :width="650" :open="visible" :destroy-on-close="true" @close="onClose">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="false"
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
		>
			<template #operator class="table-operator">
				<a-button type="primary" v-if="hasPerm('customerAccountAdd')" @click="accountAddForm.onOpen(recordData)">
					<template #icon>
						<plus-outlined />
					</template>
					<span>新增账号</span>
				</a-button>
			</template>
			<template #bodyCell="{ column, record, index }">
				<template v-if="column.dataIndex === 'serial'">
					{{ index + 1 }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a-button type="link" danger size="small" v-if="hasPerm('customerAccountDelete')" @click="deleteConfig(record)">删除</a-button>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-drawer>
	<AccountAddForm ref="accountAddForm" @successful="tableRef.refresh(true)" />
</template>

<script setup name="accountForm">
	import customerAccountApi from '@/api/biz/customerAccountApi'
	import AccountAddForm from './accountAdd.vue'
	import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
	import {Modal} from 'ant-design-vue';
	import {createVNode} from 'vue';

	const columns = [
		{
			title: '序号',
			width: 50,
			dataIndex: 'serial',
			align:'center'
		},
		{
			title: '账号',
			align:'center',
			dataIndex: 'loginAccount'
		},
	]
	const submitLoading = ref(false)
	const toolConfig = { refresh: true, height: false, columnSetting: false, striped: false }
	// 默认是关闭状态
	const visible = ref(false)
	const searchFormState = ref()
	const accountAddForm = ref()
	const recordData = ref()
	const tableRef = ref()

	// 操作栏通过权限判断是否显示
	if (hasPerm(['customerAccountDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 150
		})
	}

	// 打开抽屉
	const onOpen = (record) => {
		recordData.value = record
		searchFormState.value = {
			customerId: record.id
		}
		visible.value = true
	}
	// 加载字段数据
	const loadData = (parameter) => {
		return customerAccountApi.customerAccountPage(Object.assign(parameter, searchFormState.value)).then((res) => {
			return res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}
	// 删除
	const deleteConfig = (record) => {
		Modal.confirm({
			title: '确定删除该数据吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				let params = [
					{
						id: record.id
					}
				]

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

<style scoped>
</style>
