<template>
	<div v-loading="loading" v-if="isShowDialog">
		<el-dialog title="企业账号" v-model="isShowDialog" width="70%" :close-on-click-modal="false">
			<div class="system-user-search mb15">
				<el-input   v-model="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="editUser(0)">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增账号
				</el-button>
			</div>
			<!-- 表格 -->
			<el-table :data="tableData" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="user.real_name" label="真实姓名" show-overflow-tooltip></el-table-column>
				<el-table-column prop="user.phone" label="手机号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="user.gender" label="性别" show-overflow-tooltip></el-table-column>
				<el-table-column prop="user.id_number" 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="60">
					<template #default="scope">
						<el-button size="small" type="text" @click="editUser(scope.row.user.id, scope.row.user)">修改</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> -->
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="param.pageSizeList"
				v-model:current-page="param.page"
				background
				v-model:page-size="param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="param.total"
			>
			</el-pagination>
			<account-edit ref="accountEdit" @finished="initTableData"></account-edit>
		</el-dialog>
	</div>
</template>
<script lang="ts">
import { defineComponent, reactive, onMounted, toRefs, ref } from 'vue';
import accountEdit from './accountEdit.vue';
import User from '/@/api/User';
import { ElMessageBox, ElMessage } from 'element-plus';
const tagInfo = '[/views/company/component/account.vue]';
export default defineComponent({
	components: { accountEdit },
	setup() {
		const accountEdit = ref();
		const state = reactive({
			loading: false,
			isShowDialog: false,
			param: {
				enterprise_id: 0,
				type: 0,
				keyword: '',
				page: 1,
				pageSize: 10,
				pageSizeList: [10, 20, 30],
				total: 0,
			},
			enterprise_id: 0, //企业 ID
			tableData: [],
		});
		const onSearch = () => {
			state.param.page = 1;
			state.param.keyword = '';
			initTableData();
		};
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.param.pageSize = val;
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.param.page = val;
		};
		// 删除用户
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.user.real_name}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					ElMessage.success('删除成功');
				})
				.catch(() => {});
		};
		/**
		 * 编辑或新增
		 */
		const editUser = (id: number = 0, userInfo: any = null) => {
			console.log(tagInfo, 'editUser', id, accountEdit.value);
			accountEdit.value.openDialog(state.enterprise_id, id, userInfo);
		};
		// 打开弹窗
		const openDialog = (enterprise_id: number = 0) => {
			state.param.enterprise_id = state.enterprise_id = enterprise_id;
			onSearch();
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			closeDialog();
		};
		const initTableData = async () => {
			let res = await User.staffList(state.param);
			console.log(tagInfo, 'initTAbleData', res);
			if (res.code != 0) {
				return;
			}
			state.tableData = res.data.data;
			state.param.total = res.data.total;
			console.log('state', state, state.tableData);
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			editUser,
			accountEdit,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			onHandleCurrentChange,
			onHandleSizeChange,
			initTableData,
			onSearch,
			...toRefs(state),
		};
	},
});
</script>
