<template>
	<div class="app-container">
		<el-card shadow="always">
			<!--查询-->
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item label="函数名称" prop="name">
					<el-input placeholder="名称模糊查询" clearable @keyup.enter="handleQuery" style="width: 240px"
						v-model="state.queryParams.name" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" plain @click="handleQuery">
						<SvgIcon name="elementSearch" />
						搜索
					</el-button>
					<el-button @click="resetQuery">
						<SvgIcon name="elementRefresh" />
						重置
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="box-card">
			<template #header>
				<div class="card-header">
					<span class="card-header-text">函数应用</span>
					<div>
						<el-button type="primary" plain @click="state.open = true">
							<SvgIcon name="elementPlus" />
							新增
						</el-button>
					</div>
				</div>
			</template>
			<!--数据表格-->
			<el-table v-loading="state.loading" :data="state.tableData">
				<el-table-column label="函数名称" prop="name" :show-overflow-tooltip="true" />
				<el-table-column label="别名" prop="alias" :show-overflow-tooltip="true" />
				<el-table-column label="状态" prop="status" :show-overflow-tooltip="true">
					<template #default="scope">
						<el-tag v-if="scope.row.status == 1" type="success">已上线</el-tag>
						<el-tag v-else type="danger">开发中</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="创建用户" prop="proprietor_name" :show-overflow-tooltip="true" />
				<el-table-column label="创建时间" prop="create_time" />
				<el-table-column label="更新时间" prop="update_time" />
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
					<template #default="scope">
						<div style="display: flex; align-items: center; justify-content: center; gap: 10px">
							<div>
								<el-tooltip class="box-item" effect="dark" content="编辑" placement="top-start">
									<el-button type="primary" size="small" circle @click="handleEdit(scope.row)">
										<SvgIcon :size="12" name="elementEdit" />
									</el-button>
								</el-tooltip>
							</div>
							<div>
								<el-tooltip class="box-item" effect="dark" content="删除" placement="top-start">
									<el-button type="danger" size="small" circle @click="onTableRowDel(scope.row)">
										<SvgIcon :size="12" name="elementDelete" />
									</el-button>
								</el-tooltip>
							</div>
						</div>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页设置-->
			<div v-show="state.count > 0">
				<el-divider></el-divider>
				<el-pagination background :total="state.count" :current-page="state.queryParams.pageNum"
					:page-size="state.queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
					@size-change="handleSizeChange" @current-change="handleCurrentChange" />
			</div>
		</el-card>
	</div>

	<!-- 函数编辑框 -->
	<el-dialog :title="state.title" v-model="state.open" width="70%" center>
		<el-form ref="roleFormRef" :model="state.roleForm" label-width="120px" label-position="top">
			<el-form-item label="应用编码" prop="fnCode">
				<el-input disabled v-model="state.roleForm.fnCode" placeholder="自动生成" />
			</el-form-item>
			<el-form-item label="应用名称" prop="name">
				<el-input v-model="state.roleForm.name" placeholder="" />
			</el-form-item>
			<el-form-item label="应用别名" prop="alias">
				<el-input v-model="state.roleForm.alias" controls-position="right" :min="0" />
			</el-form-item>
			<el-form-item label="代码编辑框">
				<codemirror v-model="state.roleForm.code" placeholder="Code goes here..."
					:style="{ height: '100%', width: '100%' }" :autofocus="true" :spellcheck="true" :smartIndent="true"
					:indentWithTab="true" :styleActiveLine="true" :autoCloseBrackets="true" :highlightDifferences="true"
					:showCursorWhenSelecting="true" :autorefresh="true" :foldGutter="true" :tab-size="2"
					:extensions="extensions" @ready="handleReady" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="handleOnline">确认上线</el-button>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" :loading="state.bunLoading" @click="submitForm">确 定</el-button>
				<el-button @click="state.open = false">取 消</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script lang="ts" setup name="Partner">
import { reactive, onMounted, getCurrentInstance, shallowRef, onUnmounted, watch } from 'vue';
import { list, del, add, update, getDetail } from '@/api/system/fn';
import { useRouter } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
import { Codemirror } from 'vue-codemirror';
import { javascript } from '@codemirror/lang-javascript';
import { oneDark } from '@codemirror/theme-one-dark';

const extensions = [javascript(), oneDark];
const { proxy } = getCurrentInstance() as any;
const view = shallowRef();
const handleReady = (payload) => {
	view.value = payload.view;
};
const state = reactive({
	// 遮罩层
	loading: true,
	// 选中数组
	ids: [],
	// 非单个禁用
	single: true,
	// 非多个禁用
	multiple: true,
	// 弹出层标题
	title: '',
	// 表格数据
	tableData: [],
	// 总条数
	count: 0,
	open: false,
	roleForm: {},
	curQueryParams: {},
	queryParams: {
		// 页码
		pageNum: 1,
		// 每页大小
		pageSize: 10,
	},
});


watch(()=>state.open,(newVal)=>{
	if(!newVal){
		state.roleForm = {}
	}
})

/** 查询列表 */
const handleQuery = () => {
	state.loading = true;
	// 如果state.queryParams中有空值，则删除
	const params = state.queryParams
	Object.keys(params).forEach((key) => {
		if (!params[key]) {
			delete params[key];
		}
	});
	list(params).then((response: any) => {
		state.tableData = response.data.result;
		state.count = response.data.count;
		state.loading = false;
	});
};

const handleSizeChange = (val: number) => {
	state.queryParams.pageSize = val;
	handleQuery();
};
const handleCurrentChange = (val: number) => {
	state.queryParams.pageNum = val;
	handleQuery();
};
const handleEdit = (row) => {
	state.title = '编辑';
	state.open = true;
	getDetail(row.fn_id).then((response) => {
		if (response.status == 0) {
			const data = response.data && response.data[0];
			state.roleForm = data;
		} else {
			ElMessage.error(response.msg);
		}
	});
};

const submitForm = (row: object) => {
	if (!verForm()) return
	if (state.roleForm.fn_id) {
		update(state.roleForm.fn_id, state.roleForm).then((response) => {
			if (response.status == 0) {
				ElMessage.success('保存成功');
				state.open = false;
				handleQuery();
			} else {
				ElMessage.error(response.msg);
			}
		});
	} else {
		add(state.roleForm).then((response) => {
			if (response.status == 0) {
				ElMessage.success('保存成功');
				state.open = false;
				handleQuery();
			} else {
				ElMessage.error(response.msg);
			}
		});
	}
};


const handleOnline = (row: object) => {
	update(state.roleForm.fn_id, { status: 1, }).then((response) => {
		if (response.status == 0) {
			ElMessage.success('上线成功');
			state.open = false;
			handleQuery();
		} else {
			ElMessage.error(response.msg);
		}
	});
}

const verForm = () => {
	if (!state.roleForm.alias) {
		ElMessage.error('别名不能为空');
		return false;
	} else if (!state.roleForm.name) {
		ElMessage.error('名称不能为空');
		return false;
	} else if (!state.roleForm.code) {
		ElMessage.error('代码不能为空');
		return false;
	}
	return true;
}





/** 删除按钮操作 */
const onTableRowDel = (row: any) => {
	ElMessageBox({
		message: '是否确认删除的数据项?',
		title: '警告',
		showCancelButton: true,
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(function () {
		return del(row.fn_id).then(() => {
			handleQuery();
			ElMessage.success('删除成功');
		});
	});
};

// 页面加载时
onMounted(() => {
	handleQuery();
});
// 页面卸载时
onUnmounted(() => {
	proxy.mittBus.off('onEditPartnerModule');
});
</script>
