<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { fetchAlarmHistory } from '@/api/platform'
const formData: any = ref({
    no: null,
    type: '',
    timeRange: [],
})

const tableData: any = ref([])
tableData.value = []
const pagination = ref({
    currentPage: 1,
    pageSize: 10,
    total: 0,
})

const handleQuery = () => {
    const params: any = {
        page: pagination.value.currentPage,
        size: pagination.value.pageSize,
        type: formData.value.type,
    }
    if (formData.value.timeRange && formData.value.timeRange.length > 0) {
        params.start_time = formData.value.timeRange[0]
        params.end_time = formData.value.timeRange[1]
    }
    fetchAlarmHistory(params).then((res: any) => {
        tableData.value = res
    })
}

const handleSizeChange = (val: number) => {
    pagination.value.pageSize = val
    pagination.value.currentPage = 1
    handleQuery()
}

const handleCurrentChange = (val: number) => {
    pagination.value.currentPage = val
    handleQuery()
}
function init() {
    const params: any = {
        page: pagination.value.currentPage,
        size: pagination.value.pageSize,
    }
    if (formData.value.timeRange && formData.value.timeRange.length > 0) {
        params.start_time = formData.value.timeRange[0]
        params.end_time = formData.value.timeRange[1]
    }
    fetchAlarmHistory(params).then((res: any) => {
        tableData.value = res
    })
}
const formatDate = (date: Date): string => {
    const pad = (n: number): string => n.toString().padStart(2, '0')
    return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
}

const getDefaultTimeRange = (): [string, string] => {
    const end = new Date()
    const start = new Date()
    start.setDate(start.getDate() - 7)
    return [formatDate(start), formatDate(end)]
}

onMounted(() => {
    formData.value.no = null
    formData.value.type = ''
    formData.value.timeRange = getDefaultTimeRange()
    init()
})
</script>

<template>
    <div class="event-report-container">
        <div class="search-box">
            <!-- <el-select v-model="formData.no" placeholder="所属线路" style="width: 180px">
                <el-option label="线路一" value="线路一" />
                <el-option label="线路二" value="线路二" />
            </el-select> -->
            <!-- <el-select v-model="formData.type" placeholder="事件类型" style="width: 180px; margin-left: 15px">
                <el-option label="操作事件" value="操作事件" />
                <el-option label="报警事件" value="报警事件" />
            </el-select> -->
            <el-input v-model="formData.type" placeholder="请输入事件类型" style="width: 180px" />
            <div>
                <el-date-picker v-model="formData.timeRange" type="datetimerange" start-placeholder="开始时间"
                    end-placeholder="结束时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
                    style="width: 380px; margin-left: 15px" />
            </div>
            <el-button type="primary" style="margin-left: 15px;" @click="handleQuery">
                查询
            </el-button>
        </div>
        <div class="table-box">
            <el-table :data="tableData" style="width: 100%; height: calc(100vh - 260px)" header-align="center"
                align="center">
                <el-table-column label="序号" type="index" width="80" align="center" />
                <el-table-column label="事件所属" prop="belong">
                    <template #default="scope">
                        <span v-if="scope.row.alarm_type === 'line_alarm'">线路{{ scope.row.no }}</span>
                        <span v-else-if="scope.row.alarm_type === 'ai_alarm'">通道{{ scope.row.no }}</span>
                        <span v-else-if="scope.row.alarm_type === 'operate_alarm'">{{ scope.row.no }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="事件类型" prop="type" align="center" />
                <el-table-column label="事件等级" prop="level" width="80" align="center" />
                <el-table-column label="事件详情" prop="content" min-width="120px" align="center" />
                <el-table-column label="发生时间" prop="time" min-width="100px" align="center" />
            </el-table>
            <div class="pagination">
                <el-pagination v-model:current-page="pagination.currentPage" v-model:page-size="pagination.pageSize"
                    :page-sizes="[10, 20, 30, 40]" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :total="pagination.total" layout="prev, pager, next, sizes" />
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
.event-report-container {
    width: 100%;
    height: 100%;
    padding: 15px;
}

.search-box {
    background: #fff;
    border-radius: 6px;
    padding: 15px;
    box-shadow: 0px 0px 10px rgba(219, 225, 236, 1);
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    height: 65px;
}

.table-box {
    background: #fff;
    border-radius: 4px;
    padding: 15px;
    box-shadow: 0 0 10px rgba(219, 225, 236, 1);
    height: calc(100% - 80px);
}

.pagination {
    margin-top: 15px;
    display: flex;
    justify-content: flex-start;
}

:deep(.el-table__header-wrapper) {
    background-color: #f9fafe !important;
    height: 46px;
    line-height: 46px;
}

:deep(.el-table__header) {
    th {
        background-color: #f9fafe !important;
        font-weight: 700 !important;
        font-size: 14px !important;
        color: #333 !important;
        border: none !important;
    }
}

:deep(.el-table) {
    --el-table-row-hover-bg-color: #f8f9fa !important;
    --el-table-border-color: #f2f2f2 !important;
}

:deep(.el-table td) {
    border-bottom: 1px solid #f2f2f2 !important;
}

:deep(.el-table__cell) {
    color: #505050 !important;
}

:deep(.el-input__inner) {
    color: #363636;
    min-height: 40px;
}

:deep(el-input__wrapper) {
    min-height: 40px;
}

:deep(.el-select__wrapper) {
    min-height: 40px;
}

:deep(.el-range-editor.el-input__wrapper) {
    min-height: 40px;
}
</style>