<template>
  <div class="alarm-container">
    <div class="alarm-header">
      <span class="alarm-title">Manual Alarm Control</span>
    </div>
    <div class="alarm-buttons">
      <el-button
        type="primary"
        size="large"
        @click="handleAlarm"
        class="btn-alarm"
        :loading="loading"
      >
        Enable Manual Alarm
      </el-button>
      <el-button
        type="info"
        size="large"
        @click="handleCloseAlarm"
        class="btn-close"
        :loading="DisableLoading"
      >
        Disable Manual Alarm
      </el-button>
    </div>
    <div class="alarm-tip">
      <span class="tip-icon">ℹ️</span>
      <span>Alarm will remain active for 1 minute after activation.</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { cameraAlarm } from '@/api/setting'

const loading = ref(false)
const DisableLoading = ref(false)

async function handleCloseAlarm() {
  if (loading.value) return
  DisableLoading.value = true
  try {
    const res = await cameraAlarm({
      AlarmSettings: 0
    })
    if (res.data === 'ok\n') {
      ElMessage.success('Manual Alarm Deactivated Successfully')
    } else {
      ElMessage.warning('Manual Alarm Deactivation Error')
    }
  } catch (error) {
    ElMessage.error('Manual Alarm Deactivation Error')
  } finally {
    DisableLoading.value = false
  }
}

async function handleAlarm() {
  if (loading.value) return
  loading.value = true
  try {
    const res = await cameraAlarm({
      AlarmSettings: 1
    })
    if (res.data === 'ok\n') {
      ElMessage.success('Manual alarm activated successfully. It will remain active for 1 minute.')
    } else {
      ElMessage.warning('Manual Alarm Activation Error')
    }
  } catch (error) {
    ElMessage.error('Failed to Activate Manual Alarm.')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.alarm-container {
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  border-left: 4px solid #ff6b6b;
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    transform: translateY(-2px);
  }
}

.alarm-header {
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid rgba(255, 107, 107, 0.2);
}

.alarm-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  display: flex;
  align-items: center;
  gap: 8px;

  &::before {
    content: '';
    width: 4px;
    height: 4px;
    background: #ff6b6b;
    border-radius: 50%;
    animation: pulse 2s ease-in-out infinite;
  }
}

.alarm-buttons {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;

  @media (max-width: 600px) {
    flex-direction: column;
    gap: 12px;
  }
}

.btn-alarm,
.btn-close {
  flex: 1;
  min-width: 160px;
  height: 44px;
  font-weight: 500;
  letter-spacing: 0.5px;
  border-radius: 8px;
  transition: all 0.3s ease;
  border: none;
  font-size: 14px;

  @media (max-width: 600px) {
    width: 100%;
  }

  &:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  }

  &:active:not(:disabled) {
    transform: translateY(0);
  }
}

.btn-icon {
  margin-right: 6px;
  font-size: 16px;
}

.alarm-tip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  background: rgba(255, 193, 7, 0.1);
  border-radius: 6px;
  border-left: 3px solid #ffc107;
  font-size: 13px;
  color: #666;
  line-height: 1.5;
}

.tip-icon {
  font-size: 16px;
  flex-shrink: 0;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}
</style>