<template>
	<div class="system-tenant-container">
		<el-card shadow="hover">
			<div class="system-tenant-search mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="68px">
					<el-form-item label="租户名称" prop="tenantName">
						<el-input
							v-model="tableData.param.tenantName"
							placeholder="请输入租户名称"
							clearable
							size="default"
							style="width: 240px"
							@keyup.enter.native="tenantList"
						/>
					</el-form-item>
					<el-form-item label="租户类型" prop="tenantType">
						<el-select v-model="tableData.param.tenantType" placeholder="请选择租户类型" clearable size="default" style="width: 240px">
							<el-option label="普通类型" :value="1" />
							<el-option label="代理类型" :value="2" />
						</el-select>
					</el-form-item>
					<el-form-item label="所属城市" prop="city">
						<el-cascader
							v-model="tableData.param.city"
							:options="cityOptions"
							placeholder="请选择城市"
							clearable
							size="default"
							style="width: 240px"
						/>
					</el-form-item>
					<el-form-item>
						<el-button size="default" type="primary" class="ml10" @click="tenantList">
							<el-icon><ele-Search /></el-icon>
							查询
						</el-button>
						<el-button size="default" @click="resetQuery(queryRef)">
							<el-icon><ele-Refresh /></el-icon>
							重置
						</el-button>
						<el-button size="default" type="success" class="ml10" @click="onOpenAddTenant">
							<el-icon><ele-FolderAdd /></el-icon>
							新增租户
						</el-button>
					</el-form-item>
				</el-form>
			</div>
			<el-table :data="tableData.data" style="width: 100%" v-loading="tableData.loading">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="tenantName" label="租户名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="tenantType" label="租户类型" show-overflow-tooltip>
					<template #default="scope">
						<el-tag v-if="scope.row.tenantType === 1">普通类型</el-tag>
						<el-tag v-else-if="scope.row.tenantType === 2" type="warning">代理类型</el-tag>
						<el-tag v-else type="info">未知</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="city" label="所属城市" show-overflow-tooltip>
					<template #default="scope">
						{{ getCityName(scope.row.city) }}
					</template>
				</el-table-column>
				<el-table-column prop="contactPerson" label="联系人" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phone" label="电话" show-overflow-tooltip></el-table-column>
				<el-table-column prop="businessLicense" label="营业执照" align="center">
					<template #default="scope">
						<el-image
							style="width: 50px; height: 50px"
							:src="scope.row.businessLicense"
							:preview-src-list="[scope.row.businessLicense]"
							fit="cover"
							v-if="scope.row.businessLicense"
						/>
						<span v-else>无</span>
					</template>
				</el-table-column>
				<el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column prop="updatedAt" label="修改时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditTenant(scope.row)">修改</el-button>
						<!-- <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button> -->
					</template>
				</el-table-column>
			</el-table>
			<pagination
				v-show="tableData.total > 0"
				:total="tableData.total"
				v-model:page="tableData.param.pageNum"
				v-model:limit="tableData.param.pageSize"
				@pagination="tenantList"
			/>
		</el-card>
		<EditTenant ref="editTenantRef" @getTenantList="tenantList" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import EditTenant from './component/editTenant.vue';
import { getTenantList, deleteTenant } from '/@/api/system/tenant';
import { pcTextArr } from 'element-china-area-data';

export default defineComponent({
	name: 'systemTenant',
	components: { EditTenant },
	setup() {
		const editTenantRef = ref();
		const queryRef = ref();
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
					tenantName: '',
					tenantType: '',
					city: [],
				},
			},
		});

		// 省市数据
		const cityOptions = pcTextArr;

		// 初始化表格数据
		const tenantList = () => {
			state.tableData.loading = true;
			getTenantList(state.tableData.param)
				.then((res: any) => {
					state.tableData.data = res.data.list ?? [];
					state.tableData.total = res.data.total;
				})
				.catch(() => {
					// 模拟数据，防止报错影响演示
					state.tableData.data = [];
					state.tableData.total = 0;
				})
				.finally(() => {
					state.tableData.loading = false;
				});
		};

		// 打开新增弹窗
		const onOpenAddTenant = () => {
			editTenantRef.value.openDialog();
		};

		// 打开修改弹窗
		const onOpenEditTenant = (row: any) => {
			editTenantRef.value.openDialog(row);
		};

		// 删除
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除租户：“${row.tenantName}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					deleteTenant([row.id]).then(() => {
						ElMessage.success('删除成功');
						tenantList();
					});
				})
				.catch(() => {});
		};

		// 重置
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			tenantList();
		};

		// 解析城市名称
		const getCityName = (cityArr: string[]) => {
			if (!cityArr || cityArr.length === 0) return '';
			// 这里简单模拟，实际可以用 cityOptions 递归查找
			return cityArr.join(' / ');
		};

		onMounted(() => {
			tenantList();
		});

		return {
			queryRef,
			editTenantRef,
			onOpenAddTenant,
			onOpenEditTenant,
			onRowDel,
			tenantList,
			resetQuery,
			cityOptions,
			getCityName,
			...toRefs(state),
		};
	},
});
</script>
