<template>
	<div class="system-user-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
				<el-input   v-model="tableData.param.keyword" placeholder="请输入企业名称" style="max-width: 180px"> </el-input>
				<el-button   type="primary" class="ml10" @click="onSearch">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button   type="success" class="ml10" @click="onOpenEdit">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增企业
				</el-button>
			</div>
			<el-table :data="tableData.data" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="企业名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="size" label="规模" show-overflow-tooltip></el-table-column>
				<el-table-column prop="register_amount" label="注册金额" show-overflow-tooltip></el-table-column>
				<el-table-column prop="establish_time" label="成立时间" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phone" label="手机号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="create_time" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column prop="update_time" label="更新时间" show-overflow-tooltip> </el-table-column>
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button size="small" type="text" @click="onOpenEdit(scope.row.id)">修改</el-button>
						<el-button size="small" type="text" @click="onRowDel(scope.row)">删除</el-button>
						<el-button size="small" type="text" @click="onAccountShow(scope.row)">账号</el-button>
						<!-- <el-button size="small" type="text" @click="onPhotoShow(scope.row)">相册</el-button> -->
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="tableData.param.pageNum"
				background
				v-model:page-size="tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableData.total"
			>
			</el-pagination>
		</el-card>
		<edit ref="editRef" @finished="initTableData" />
		<edit ref="addRef" @finished="onSearch" />
		<account ref="accountRef" />
		<photo ref="photoRef" />
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import account from './component/account.vue';
import photo from './component/photo.vue';
import edit from './component/edit.vue';
import Company from '/@/api/Company';
const tagInfo = '[/src/views/company/index.vue]:';
// 定义接口来定义对象的类型
interface TableDataRow {
	name: string;
	size: string;
	register_amount: string;
	establish_time: string;
	detail: string;
	phone: string;
	logo: string;
}

interface TableDataState {
	tableData: {
		data: Array<any>;
		total: number;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
		};
	};
}

export default defineComponent({
	name: 'systemUser',
	components: { edit, account,photo },
	setup() {
		const addRef = ref();
		const editRef = ref();
		const accountRef = ref();
		const photoRef = ref();
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
					keyword: '',
				},
			},
			accountShow: false,
		});
		const onSearch = () => {
			state.tableData.param.pageNum = 1;
			initTableData();
		};
		// 初始化表格数据
		const initTableData = async () => {
			const data: Array<any> = [];
			let res = await Company.getEnterpriseList(state.tableData.param.keyword, state.tableData.param.pageNum, state.tableData.param.pageSize);
			if (res.code != 0) {
				ElMessage.error(res.msg);
				return;
			}
			state.tableData.total = res.data.total;
			state.tableData.data = res.data.data;
		};
		// 打开新增或修改用户弹窗
		const onOpenEdit = (id = 0) => {
			console.log('onOpenEdit', id);
			if (id > 0) {
				editRef.value.openDialog(id);
			} else {
				addRef.value.openDialog(0);
			}
		};
		//打开账号列表弹窗
		const onAccountShow = (row: any) => {
			accountRef.value.openDialog(row.id);
		};
		//打开相册列表弹窗
		const onPhotoShow = (row: any) => {
			photoRef.value.openDialog(row.id);
		};

		// 删除用户
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久企业：“${row.name}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(async () => {
					ElMessage.success('删除成功');
					await Company.delEnterPrise(row.id);
					initTableData();
				})
				.catch(() => {});
		};
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
			initTableData();
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
			initTableData();
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			editRef,
			addRef,
			photoRef,
			accountRef,
			onOpenEdit,
			onAccountShow,
			onPhotoShow,
			onRowDel,
			onHandleSizeChange,
			onHandleCurrentChange,
			onSearch,
			initTableData,
			...toRefs(state),
		};
	},
});
</script>
