<template>
  <div class="g-p20 record">
    <base-search :timeValue="timeValue" @dateChange="dateChange" :isShowDate="activeName === '2'" @on-search="onSearch"
      @reset="onReset">
    </base-search>
    <div class="list g-p20">
      <pro-pagination v-show="activeName === '1' ? table.data.totalCount !== 0 : tableHistory.data.totalCount !== 0"
        class="g-pagination" :pageObj="pageObj" @change="change"
        :total="activeName === '1' ? table.data.totalCount : tableHistory.data.totalCount" ref="page"></pro-pagination>
      <el-tabs v-model="activeName" class="tabs" @tab-click="handleClick">
        <el-tab-pane label="当前报警" name="1">
          <pro-table :data="currentData.arr" :loading="table.loading" :config="tableConfig">
            <template #default="{ row }">
              <div class="table-status" :class="{ 'table-status-warning': row.status == '触发报警' }">
                {{ row.status }}
              </div>
            </template>
          </pro-table>
        </el-tab-pane>
        <el-tab-pane label="历史报警" name="2">
          <pro-table :data="tableHistory.items" :loading="tableHistory.loading" :config="tableConfig1">
            <template #default="{ row }">
              <div class="table-status" :class="{ 'table-status-warning': row.status == '触发报警' }">
                {{ row.status }}
              </div>
            </template>
          </pro-table>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getAlertRecords, getRealTimeAlert } from '@/api/variableRecord'
import { useSidebarStore } from '@/store/sidebar'
import Pinyin from 'pinyin-match'

const timeValue = ref('')
const activeName = ref('1')
const searchValue = ref('')
const page: any = ref(null)
const sidebar = useSidebarStore()
const tableConfig = [
  {
    label: '报警内容',
    prop: 'message'
  },
  {
    label: '报警时间',
    prop: 'triggerTime'
  },
  {
    label: '报警状态',
    prop: 'status',
    width: 150,
    slot: true
  }
]
const tableConfig1 = [
  {
    label: '报警内容',
    prop: 'message'
  },
  {
    label: '报警时间',
    prop: 'triggerTime'
  },
  {
    label: '恢复时间',
    prop: 'triggerTime'
  },
  {
    label: '报警状态',
    prop: 'status',
    width: 150,
    slot: true
  }
]
let pageObj = ref({
  SkipCount: 1,
  MaxResultCount: 10
})
let currentData: any = reactive({ arr: [] })
const onSearch = (v: any) => {
  searchValue.value = v.iptValue
  if (activeName.value == '1') {
    if (!v) return getCurrentData()
    currentData.arr = table.data.items.filter((el: any) => {
      return Pinyin.match(el.message, v)
    })
  } else {
    page?.resetPage
    tableHistory.getData()
  }
}
const change = (e: any) => {
  if (activeName.value == '1') {
    table.getData()
  } else {
    tableHistory.getData()
  }
}
const filterObj = ref({})
const onReset = () => {
  searchValue.value = ''
  filterObj.value = {}
  pageObj.value = {
    SkipCount: 1,
    MaxResultCount: 10
  }
  if (activeName.value == '1') {
    getCurrentData()
  } else {
    tableHistory.getData()
  }
}
const handleClick = (e: any) => {
  pageObj.value = {
    SkipCount: 1,
    MaxResultCount: 10
  }
  if (e == '1') {
    table.getData()
  } else {
    tableHistory.getData()
  }
}
const table = useRequest(
  () =>
    getRealTimeAlert({
      DeviceId: useQuery('id'),
      Keyword: searchValue.value,
      MaxResultCount: pageObj.value.MaxResultCount,
      SkipCount: (pageObj.value.SkipCount - 1) * pageObj.value.MaxResultCount
    }),
  false
)
const tableHistory = useRequest(
  () =>
    getAlertRecords({
      DeviceId: sidebar.equipmentInfo.id,
      Keyword: searchValue.value,
      ...filterObj.value,
      MaxResultCount: pageObj.value.MaxResultCount,
      SkipCount: (pageObj.value.SkipCount - 1) * pageObj.value.MaxResultCount
    }),
  false
)
const dateChange = (e: any) => {
  filterObj.value = e
}
onMounted(() => {
  getCurrentData()
})
const getCurrentData = async () => {
  await table.getData()
  currentData.arr = table.data.items
}
</script>

<style lang="less" scoped>
.record {
  background: #c8cfdf;
  min-height: calc(100vh - 90px);
}

.list {
  min-height: calc(100vh - 210px);
  width: 100%;
  background: #fff;
  border-radius: 10px;
  position: relative;
  margin-bottom: 50px;
}

.table-status {
  width: 80px;
  height: 26px;
  font-weight: 400;
  border-radius: 7px;
  font-size: 12px;
  line-height: 26px;
  text-align: center;
  background: #e1f4e1;
  color: #55b758;
}

.table-status-warning {
  color: #ec615a;
  background: #ffdedd;
}

:deep(.tabs) {
  min-height: 500px;
  padding-bottom: 50px;

  .el-tabs__item {
    color: #333;
    font-weight: bold;
  }

  .el-tabs__active-bar {
    width: 70px;
    height: 5px;
    background: #2dcadc;
    border-radius: 2px;
    bottom: -11px;
  }

  .el-tabs__nav-scroll {
    height: 50px;
  }
}
</style>
