<!--
  Description: 报警管理页面
  Author: 李亚光
  Date: 2023-04-23
 -->
<script lang="ts" setup name="alarmlist">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import imgDialog from './imageDialog.vue'
import { alarmCancel, alarmDel, getAlarmListPage } from '@/api/alarm'
import { getDictByCode } from '@/api/system/dict'
const $route = useRoute()
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  workTitle: '',
  projectState: '',
  alarmType: '',
  alarmStatus: '',
  alarmEndTime: '',
  alarmStartTime: '',
  sort: 'desc',
  orderBy: 'alarmTime',
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '作业名称',
    value: 'workTitle',
    align: 'center',
    // width: 170,
  },
  {
    text: '作业道路',
    value: 'workRoad',
    align: 'center',
    // width: 100,
  },
  {
    text: '报警信息',
    value: 'alarmContent',
    align: 'center',
  },
  {
    text: '报警类型',
    value: 'alarmTypeName',
    align: 'center',
  },
  {
    text: '负责人',
    value: 'workPersonName',
    align: 'center',
    width: '90',
  },
  {
    text: '负责人联系方式',
    value: 'workPersonPhoneNumber',
    align: 'center',
    width: '130',
  },
  {
    text: '报警时间',
    value: 'alarmTime',
    align: 'center',
  },
  {
    text: '当前状态',
    value: 'alarmStatusName',
    align: 'center',
    width: '120',
  },
])
const list = ref([])
const total = ref(20)
const listLoading = ref(true)
// 获取日志数据
const fetchData = () => {
  listLoading.value = true
  getAlarmListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}

// 查询数据
const search = () => {
  fetchData()
}
// 获取报警类型
const alarmTypeList = ref<any[]>([])
const fetchAlarmType = () => {
  getDictByCode('alarmType').then((response) => {
    alarmTypeList.value = response.data
  })
}
fetchAlarmType()
// 获取报警状态
const alarmStatusList = ref<any[]>([])
const fetchAlarmStatus = () => {
  getDictByCode('alarmStatus').then((response) => {
    alarmStatusList.value = response.data
  })
}
fetchAlarmStatus()
// 消除报警
const eliminate = (row: any) => {
  ElMessageBox.confirm('确认取消该报警?', '提示', {
    confirmButtonText: '是',
    cancelButtonText: '否',
    type: 'info',
  }).then((res) => {
    // 发送请求
    alarmCancel(row.id).then((res) => {
      ElMessage.success('消警成功')
      search()
    })
  })
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 删除
const del = () => {
  if (selectList.value.length) {
    ElMessageBox.confirm('是否删除所选的数据?', '提示', {
      confirmButtonText: '是',
      cancelButtonText: '否',
      type: 'info',
    }).then((res) => {
      // 点击确认
      const ids = selectList.value.map(item => item.id)
      alarmDel(ids).then((res) => {
        if (res.code === 200) {
          selectList.value = []
          ElMessage.success('删除成功')
          fetchData()
        }
      })
    }).catch(() => {
      // 点击取消
      selectList.value = []
    })
  }
  else {
    ElMessage.warning('请先选择需要删除的数据')
  }
}
onMounted(() => {
  // 判断是否从首页看板跳转来到此页面
  const type = $route.query.type
  if (type) {
    listQuery.alarmType = type as string
  }
  search()
})
const imgRef = ref()
const detail = (row: any) => {
  imgRef.value.initDialog(row.alarmImage)
}
</script>

<template>
  <div class="list-log">
    <!-- 布局 -->
    <app-container>
      <img-dialog ref="imgRef" />
      <!-- 筛选条件 -->
      <search-area @search="search">
        <search-item>
          <el-input v-model.trim="listQuery.workTitle" placeholder="作业名称" clearable />
        </search-item>
        <!-- <search-item>
          <el-select v-model="listQuery.projectState" clearable placeholder="作业状态状态">
            <el-option v-for="item in []" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item> -->
        <search-item>
          <el-select v-model="listQuery.alarmType" clearable placeholder="报警类型">
            <el-option v-for="item in alarmTypeList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <search-item>
          <el-select v-model="listQuery.alarmStatus" clearable placeholder="报警状态">
            <el-option v-for="item in alarmStatusList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <search-item>
          <el-date-picker
            v-model="listQuery.alarmStartTime" type="datetime" value-format="YYYY/MM/DD HH:mm:ss"
            placeholder="报警开始时间"
          />
        </search-item>
        <search-item>
          <el-date-picker
            v-model="listQuery.alarmEndTime" type="datetime" value-format="YYYY/MM/DD HH:mm:ss"
            placeholder="报警结束时间"
          />
        </search-item>
        <template #btns>
          <!-- <el-button class="search-btn" type="primary" @click="add">
            新增
          </el-button> -->
          <!-- <el-button v-if="proxy.hasPerm('/alarm/del')" class="search-btn" type="info" @click="del">
            删除
          </el-button> -->
        </template>
      </search-area>
      <table-container>
        <!-- 查询结果Table显示 -->
        <normal-table
          :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading" @change="changePage" @multi-select="multiSelect"
        >
          <template #columns>
            <el-table-column v-if="proxy.hasPerm('/alarm/cancel')" label="消警" width="90" align="center">
              <template #default="scope">
                <el-button
                  link type="primary" :disabled="scope.row.alarmStatus !== '0'" size="small"
                  @click="eliminate(scope.row)"
                >
                  消警
                </el-button>
              </template>
            </el-table-column>
            <el-table-column label="详情" width="90" align="center">
              <template #default="scope">
                <el-button
                  link type="primary" :disabled="!scope.row.alarmImage" size="small"
                  @click="detail(scope.row)"
                >
                  查看
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>

