<script lang="ts" setup name="AlarmList">
import { reactive, ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import { exportWarm, getListPage } from '@/api/home/alarm/alarm'
import { getDictByCode } from '@/api/system/dict'
import { exportFile } from '@/utils/exportUtils'
import { downloadImg } from '@/utils/download'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  assessedDeptName: '', // 考核部门名称
  endTime: '', // 结束时间
  startTime: '', // 开始时间
  warnNo: '', // 预警编号
  warnType: '', // 预警风险类型
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '预警结果编号',
    value: 'warnNo',
    align: 'center',
  },
  {
    text: '预警类型',
    value: 'warnTypeName',
    align: 'center',
  },
  {
    text: '预警概述',
    value: 'warnDesc',
    align: 'center',
  },
  {
    text: '预警结果',
    value: 'warnVal',
    align: 'center',
  },
  {
    text: '风险类型',
    value: 'warnLevelName',
    align: 'center',
  },
  {
    text: '具体考核对象',
    value: 'assessedDeptName',
    align: 'center',
  },
  {
    text: '考核结果编号',
    value: 'assNo',
    align: 'center',
  },
  {
    text: '考核时间',
    value: 'timeFmt',
    align: 'center',
  },

])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  if (newVal.length) {
    listQuery.startTime = `${newVal[0]} 00:00:00`
    listQuery.endTime = `${newVal[1]} 23:59:59`
  }
  else {
    listQuery.startTime = ''
    listQuery.endTime = ''
  }
})

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.assessedDeptName = '' // 考核部门名称
  listQuery.endTime = '' // 结束时间
  listQuery.startTime = '' // 开始时间
  listQuery.warnNo = '' // 预警编号
  listQuery.warnType = '' // 预警风险类型
  listQuery.offset = 1
  listQuery.limit = 20
  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 $router = useRouter()

// 指标类型下拉列表
const typeList = ref<{ id: string; value: string; name: string }[]>()
// 获取字典
const fetchSelectList = () => {
  getDictByCode('warn_level').then((res) => {
    typeList.value = res.data
  })
}
fetchSelectList()
// 导出列表
const exportList = () => {
  if (list.value.length) {
    const loading = ElLoading.service({
      lock: true,
      text: 'Loading',
      background: 'rgba(255, 255, 255, 0.8)',
    })
    const data = {
      ...listQuery,
      offset: undefined,
      limit: undefined,
      // ids: selectList.value.map(item => item.id),
    }
    exportWarm(data).then((res) => {
      exportFile(res.data, '预警结果')
      loading.close()
    })
      .catch((_) => {
        loading.close()
      })
  }
  else {
    ElMessage.warning('无可导出内容')
  }
}
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.warnNo" placeholder="预警结果编号" clearable />
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.warnType" placeholder="预警风险类型" clearable>
          <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.assessedDeptName" placeholder="考核部门名称" clearable />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="考核开始时间" end-placeholder="考核结束时间"
        />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <el-button type="primary" @click="exportList">
          导出
        </el-button>
      </template>
      <!-- 普通表格 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="false" :is-multi="true" @change="changePage"
      />
    </table-container>
  </app-container>
</template>

