<template>
  <div class="robotic-arm-panel">
    <!-- 标题区域 -->
    <div class="panel-title">
      <!-- 左侧标题+状态指示灯组合 -->
      <div class="title-with-status">
        <div class="title-left">
          <div class="title-text">机械臂</div>
          <div class="title-line"></div>
        </div>

        <!-- 状态指示灯：紧挨着标题文字右侧 -->
        <div class="status-indicator">
          <div class="status-light" :class="statusClass"></div>
          <div class="status-text">{{ statusText }}</div>
        </div>
      </div>

      <!-- 上料按钮 -->
      <div class="title-right">
        <button class="add-button" @click="showAddDialog = true">
          <i class="el-icon-plus"></i> 上料
        </button>
        <button class="home-button" @click="handleHome">
          <i class="el-icon-refresh"></i> 回零
        </button>
        <button class="stop-button" @click="handleStop">
          <i class="el-icon-circle-close"></i> 停止
        </button>
      </div>
    </div>

    <!-- 扫码对话框 -->
    <div class="dialog-mask" v-if="showAddDialog" @click.self="closeDialog">
      <div class="dialog-container">
        <div class="dialog-header">上料操作</div>
        <div class="dialog-body">
          <div class="dialog-content">
            <div class="scan-prompt">请扫描托盘二维码</div>
            <div class="scan-input">
              <input
                type="text"
                v-model="trayCode"
                placeholder="手动输入或扫码"
                ref="trayInput"
                @keyup.enter="confirmAdd"
              >
            </div>
          </div>
        </div>
        <div class="dialog-footer">
          <button class="cancel-button" @click="closeDialog">取消</button>
          <button class="confirm-button" @click="confirmAdd">确定</button>
        </div>
      </div>
    </div>

    <!-- 主内容区：指令区+机械臂 -->
    <div class="main-content">
      <!-- 左侧：待上料指令 -->
      <div class="loading-command">
        <div class="command-title">待上料指令</div>
        <div class="command-list">
          <div
            v-for="(cmd, index) in loadingCommands"
            :key="index"
            class="command-item"
            :class="getCommandStatusClass(cmd.status)"
            @click="handleCommandClick(cmd)"
          >
            <div class="cmd-info">
              <div class="cmd-tray">托盘: {{ cmd.trayCode }}</div>
              <div class="cmd-position">位置: {{ cmd.position }}</div>
              <div class="cmd-status">状态: {{ getStatusText(cmd.status) }}</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 中间：机械臂主体 -->
      <div class="arm-center-wrapper">
        <div class="robotic-arm-container">
          <div class="robotic-arm">
            <!-- 机械臂各部件 -->
            <div class="arm-base">
              <div class="base-top"></div>
              <div class="base-bottom"></div>
            </div>
            <div class="arm-joint joint-1">
              <div class="joint-body"></div>
            </div>
            <div class="arm-segment segment-1">
              <div class="segment-body"></div>
            </div>
            <div class="arm-joint joint-2">
              <div class="joint-body"></div>
            </div>
            <div class="arm-segment segment-2">
              <div class="segment-body"></div>
            </div>
            <div class="arm-gripper">
              <div class="gripper-left"></div>
              <div class="gripper-right"></div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧：待下料指令 -->
      <div class="unloading-command">
        <div class="command-title">待下料指令</div>
        <div class="command-list">
          <div
            v-for="(cmd, index) in unloadingCommands"
            :key="index"
            class="command-item"
            :class="getCommandStatusClass(cmd.status)"
            @click="handleCommandClick(cmd)"
          >
            <div class="cmd-info">
              <div class="cmd-tray">托盘: {{ cmd.trayCode }}</div>
              <div class="cmd-position">位置: {{ cmd.position }}</div>
              <div class="cmd-status">状态: {{ getStatusText(cmd.status) }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>


    <!-- 上电对话框 -->
    <div class="dialog-mask" v-if="showPowerOnDialog" @click.self="closePowerOnDialog">
      <div class="dialog-container">
        <div class="dialog-header">上电操作</div>
        <div class="dialog-body">
          <div class="dialog-content">
            <div class="scan-prompt">确认对以下托盘执行上电操作？</div>
            <div class="power-on-info">
              <div><span class="label">托盘编号:</span> {{ selectedCommand.trayCode }}</div>
              <div><span class="label">位置:</span> {{ selectedCommand.position }}</div>
            </div>
          </div>
        </div>
        <div class="dialog-footer">
          <button class="cancel-button" @click="closePowerOnDialog">取消</button>
          <button class="confirm-button" @click="confirmPowerOn">确认上电</button>
        </div>
      </div>
    </div>

    <!-- 确认执行完成对话框 -->
    <div class="dialog-mask" v-if="showSureCompleteDialog" @click.self="closePowerOnDialog">
      <div class="dialog-container">
        <div class="dialog-header">确认执行完成</div>
        <div class="dialog-body">
          <div class="dialog-content">
            <div class="scan-prompt">确认此条指令执行完成？</div>
            <div class="power-on-info">
              <div><span class="label">托盘编号:</span> {{ selectedCommand.trayCode }}</div>
              <div><span class="label">位置:</span> {{ selectedCommand.position }}</div>
            </div>
          </div>
        </div>
        <div class="dialog-footer">
          <button class="cancel-button" @click="closeSureCompleteDialog">取消</button>
          <button class="confirm-button" @click="confirmSureComplete">确认完成</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import {addCommand,powerOnCommand,sendHomeCommand,sendStopCommand,sureCompletedCommand} from "@/api/robotArm/robotArmCommand"

export default {
  name: 'RoboticArm',
  data() {
    return {
      status: 'idle', // idle, running, error
      showAddDialog: false,
      trayCode: '',
      loadingCommands: [
      ],
      unloadingCommands: [
      ],
      websocket: null,
      reconnectInterval: null,
      showPowerOnDialog: false,
      showSureCompleteDialog: false,
      selectedCommand: null
    };
  },
  computed: {
    statusClass() {
      return {
        'idle': this.status === 'idle',
        'running': this.status === 'running',
        'error': this.status === 'error',
        'unknown': this.status === 'unknown'
      };
    },
    statusText() {
      return {
        'idle': '空闲中',
        'running': '运行中',
        'error': '故障',
        'unknown': '未知'
      }[this.status];
    }
  },
  mounted() {
    this.initWebSocket();
  },
  beforeDestroy() {
    this.disconnectWebSocket();
  },
  watch: {
    showAddDialog(val) {
      if (val) {
        this.$nextTick(() => {
          this.$refs.trayInput.focus();
        });
      } else {
        this.trayCode = '';
      }
    }
  },
  methods: {
    initWebSocket() {
      // 从环境变量获取基础URL，默认使用Nginx代理地址
      const backendUrl = process.env.VUE_APP_API_BASE_URL || 'http://localhost:8082';
      // 根据需要切换不同的WebSocket端点
      const wsPath = '/agecal/ws-robot-arm'; // 或 '/agecal/ws-aging-cabinet'

      // 替换协议并添加完整路径
      const wsUrl = backendUrl.replace('http', 'ws') + wsPath;

      try {
        this.websocket = new WebSocket(wsUrl);

        this.websocket.onopen = () => {
          console.log('机械臂指令WebSocket连接成功');
          this.status = 'idle';
          this.sendWebSocketMessage({ type: 'request', commands: ['loading', 'unloading'] });
        };

        this.websocket.onmessage = (event) => {
          try {
            const message = JSON.parse(event.data);
            if (message.type === 'loading') {
              this.loadingCommands = message.data.map(cmd => ({
                robotArmCommandId: cmd.robotArmCommandId,
                trayCode: cmd.trayCode,
                position: cmd.storeyCode,
                status: cmd.status || '0' // 默认待执行状态
              }));
            }
            else if (message.type === 'unloading') {
              this.unloadingCommands = message.data.map(cmd => ({
                robotArmCommandId: cmd.robotArmCommandId,
                trayCode: cmd.trayCode,
                position: cmd.storeyCode,
                status: cmd.status || '0' // 默认待执行状态
              }));
            }
            // 新增：处理状态更新消息
            else if (message.type === 'status') {
              this.status = message.data; // 更新机械臂状态
            }
          } catch (e) {
            console.error('解析WebSocket消息失败:', e);
          }
        };

        this.websocket.onerror = (error) => {
          console.error('WebSocket错误:', error);
          this.status = 'error';
          this.scheduleReconnect();
        };

        this.websocket.onclose = () => {
          console.log('WebSocket连接关闭');
          this.scheduleReconnect();
        };

      } catch (e) {
        console.error('创建WebSocket失败:', e);
        this.scheduleReconnect();
      }
    },

    sendWebSocketMessage(message) {
      if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
        this.websocket.send(JSON.stringify(message));
      }
    },

    scheduleReconnect() {
      if (this.reconnectInterval) clearInterval(this.reconnectInterval);
      this.reconnectInterval = setInterval(() => {
        if (!this.websocket || this.websocket.readyState !== WebSocket.OPEN) {
          console.log('尝试重新连接WebSocket...');
          this.disconnectWebSocket();
          this.initWebSocket();
        }
      }, 5000);
    },

    disconnectWebSocket() {
      if (this.websocket) {
        try {
          this.websocket.close();
        } catch (e) {
          console.error('关闭WebSocket时出错:', e);
        }
      }
      if (this.reconnectInterval) {
        clearInterval(this.reconnectInterval);
        this.reconnectInterval = null;
      }
      this.websocket = null;
    },

    // 回零
    handleHome() {
      sendHomeCommand().then( response => {
        this.$message.success("已发送");
      })
    },

    // 停止
    handleStop() {
      sendStopCommand().then( response => {
        this.$message.success("已发送");
      })
    },

    closeDialog() {
      this.showAddDialog = false;
    },

    confirmAdd() {
      if (!this.trayCode.trim()) {
        alert('请输入托盘编号');
        return;
      }

      const robotArmCommand = {
        trayCode: this.trayCode,
        storeyCode: '待分配位置',
        type: '0'
      };
      addCommand(robotArmCommand).then(res => {
        if(res.code === 200) {
          this.msgSuccess("添加成功");
          this.showAddDialog = false;
        } else {
          this.msgSuccess("添加失败");
        }
      })
    },

    // 更新状态文本映射
    getStatusText(status) {
      const statusMap = {
        '0': '待分配位置',
        '1': '待执行',
        '2': '执行中',
        '3': '未上电',
        '4': '执行结束'
      };
      return statusMap[status] || '未知状态';
    },
    // 更新状态样式类映射
    getCommandStatusClass(status) {
      return {
        'status-0': status == '0',   // 待分配位置
        'status-1': status == '1',   // 待执行
        'status-2': status == '2',   // 执行中
        'status-3': status == '3',   // 未上电
        'status-4': status == '4'    // 执行结束
      };
    },

    handleCommandClick(cmd) {
      // 只有未上电状态的指令才可点击
      if (cmd.status === '3') {
        this.selectedCommand = cmd;
        this.showPowerOnDialog = true;
      } else if(cmd.status === '2') {
        this.selectedCommand = cmd;
        this.showSureCompleteDialog = true;
      }
    },

    closeSureCompleteDialog() {
      this.showSureCompleteDialog = false;
      this.selectedCommand = null;
    },
    closePowerOnDialog() {
      this.showPowerOnDialog = false;
      this.selectedCommand = null;
    },
    confirmSureComplete() {
      if (!this.selectedCommand) return;
      sureCompletedCommand(this.selectedCommand.robotArmCommandId).then(res => {
        if(res.code === 200) {
          this.msgSuccess("确认完成成功");

          this.closeSureCompleteDialog();
        } else {
          this.msgError("确认完成失败");
        }
      }).catch(err => {
        this.msgError("确认完成失败");
      })
    },
    confirmPowerOn() {
      if (!this.selectedCommand) return;

      powerOnCommand(this.selectedCommand.robotArmCommandId).then(res => {
        if(res.code === 200) {
          this.msgSuccess("上电操作成功");

          this.closePowerOnDialog();
        } else {
          this.msgError("上电操作失败");
        }
      }).catch(err => {
        this.msgError("上电操作失败");
      });
    },
  }
};
</script>

<style scoped>
/* 标题区域样式 */

.panel-title {
  position: absolute;
  top: 20px;
  left: 20px;
  right: 20px;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between; /* 标题组合居左，按钮居右 */
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid rgba(64, 158, 255, 0.3);
}

/* 标题+状态指示灯组合容器：水平排列 */
.title-with-status {
  display: flex;
  align-items: center; /* 垂直居中对齐 */
  gap: 15px; /* 标题与状态指示灯之间的间距 */
}

.title-left {
  display: flex;
  flex-direction: column;
}

.title-text {
  font-size: 22px;
  font-weight: bold;
  color: #ffffff;
  letter-spacing: 1px;
  text-shadow: 0 0 10px rgba(64, 158, 255, 0.5);
}

.title-line {
  height: 3px;
  width: 100px;
  background: linear-gradient(to right, #409EFF, transparent);
  margin-top: 8px;
  border-radius: 2px;
}

/* 状态指示灯：紧挨着标题文字右侧 */
.status-indicator {
  display: flex;
  align-items: center;
  padding: 6px 12px;
  background: rgba(0, 30, 60, 0.4);
  border-radius: 20px;
  border: 1px solid rgba(64, 158, 255, 0.3);
  margin: 0; /* 清除原有margin */
}

.status-light {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 8px;
  box-shadow: 0 0 6px currentColor;
}

.status-light.idle {
  background-color: #64ffaa;
  color: #64ffaa;
}

.status-light.running {
  background-color: #409EFF;
  color: #409EFF;
  animation: pulse 1.5s infinite;
}

.status-light.error {
  background-color: #ff4d4f;
  color: #ff4d4f;
  animation: blink 1s infinite;
}

/* 添加未知状态样式 */
.status-light.unknown {
  background-color: #a0a0a0;
  color: #a0a0a0;
  animation: blink-gray 1s infinite;
}
@keyframes blink-gray {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.status-text {
  font-size: 14px;
  font-weight: bold;
  color: #64c8ff;
}

.title-right {
  display: flex;
  align-items: center;
}
/* 回零按钮样式 */
.home-button {
  background: linear-gradient(to right, #67C23A, #85ce61);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  transition: all 0.3s;
  margin-left: 10px; /* 与其他按钮保持间距 */
}

.home-button:hover {
  background: linear-gradient(to right, #85ce61, #67C23A);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
}

.home-button i {
  margin-right: 5px;
}

/* 停止按钮样式 */
.stop-button {
  background: linear-gradient(to right, #F56C6C, #f78989);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  transition: all 0.3s;
  margin-left: 10px; /* 与其他按钮保持间距 */
}

.stop-button:hover {
  background: linear-gradient(to right, #f78989, #F56C6C);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
}

.stop-button i {
  margin-right: 5px;
}
.add-button {
  background: linear-gradient(to right, #409EFF, #64c8ff);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  transition: all 0.3s;
}

.add-button:hover {
  background: linear-gradient(to right, #64c8ff, #409EFF);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
}

.add-button i {
  margin-right: 5px;
}

/* 对话框样式（不变） */
.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-container {
  width: 400px;
  background: linear-gradient(145deg, #1a2a3a, #0f1c2a);
  border-radius: 10px;
  border: 1px solid rgba(64, 158, 255, 0.4);
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.7);
  overflow: hidden;
}

.dialog-header {
  padding: 16px 20px;
  background: rgba(0, 30, 60, 0.8);
  border-bottom: 1px solid rgba(64, 158, 255, 0.3);
  font-size: 18px;
  font-weight: bold;
  color: #64c8ff;
  text-align: center;
}

.dialog-body {
  padding: 30px 20px;
}

.dialog-content {
  text-align: center;
}

.scan-prompt {
  font-size: 18px;
  color: #a0d2ff;
  margin-bottom: 20px;
}

.scan-input input {
  width: 100%;
  padding: 12px 15px;
  background: rgba(0, 20, 40, 0.6);
  border: 1px solid rgba(64, 158, 255, 0.5);
  border-radius: 6px;
  color: white;
  font-size: 16px;
  outline: none;
  transition: all 0.3s;
}

.scan-input input:focus {
  border-color: #409EFF;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.5);
}

.dialog-footer {
  padding: 15px 20px;
  display: flex;
  justify-content: flex-end;
  background: rgba(0, 20, 40, 0.5);
  border-top: 1px solid rgba(64, 158, 255, 0.3);
}

.cancel-button, .confirm-button {
  padding: 8px 20px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s;
  margin-left: 10px;
}

.cancel-button {
  background: rgba(100, 100, 100, 0.4);
  color: #a0d2ff;
  border: 1px solid rgba(100, 150, 255, 0.3);
}

.cancel-button:hover {
  background: rgba(120, 120, 120, 0.5);
  border-color: rgba(100, 150, 255, 0.5);
}

.confirm-button {
  background: linear-gradient(to right, #409EFF, #64c8ff);
  color: white;
  border: none;
}

.confirm-button:hover {
  background: linear-gradient(to right, #64c8ff, #409EFF);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

/* 外层容器（不变） */
.robotic-arm-panel {
  width: 100%;
  height: 100%;
  min-height: 100%;
  background: rgba(10, 20, 40, 0.85);
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(64, 158, 255, 0.3);
  box-shadow: 0 0 20px rgba(0, 50, 120, 0.3);
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
}

/* 主内容区（不变） */
.main-content {
  display: flex;
  flex: 1;
  gap: 15px;
  min-height: 0;
  overflow: hidden;
  margin-top: 60px; /* 为标题区域留出空间 */
}

.arm-center-wrapper {
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 0;
}

.robotic-arm-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

.robotic-arm {
  position: relative;
  width: 200px;
  height: 330px;
  transform: scale(0.9);
}

/* 指令区域（不变） */
.loading-command, .unloading-command {
  width: 160px;
  min-width: 160px;
  background: rgba(0, 30, 60, 0.4);
  border-radius: 8px;
  padding: 12px;
  border: 1px solid rgba(64, 158, 255, 0.3);
  display: flex;
  flex-direction: column;
  height: 100%;
  box-sizing: border-box;
}

.command-title {
  font-size: 15px;
  font-weight: bold;
  color: #64c8ff;
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid rgba(100, 180, 255, 0.2);
}

.command-list {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.command-list::-webkit-scrollbar {
  width: 6px;
}

.command-list::-webkit-scrollbar-thumb {
  background: rgba(64, 158, 255, 0.5);
  border-radius: 3px;
}

.command-item {
  padding: 8px;
  margin-bottom: 8px;
  background: rgba(0, 40, 80, 0.3);
  border-radius: 6px;
  transition: all 0.3s;
  border: 1px solid transparent;
}

.command-item:hover {
  border-color: rgba(100, 200, 255, 0.5);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.cmd-info {
  width: 100%;
  font-size: 13px;
}

.cmd-tray, .cmd-position {
  margin-bottom: 4px;
  color: #a0d2ff;
  display: flex;
  justify-content: space-between;
}

/* 机械臂各部件样式（不变） */
.arm-base {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 20px;
  z-index: 10;
}

.base-top {
  width: 100%;
  height: 10px;
  background: #3a506b;
  border-radius: 5px 5px 0 0;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}

.base-bottom {
  width: 120%;
  margin-left: -10%;
  height: 10px;
  background: #2c3e50;
  border-radius: 0 0 8px 8px;
}

.arm-joint {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(145deg, #1e2a3a, #2c3e50);
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9;
}

.joint-body {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #3a506b;
  border: 2px solid #409EFF;
}

.joint-1 {
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  animation: rotateJoint1 8s infinite linear;
}

.arm-segment {
  position: absolute;
  background: #3a506b;
  border: 1px solid rgba(64, 158, 255, 0.5);
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  transform-origin: top center;
}

.segment-body {
  height: 100%;
  background: linear-gradient(to right, #2c3e50, #3a506b, #2c3e50);
  border-left: 1px solid rgba(100, 200, 255, 0.3);
  border-right: 1px solid rgba(100, 200, 255, 0.3);
}

.segment-1 {
  width: 20px;
  height: 120px;
  bottom: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 8;
  animation: moveSegment1 8s infinite ease-in-out;
}

.joint-2 {
  bottom: 180px;
  left: 50%;
  transform: translateX(-50%);
  animation: rotateJoint2 8s infinite linear reverse;
}

.segment-2 {
  width: 15px;
  height: 100px;
  bottom: 220px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 7;
  animation: moveSegment2 8s infinite ease-in-out;
}

.arm-gripper {
  position: absolute;
  bottom: 320px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 30px;
  display: flex;
  justify-content: space-between;
  z-index: 6;
  animation: moveGripper 8s infinite ease-in-out;
}

.gripper-left, .gripper-right {
  width: 10px;
  height: 30px;
  background: #3a506b;
  border: 1px solid rgba(64, 158, 255, 0.5);
  border-radius: 5px;
}

.gripper-left {
  transform: rotate(-15deg);
}

.gripper-right {
  transform: rotate(15deg);
}

/* 动画效果（不变） */
@keyframes rotateJoint1 {
  0%, 100% { transform: translateX(-50%) rotate(0deg); }
  25% { transform: translateX(-50%) rotate(45deg); }
  50% { transform: translateX(-50%) rotate(0deg); }
  75% { transform: translateX(-50%) rotate(-45deg); }
}

@keyframes rotateJoint2 {
  0%, 100% { transform: translateX(-50%) rotate(0deg); }
  25% { transform: translateX(-50%) rotate(30deg); }
  50% { transform: translateX(-50%) rotate(0deg); }
  75% { transform: translateX(-50%) rotate(-30deg); }
}

@keyframes moveSegment1 {
  0%, 100% { height: 120px; }
  50% { height: 140px; }
}

@keyframes moveSegment2 {
  0%, 100% { height: 100px; }
  50% { height: 80px; }
}

@keyframes moveGripper {
  0%, 100% { transform: translateX(-50%); }
  25% { transform: translateX(-60%); }
  75% { transform: translateX(-40%); }
}

@keyframes pulse {
  0% { opacity: 0.6; }
  50% { opacity: 1; }
  100% { opacity: 0.6; }
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* 响应式调整 */
@media (max-width: 800px) {
  .main-content {
    flex-direction: column;
    align-items: center;
    gap: 15px;
    overflow-y: auto;
  }

  .arm-center-wrapper {
    width: 100%;
    min-height: 350px;
  }

  .loading-command, .unloading-command {
    width: 90%;
    max-width: 300px;
    height: auto;
    min-height: 200px;
  }

  /* 标题区域响应式调整 */
  .title-with-status {
    flex-direction: column; /* 小屏幕下标题和状态指示灯垂直排列 */
    align-items: flex-start;
    gap: 8px;
  }

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

  .panel-title {
    flex-wrap: wrap;
  }

  .title-right {
    margin-top: 10px;
    order: 3;
  }
}

@media (max-width: 500px) {
  .robotic-arm {
    transform: scale(0.8);
  }

  .command-title {
    font-size: 14px;
  }

  .cmd-info {
    font-size: 12px;
  }

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

  .title-line {
    width: 80px;
  }

  .dialog-container {
    width: 90%;
  }
}

@media (max-width: 300px) {
  .robotic-arm {
    transform: scale(0.7);
  }

  .loading-command, .unloading-command {
    width: 100%;
    min-width: auto;
  }
}
/* 指令状态样式（核心修改部分） */
.command-item.status-0 {
  /* 待分配位置：紫蓝色调 */
  background: rgba(50, 50, 100, 0.3);
  border: 1px solid rgba(160, 160, 255, 0.5);
}

.command-item.status-1 {
  /* 待执行：天蓝色调（保留原待执行样式基础） */
  background: rgba(0, 40, 80, 0.3);
  border: 1px solid rgba(100, 200, 255, 0.5);
}

.command-item.status-2 {
  /* 执行中：青绿色调，带动态效果 */
  background: rgba(0, 80, 40, 0.3);
  border: 1px solid rgba(100, 255, 170, 0.5);
  animation: pulse-green 1.5s infinite; /* 保持原执行中动画 */
}

.command-item.status-3 {
  /* 未上电：红色调，带提醒效果 */
  background: rgba(80, 0, 0, 0.3);
  border: 1px solid rgba(255, 100, 100, 0.5);
  cursor: pointer;
  animation: blink-red 1s infinite; /* 保持原未上电动画 */
}

.command-item.status-4 {
  /* 执行结束：灰色调，无动态效果 */
  background: rgba(60, 60, 60, 0.3);
  border: 1px solid rgba(180, 180, 180, 0.5);
}

/* 状态文本颜色（核心修改部分） */
.status-0 .cmd-status {
  color: #a0a0ff; /* 待分配位置文本色 */
}

.status-1 .cmd-status {
  color: #64c8ff; /* 待执行文本色 */
}

.status-2 .cmd-status {
  color: #64ffaa; /* 执行中文本色 */
}

.status-3 .cmd-status {
  color: #ff6464; /* 未上电文本色 */
}

.status-4 .cmd-status {
  color: #a0a0a0; /* 执行结束文本色 */
}

/* 状态文本样式 */
.cmd-status {
  font-size: 12px;
  margin-top: 4px;
  font-weight: bold;
}

.status-executing .cmd-status {
  color: #64ffaa;
}

.status-poweroff .cmd-status {
  color: #ff6464;
}

.status-completed .cmd-status {
  color: #a0a0a0;
}

/* 上电对话框样式 */
.power-on-info {
  background: rgba(0, 20, 40, 0.4);
  padding: 15px;
  border-radius: 6px;
  margin-top: 15px;
  text-align: left;
}

.power-on-info div {
  margin-bottom: 10px;
}

.power-on-info .label {
  display: inline-block;
  width: 80px;
  color: #64c8ff;
  font-weight: bold;
}

/* 新增动画 */
@keyframes pulse-green {
  0% { opacity: 0.8; box-shadow: 0 0 5px rgba(100, 255, 180, 0.5); }
  50% { opacity: 1; box-shadow: 0 0 15px rgba(100, 255, 180, 0.8); }
  100% { opacity: 0.8; box-shadow: 0 0 5px rgba(100, 255, 180, 0.5); }
}

@keyframes blink-red {
  0%, 100% { opacity: 0.8; }
  50% { opacity: 0.5; }
}

/* 响应式调整 */
@media (max-width: 500px) {
  .cmd-status {
    font-size: 11px;
  }
}
</style>
