<template>
	<div class="page">
		<el-card shadow="nover">
			<el-form :model="params" inline ref="queryRef">
				<el-form-item label="名称搜索" prop="name">
					<el-input v-model="params.name" placeholder="请输入租户名称" clearable style="width: 180px" @keyup.enter.native="getList(1)" />
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-select v-model="params.status" placeholder="租户状态" clearable style="width: 120px">
						<el-option label="全部" :value="-1" />
						<el-option label="启用" :value="1" />
						<el-option label="禁用" :value="0" />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" class="ml10" @click="getList(1)">
						<el-icon>
							<ele-Search />
						</el-icon>
						查询
					</el-button>
					<el-button type="primary" @click="addOrEdit()" v-auth="'add'">
						<el-icon>
							<ele-FolderAdd />
						</el-icon>
						新增授权配置
					</el-button>
				</el-form-item>
			</el-form>
			<el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%" v-loading="loading" row-key="id">
				<el-table-column prop="name" label="名称" width="200" align="center" />
				<el-table-column prop="appId" label="APPID" min-width="120" align="center" show-overflow-tooltip />
				<el-table-column prop="callbackUrl" label="回调地址" min-width="120" align="center" show-overflow-tooltip />
				<el-table-column prop="types" label="应用类型" width="150" align="center" :formatter="(row: any) => row.types ? '内部独立应用' : '第三方应用'" />
				<el-table-column prop="status" label="状态" width="100" align="center">
					<template #default="scope">
						<el-switch v-model="scope.row.status" inline-prompt :active-value="1" :inactive-value="0" active-text="启" inactive-text="禁" @change="handleStatusChange(scope.row)">
						</el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="120" align="center">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="view(scope.row)">查看</el-button>
						<el-button size="small" text type="warning" @click="addOrEdit(scope.row)" v-auth="'edit'">修改</el-button>
						<el-button size="small" text type="info" @click="onDel(scope.row)" v-auth="'del'">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />
		</el-card>
		<EditForm ref="editFormRef" @getList="getList(1)"></EditForm>
		<viewVue ref="viewRef"></viewVue>
	</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import EditForm from './edit.vue'
import viewVue from './view.vue'
import api from '/@/api/system'
import { ElMessageBox, ElMessage } from 'element-plus'
import { useSearch } from '/@/hooks/useCommon'

const editFormRef = ref()
const viewRef = ref()
const queryRef = ref()
const ids = ref<number[]>([])

const { params, tableData, getList, loading } = useSearch(api.sso.list, 'Data', { name: '', keyWord: '', status: -1 })

getList()

const addOrEdit = async (row?: any) => {
	if (row) {
		editFormRef.value.open(row)
		return
	} else {
		editFormRef.value.open()
	}
}

const view = async (row?: any) => {
	viewRef.value.open(row)
}

const handleSelectionChange = (selection: any[]) => {
	ids.value = selection.filter(item => item.types === 2).map((item) => item.id);
};

const onDel = (row: any) => {
	ElMessageBox.confirm(`此操作将删除接口：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(async () => {
		await api.sso.del([row.id as number])
		ElMessage.success('删除成功')
		getList()
	})
}

// 状态修改
const handleStatusChange = (row: any) => {
	let text = row.status === 1 ? '启用' : '停用';
	ElMessageBox.confirm('确认要"' + text + '"："' + row.name + '"吗?', '警告', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(function () {
			return api.sso.editStatus(row.id, row.status);
		})
		.then(() => {
			ElMessage.success(text + '成功');
		})
		.catch(function () {
			row.status = row.status === 0 ? 1 : 0;
		});
};
</script>
