<template>
	<div class="page">
		<el-card shadow="never">
			<div class="search">
				<el-form :model="params" inline ref="queryRef">
					<!-- 模板名称 -->
					<el-form-item label="模板名称" prop="title">
						<!-- 请输入模板名称 -->
						<el-input v-model="params.title" placeholder="请输入模板名称" clearable style="width: 240px" @keyup.enter.native="getList(1)" />
					</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 @click="resetQuery()">
							<el-icon>
								<ele-Refresh />
							</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>
			</div>
			<el-table :data="tableData" style="width: 100%" v-loading="loading">
				<!-- 序号 -->
				<el-table-column type="index" label="序号" width="80" align="center" />
				<!-- 模板名称 -->
				<el-table-column prop="title" label="模板名称" align="center" show-overflow-tooltip></el-table-column>
				<!-- 模式 -->
				<el-table-column prop="mode" label="模式" align="center" width="120" show-overflow-tooltip>
					<template #default="{ row }">
						<!-- 顺序读取/批量读取 -->
						{{ row.mode === 0 ? '顺序读取' : '批量读取' }}
					</template>
				</el-table-column>
				<!-- 备注 -->
				<el-table-column prop="remarks" label="备注" align="center" show-overflow-tooltip></el-table-column>
				<!-- 操作 -->
				<el-table-column label="操作" width="160" align="center">
					<template #default="scope">
						<!-- 导入 -->
						<el-button size="small" text type="primary" v-auth="'upload'" @click="handleImport(scope.row)">导入</el-button>
						<!-- 导出 -->
						<el-button size="small" text type="primary" v-auth="'download'" @click="handleExport(scope.row)">导出</el-button>
						<!-- 详情 -->
						<el-button size="small" text type="primary" v-auth="'detail'" @click="addOrEdit(scope.row)">详情</el-button>
						<!-- 删除 -->
						<el-button size="small" text type="info" v-auth="'del'" @click="onDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-if="params.total" :total="params.total" v-model:page="params.page" v-model:limit="params.size" @pagination="getList()" />

			<EditForm ref="editFormRef" @getList="getList(1)"></EditForm>
			<ImportFile ref="importFile" />
		</el-card>
	</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import EditForm from './component/edit.vue';
import ImportFile from './component/importFile.vue';
import api from '/@/api/device/modbus';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useSearch } from '/@/hooks/useCommonModbus';
import { getOtherServersOrigin } from '/@/utils/origin'

const importFile = ref();
const editFormRef = ref();
const queryRef = ref();

const { params, tableData, getList, loading } = useSearch(api.template.getList, 'list', { title: '', number: '' });

getList();

const addOrEdit = async (row?: any) => {
	editFormRef.value.open(row ? 'update' : 'create', row);
};

const handleImport = async (row: any) => {
	importFile.value.templateNumber = row.number;
	importFile.value.openDialog();
};

const handleExport = async (row: any) => {
	window.open(getOtherServersOrigin(import.meta.env.VITE_MODBUS_API + '/data_area/export?number=' + row.number))
};

// 重置表单
const resetQuery = () => {
	queryRef.value.resetFields();
	getList(1);
};

const onDel = (row: any) => {
	// `此操作将删除模板：“${row.title}”，是否继续?
	ElMessageBox.confirm(
		`此操作将删除模板：“${row.title}”，是否继续?`,
		// 提示
		'提示',
		{
			// 确认
			confirmButtonText: '确认',
			// 取消
			cancelButtonText: '取消',
			type: 'warning',
		}).then(async () => {
			await api.template.deleteTemplate({ number: row.number });
			ElMessage.success('删除成功');
			getList();
		});
};
</script>
