<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button
      type="primary"
      plain
      @click="openForm('create')"
      v-hasPermi="['iot:alert:create']"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编码" align="center" prop="alertCode" />
      <el-table-column label="名称" align="center" prop="alertName" />
      <el-table-column label="告警类型" align="center" prop="alertType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.IOT_ALERT_TYPE" :value="scope.row.alertType" />
        </template>
      </el-table-column>
      <el-table-column label="告警等级" align="center" prop="alertLevel">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.IOT_ALERT_LEVEL" :value="scope.row.alertLevel" />
        </template>
      </el-table-column>
      <el-table-column label="告警内容" align="center" prop="content" />
      <el-table-column label="告警条件" align="center" prop="condition" />
      <el-table-column label="逻辑表达式" align="center" prop="conditionFormula" />
      <el-table-column label="数据值" align="center" prop="dataValue" />
      <el-table-column label="数据类型" align="center" prop="dataType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.IOT_DEVICE_DATA_TYPE" :value="scope.row.dataType" />
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="dataUnit" />
      <el-table-column label="描述" align="center" prop="dataTypeRemark" />
       <el-table-column label="告警音频" align="center" prop="alertAudio" />
      <el-table-column label="重复告警" align="center" prop="isRepeat">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isRepeat" />
        </template>
      </el-table-column>
      <el-table-column label="不重复间隔" align="center" prop="noRepeatDuration" />
      <el-table-column label="来源ID" align="center" prop="dataId" />
      <el-table-column label="来源编码" align="center" prop="dataCode" />
      <el-table-column label="来源名称" align="center" prop="dataName" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="170px"
        fixed="right"
      />
      <el-table-column label="操作" align="center" fixed="right"  width="150px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['iot:alert:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['iot:alert:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
    <!-- 表单弹窗：添加/修改 -->
    <AlertRecordForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { AlertApi } from '@/api/iot/alert'
import AlertRecordForm from './AlertRecordForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  alertId?: number // 告警ID（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  alertId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.alertId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.alertId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AlertApi.getAlertRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.alertId) {
    message.error('请选择一个告警配置')
    return
  }
  formRef.value.open(type, id, props.alertId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AlertApi.deleteAlertRecord(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>
