<template>
	<div class="table_item">
		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			:row-key="(record) => record.id"
			bordered
			@resizeColumn="handleResizeColumn"
			:row-selection="options.rowSelection"
		>
			<template #operator>
				<!-- 搜索区域 -->
				<div class="table-search">
					<div class="table-search-form">
						<a-row :gutter="10">
							<a-form
								ref="searchFormRef"
								name="advanced_search"
								layout="inline"
								:label-col="{ style: { width: '70px', justifyContent: 'end' } }"
								:model="searchFormState"
								class="ant-advanced-search-form"
							>
								<a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8"
									><a-form-item label="真实姓名" name="name">
										<a-input v-model:value="searchFormState.name" placeholder="请输入真实姓名" /> </a-form-item
								></a-col>
								<a-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8"
									><a-form-item label="微信昵称" name="nickName">
										<a-input v-model:value="searchFormState.nickName" placeholder="请输入微信昵称" /> </a-form-item
								></a-col>
							</a-form>
						</a-row>
					</div>
					<div class="table-search-buttons">
						<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
						<a-button class="xn-mg08" @click="reset">重置</a-button>
					</div>
				</div>

				<!-- 其他操作区域 -->
				<div class="table-head-btn">
					<div class="btn-left" style="display: flex">
						<!-- <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('alarmUserAdd')">
							<template #icon><plus-outlined /></template>新增
						</a-button> -->
						<xn-batch-button
							class="xn-mg08"
							v-if="hasPerm('alarmUserBatchDelete')"
							buttonName="批量删除"
							icon="DeleteOutlined"
							:selectedRowKeys="selectedRowKeys"
							@batchCallBack="deleteBatchAlarmUser"
						/>
					</div>
				</div>
			</template>
			<template #bodyCell="{ column, record, index }">
				<template v-if="column.dataIndex === 'index'">{{ index + 1 }} </template>

				<template v-if="column.dataIndex === 'avatar'">
					<a-avatar v-if="record.avatar" :src="record.avatar" class="xn-wh25" />
				</template>

				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<!-- <a @click="formRef.onOpen(record)" v-if="hasPerm('alarmUserEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['alarmUserEdit', 'alarmUserDelete'], 'and')" /> -->

						<a @click="perfectformRef.onOpen(record)">完善信息</a>

						<a-divider type="vertical" v-if="hasPerm(['alarmUserEdit', 'alarmUserDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteAlarmUser(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('alarmUserDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>

		<Form ref="formRef" @successful="tableRef.refresh()" />

		<perfectForm ref="perfectformRef" @successful="tableRef.refresh()" />
	</div>
</template>

<script setup name="alarmuser">
	import { cloneDeep } from 'lodash-es'
	import Form from './form.vue'
	import perfectForm from './perfectForm.vue'

	import alarmUserApi from '@/api/coldchain/alarmUserApi'
	const searchFormState = ref({})
	const searchFormRef = ref()
	const tableRef = ref()
	const formRef = ref()
	const perfectformRef = ref()

	const columns = [
		{
			title: '序号',
			dataIndex: 'index',
			align: 'center',
			width: 50
		},
		{
			title: '真实姓名',
			dataIndex: 'name',
			align: 'center',
			ellipsis: true,
			resizable: true
		},
		{
			title: '手机号码',
			dataIndex: 'phone',
			align: 'center',
			ellipsis: true,
			resizable: true
		},

		{
			title: '微信昵称',
			dataIndex: 'nickName',
			align: 'center',
			ellipsis: true,
			resizable: true
		},

		{
			title: '组织',
			dataIndex: 'createOrgName',
			align: 'center',
			ellipsis: true,
			resizable: true
		},
		{
			title: '是否关注',
			dataIndex: 'subscribed',
			align: 'center',
			ellipsis: true,
			resizable: true,
			customRender({ value }) {
				return value == '0' ? '否' : '是'
			}
		},

		{
			title: '用户头像',
			dataIndex: 'avatar',
			align: 'center',
			ellipsis: true,
			resizable: true
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			align: 'center',
			ellipsis: true,
			resizable: true
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['alarmUserEdit', 'alarmUserDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 250
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	// 可伸缩列
	const handleResizeColumn = (w, col) => {
		col.width = w
	}
	const loadData = (parameter) => {
		const searchFormParam = cloneDeep(searchFormState.value)
		return alarmUserApi.alarmUserPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 删除
	const deleteAlarmUser = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		alarmUserApi.alarmUserDelete(params).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchAlarmUser = (params) => {
		alarmUserApi.alarmUserDelete(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
</script>

<style lang="less" scoped>
	.table_item {
		padding: 15px 20px;
		background-color: #ffffff;
		:deep(.ant-table-pagination-right) {
			justify-content: center !important;
		}
	}
</style>
