﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { fetchAiAlarmSetting, fetchLineAlarmSetting, saveAiAlarmSetting, saveLineAlarmSetting } from '../api/platform'
import type { AiAlarmSettingItem, LineAlarmSettingPayload } from '../types/platform'
import { ensureSaveAuthorized } from '../utils/saveGuard'

defineProps<{ store: any; actions: any }>()

const lineAlarm = reactive<LineAlarmSettingPayload>({
  line_no: 1,
  over_limit_alarm: [
    { category: '电压', limit: 180, delay: 180, output_node: '开出1', enabled: true },
    { category: '电流', limit: 180, delay: 180, output_node: '开出1', enabled: true },
    { category: '差流', limit: 180, delay: 180, output_node: '开出1', enabled: false },
    { category: '频率', limit: 180, delay: 180, output_node: '开出1', enabled: false },
  ],
  fault_alarm: [{ category: 'PT断线', delay: 180, output_node: '开出1', enabled: true }],
})

const aiAlarm = reactive<AiAlarmSettingItem[]>([
  {
    channel_no: 1,
    singal_type: '4-20mA',
    limit_low: 0,
    limit_high: 20,
    delay: 180,
    output_node: '开出1',
    enabled: true,
  },
])

const result = ref('未保存')
const saving = ref(false)

onMounted(async () => {
  try {
    const [lineData, aiData] = await Promise.all([fetchLineAlarmSetting(), fetchAiAlarmSetting()])
    lineAlarm.line_no = lineData.line_no
    lineData.over_limit_alarm.forEach((item, index) => {
      if (lineAlarm.over_limit_alarm[index]) {
        Object.assign(lineAlarm.over_limit_alarm[index], item)
      } else {
        lineAlarm.over_limit_alarm.push(item)
      }
    })
    lineData.fault_alarm.forEach((item, index) => {
      if (lineAlarm.fault_alarm[index]) {
        Object.assign(lineAlarm.fault_alarm[index], item)
      } else {
        lineAlarm.fault_alarm.push(item)
      }
    })
    aiData.forEach((item, index) => {
      if (aiAlarm[index]) {
        Object.assign(aiAlarm[index], item)
      } else {
        aiAlarm.push(item)
      }
    })
    result.value = '已加载当前报警设置'
  } catch (error) {
    result.value = error instanceof Error ? error.message : '加载报警设置失败'
  }
})

async function save() {
  saving.value = true
  try {
    const guard = await ensureSaveAuthorized()
    if (!guard.ok) {
      result.value = guard.message
      return
    }

    const [lineResponse, aiResponse] = await Promise.all([saveLineAlarmSetting(lineAlarm), saveAiAlarmSetting(aiAlarm)])
    result.value = `${lineResponse.msg} / ${aiResponse.msg}`
  } catch (error) {
    result.value = error instanceof Error ? error.message : '保存报警设置失败'
  } finally {
    saving.value = false
  }
}
</script>

<template>
  <section class="panel">
    <h2>报警设置</h2>
    <div class="form-grid">
      <label>线路号<input v-model.number="lineAlarm.line_no" type="number" min="1" max="4" /></label>
      <label>电压越限<input v-model.number="lineAlarm.over_limit_alarm[0].limit" type="number" /></label>
      <label>电流越限<input v-model.number="lineAlarm.over_limit_alarm[1].limit" type="number" /></label>
      <label>差流越限<input v-model.number="lineAlarm.over_limit_alarm[2].limit" type="number" /></label>
      <label>频率越限<input v-model.number="lineAlarm.over_limit_alarm[3].limit" type="number" /></label>
      <label>动作延时<input v-model.number="lineAlarm.over_limit_alarm[0].delay" type="number" /></label>
      <label>输出节点<select v-model="lineAlarm.over_limit_alarm[0].output_node"><option>开出1</option><option>开出2</option></select></label>
      <label>PT断线延时<input v-model.number="lineAlarm.fault_alarm[0].delay" type="number" /></label>
      <label>AI通道号<input v-model.number="aiAlarm[0].channel_no" type="number" min="1" max="12" /></label>
      <label>AI信号类型<input v-model="aiAlarm[0].singal_type" /></label>
      <label>AI下限<input v-model.number="aiAlarm[0].limit_low" type="number" /></label>
      <label>AI上限<input v-model.number="aiAlarm[0].limit_high" type="number" /></label>
    </div>
    <div class="actions">
      <button class="primary" :disabled="saving" @click="save">{{ saving ? '保存中...' : '保存报警设置' }}</button>
    </div>
    <p>保存结果：{{ result }}</p>
  </section>
</template>
