<template>
	<div class="dataShare-userInfo-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" @submit.native.prevent inline>
				<el-form-item label="平台名称" prop="Keyword">
					<el-input v-model="state.queryParams.Keyword" placeholder="平台名称" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label-width="0">
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: queryList,
				}"
			>
				<template #buttons>
					<el-button type="primary" @click="onAdd(false)" :loading="state.loading" title="新增账号">
						<SvgIcon name="ele-Plus" class="mr5" />新增账号
					</el-button>
				</template>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="dataShareUserInfo"
					:custom-config="{ storage: true }"
				>
					<vxe-column field="accountId" title="账号"></vxe-column>
					<vxe-column field="accountSecret" title="密码"></vxe-column>
					<vxe-column field="platformSource" title="所属平台code"></vxe-column>
					<vxe-column field="platformSourceName" title="所属平台名称"></vxe-column>
					<vxe-column field="addressUrl" title="数据推送地址"></vxe-column>
					<vxe-column field="state" title="状态" width="160">
						<template #default="{ row }">
							{{ row.state === '0' ? '未启用' : row.state === '1' ? '正常' : '禁用' }}
						</template>
					</vxe-column>
					<vxe-column field="remark" title="备注"></vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
		<el-dialog title="新增账号" v-model="state.dialogVisible" destroy-on-close append-to-body draggable>
			<el-form :model="state.form" ref="formRef" label-width="120px">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="账号" prop="accountId" :rules="[{ required: true, message: '请填写账号', trigger: 'blur' }]">
							<el-input v-model="state.form.accountId" placeholder="请填写账号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="密码" prop="accountSecret" :rules="[{ required: true, message: '请填写密码', trigger: 'blur' }]">
							<el-input v-model="state.form.accountSecret" placeholder="请填写密码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="所属平台code" prop="platformSource" :rules="[{ required: true, message: '请填写所属平台code', trigger: 'blur' }]">
							<el-input v-model="state.form.platformSource" placeholder="请填写所属平台code" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="数据推送地址" prop="addressUrl" :rules="[{ required: false, message: '请填写数据推送地址', trigger: 'blur' }]">
							<el-input v-model="state.form.addressUrl" placeholder="请填写数据推送地址" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item
							label="所属平台名称"
							prop="platformSourceName"
							:rules="[{ required: true, message: '请填写所属平台名称', trigger: 'blur' }]"
						>
							<el-input v-model="state.form.platformSourceName" placeholder="请填写所属平台名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
						<el-form-item label="备注" prop="remark" :rules="[{ required: false, message: '请填写备注', trigger: 'blur' }]">
							<el-input v-model="state.form.remark" placeholder="请填写备注" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<el-button @click="state.dialogVisible = false" class="default-button">取 消</el-button>
				<el-button type="primary" @click="onSubmit(formRef)" :loading="state.loading">确 定</el-button>
			</template>
		</el-dialog>
	</div>
</template>
<script setup lang="tsx" name="dataShareUserInfo">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage } from 'element-plus';
import { getUserList, platformSourceAddUser } from '@/api/dataShare';

// 引入组件
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	dialogVisible: false,
	form: {},
});
const ruleFormRef = ref<RefType>(); // 表单ref
// 手动查询，将页码设置为1
const handleQuery = () => {
	state.queryParams.PageIndex = 1;
	queryList();
};
/** 获取列表 */
const queryList = () => {
	state.loading = true;
	getUserList(state.queryParams)
		.then((response: any) => {
			state.tableData = response?.result.items ?? [];
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
};
// 新增账号
const onAdd = (row: any) => {
	state.dialogVisible = true;
};
const formRef = ref<RefType>();
const onSubmit = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.validate((valid: boolean) => {
		if (!valid) return;
		state.loading = true;
		platformSourceAddUser(state.form)
			.then(() => {
				state.loading = false;
				state.dialogVisible = false;
				queryList();
				ElMessage.success('操作成功');
			})
			.catch(() => {
				state.loading = false;
			});
	});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
