<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
			<el-form-item label="船名" prop="iboatName">
				<el-input v-model="queryParams.iboatName" placeholder="请输入船名" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="船名-拼音" prop="iboatNameSpell">
				<el-input v-model="queryParams.iboatNameSpell" placeholder="请输入船名-拼音" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="设备sn" prop="sn">
				<el-input v-model="queryParams.sn" placeholder="请输入设备sn" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="状态" prop="state">
				<el-input v-model="queryParams.state" placeholder="请输入状态" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="ip" prop="ip">
				<el-input v-model="queryParams.ip" placeholder="请输入ip" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="有效期" prop="termOfValidity">
				<el-date-picker clearable v-model="queryParams.termOfValidity" type="date" value-format="YYYY-MM-DD"
					placeholder="请选择有效期">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="归属部门" prop="deptId">
				<el-input v-model="queryParams.deptId" placeholder="请输入归属部门" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="是否航行" prop="isSail">
				<el-input v-model="queryParams.isSail" placeholder="请输入是否航行" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="仓容数" prop="storageCapacityNum">
				<el-input v-model="queryParams.storageCapacityNum" placeholder="请输入仓容数" clearable
					@keyup.enter="handleQuery" />
			</el-form-item> -->
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @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" @click="handleAdd" v-hasPermi="['business:config:add']">新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
					v-hasPermi="['business:config:edit']">修改
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
					v-hasPermi="['business:config:remove']">删除
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport"
					v-hasPermi="['business:config:export']">导出
				</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<!-- <el-table-column label="主键id" align="center" prop="id" /> -->
			<el-table-column label="船名" align="center" prop="iboatName" />
			<el-table-column label="船名-拼音" align="center" prop="iboatNameSpell" />
			<el-table-column label="设备sn" align="center" prop="sn" />
			<el-table-column label="状态" align="center" prop="state" />
			<el-table-column label="ip" align="center" prop="ip" />
			<el-table-column label="有效期" align="center" prop="termOfValidity" width="180">
				<template #default="scope">
					<span>{{ parseTime(scope.row.termOfValidity, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<!-- <el-table-column label="归属部门" align="center" prop="deptId" /> -->
			<el-table-column label="归属部门" align="center" key="deptName" prop="dept.deptName"
				:show-overflow-tooltip="true" />
			<el-table-column label="配置" align="center" prop="sheet" />
			<el-table-column label="是否航行" align="center" prop="isSail" />
			<el-table-column label="仓容数" align="center" prop="storageCapacityNum" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
				<template #default="scope">
					<!-- <el-button link type="primary" icon="View" @click="handleView(scope.row)"
						v-hasPermi="['business:config:query']">详细
					</el-button> -->

					<el-button link type="primary" icon="VideoCamera" @click="handleUpdateCameraConfig(scope.row)"
						v-hasPermi="['cameraConfig:cameraConfig:list']">配置摄像头
					</el-button>
					<!-- <el-button link type="primary" icon="User" @click="handleUpdateUserConfig(scope.row)"
						v-hasPermi="['system:user:list']">配置用户
					</el-button> -->
					<el-button link type="primary" icon="OfficeBuilding" @click="handleUpdateDeptConfig(scope.row)"
						v-hasPermi="['system:dept:list']">配置部门
					</el-button>
					
					
					<el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['business:config:edit']">修改
					</el-button>
					<el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
						v-hasPermi="['business:config:remove']">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>

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

		<!-- 添加或修改设备配置对话框 -->
		<el-dialog :title="title" v-model="open" width="500px" append-to-body>
			<el-form ref="configRef" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="船名" prop="iboatName">
					<el-input v-model="form.iboatName" placeholder="请输入船名" />
				</el-form-item>
				<el-form-item label="船名-拼音" prop="iboatNameSpell">
					<el-input v-model="form.iboatNameSpell" placeholder="请输入船名-拼音" />
				</el-form-item>
				<el-form-item label="设备sn" prop="sn">
					<el-input v-model="form.sn" placeholder="请输入设备sn" />
				</el-form-item>
				<el-form-item label="状态" prop="state">
					<el-input v-model="form.state" placeholder="请输入状态" />
				</el-form-item>
				<el-form-item label="ip" prop="ip">
					<el-input v-model="form.ip" placeholder="请输入ip" />
				</el-form-item>
				<el-form-item label="有效期" prop="termOfValidity">
					<el-date-picker clearable v-model="form.termOfValidity" type="date" value-format="YYYY-MM-DD"
						placeholder="请选择有效期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="归属部门" prop="deptId">
					<el-tree-select v-model="form.deptId" :data="deptOptions"
						:props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
						placeholder="请选择归属部门" check-strictly />
					<!-- <el-input v-model="form.deptId" placeholder="请输入归属部门" /> -->
				</el-form-item>
				<el-form-item label="配置" prop="sheet">
					<el-input v-model="form.sheet" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="是否航行" prop="isSail">
					<el-input v-model="form.isSail" placeholder="请输入是否航行" />
				</el-form-item>
				<el-form-item label="仓容数" prop="storageCapacityNum">
					<el-input v-model="form.storageCapacityNum" placeholder="请输入仓容数" type="number" :min="1" />
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
		
		
		
		
		
		
		<!-- 配置部门对话框 -->
		<el-dialog :title="title" v-model="openDeptConfig" width="500px" append-to-body>
		
		
		
		</el-dialog>
		
		
		
	</div>





</template>

<script setup name="Config">
	import {
		listConfig,
		getConfig,
		delConfig,
		addConfig,
		updateConfig
	} from "@/api/business/config";
	import {
		treeselect
	} from "@/api/system/dept";

	const {
		proxy
	} = getCurrentInstance();
	const configList = ref([]);
	const open = ref(false);
	const openUserConfig = ref(false);
	const openDeptConfig = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const deptOptions = ref(undefined);

	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			iboatName: null,
			iboatNameSpell: null,
			sn: null,
			state: null,
			ip: null,
			termOfValidity: null,
			deptId: null,
			sheet: null,
			isSail: null,
			storageCapacityNum: null
		},
		rules: {
			iboatName: [{
				required: true,
				message: "船名不能为空",
				trigger: "blur"
			}],
			sn: [{
				required: true,
				message: "设备sn不能为空",
				trigger: "blur"
			}],
			storageCapacityNum: [{
				required: true,
				message: "仓容数不能为空",
				trigger: "blur"
			}],
		}
	});

	const {
		queryParams,
		form,
		rules
	} = toRefs(data);

	/** 查询设备配置列表 */
	function getList() {
		loading.value = true;
		listConfig(queryParams.value).then(response => {
			configList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	function addList() {
		loading.value = true;
		listConfig(queryParams.value)
			.then(response => {
				configList.value.push(...response.rows);
				total.value = response.total;
				if (configList.value.length >= total.value) finished.value = true;
				loading.value = false;
			})
			.catch(response => {
				loading.value = false;
			});
	}

	// 下拉刷新
	function handlePullDownRefresh() {
		queryParams.value.pageNum = 1;
		configList.value = [];
		addList();
	}

	// 滚动到底部的事件
	function handleReachBottom() {
		queryParams.value.pageNum++;
		addList();
	}

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

	// 表单重置
	function reset() {
		form.value = {
			id: null,
			iboatName: null,
			iboatNameSpell: null,
			sn: null,
			state: null,
			ip: null,
			termOfValidity: null,
			deptId: null,
			sheet: null,
			isSail: null,
			storageCapacityNum: 1
		};
		proxy.resetForm("configRef");
	}

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

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

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

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加设备配置";
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const id =
			row.configId || ids.value
		getConfig(id).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改设备配置";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["configRef"].validate(valid => {
			if (valid) {
				if (form.value.configId != null) {
					updateConfig(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addConfig(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const ids = row.configId || ids.value;
		proxy.$modal.confirm('是否确认删除设备配置编号为"' + ids + '"的数据项？').then(function() {
			return delConfig(ids);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		downloadConfig(queryParams.value)
		// proxy.download('business/config/export', {
		//   ...queryParams.value
		// }, `config_${new Date().getTime()}.xlsx`)
	}

	/** 详细按钮操作 */
	function handleView(row) {}


	const handleUpdateCameraConfig = (row) => {
		proxy.$router.push(`/data/cameraConfig1?sn=${row.sn}`)
	}

	/** 查询部门下拉树结构 */
	function getTreeselect() {
		treeselect().then((response) => {
			deptOptions.value = response.data;
		});
	}


	const handleUpdateUserConfig = (row) => {
		openUserConfig.value = true;
	}
	
	const handleUpdateDeptConfig = (row) => {
		title.value = "配置部门";
		openDeptConfig.value = true;
	}
	
	

	queryParams.value.state = 2;
	getTreeselect();
	getList();
</script>