<template>
	<a-card :bordered="false" style="margin-bottom: 10px" class="mb-2">
		<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="userName">
						<a-input v-model:value="searchFormState.userName" placeholder="姓名查询" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="tableRef.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
<!--					<a @click="toggleAdvanced" style="margin-left: 8px">
						{{ advanced ? '收起' : '展开' }}
						<component :is="advanced ? 'up-outlined' : 'down-outlined'" />
					</a>-->
				</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('consumptionRecordAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record,index }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="detailForm.onOpen(record)" v-if="hasPerm('warnDetail')">详情</a>
					</a-space>
				</template>
				<template v-if="column.dataIndex === 'consumptionOperate'">
					<a-tag
						:color="
							record.consumptionOperate === '1'
								? 'orange'
								: record.consumptionOperate === '2'
								  ? 'green'
								  : record.consumptionOperate === '3'
								    ? 'cyan'
								      : 'purple'
						"
					>
						{{ $TOOL.dictTypeData('consumption_operate', record.consumptionOperate) }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'serial'">
					{{ index + 1 }}
				</template>
				<template v-if="column.dataIndex === 'userStatus'">
					<a-switch
						:loading="loading"
						:checked="record.userStatus === 'ENABLE'"
						@change="editStatus(record)"
						v-if="hasPerm('bizUserUpdataStatus')"
					/>
					<a-tag v-else :color="record.userStatus === 'ENABLE' ? 'blue' : 'pink'">{{
							$TOOL.dictTypeData('COMMON_STATUS', record.userStatus)
						}}</a-tag>
					<!--					<span v-else>{{ $TOOL.dictTypeData('COMMON_STATUS', record.userStatus) }}</span>-->
				</template>
			</template>
		</s-table>
	</a-card>
	<DetailForm ref="detailForm" @successful="tableRef.refresh()" />
</template>

<script setup name="consumptionrecord">
	import { cloneDeep } from 'lodash-es'
	import consumptionRecordApi from '@/api/biz/consumptionRecordApi'
	import tool from '@/utils/tool'
	import bizOrgApi from '@/api/biz/bizOrgApi'
	import DetailForm from './detail.vue'
	import Consumption from "@/views/biz/member/consumption.vue";
	import bizUserApi from '@/api/biz/bizUserApi'

	const tableRef = ref()
	const formRef = ref()
	const detailForm = ref()

	const searchFormRef = ref()
	let searchFormState = reactive({})
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const loading = ref(false)
	const consumptionOperateList = tool.dictList('consumption_operate')
	const treeData = ref([])

	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '序号',
			dataIndex: 'serial',
			align: 'center',
			width: 50
		},
		{
			title: '推荐人姓名',
			dataIndex: 'name',
			align: 'center',
		},
		{
			title: '账号',
			dataIndex: 'account',
			align: 'center',
		},
		{
			title: '手机号',
			dataIndex: 'phone',
			align: 'center',
		},
		{
			title: '退款次数',
			dataIndex: 'count',
			align: 'center',
		},
		{
			title: '状态',
			dataIndex: 'userStatus',
			align: 'center',
		},

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

	// 获取机构树并加入顶级
	bizOrgApi.orgTreeSelector().then((res) => {
		treeData.value = res
	})

	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		// planTime范围查询条件重载
		if (searchFormParam.consumptionTime) {
			searchFormParam.consumptionTimeBegin = searchFormParam.consumptionTime[0]
			searchFormParam.consumptionTimeEnd = searchFormParam.consumptionTime[1]
			delete searchFormParam.consumptionTime
		}
		return consumptionRecordApi.warnPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteConsumptionRecord = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		consumptionRecordApi.consumptionRecordDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchConsumptionRecord = (params) => {
		consumptionRecordApi.consumptionRecordDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}

	// 修改状态
	const editStatus = (record) => {
		loading.value = true
		if (record.userStatus === 'ENABLE') {
			bizUserApi
				.userDisableUser(record)
				.then(() => {
					tableRef.value.refresh()
				})
				.finally(() => {
					loading.value = false
				})
		} else {
			bizUserApi
				.userEnableUser(record)
				.then(() => {
					tableRef.value.refresh()
				})
				.finally(() => {
					loading.value = false
				})
		}
	}
</script>
