<template>
	<div class="app-container">
		<el-form
			:model="queryParams"
			ref="queryForm"
			:inline="true"
			v-show="showSearch"
			label-width="68px"
		>
			<el-form-item label="名称" prop="name">
				<el-input
					v-model="queryParams.name"
					placeholder="请输入表达式名称"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select
					v-model="queryParams.status"
					placeholder="请选择状态"
					clearable
				>
					<el-option
						v-for="dict in sys_common_status"
						:key="dict.value"
						:label="dict.label"
						:value="dict.value"
					/>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button
					type="primary"
					:icon="Search"
					size="default"
					@click="handleQuery"
					>搜索</el-button
				>
				<el-button
					:icon="Refresh"
					size="default"
					@click="resetQuery"
					>重置</el-button
				>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="primary"
					plain
					:icon="Plus"
					size="small"
					@click="handleAdd"
					v-hasPermi="['flowable:expression:add']"
					>新增</el-button
				>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					plain
					:icon="Edit"
					size="small"
					:disabled="single"
					@click="handleUpdate"
					v-hasPermi="['flowable:expression:edit']"
					>修改</el-button
				>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					plain
					:icon="Delete"
					size="small"
					:disabled="multiple"
					@click="handleDelete"
					v-hasPermi="['flowable:expression:remove']"
					>删除</el-button
				>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="warning"
					plain
					:icon="Download"
					size="small"
					@click="handleExport"
					v-hasPermi="['flowable:expression:export']"
					>导出</el-button
				>
			</el-col>
			<right-toolbar
				:showSearch.sync="showSearch"
				@queryTable="getList"
			></right-toolbar>
		</el-row>

		<el-table
			v-loading="loading"
			:data="expressionList.value"
			@selection-change="handleSelectionChange"
		>
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="主键" align="center" prop="id" />
			<el-table-column label="名称" align="center" prop="name" />
			<el-table-column
				label="表达式内容"
				align="center"
				prop="expression"
			/>
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column
				label="操作"
				align="center"
				class-name="small-padding fixed-width"
			>
				<template #default="scope">
					<el-button
						size="mini"
						type="text"
						:icon="Edit"
						@click="handleUpdate(scope.row)"
						v-hasPermi="['flowable:expression:edit']"
						>修改</el-button
					>
					<el-button
						size="mini"
						type="text"
						:icon="Delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['flowable:expression:remove']"
						>删除</el-button
					>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total > 0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 添加或修改流程达式对话框 -->
		<el-dialog
			v-model="open"
			:title="title"
			width="500px"
		>
			<el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="名称" prop="name">
					<el-input
						v-model="form.name"
						placeholder="请输入表达式名称"
					/>
				</el-form-item>
				<el-form-item label="内容" prop="expression">
					<el-input
						v-model="form.expression"
						placeholder="请输入表达式内容"
					/>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-radio-group v-model="form.status">
						<el-radio
							v-for="dict in sys_common_status"
							:key="dict.value"
							:label="parseInt(dict.value)"
							>{{ dict.label }}</el-radio
						>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" placeholder="请输入备注" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm(formRef)">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script lang="ts" name="FlowExp" setup>
import {
	listExpression,
	getExpression,
	delExpression,
	addExpression,
	updateExpression,
} from "@/api/flowable/expression";
import {
	ref,
	reactive,
	watchEffect,
	getCurrentInstance,
} from "vue";
import { Search, Refresh, Plus, Edit, Delete, Download } from '@element-plus/icons-vue'
import type { FormInstance, FormRules, ElForm } from "element-plus";

// 引入getCurrentInstance,使用全局方法,并且类型断言
const { proxy } = getCurrentInstance() as any;
const { sys_common_status } = proxy.useDict("sys_common_status");

const queryForm = ref<InstanceType<typeof ElForm>>();
const formRef = ref<FormInstance>()

// 遮罩层
const loading = ref<boolean>(true);
// 选中数组
const ids = reactive({
	value: [],
});
// 非单个禁用
const single = ref<boolean>(true);
// 非多个禁用
const multiple = ref<boolean>(true);
// 显示搜索条件
const showSearch = ref<boolean>(true);
// 总条数
const total = ref<number>(0);
// 流程达式表格数据
const expressionList = reactive({
	value: [],
});
// 弹出层标题
const title = ref<string>("");
// 是否显示弹出层
const open = ref<boolean>(false);
// 查询参数
const queryParams = reactive({
	pageNum: 1,
	pageSize: 10,
	name: null,
	expression: null,
	status: '',
});
// 表单参数
const form = reactive({
	id: null,
	name: null,
	expression: null,
	createTime: null,
	updateTime: null,
	createBy: null,
	updateBy: null,
	status: '',
	remark: null,
});
// 表单校验
const rules = reactive<FormRules>({})

/** 查询流程达式列表 */
const getList = () => {
	loading.value = true;
	listExpression(queryParams).then((res: any) => {
		if (res.code === 200) {
			// prettier-ignore
			expressionList.value = res.rows;
			total.value = res.total;
			loading.value = false;
		}
	});
};
getList();

// 取消按钮
const cancel = () => {
	open.value = false;
	reset();
};

// 表单重置
const reset = () => {
	proxy.resetForm(formRef);
};

/** 搜索按钮操作 */
const handleQuery = () => {
	queryParams.pageNum = 1;
	getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
	proxy.resetForm(queryForm);
	handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection: any) => {
	ids.value = selection.map((item: any) => item.id);
	single.value = selection.length !== 1;
	multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
	reset();
	open.value = true;
	title.value = "添加流程达式";
};

/** 修改按钮操作 */
const handleUpdate = (row: any) => {
	reset();
	const id = row.id || ids.value;
	getExpression(id).then((response) => {
		form.id = response.data.id;
		form.name = response.data.name;
		form.expression = response.data.expression;
		form.createTime = response.data.createTime;
		form.updateTime = response.data.updateTime;
		form.createBy = response.data.createBy;
		form.updateBy = response.data.updateBy;
		form.status = response.data.status;
		form.remark = response.data.remark;
		open.value = true;
		title.value = "修改流程达式";
	});
};

/** 提交按钮 */
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      if (form.id != null) {
        updateExpression(form).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addExpression(form).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  })
}

/** 删除按钮操作 */
const handleDelete = (row: any) => {
	const id = row.id || ids.value;
	proxy.$modal
		.confirm('是否确认删除流程达式编号为"' + id + '"的数据项？')
		.then(function () {
			return delExpression(id);
		})
		.then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		})
		.catch(() => {});
}

/** 导出按钮操作 */
const handleExport = () => {
	proxy.download(
		"flowable/expression/export",
		{
			...queryParams,
		},
		`expression_${new Date().getTime()}.xlsx`
	);
};
</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
	justify-content: flex-end;
	.el-button:nth-child(2) {
		margin-right: 10px;
	}
}
</style>
