<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="90px">
            <el-form-item label="报警名称" prop="alarmName">
                <el-input v-model="queryParams.alarmName" 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-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
				<el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
            </el-form-item>
        </el-form>
        <el-card shadow="never" :body-style="{padding: '0px'}">
            <el-table v-loading="loading" :data="alarmConfigList" 
				:header-cell-style="{ color: 'var(--white)',background: '#30B08E' }"
				@selection-change="handleSelectionChange">
                <el-table-column label="序号" align="center" type="index" width="50" />
                <el-table-column label="编号" align="center" prop="alarmId" show-overflow-tooltip />
				<el-table-column label="报警名称" align="center" prop="alarmName" width="100"></el-table-column>
                <el-table-column label="最小值" align="center" prop="alarmMinValue"/>
				<el-table-column label="最大值" align="center" prop="alarmMaxValue"/>
				<el-table-column label="持续时间" align="center" prop="durationMinutes"/>
                <el-table-column label="传感器" align="center" prop="sensorId">
					<template #default="scope">
						<TextTag :options="sensorOptions" column="sensorId" label="sensorName" :value="scope.row.sensorId" />
					</template>
				</el-table-column>
                <el-table-column label="备注" align="center" prop="remark" />
					
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200"
                    fixed="right">
                    <template #default="scope">
                        <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)">
							修改
                        </el-button>
                        <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">
							删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <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="alarmRef" :model="form" :rules="rules" label-width="120px">
				<el-form-item label="传感器" prop="sensorId" class="flex">
					<el-select v-model="form.sensorId" placeholder="请选择">
						<template v-for="item in sensorOptions" :key="item.sensorId">
							<el-option :label="item.sensorName" :value="item.sensorId"></el-option>
						</template>
					</el-select>
				</el-form-item>
                <el-form-item label="报警名称" prop="alarmName">
                    <el-input v-model="form.alarmName" placeholder="请输入报警名称" />
                </el-form-item>
				<el-form-item label="最小值" prop="alarmMinValue">
					<el-input v-model="form.alarmMinValue" placeholder="请输入最小值" />
				</el-form-item>
				<el-form-item label="最大值" prop="alarmMaxValue">
					<el-input v-model="form.alarmMaxValue" placeholder="请输入最大值" />
				</el-form-item>
				<el-form-item label="持续时间" prop="durationMinutes">
					<el-input v-model="form.durationMinutes" placeholder="请输入持续时间" />
				</el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注" />
                </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>
    </div>
</template>

<script setup name="Certificate">
import { listAlarmConfig, getAlarmConfig, updateAlarmConfig, addAlarmConfig, delAlarmConfig, downloadAlarmConfig } from "@/api/Incinerator/alarmApi";
import { listSensor } from "@/api/Incinerator/sensorApi";

const { proxy } = getCurrentInstance();
const alarmConfigList = ref([]);
const open = ref(false);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const sensorOptions = ref(undefined);
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        alarmName: null
    },
    rules: {
        sensorId: [{
            required: true, message: "传感器不能为空", trigger: "blur"
        }],
        alarmName: [{
			required: true, message: "报警名称不能为空", trigger: "blur"
		}],
        alarmMinValue: [{
			required: true, message: "最小值不能为空", trigger: "blur"
		}],
		alarmMaxValue: [{
			required: true, message: "最大值不能为空", trigger: "blur"
		}],
		durationMinutes: [{
			required: true, message: "持续时间不能为空", trigger: "blur"
		}],
    }
});

const { queryParams, form, rules } = toRefs(data);
const baseUrl = import.meta.env.VITE_APP_BASE_API;

/** 查询证书列表 */
function getList() {
    loading.value = true;
    listAlarmConfig(queryParams.value).then(response => {
        alarmConfigList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

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

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

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

// 表单重置
function reset() {
    form.value = {
		alarmId: null,
		sensorId: null,
		alarmName: null,
		alarmMinValue: 0,
		alarmMaxValue: 100,
		durationMinutes: 0,
		remark: null
    };
    proxy.resetForm("alarmRef");
}

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

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

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.alarmId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加报警项";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const alarmIds =
        row.alarmId || ids.value
    getAlarmConfig(alarmIds).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改报警项";
    });
}

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

/** 删除按钮操作 */
function handleDelete(row) {
    const alarmIds = row.alarmId || ids.value;
    proxy.$modal.confirm('是否确认删除报警项为"' + row.alarmName + '"的数据项？').then(function () {
        return delAlarmConfig(alarmIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    downloadAlarmConfig(queryParams.value)
}

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

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

function initialData() {
    getTreeselect();
    getList();
}

onActivated(() => {
    initialData();
})
onMounted(() => {
    initialData();
})
</script>

