<template>
  <a-modal :visible="visible" title="采集配置" :mask-closable="false" :ok-loading="loading" @ok="handleSubmit" @cancel="handleCancel">
    <a-form :model="form" layout="vertical">
      <a-form-item label="采集方式">
        <a-radio-group v-model="form.collect_method" type="button">
          <a-radio value="api">API</a-radio>
          <a-radio value="snmp">SNMP</a-radio>
        </a-radio-group>
      </a-form-item>

      <template v-if="form.collect_method === 'api'">
        <a-form-item label="采集地址(HTTP/HTTPS GET)">
          <a-input v-model="form.agent_config" placeholder="周期采集地址，返回 JSON 格式指标数组" />
          <template #extra>
            <span style="color: #86909c">采集地址应返回 JSON 格式的指标数据，支持 {"metrics":[...]} 或数组形式</span>
          </template>
        </a-form-item>
      </template>
      <template v-else>
        <a-row :gutter="20">
          <a-col :span="12">
            <a-form-item label="SNMP目标地址">
              <a-input v-model="form.snmp_target" placeholder="例如 192.168.1.10" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="SNMP端口">
              <a-input-number v-model="form.snmp_port" :min="1" :max="65535" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-form-item label="SNMP版本">
          <a-radio-group v-model="form.snmp_version" type="button">
            <a-radio value="v2c">v2c</a-radio>
            <a-radio value="v3">v3</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-row :gutter="20">
          <a-col v-if="form.snmp_version !== 'v3'" :span="12">
            <a-form-item label="Community">
              <a-input v-model="form.snmp_community" placeholder="SNMP v2c community" />
            </a-form-item>
          </a-col>
          <a-col v-else :span="12">
            <a-form-item label="Security Name">
              <a-input v-model="form.snmp_v3_security_name" placeholder="SNMP v3 用户名" />
            </a-form-item>
          </a-col>
          <a-col v-if="form.snmp_version === 'v3'" :span="12">
            <a-form-item label="Security Level">
              <a-select v-model="form.snmp_v3_security_level">
                <a-option value="noAuthNoPriv">noAuthNoPriv</a-option>
                <a-option value="authNoPriv">authNoPriv</a-option>
                <a-option value="authPriv">authPriv</a-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="超时(ms)">
              <a-input-number v-model="form.snmp_timeout_ms" :min="1" :max="60000" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="重试次数">
              <a-input-number v-model="form.snmp_retries" :min="0" :max="10" style="width: 100%" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row v-if="form.snmp_version === 'v3'" :gutter="20">
          <a-col :span="8">
            <a-form-item label="Auth协议">
              <a-select v-model="form.snmp_v3_auth_protocol">
                <a-option value="SHA">SHA</a-option>
                <a-option value="MD5">MD5</a-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="16">
            <a-form-item label="Auth密码">
              <a-input-password v-model="form.snmp_v3_auth_password" placeholder="authNoPriv/authPriv 必填" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row v-if="form.snmp_version === 'v3' && form.snmp_v3_security_level === 'authPriv'" :gutter="20">
          <a-col :span="8">
            <a-form-item label="Priv协议">
              <a-select v-model="form.snmp_v3_priv_protocol">
                <a-option value="AES">AES</a-option>
                <a-option value="DES">DES</a-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="Priv密码">
              <a-input-password v-model="form.snmp_v3_priv_password" placeholder="authPriv 必填" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="Context">
              <a-input v-model="form.snmp_v3_context_name" placeholder="可选" />
            </a-form-item>
          </a-col>
        </a-row>
      </template>

      <a-form-item label="参与周期采集">
        <a-switch v-model="form.collect_on" />
      </a-form-item>

      <a-form-item v-if="form.collect_on" label="采集间隔(秒)">
        <a-input-number v-model="form.collect_interval" placeholder="默认60秒" :min="10" :max="3600" style="width: 100%" allow-clear />
        <template #extra>
          <span style="color: #86909c">分桶间隔（秒），系统将按此间隔周期采集</span>
        </template>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import { Message } from '@arco-design/web-vue'
import { patchRoomDeviceCollect, type RoomDeviceCollectData } from '@/api/ops/room-device'

interface Props {
  visible: boolean
  record: any
}

const props = defineProps<Props>()
const emit = defineEmits(['update:visible', 'success'])

const loading = ref(false)

const form = ref({
  collect_method: 'api' as 'api' | 'snmp',
  collect_on: true,
  collect_interval: 60,
  agent_config: '',
  snmp_target: '',
  snmp_port: 161,
  snmp_version: 'v2c' as 'v2c' | 'v3',
  snmp_community: '',
  snmp_v3_security_level: 'noAuthNoPriv',
  snmp_v3_security_name: '',
  snmp_v3_auth_protocol: 'SHA',
  snmp_v3_auth_password: '',
  snmp_v3_priv_protocol: 'AES',
  snmp_v3_priv_password: '',
  snmp_v3_context_name: '',
  snmp_timeout_ms: 3000,
  snmp_retries: 1,
})

watch(
  () => props.visible,
  (val) => {
    if (val && props.record) {
      form.value.collect_method = props.record.collect_method || 'api'
      form.value.collect_on = props.record.collect_on ?? true
      form.value.collect_interval = props.record.collect_interval || 60
      form.value.agent_config = props.record.agent_config || ''
      form.value.snmp_target = props.record.snmp_target || ''
      form.value.snmp_port = props.record.snmp_port || 161
      form.value.snmp_version = props.record.snmp_version || 'v2c'
      form.value.snmp_community = props.record.snmp_community || ''
      form.value.snmp_v3_security_level = props.record.snmp_v3_security_level || 'noAuthNoPriv'
      form.value.snmp_v3_security_name = props.record.snmp_v3_security_name || ''
      form.value.snmp_v3_auth_protocol = props.record.snmp_v3_auth_protocol || 'SHA'
      form.value.snmp_v3_auth_password = ''
      form.value.snmp_v3_priv_protocol = props.record.snmp_v3_priv_protocol || 'AES'
      form.value.snmp_v3_priv_password = ''
      form.value.snmp_v3_context_name = props.record.snmp_v3_context_name || ''
      form.value.snmp_timeout_ms = props.record.snmp_timeout_ms || 3000
      form.value.snmp_retries = props.record.snmp_retries ?? 1
    }
  }
)

const handleSubmit = async () => {
  loading.value = true
  try {
    if (form.value.collect_method === 'api' && !form.value.agent_config?.trim()) {
      Message.warning('API 模式下请填写采集地址')
      return
    }
    if (form.value.collect_method === 'snmp') {
      if (!form.value.snmp_target?.trim()) {
        Message.warning('SNMP 模式下请填写目标地址')
        return
      }
      if (form.value.snmp_version === 'v3') {
        if (!form.value.snmp_v3_security_name?.trim()) {
          Message.warning('SNMP v3 请填写 Security Name')
          return
        }
        if (form.value.snmp_v3_security_level !== 'noAuthNoPriv' && !form.value.snmp_v3_auth_password?.trim()) {
          Message.warning('SNMP v3 认证级别请填写 Auth 密码')
          return
        }
        if (form.value.snmp_v3_security_level === 'authPriv' && !form.value.snmp_v3_priv_password?.trim()) {
          Message.warning('SNMP v3 authPriv 请填写 Priv 密码')
          return
        }
      } else if (!form.value.snmp_community?.trim()) {
        Message.warning('SNMP v2c 模式下请填写 community')
        return
      }
    }
    const data: RoomDeviceCollectData = {
      collect_method: form.value.collect_method,
      collect_on: form.value.collect_on,
      collect_interval: form.value.collect_interval,
      agent_config: form.value.collect_method === 'api' ? form.value.agent_config : undefined,
      snmp_target: form.value.collect_method === 'snmp' ? form.value.snmp_target : undefined,
      snmp_port: form.value.collect_method === 'snmp' ? form.value.snmp_port : undefined,
      snmp_version: form.value.collect_method === 'snmp' ? form.value.snmp_version : undefined,
      snmp_community: form.value.collect_method === 'snmp' ? form.value.snmp_community : undefined,
      snmp_v3_security_level: form.value.collect_method === 'snmp' ? form.value.snmp_v3_security_level : undefined,
      snmp_v3_security_name: form.value.collect_method === 'snmp' ? form.value.snmp_v3_security_name : undefined,
      snmp_v3_auth_protocol: form.value.collect_method === 'snmp' ? form.value.snmp_v3_auth_protocol : undefined,
      snmp_v3_auth_password: form.value.collect_method === 'snmp' ? form.value.snmp_v3_auth_password : undefined,
      snmp_v3_priv_protocol: form.value.collect_method === 'snmp' ? form.value.snmp_v3_priv_protocol : undefined,
      snmp_v3_priv_password: form.value.collect_method === 'snmp' ? form.value.snmp_v3_priv_password : undefined,
      snmp_v3_context_name: form.value.collect_method === 'snmp' ? form.value.snmp_v3_context_name : undefined,
      snmp_timeout_ms: form.value.collect_method === 'snmp' ? form.value.snmp_timeout_ms : undefined,
      snmp_retries: form.value.collect_method === 'snmp' ? form.value.snmp_retries : undefined,
    }

    await patchRoomDeviceCollect(props.record.id, data)

    Message.success('配置成功')
    emit('success')
    emit('update:visible', false)
  } catch (error) {
    Message.error('配置失败')
  } finally {
    loading.value = false
  }
}

const handleCancel = () => {
  emit('update:visible', false)
}
</script>
