<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="设备ID" prop="device_id">
    <el-input class="w-[280px]" v-model="queryParams.device_id" clearable placeholder="请输入设备ID" />
</el-form-item>
                <el-form-item label="监测类型" prop="type">
    <el-select class="w-[280px]" v-model="queryParams.type" clearable placeholder="请选择监测类型">
        <el-option label="全部" value=""></el-option>
        <el-option 
            v-for="(item, index) in dictData.alarm_type"
            :key="index" 
            :label="item.name"
            :value="item.value"
        />
    </el-select>
</el-form-item>
<el-form-item label="报警时间" prop="create_time">
    <daterange-picker
        v-model:startTime="queryParams.start_time"
        v-model:endTime="queryParams.end_time"
    />
</el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="ID" prop="id" width="80" show-overflow-tooltip />
                    <el-table-column label="设备信息" width="200">
                        <template #default="{ row }">
                            <el-tag class="mr-2" type="info">ID: {{ row.device_id }}</el-tag>
                            <el-tag class="mr-2" type="info">名称: {{ row.device.name }}</el-tag>
                        </template>
                    </el-table-column> 
                    <el-table-column label="监测类型" align="center" prop="type">
                        <template #default="{ row }">
                            <dict-value :options="dictData.monitor_item" :value="row.type" />
                        </template>
                    </el-table-column>
                    <el-table-column label="报警内容" width="300" prop="content" show-overflow-tooltip />
                    <el-table-column label="报警数值" prop="value" show-overflow-tooltip />
                    <el-table-column label="报警时间" align="center" prop="create_time">
                        <template #default="{ row }">
                          <span>{{ row.create_time ? timeFormat(row.create_time, 'yyyy-mm-dd hh:MM:ss') : '' }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="140" align="center" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleDetail(row)"
                            >
                                详情
                            </el-button>
                            <el-button
                                v-perms="['device.monitor_alarm/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>

    <el-dialog v-model="dialogTableVisible" title="监测报警数据">
      <el-table :data="detailData.gridData">
        <el-table-column property="item" label="监测项" width="150" />
        <el-table-column property="value" label="监测数据" />
      </el-table>
    </el-dialog>
</template>

<script lang="ts" setup name="monitorAlarmLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiMonitorAlarmLists, apiMonitorAlarmDelete } from '@/api/monitor_alarm'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const { query } = useRoute()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

let device_id = query.device_id
if (typeof device_id == 'undefined') {
    device_id = ''
}

// 查询条件
const queryParams = reactive({
    device_id: device_id,
    type: '',
    start_time: '',
    end_time: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('monitor_item')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiMonitorAlarmLists,
    params: queryParams
})

const dialogTableVisible = ref(false)
const detailData = reactive({
    gridData : [{}]
})

// 详情
const handleDetail = (data: any) => {
    detailData.gridData = [{}]
    let deviceType = ''
    for (const key in dictData['monitor_item']) {
        if (dictData['monitor_item'][key]['value'] == data.type) {
            deviceType = dictData['monitor_item'][key]['name']
        }
    }
    detailData.gridData.unshift({
        item: '报警时间',
        value: data.create_time ? timeFormat(data.create_time, 'yyyy-mm-dd hh:MM:ss') : ''
    })
    detailData.gridData.unshift({
        item: '解决方案',
        value: data.solution
    })
    detailData.gridData.unshift({
        item: '报警数值',
        value: data.value
    })
    detailData.gridData.unshift({
        item: '报警内容',
        value: data.content
    })
    detailData.gridData.unshift({
        item: '监测类型',
        value: deviceType
    })
    detailData.gridData.unshift({
        item: '设备名称',
        value: data.device.name
    })
    detailData.gridData.unshift({
        item: '设备ID',
        value: data.device_id
    })
    dialogTableVisible.value = true
}


// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiMonitorAlarmDelete({ id })
    getLists()
}

getLists()
</script>

