<template>
    <div class="app-container app-container-bg">
        <el-card class="first-card" ref='firstCard' shadow="always">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" @submit.native.prevent>
                <el-form-item label="测站名称" prop="stnm">
                    <el-input v-model="queryParams.stnm" placeholder="请输入测站名称" clearable @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="故障类型" prop="errorId">
                    <el-select v-model="queryParams.errorId" placeholder="请选择故障类型" @change="handleQuery">
                        <el-option v-for="t in errorList" :key="t.id" :label="t.name" :value="t.id"></el-option>
                    </el-select>
                </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="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
                </el-col>

            </el-row>
        </el-card>
        <div class="el-card-p card-shadow carder-border mt10 pad10 ">
            <el-table class="table-box" v-table-height v-loading="loading" :data="monitorList" border @selection-change="handleSelectionChange" :span-method="objectSpanMethod">
                <el-table-column type="selection" width="55" :align="alignment" />
                <el-table-column type="index" width="55" label="序号" :align="alignment" />
                <el-table-column label="测站名称" :align="alignment" prop="stnm" />
                <el-table-column label="故障名称" :align="alignment" prop="errorName" />
                <el-table-column label="汛期阈值" :align="alignment" prop="floodValue" />
                <el-table-column label="非汛期阈值" :align="alignment" prop="value" />
                <el-table-column label="操作类型" :align="alignment" prop="updateType">
                    <template #default="scope">
                        <el-tag v-if="scope.row.updateType == 0" type="success">新增</el-tag>
                        <el-tag v-if="scope.row.updateType == 1" type="danger">修改</el-tag>
                        <el-tag v-if="scope.row.updateType == 2" type="danger">删除</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作时间" :align="alignment" prop="updateTime" />
                <el-table-column label="操作人员" :align="alignment" prop="updateUser" />
                <el-table-column label="操作" :align="alignment" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button text type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.limit" @pagination="getList" />
        </div>

    </div>
</template>
<script setup>
    import { ref, reactive, onMounted, } from 'vue'
    const router = useRouter();
    const { proxy } = getCurrentInstance()
    const alignment = 'center'
    const showSearch = ref(true)
    const queryParams = reactive({
        page: 1,
        limit: 10,
        stnm: '',
        errorId: ""
    })

    const loading = ref(false)
    const total = ref(0)
    const monitorList = ref([])
    const getList = async () => {
        loading.value = true;
        try {
            const res = await proxy.axiosGet('/basic/yuzhiLog/list', queryParams)
            if (res.code == 0) {
                let data = res.data
                monitorList.value = data.map(item => {
                    item.errorName = getUnit(item)
                    return item
                })
                total.value = res.count
            }
        } catch (error) {

        } finally {
            loading.value = false
        }
    }
    const getUnit = (row) => {
        if (row.errorId == 1) {
            row.errorName = row.errorName + "(h)";
        } else if (row.errorId == 5) {
            row.errorName = row.errorName + "(V)";
        } else {
            if (row.stnmType == 'A') {
                row.errorName = row.errorName + "(" + row.stnmTypeName + "·mm)";
            } else {
                row.errorName = row.errorName + "(" + row.stnmTypeName + "·m)";
            }
        }
        return row.errorName
    }
    const errorList = ref([])
    const getErrorList = async () => {
        try {
            const res = await proxy.axiosGet('/error/monitor/getlist')
            if (res.code == 0) {
                errorList.value = res.data
            }
        } catch (error) {

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

    // 多选框选中数据
    let ids = ref([])
    let names = ref([])
    let single = ref(true)
    let multiple = ref(true)
    const handleSelectionChange = (selection) => {
        ids.value = selection.map(item => item.id)
        names.value = selection.map(item => item.stnm)
        single.value = selection.length !== 1
        multiple.value = !selection.length
    }

    /** 删除按钮操作 */
    const handleDelete = (row) => {
        let stnms = row.stnm || names.value
        const stnmIds = row.id || ids.value;
        proxy.$modal.confirm('是否确认删除站点名称为"' + stnms + '"的数据项？').then(async function () {
            let res = await proxy.axiosDelete('/basic/yuzhiLog/delete/' + stnmIds);
            if (res.code === 0) {
                proxy.$modal.msgSuccess("删除成功");
                getList();
                resetCacheConfirm()
            }
        })
    }


    onMounted(() => {
        getList()
        getErrorList()
    })

</script>