<template>
  <!-- 闸机控制 - 智能仓储风格 -->
  <div class="control-gate-page">
    <!-- 背景层 -->
    <div class="bg-layer" :style="{ backgroundImage: `url(${bgImg})` }" />

    <!-- 顶部标题区域 -->
    <div class="header-section">
      <button class="logout-btn" @click="goHome">
        <i class="fas fa-home" />
        <span>主页</span>
      </button>
      <h1 class="page-title">闸机控制</h1>
      <!-- <div class="header-actions">
        <button class="action-btn refresh-btn" @click="getStatus">
          <i class="fas fa-sync-alt" />
          <span>获取状态</span>
        </button>
      </div> -->
    </div>

    <!-- 主内容区域 -->
    <main class="main-content">
      <!-- 控制面板卡片 -->
      <div class="control-panel">
        <div class="panel-header">
          <div class="panel-icon">
            <i class="fas fa-door-open" />
          </div>
          <div class="panel-title">
            <h2>闸机操作</h2>
            <p>选择以下操作来控制闸机</p>
          </div>
          <div class="panel-status">
            <h2>{{ status }}</h2>
            <!-- <p>闸机状态正常</p> -->
          </div>
        </div>

        <!-- 操作按钮网格 -->
        <div class="control-grid">
          <!-- 闸机常开 -->
          <button class="control-btn open-btn" :disabled="loading" @click="handleControl('OPEN', '闸机常开', false)">
            <div class="btn-icon">
              <i class="fas fa-unlock" />
            </div>
            <div class="btn-text">
              <span class="btn-title">闸机常开</span>
              <span class="btn-desc">保持闸机开启状态</span>
            </div>
          </button>

          <!-- 闸机关门 -->
          <button class="control-btn close-btn" :disabled="loading" @click="handleControl('CLOSE', '闸机关门', false)">
            <div class="btn-icon">
              <i class="fas fa-lock" />
            </div>
            <div class="btn-text">
              <span class="btn-title">闸机关门</span>
              <span class="btn-desc">关闭闸机通道</span>
            </div>
          </button>

          <!-- 闸机报警 -->
          <button class="control-btn alarm-btn" :disabled="loading" @click="handleControl('ALARM', '闸机报警', false)">
            <div class="btn-icon">
              <i class="fas fa-bell" />
            </div>
            <div class="btn-text">
              <span class="btn-title">闸机报警</span>
              <span class="btn-desc">触发闸机报警</span>
            </div>
          </button>

          <!-- 左闸机开门 -->
          <button class="control-btn left-btn" :disabled="loading" @click="handleControl('SHOTOPEN', '闸机短开', false)">
            <div class="btn-icon">
              <i class="fas fa-arrow-left" />
            </div>
            <div class="btn-text">
              <span class="btn-title">闸机短开</span>
              <span class="btn-desc">闸机短开</span>
            </div>
          </button>

          <!-- 右闸机开门 -->
          <!-- <button class="control-btn right-btn" :disabled="loading" @click="handleControl('RIGHTOPEN', '右闸机开门', false)">
            <div class="btn-icon">
              <i class="fas fa-arrow-right" />
            </div>
            <div class="btn-text">
              <span class="btn-title">右闸机开门</span>
              <span class="btn-desc">开启右侧闸机</span>
            </div>
          </button> -->

          <button class="control-btn big-open-btn" :disabled="loading" @click="handleControl('OPEN', '大件运输常开', true)">
            <div class="btn-icon">
              <i class="fas fa-truck" />
            </div>
            <div class="btn-text">
              <span class="btn-title">大件运输常开</span>
              <span class="btn-desc">大件运输模式保持开启</span>
            </div>
          </button>

          <button class="control-btn big-close-btn" :disabled="loading" @click="handleControl('CLOSE', '大件运输关闭', true)">
            <div class="btn-icon">
              <i class="fas fa-truck-loading" />
            </div>
            <div class="btn-text">
              <span class="btn-title">关闭大件运输常开</span>
              <span class="btn-desc">大件运输模式保持关闭</span>
            </div>
          </button>

          <!-- 重启闸机 -->
          <button class="control-btn reboot-btn" :disabled="loading" @click="handleControl('REBOOT', '重启闸机', false)">
            <div class="btn-icon">
              <i class="fas fa-sync-alt" />
            </div>
            <div class="btn-text">
              <span class="btn-title">重启闸机</span>
              <span class="btn-desc">重新启动闸机系统</span>
            </div>
          </button>
        </div>
      </div>
    </main>

    <!-- Loading -->
    <div v-if="loading" class="loading-overlay">
      <div class="loading-dots">
        <div class="dot" />
        <div class="dot" />
        <div class="dot" />
      </div>
      <span class="loading-text">执行中...</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { showNotify } from 'vant';
import { controlGate, controlGateByBigInv, getGateStatus } from '../api/gate.js';
import { gateController } from '../hardware/GateOperate.js';
// 图片资源
import bgImg from '../assets/images/bj.png';
// import { plugin } from 'postcss';

const router = useRouter();

// 加载状态
const loading = ref(false);

const status = ref('');

// 返回主页
const goHome = () => {
    router.push('/home');
};


// 执行闸机控制
const handleControl = async (command, actionName, isBigInv) => {
    loading.value = true;
    try {
        let res = null;
        if (isBigInv) {
            res = await controlGateByBigInv(command);
        }
        // } else {
        //     res = await controlGate(command);
        // }
        gateController(command);
        
        showNotify({ type: 'success', message: `${actionName}操作成功`, duration: 2000 });
        // if (res.errorCode === 0) {
        //     showNotify({ type: 'success', message: `${actionName}操作成功`, duration: 2000 });
        //     if (actionName.includes('开')) {
        //         plugin.soundPlay.playOpen();
        //     } else if (actionName.includes('关')) {
        //         plugin.soundPlay.playClose();
        //     } else if (actionName.includes('报')) {
        //         plugin.soundPlay.playAlarm();
        //     }
        // } else {
        //     showNotify({ type: 'danger', message: res.errorMessage || `${actionName}操作失败`, duration: 3000 });
        // }
    } catch (error) {
        console.error('闸机控制失败:', error);
        showNotify({ type: 'danger', message: `${actionName}操作失败`, duration: 3000 });
    } finally {
        loading.value = false;
    }
};

// 获取闸机状态
const getStatus = async () => {
    loading.value = true;
    try {
        let res = await getGateStatus();
        if (res.errorCode === 0) {
            showNotify({ type: 'success', message: `闸机状态: ${res.data.gateStatus}`, duration: 3000 });
            status.value = res.data.gateStatus;
        } else {
            showNotify({ type: 'danger', message: res.errorMessage || '闸机状态获取失败', duration: 3000 });
        }
    } catch (error) {
        console.error('闸机状态获取失败:', error);
        showNotify({ type: 'danger', message: '闸机状态获取失败', duration: 3000 });
    } finally {
        loading.value = false;
    }
};

onMounted(() => {
    // getStatus();
});
</script>

<style scoped>
/* ========== 基础样式 ========== */
.control-gate-page {
  width: 100%;
  height: 100vh;
  max-height: 100vh;
  position: relative;
  font-family: 'Microsoft YaHei', sans-serif;
  color: #fff;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* 背景层 */
.bg-layer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #041c3d;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  z-index: 0;
}

/* ========== 顶部标题区域 ========== */
.header-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 30px;
  position: relative;
  z-index: 10;
  flex-shrink: 0;
}

.page-title {
  font-size: 28px;
  font-weight: bold;
  color: #fff;
  text-shadow: 0 2px 10px rgba(0, 191, 255, 0.5);
  margin: 0;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.logout-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: linear-gradient(90deg, #1a4a7a 0%, #0d3a5a 100%);
  border: 1px solid #2a7fff;
  border-radius: 8px;
  color: #7ec8ff;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.logout-btn:hover {
  background: linear-gradient(90deg, #2a5a8a 0%, #1d4a6a 100%);
  box-shadow: 0 0 15px rgba(42, 127, 255, 0.4);
}

.logout-btn i {
  font-size: 18px;
}

.header-actions {
  width: 160px;
  margin-top: 14px;
}

/* ========== 主内容区域 ========== */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 20px 30px;
  position: relative;
  z-index: 10;
  min-height: 0;
  overflow-y: auto;
}

/* ========== 控制面板卡片 ========== */
.control-panel {
  background: rgba(9, 61, 140, 0.5);
  border: 1px solid #049FD8;
  border-radius: 20px;
  padding: 40px 50px;
  width: 100%;
  max-width: 1200px;
}

.panel-header {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 35px;
}

.panel-icon {
  width: 70px;
  height: 70px;
  background: linear-gradient(135deg, #1e90ff 0%, #00bfff 100%);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  color: #fff;
  box-shadow: 0 8px 25px rgba(0, 191, 255, 0.3);
}

.panel-title h2 {
  font-size: 28px;
  font-weight: 600;
  color: #fff;
  margin: 0 0 8px 0;
}

.panel-title p {
  font-size: 16px;
  color: #7ec8ff;
  margin: 0;
}

.panel-status {
  flex: 1;
  text-align: right;
}

.panel-status h2 {
  font-size: 28px;
  font-weight: 600;
  color: #7ec8ff;
  margin: 0 0 8px 0;
}

.panel-status p {
  font-size: 16px;
  color: #7ec8ff;
  margin: 0;
}

/* ========== 控制按钮网格 ========== */
.control-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.control-btn {
  background: rgba(13, 58, 106, 0.6);
  border: 2px solid rgba(42, 127, 255, 0.4);
  border-radius: 16px;
  padding: 30px 25px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.control-btn:hover:not(:disabled) {
  transform: translateY(-5px);
  box-shadow: 0 10px 30px rgba(0, 191, 255, 0.3);
}

.control-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-icon {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  color: #fff;
  transition: all 0.3s;
}

.btn-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.btn-title {
  font-size: 20px;
  font-weight: 600;
  color: #fff;
}

.btn-desc {
  font-size: 14px;
  color: #7ec8ff;
}

/* 闸机常开 - 绿色 */
.open-btn {
  border-color: rgba(16, 185, 129, 0.5);
}

.open-btn:hover:not(:disabled) {
  border-color: #10b981;
  background: rgba(16, 185, 129, 0.2);
}

.open-btn .btn-icon {
  background: linear-gradient(135deg, #10b981 0%, #34d399 100%);
}

/* 闸机关门 - 红色 */
.close-btn {
  border-color: rgba(239, 68, 68, 0.5);
}

.close-btn:hover:not(:disabled) {
  border-color: #ef4444;
  background: rgba(239, 68, 68, 0.2);
}

.close-btn .btn-icon {
  background: linear-gradient(135deg, #ef4444 0%, #f87171 100%);
}

.big-open-btn {
  border-color: rgba(34, 211, 238, 0.5);
}

.big-open-btn:hover:not(:disabled) {
  border-color: #22d3ee;
  background: rgba(34, 211, 238, 0.2);
}

.big-open-btn .btn-icon {
  background: linear-gradient(135deg, #06b6d4 0%, #22d3ee 100%);
}

.big-close-btn {
  border-color: rgba(220, 38, 38, 0.55);
}

.big-close-btn:hover:not(:disabled) {
  border-color: #dc2626;
  background: rgba(220, 38, 38, 0.2);
}

.big-close-btn .btn-icon {
  background: linear-gradient(135deg, #dc2626 0%, #f87171 100%);
}

/* 闸机报警 - 橙色 */
.alarm-btn {
  border-color: rgba(245, 158, 11, 0.5);
}

.alarm-btn:hover:not(:disabled) {
  border-color: #f59e0b;
  background: rgba(245, 158, 11, 0.2);
}

.alarm-btn .btn-icon {
  background: linear-gradient(135deg, #f59e0b 0%, #fbbf24 100%);
}

/* 左闸机开门 - 蓝色 */
.left-btn {
  border-color: rgba(59, 130, 246, 0.5);
}

.left-btn:hover:not(:disabled) {
  border-color: #3b82f6;
  background: rgba(59, 130, 246, 0.2);
}

.left-btn .btn-icon {
  background: linear-gradient(135deg, #3b82f6 0%, #60a5fa 100%);
}

/* 右闸机开门 - 紫色 */
.right-btn {
  border-color: rgba(139, 92, 246, 0.5);
}

.right-btn:hover:not(:disabled) {
  border-color: #8b5cf6;
  background: rgba(139, 92, 246, 0.2);
}

.right-btn .btn-icon {
  background: linear-gradient(135deg, #8b5cf6 0%, #a78bfa 100%);
}

/* 重启闸机 - 青色 */
.reboot-btn {
  border-color: rgba(6, 182, 212, 0.5);
}

.reboot-btn:hover:not(:disabled) {
  border-color: #06b6d4;
  background: rgba(6, 182, 212, 0.2);
}

.reboot-btn .btn-icon {
  background: linear-gradient(135deg, #06b6d4 0%, #22d3ee 100%);
}

/* ========== Loading ========== */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(4, 28, 61, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  gap: 20px;
}

.loading-dots {
  display: flex;
  gap: 10px;
}

.loading-dots .dot {
  width: 16px;
  height: 16px;
  background: #00bfff;
  border-radius: 50%;
  animation: dotPulse 1.4s infinite ease-in-out both;
}

.loading-dots .dot:nth-child(1) {
  animation-delay: -0.32s;
}

.loading-dots .dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes dotPulse {

  0%,
  80%,
  100% {
    transform: scale(0);
    opacity: 0.5;
  }

  40% {
    transform: scale(1);
    opacity: 1;
  }
}

.loading-text {
  font-size: 18px;
  color: #7ec8ff;
}
.action-btn {
  color: #7ec8ff !important;
  font-size: 16px !important;
}
/* ========== 响应式 - 横屏 1920*1080 ========== */
@media screen and (orientation: landscape) {
  .header-section {
    padding: 42px 20px !important;
  }

  .page-title {
    font-size: 24px;
  }

  .logout-btn {
    padding: 8px 16px;
    font-size: 14px;
  }

  .action-btn {
    padding: 6px 14px;
    font-size: 13px;
    gap: 6px;
  }

  .action-btn i {
    font-size: 14px;
  }

  .main-content {
    padding: 20px 40px;
    justify-content: center;
  }

  .control-panel {
    padding: 35px 45px;
    max-width: 95%;
  }

  .panel-header {
    margin-bottom: 30px;
    gap: 18px;
  }

  .panel-icon {
    width: 60px;
    height: 60px;
    font-size: 28px;
  }

  .panel-title h2 {
    font-size: 24px;
  }

  .panel-title p {
    font-size: 15px;
  }

  .control-grid {
    gap: 20px;
  }

  .control-btn {
    padding: 28px 20px;
    gap: 15px;
  }

  .btn-icon {
    width: 65px;
    height: 65px;
    font-size: 28px;
  }

  .btn-title {
    font-size: 18px;
  }

  .btn-desc {
    font-size: 13px;
  }
}

/* ========== 响应式 - 竖屏 1080*1920 ========== */
@media screen and (orientation: portrait) {
  .header-section {
    padding: 64px 20px !important;
  }

  .page-title {
    font-size: 32px;
  }

  .logout-btn {
    padding: 12px 24px;
    font-size: 18px;
  }

  .main-content {
    padding: 30px;
    justify-content: flex-start;
  }

  .control-panel {
    padding: 40px 35px;
    max-width: 100%;
  }

  .panel-header {
    margin-bottom: 40px;
    gap: 20px;
  }

  .panel-icon {
    width: 70px;
    height: 70px;
    font-size: 32px;
  }

  .panel-title h2 {
    font-size: 28px;
  }

  .panel-title p {
    font-size: 16px;
  }

  .control-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
  }

  .control-btn {
    padding: 30px 20px;
    gap: 15px;
  }

  .btn-icon {
    width: 65px;
    height: 65px;
    font-size: 28px;
  }

  .btn-title {
    font-size: 20px;
  }

  .btn-desc {
    font-size: 14px;
  }

  .loading-dots .dot {
    width: 20px;
    height: 20px;
  }

  .loading-text {
    font-size: 22px;
  }
}
</style>