<template>
  <view class="container">
    <!-- 设备头部信息区域 -->
    <view class="device-header">
      <view class="device-info-row">
        <view class="device-name-container">
          <text class="device-name">{{ deviceInfo.machineName }}</text>
          <view 
            class="status-tag" 
            :class="deviceInfo.onlineStatus == 1 ? 'status-online' : 'status-offline'"
          >
            <view class="status-dot" :class="{'offline-dot': deviceInfo.onlineStatus == 0}"></view>
            {{ deviceInfo.onlineStatus == 1 ? '在线' : '离线' }}  
          </view>
        </view>
        
        <view class="refresh-btn" :class="{'refreshing': isRefreshing}" @tap="refreshData">
          <image src="/static/icons/refresh_icon.png" mode="aspectFit" style="width: 22px; height: 22px;"></image>
        </view>
      </view>
      
      <view class="device-meta-row">
        <view class="device-meta-item">
          <view class="meta-icon">
            <image src="/static/icons/device_icon.png" mode="aspectFit" style="width: 36rpx; height: 36rpx;"></image>
          </view>
          <text class="meta-label">设备编号：</text>
          <text class="meta-value">{{ deviceInfo.machineCode }}</text>
        </view>
        
        <view class="device-meta-item">
          <view class="meta-icon">
            <image src="/static/icons/location_icon.png" mode="aspectFit" style="width: 36rpx; height: 36rpx;"></image>
          </view>
          <text class="meta-label">工作区域：</text>
          <text class="meta-value">{{ deviceInfo.location }}</text>
        </view>
        
        <view class="device-meta-item">
          <view class="meta-icon">
            <image src="/static/icons/clock_icon.png" mode="aspectFit" style="width: 36rpx; height: 36rpx;"></image>
          </view>
          <text class="meta-label">最近更新：</text>
          <text class="meta-value">{{ deviceInfo.updateTime }}</text>
        </view>
      </view>
    </view>

    <!-- 作业任务列表（竖向排列） -->
    <view class="task-section">
      <view class="section-title task-title-row">
        <view class="task-title-left">
          <text class="task-title-text">作业任务列表</text>
          <text class="task-title-sub">配置并选择当前设备的作业</text>
        </view>
        <view class="task-add-btn" @click.stop="goToCreateJob">
          <text class="task-add-plus">+</text>
        </view>
      </view>

      <view v-if="taskList.length" class="task-list">
        <view
          v-for="task in taskList"
          :key="task.id"
          class="task-card"
          :class="{ active: task.id === selectedTaskId }"
          @click="selectTask(task)"
        >
          <view class="task-header">
            <view class="task-icon">
              <image src="/static/icons/task_icon.png" mode="aspectFit" class="task-icon-img"></image>
            </view>
            <view class="task-title-wrap">
              <text class="task-title">{{ task.name }}</text>
              <text class="task-sub">{{ task.fieldArea }}</text>
            </view>
            <view class="task-area-type">{{ task.areaTypeText }}</view>
            <view class="task-radio">
              <view class="radio-outer" :class="{ checked: task.id === selectedTaskId }">
                <view class="radio-inner"></view>
              </view>
            </view>
          </view>
          <view class="task-meta">
            <text class="task-time">计划时间：{{ task.planTime }}</text>
            <view class="task-meta-right">
              <text class="task-status">{{ task.statusText }}</text>
              <button class="task-delete-btn" @click.stop="confirmDeleteTask(task)">删除</button>
            </view>
          </view>
        </view>
      </view>

      <view v-else class="task-empty">
        <text class="task-empty-text">当前设备暂无作业任务</text>
      </view>
    </view>

    <!-- 设备状态信息区域 -->
    <!-- <view class="status-section">
      <view class="section-title">
        <text>设备状态</text>
      </view>
      
      <view class="status-grid">
        <!-- 作业状态
        <view class="status-item" hover-class="status-item-hover">
          <text class="status-label">作业状态</text>
          <view class="status-value-container">
            <text class="status-value" :class="{
              'status-working': deviceInfo.workStatus === 'working',
              'status-idle': deviceInfo.workStatus === 'idle',
              'status-error': deviceInfo.workStatus === 'error'
            }">{{ getWorkStatusText(deviceInfo.workStatus) }}</text>
          </view>
          <text class="status-time">{{ deviceInfo.statusUpdateTime || '1分钟前' }}</text>
        </view>
        
        <!-- 电量状态
        <view class="status-item" hover-class="status-item-hover">
          <text class="status-label">电量</text>
          <view class="status-value-container">
            <text class="status-value" :class="{
              'value-warning': deviceInfo.battery < 30,
              'value-alert': deviceInfo.battery < 15
            }">{{ deviceInfo.battery || 85 }}</text>
            <text class="status-unit">%</text>
          </view>
          <text class="status-time">{{ deviceInfo.batteryUpdateTime || '30秒前' }}</text>
        </view>
        
        <!-- GPS信号
        <view class="status-item" hover-class="status-item-hover">
          <text class="status-label">GPS信号</text>
          <view class="status-value-container">
            <text class="status-value" :class="{
              'status-good': deviceInfo.gpsSignal >= 80,
              'status-normal': deviceInfo.gpsSignal >= 60 && deviceInfo.gpsSignal < 80,
              'status-weak': deviceInfo.gpsSignal < 60
            }">{{ getGpsSignalText(deviceInfo.gpsSignal) }}</text>
          </view>
          <text class="status-time">{{ deviceInfo.gpsUpdateTime || '15秒前' }}</text>
        </view>
        
        <!-- 工作时长
        <view class="status-item" hover-class="status-item-hover">
          <text class="status-label">今日工作</text>
          <view class="status-value-container">
            <text class="status-value">{{ deviceInfo.workDuration || '2.5' }}</text>
            <text class="status-unit">小时</text>
          </view>
          <text class="status-time">累计时长</text>
        </view>
        
        <!-- 作业面积
        <view class="status-item" hover-class="status-item-hover">
          <text class="status-label">今日面积</text>
          <view class="status-value-container">
            <text class="status-value">{{ deviceInfo.workArea || '15.6' }}</text>
            <text class="status-unit">亩</text>
          </view>
          <text class="status-time">已完成</text>
        </view>
        
        <!-- 当前速度
        <view class="status-item" hover-class="status-item-hover">
          <text class="status-label">当前速度</text>
          <view class="status-value-container">
            <text class="status-value">{{ deviceInfo.currentSpeed || '0' }}</text>
            <text class="status-unit">km/h</text>
          </view>
          <text class="status-time">{{ deviceInfo.speedUpdateTime || '实时' }}</text>
        </view>
      </view>
    </view> -->

    <!-- 设备控制区域 
    <view class="control-section">
      <view class="section-title">
        <text>设备控制</text>
        <view class="engine-status" :class="{'engine-on': isEngineOn}">
          <text>{{ isEngineOn ? '引擎开启' : '引擎关闭' }}</text>
        </view>
      </view>
      
      <view class="control-container">
        <!-- 启动/停止按钮
        <view class="engine-control">
          <view 
            class="engine-button" 
            :class="{'engine-on': isEngineOn, 'disabled': deviceInfo.status === 'offline'}"
            @click="toggleEngine"
          >
            <view class="engine-icon">
              <image 
                :src="isEngineOn ? '/static/icons/engine_stop.svg' : '/static/icons/engine_start.svg'" 
                mode="aspectFit" 
                style="width: 28px; height: 28px;"
              ></image>
            </view>
            <text class="engine-text">{{ isEngineOn ? '停止' : '启动' }}</text>
          </view>
        </view>
        
        <!-- 方向控制
        <view class="direction-control">
          <!-- 前进按钮 
          <view class="control-btn control-forward" 
                :class="{'disabled': !isEngineOn || deviceInfo.status === 'offline', 'active': activeControl === 'forward'}"
                @touchstart="startControl('forward')" 
                @touchend="stopControl">
            <image src="/static/icons/arrow_up.svg" mode="aspectFit" style="width: 24px; height: 24px;"></image>
            <text>前进</text>
          </view>
          
          <!-- 左右控制行 
          <view class="control-row">
            <!-- 左转按钮 
            <view class="control-btn control-left" 
                  :class="{'disabled': !isEngineOn || deviceInfo.status === 'offline', 'active': activeControl === 'left'}"
                  @touchstart="startControl('left')" 
                  @touchend="stopControl">
              <image src="/static/icons/arrow_left.svg" mode="aspectFit" style="width: 24px; height: 24px;"></image>
              <text>左转</text>
            </view>
            
            <!-- 中心状态显示
            <view class="control-center">
              <view class="speed-display">
                <text class="speed-value">{{ currentSpeed }}</text>
                <text class="speed-unit">km/h</text>
              </view>
            </view>
            
            <!-- 右转按钮
            <view class="control-btn control-right" 
                  :class="{'disabled': !isEngineOn || deviceInfo.status === 'offline', 'active': activeControl === 'right'}"
                  @touchstart="startControl('right')" 
                  @touchend="stopControl">
              <image src="/static/icons/arrow_right.svg" mode="aspectFit" style="width: 24px; height: 24px;"></image>
              <text>右转</text>
            </view>
          </view>
          
          <!-- 后退按钮
          <view class="control-btn control-backward" 
                :class="{'disabled': !isEngineOn || deviceInfo.status === 'offline', 'active': activeControl === 'backward'}"
                @touchstart="startControl('backward')" 
                @touchend="stopControl">
            <image src="/static/icons/arrow_down.svg" mode="aspectFit" style="width: 24px; height: 24px;"></image>
            <text>后退</text>
          </view>
        </view>
        
        <!-- 快捷操作按钮
        <view class="quick-controls">
          <view class="quick-control-btn" 
                :class="{'disabled': deviceInfo.status === 'offline'}"
                @click="emergencyStop">
            <view class="quick-icon emergency">
              <image src="/static/icons/emergency_stop.svg" mode="aspectFit" style="width: 24px; height: 24px;"></image>
            </view>
            <text>紧急停止</text>
          </view>
          
          <view class="quick-control-btn" 
                :class="{'disabled': deviceInfo.status === 'offline'}"
                @click="returnHome">
            <view class="quick-icon">
              <image src="/static/icons/home.svg" mode="aspectFit" style="width: 24px; height: 24px;"></image>
            </view>
            <text>返回充电</text>
          </view>
          
          <view class="quick-control-btn" 
                :class="{'disabled': deviceInfo.status === 'offline'}"
                @click="autoMode">
            <view class="quick-icon">
              <image src="/static/icons/auto_mode.svg" mode="aspectFit" style="width: 24px; height: 24px;"></image>
            </view>
            <text>自动模式</text>
          </view>
        </view>
      </view>
    </view>-->

    <!-- 底部“开始作业”按钮 -->
    <view class="task-footer">
      <button
        class="start-btn"
        :class="{ disabled: !taskList.length || !selectedTaskId || deviceInfo.onlineStatus !== 1 }"
        @click="startWork"
      >
        开始作业
      </button>
    </view>

    <!-- 告警信息列表 -->
    <view class="alerts-section">
      <view class="section-title">
        <text>告警信息</text>
        <view class="alert-badge" v-if="getUnhandledAlerts.length > 0">{{ getUnhandledAlerts.length }}</view>
      </view>
      
      <view class="alerts-list" v-if="getUnhandledAlerts.length > 0">
        <view 
          v-for="(item, index) in getUnhandledAlerts" 
          :key="index"
          class="alert-item"
          :class="{
            'alert-urgent': item.level === 'high', 
            'alert-warning': item.level === 'medium', 
            'alert-info': item.level === 'low'
          }"
          @click="handleAlert(item)"
        >
          <view class="alert-item-icon">
            <image v-if="item.level === 'high'" src="/static/icons/warning_icon.png" mode="aspectFit" style="width: 24px; height: 24px;"></image>
            <image v-else-if="item.level === 'medium'" src="/static/icons/info_icon.png" mode="aspectFit" style="width: 24px; height: 24px;"></image>
            <image v-else src="/static/icons/success_icon.png" mode="aspectFit" style="width: 24px; height: 24px;"></image>
          </view>
          
          <view class="alert-item-info">
            <text class="alert-item-type">{{ item.title }}</text>
            <text class="alert-item-level">
              {{ item.level === 'high' ? '紧急' : item.level === 'medium' ? '警告' : '提示' }}
            </text>
          </view>
          
          <view class="alert-item-time">{{ item.time }}</view>
        </view>
      </view>
      
      <view v-else class="empty-alert">
        <text class="empty-text">暂无告警信息</text>
      </view>
    </view>
  </view>
</template>

<script>
import { machineAlarmRecordsList } from '@/api/services/machineAlarmRecords';
import { deviceTasksList, startTask, deleteTask } from '@/api/services/job';
export default {
  data() {
    return {
      // 设备信息
      deviceInfo: {
        // id: '',
        // name: '智能播种机-001',
        // deviceId: 'AGM-001-2024',
        // location: '东区水稻田A块',
        // status: 'online', // online, offline
        // workStatus: 'idle', // working, idle, error
        // battery: 85,
        // gpsSignal: 92,
        // workDuration: '2.5',
        // workArea: '15.6',
        // currentSpeed: '0',
        // lastUpdate: '1分钟前',
        // statusUpdateTime: '1分钟前',
        // batteryUpdateTime: '30秒前',
        // gpsUpdateTime: '15秒前',
        // speedUpdateTime: '实时'
      },

      // 作业任务列表
      taskList: [],
      selectedTaskId: null,
      
      // 控制状态
      isEngineOn: false,
      activeControl: '', // forward, backward, left, right
      currentSpeed: 0,
      
      // 页面状态
      isRefreshing: false,
      alarmTypeMap: {
        0: '其他',
        1: '发动机',
        2: '燃油',
        3: '温度',
        4: '压力',
        5: '定位',
      },
      // 告警数据
      alerts: [
        // {
        //   id: 1,
        //   title: '电量不足警告',
        //   description: '设备电量低于30%，建议及时充电',
        //   level: 'medium',
        //   time: '5分钟前',
        //   handled: false
        // },
        // {
        //   id: 2,
        //   title: 'GPS信号弱',
        //   description: '当前GPS信号较弱，可能影响导航精度',
        //   level: 'low',
        //   time: '10分钟前',
        //   handled: false
        // },
        // {
        //   id: 3,
        //   title: '作业异常',
        //   description: '检测到播种深度异常，请检查设备状态',
        //   level: 'high',
        //   time: '15分钟前',
        //   handled: false
        // }
      ]
    }
  },
  
  computed: {
    // 未处理的告警
    getUnhandledAlerts() {
      return this.alerts.filter(alert => !alert.handled);
    }
  },
  
  onLoad(options) {
    this.deviceInfo.id = options.id;
    // 获取设备ID参数
    uni.$once('agriculturalMachinesData', (data) => {
      if (data ) {
        this.deviceInfo.machineCode = data.machineCode;
        this.deviceInfo.machineName = data.machineName;
        this.deviceInfo.onlineStatus = data.onlineStatus;
        this.deviceInfo.updateTime = data.updateTime;
      }
    });
    
    // 设置导航栏标题
    uni.setNavigationBarTitle({
      title: '农机设备详情'
    });
    
    // 加载设备数据
    this.loadDeviceData();
    // 默认加载当前设备的作业任务
    this.loadTaskList();
    // 加载设备告警数据
    this.loadDeviceAlarmData();
  },
  
  onShow() {
    // 页面显示时刷新数据
    this.refreshData();
  },
  
  onHide() {
    // 页面隐藏时停止所有控制操作
    this.stopAllControls();
  },
  
  methods: {
    // 加载设备告警数据
    loadDeviceAlarmData() {
      return machineAlarmRecordsList({
        machineId: this.deviceInfo.id
      }).then(res => {
        console.log('设备告警数据:', res);
        if (res.data.code === 200 && res.data.rows) {
          this.alerts = res.data.rows.map(item => ({
            id: item.id,
            // title: this.formatAlarmType(item.alarmType),
            title: item.alarmDesc,
            level: item.alarmLevel === 3 ? 'high' : item.alarmLevel === 2 ? 'medium' : 'low',
            time: item.alarmTime
          }));
        }
      });
    },
    formatAlarmType(level) {
      return this.alarmTypeMap[level] || '其他';
    },
    // 加载设备数据
    loadDeviceData() {
      // 这里可以调用API获取设备详细信息
      console.log('加载设备数据:', this.deviceInfo.deviceId);
    },

    // 加载当前设备的作业任务列表
    loadTaskList() {
      const params = {
        pageNum: 1,
        pageSize: 10,
        // 文档未声明 deviceId 过滤，但为了兼容后端如支持按设备筛选，这里传入
        deviceId: this.deviceInfo.id
      };

      return deviceTasksList(params)
        .then((res) => {
          console.log("res任务作业列表",res);
          
          const { data } = res || {};
          if (data && data.code === 200) {
            const list = (data.rows) ? data.rows : [];

            // 适配页面字段
            const areaTypeTextMap = {
              1: '回字形',
              2: '弓字形',
              3: '自定义',
              4: '垄沟'
            }

            this.taskList = list.map((item) => ({
              id: item.id,
              name: item.taskName,
              fieldArea: item.workArea ? item.workArea.areaName : '',
              planTime: item.createTime,
              status: item.taskStatus,
              statusText: item.taskStatusDesc,
              areaType: item.workAreas && item.workAreas.areaType,
              areaTypeText: areaTypeTextMap[item.workAreas && item.workAreas.areaType || 0] || '未知'
            }));
            console.log('taskList',this.taskList);

            // 默认选中第一条
            if (this.taskList.length) {
              // 若当前已选中且仍存在，则保持；否则选第一条
              const exists = this.selectedTaskId && this.taskList.some(t => t.id === this.selectedTaskId);
              this.selectedTaskId = exists ? this.selectedTaskId : this.taskList[0].id;
            } else {
              this.selectedTaskId = null;
            }
          } else {
            this.taskList = [];
            this.selectedTaskId = null;
            uni.showToast({
              title: (data && data.msg) ? data.msg : '获取作业任务失败',
              icon: 'none'
            });
          }
        })
        .catch((err) => {
          console.error('获取作业任务列表失败:', err);
          this.taskList = [];
          this.selectedTaskId = null;
          uni.showToast({
            title: '获取作业任务失败',
            icon: 'none'
          });
        });
    },
    
    // 刷新数据
    refreshData() {
      this.isRefreshing = true;

      Promise.all([
        this.loadTaskList(),
        this.loadDeviceAlarmData()
      ]).finally(() => {
        this.isRefreshing = false;
        // 更新最近更新时间
        this.deviceInfo.lastUpdate = '刚刚';

        uni.showToast({
          title: '刷新成功',
          icon: 'success',
          duration: 1500
        });
      });
    },
    
    // 获取工作状态文本
    getWorkStatusText(status) {
      const statusMap = {
        working: '工作中',
        idle: '空闲',
        error: '故障'
      };
      return statusMap[status] || '未知';
    },
    
    // 获取GPS信号文本
    getGpsSignalText(signal) {
      if (signal >= 80) return '强';
      if (signal >= 60) return '中等';
      if (signal >= 40) return '弱';
      return '无信号';
    },
    
    // 切换引擎状态
    toggleEngine() {
      if (this.deviceInfo.status === 'offline') {
        uni.showToast({
          title: '设备离线，无法操作',
          icon: 'none'
        });
        return;
      }
      
      this.isEngineOn = !this.isEngineOn;
      
      if (this.isEngineOn) {
        this.deviceInfo.workStatus = 'idle';
        uni.showToast({
          title: '引擎已启动',
          icon: 'success'
        });
      } else {
        this.deviceInfo.workStatus = 'idle';
        this.currentSpeed = 0;
        this.stopAllControls();
        uni.showToast({
          title: '引擎已关闭',
          icon: 'success'
        });
      }
    },
    
    // 开始控制
    startControl(direction) {
      if (!this.isEngineOn || this.deviceInfo.status === 'offline') {
        return;
      }
      
      this.activeControl = direction;
      this.deviceInfo.workStatus = 'working';
      
      // 模拟速度变化
      switch (direction) {
        case 'forward':
          this.currentSpeed = 5;
          break;
        case 'backward':
          this.currentSpeed = 2;
          break;
        case 'left':
        case 'right':
          this.currentSpeed = 3;
          break;
      }
      
      // 发送控制指令
      this.sendControlCommand(direction, true);
    },
    
    // 停止控制
    stopControl() {
      if (this.activeControl) {
        this.sendControlCommand(this.activeControl, false);
      }
      
      this.activeControl = '';
      this.currentSpeed = 0;
      this.deviceInfo.workStatus = 'idle';
    },
    
    // 停止所有控制
    stopAllControls() {
      this.activeControl = '';
      this.currentSpeed = 0;
      if (this.isEngineOn) {
        this.deviceInfo.workStatus = 'idle';
      }
    },
    
    // 发送控制指令
    sendControlCommand(direction, isStart) {
      console.log(`发送控制指令: ${direction}, 开始: ${isStart}`);
      // 这里可以调用API发送实际的控制指令
    },
    
    // 紧急停止
    emergencyStop() {
      if (this.deviceInfo.status === 'offline') {
        uni.showToast({
          title: '设备离线，无法操作',
          icon: 'none'
        });
        return;
      }
      
      uni.showModal({
        title: '紧急停止',
        content: '确定要执行紧急停止吗？设备将立即停止所有操作。',
        success: (res) => {
          if (res.confirm) {
            this.isEngineOn = false;
            this.stopAllControls();
            
            uni.showToast({
              title: '紧急停止已执行',
              icon: 'success'
            });
          }
        }
      });
    },
    
    // 返回充电
    returnHome() {
      if (this.deviceInfo.status === 'offline') {
        uni.showToast({
          title: '设备离线，无法操作',
          icon: 'none'
        });
        return;
      }
      
      uni.showToast({
        title: '设备正在返回充电站',
        icon: 'success'
      });
    },
    
    // 自动模式
    autoMode() {
      if (this.deviceInfo.status === 'offline') {
        uni.showToast({
          title: '设备离线，无法操作',
          icon: 'none'
        });
        return;
      }
      
      uni.showToast({
        title: '已切换到自动模式',
        icon: 'success'
      });
    },

    // 跳转到新增作业页面
    goToCreateJob() {
      uni.navigateTo({
        url: `/pages/device/job-create/index?machineCode=${this.deviceInfo.machineCode}&id=${this.deviceInfo.id}`
      });
    },

    // 选择作业任务
    selectTask(task) {
      this.selectedTaskId = task.id;
    },

    // 开始作业
    startWork() {
      if (!this.taskList.length) {
        uni.showToast({
          title: '暂无可执行的作业任务',
          icon: 'none'
        });
        return;
      }
      if (!this.selectedTaskId) {
        uni.showToast({
          title: '请先选择一个作业任务',
          icon: 'none'
        });
        return;
      }
      if (this.deviceInfo.onlineStatus !== 1) {
        uni.showToast({
          title: '设备未在线，无法开始作业',
          icon: 'none'
        });
        return;
      }

      const task = this.taskList.find(t => t.id === this.selectedTaskId);
      uni.showModal({
        title: '开始作业',
        content: `确定开始执行「${task.name}」吗？`,
        success: async (res) => {
          if (res.confirm) {
            try {
              uni.showLoading({ title: '启动中...' });
              const resp = await startTask(this.selectedTaskId);
              const { data } = resp || {};
              if (data && data.code === 200) {
                uni.showToast({
                  title: '作业已启动',
                  icon: 'success'
                });
                this.isEngineOn = true;
                this.deviceInfo.workStatus = 'working';

                // 跳转到作业详情页
                setTimeout(() => {
                  uni.navigateTo({
                    url: `/pages/device/job-detail/index?id=${this.selectedTaskId}&deviceId=${this.deviceInfo.machineCode}&deviceName=${this.deviceInfo.machineName}`
                  });
                }, 300);
              } else {
                uni.showToast({
                  title: (data && data.msg) ? data.msg : '启动失败',
                  icon: 'none'
                });
              }
            } catch (e) {
              console.error('开始作业失败:', e);
              uni.showToast({
                title: '网络异常，启动失败',
                icon: 'none'
              });
            } finally {
              uni.hideLoading();
            }
          }
        }
      });
    },
    
    // 删除作业
    confirmDeleteTask(task) {
      if (!task || !task.id) return

      uni.showModal({
        title: '确认删除',
        content: `确定删除作业「${task.name}」吗？删除后不可恢复。`,
        confirmText: '删除',
        confirmColor: '#F56C6C',
        success: async (res) => {
          if (!res.confirm) return

          try {
            uni.showLoading({ title: '删除中...' })
            const resp = await deleteTask(task.id)
            const { data } = resp || {}
            if (data && data.code === 200) {
              uni.showToast({ title: '删除成功', icon: 'success' })

              // 若删除的是当前选中任务，清理选中态
              if (this.selectedTaskId === task.id) {
                this.selectedTaskId = null
              }

              // 重新拉取列表
              await this.loadTaskList()
            } else {
              uni.showToast({
                title: (data && data.msg) ? data.msg : '删除失败',
                icon: 'none'
              })
            }
          } catch (e) {
            console.error('删除作业失败:', e)
            uni.showToast({ title: '网络异常，删除失败', icon: 'none' })
          } finally {
            uni.hideLoading()
          }
        }
      })
    },

    // 处理告警
    handleAlert(alert) {
      uni.showModal({
        title: alert.title,
        content: alert.description + '\n\n是否标记为已处理？',
        success: (res) => {
          if (res.confirm) {
            const index = this.alerts.findIndex(item => item.id === alert.id);
            if (index !== -1) {
              this.alerts[index].handled = true;
            }
            
            uni.showToast({
              title: '已标记为处理',
              icon: 'success'
            });
          }
        }
      });
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: #F8FCF9;
  padding-bottom: 30rpx;
}

/* 设备头部信息 */
.device-header {
  background-color: #FFFFFF;
  border-radius: 20rpx;
  padding: 30rpx;
  margin: 30rpx 30rpx 30rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
  border: 1rpx solid rgba(210, 237, 217, 0.5);
}

.device-info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 28rpx;
}

.device-name-container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.device-name {
  font-size: 36rpx;
  color: #333333;
  font-weight: 600;
  margin-bottom: 12rpx;
}

.status-tag {
  padding: 6rpx 16rpx 6rpx 28rpx;
  border-radius: 30rpx;
  font-size: 24rpx;
  font-weight: 500;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}

.status-online {
  background-color: rgba(76, 175, 80, 0.1);
  color: #3BB44A;
}

.status-offline {
  background-color: rgba(245, 108, 108, 0.1);
  color: #F56C6C;
  padding-left: 28rpx;
}

.status-dot {
  position: absolute;
  width: 6rpx;
  height: 6rpx;
  background-color: #3BB44A;
  border-radius: 50%;
  top: 50%;
  left: 12rpx;
  transform: translateY(-50%);
  box-shadow: 0 0 4rpx rgba(76, 175, 80, 0.8);
  animation: blink 1.5s infinite;
  display: inline-block;
}

.status-dot.offline-dot {
  background-color: #F56C6C;
  box-shadow: 0 0 4rpx rgba(245, 108, 108, 0.8);
}

.refresh-btn {
  width: 48rpx;
  height: 48rpx;
  background-color: rgba(76, 175, 80, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12rpx;
  transition: transform 0.3s ease;
}

.refresh-btn:active {
  transform: rotate(180deg);
}

.refresh-btn.refreshing {
  animation: spin 1.2s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

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

.device-meta-row {
  display: flex;
  flex-direction: column;
  background-color: #F9FCFA;
  padding: 20rpx 24rpx;
  border-radius: 16rpx;
  border: 1rpx solid rgba(210, 237, 217, 0.8);
}

.device-meta-item {
  display: flex;
  align-items: center;
  font-size: 28rpx;
  margin-top: 18rpx;
}

.device-meta-item:first-child {
  margin-top: 0;
}

.meta-icon {
  width: 36rpx;
  height: 36rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12rpx;
}

.meta-label {
  color: #777777;
  min-width: 140rpx;
  font-size: 28rpx;
}

.meta-value {
  color: #333333;
  flex: 1;
  font-size: 28rpx;
  font-weight: 500;
}

/* 设备状态区域 */
.status-section {
  background: #ffffff;
  margin: 0 24rpx 20rpx;
  padding: 32rpx 24rpx;
  border-radius: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 32rpx;
  font-weight: 600;
  color: #333333;
  margin-bottom: 24rpx;
  padding-bottom: 16rpx;
  border-bottom: 2rpx solid #f0f0f0;
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
}

.status-item {
  background: #f8f9fa;
  padding: 24rpx;
  border-radius: 16rpx;
  border: 2rpx solid transparent;
  transition: all 0.3s ease;
}

.status-item-hover {
  border-color: #40a9ff;
  box-shadow: 0 4rpx 12rpx rgba(64, 169, 255, 0.15);
}

.status-label {
  font-size: 24rpx;
  color: #999999;
  margin-bottom: 8rpx;
  display: block;
}

.status-value-container {
  display: flex;
  align-items: baseline;
  margin-bottom: 8rpx;
}

.status-value {
  font-size: 36rpx;
  font-weight: 600;
  color: #333333;
}

.status-unit {
  font-size: 24rpx;
  color: #666666;
  margin-left: 4rpx;
}

.status-time {
  font-size: 20rpx;
  color: #cccccc;
}

/* 状态颜色 */
.status-working {
  color: #52c41a;
}

.status-idle {
  color: #1890ff;
}

.status-error {
  color: #ff4d4f;
}

.status-good {
  color: #52c41a;
}

.status-normal {
  color: #faad14;
}

.status-weak {
  color: #ff4d4f;
}

.value-warning {
  color: #faad14;
}

.value-alert {
  color: #ff4d4f;
}

/* 控制区域 */
.control-section {
  background: #ffffff;
  margin: 0 24rpx 20rpx;
  padding: 32rpx 24rpx;
  border-radius: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
}

.engine-status {
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
  background-color: #fff1f0;
  color: #ff4d4f;
}

.engine-status.engine-on {
  background-color: #e8f5e8;
  color: #52c41a;
}

.control-container {
  display: flex;
  flex-direction: column;
  gap: 32rpx;
}

/* 引擎控制 */
.engine-control {
  display: flex;
  justify-content: center;
}

.engine-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24rpx;
  background: #ffffff;
  border-radius: 20rpx;
  border: 3rpx solid #4CAF50;
  transition: all 0.3s ease;
  width: 160rpx;
  box-shadow: 0 4rpx 12rpx rgba(76, 175, 80, 0.2);
}

.engine-button.engine-on {
  background: #F44336;
  border-color: #F44336;
  color: white;
  box-shadow: 0 4rpx 12rpx rgba(244, 67, 54, 0.3);
}

.engine-button:not(.engine-on) {
  background: #4CAF50;
  border-color: #4CAF50;
  color: white;
}

.engine-button.disabled {
  opacity: 0.3;
  pointer-events: none;
  background: #DDDDDD;
  border-color: #DDDDDD;
}

.engine-button:active {
  transform: scale(0.95);
}

.engine-icon {
  margin-bottom: 12rpx;
}

.engine-text {
  font-size: 28rpx;
  font-weight: 600;
}

/* 方向控制 */
.direction-control {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20rpx;
}

.control-row {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.control-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 120rpx;
  height: 120rpx;
  background: #f8f9fa;
  border-radius: 12rpx;
  border: 2rpx solid #e0e0e0;
  transition: all 0.3s ease;
  font-size: 24rpx;
  color: #999999;
  gap: 8rpx;
}

.control-btn.active {
  background: rgba(76, 175, 80, 0.1);
  border-color: #4CAF50;
  color: #4CAF50;
  transform: scale(0.9);
  box-shadow: 0 2rpx 8rpx rgba(76, 175, 80, 0.3);
}

.control-btn.disabled {
  opacity: 0.3;
  pointer-events: none;
  background: #DDDDDD;
  border-color: #DDDDDD;
}

.control-center {
  width: 120rpx;
  height: 120rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
  border-radius: 20rpx;
  border: 2rpx solid #d0d0d0;
}

.speed-display {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.speed-value {
  font-size: 32rpx;
  font-weight: 600;
  color: #333333;
}

.speed-unit {
  font-size: 20rpx;
  color: #999999;
}

/* 快捷控制 */
.quick-controls {
  display: flex;
  justify-content: space-around;
  padding-top: 20rpx;
  border-top: 2rpx solid #f0f0f0;
}

.quick-control-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8rpx;
  padding: 16rpx;
  border-radius: 16rpx;
  transition: all 0.3s ease;
}

.quick-control-btn.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.quick-control-btn:active {
  background: #f0f0f0;
}

.quick-icon {
  width: 48rpx;
  height: 48rpx;
  background: #f8f9fa;
  border-radius: 12rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2rpx solid #e0e0e0;
}

.quick-icon.emergency {
  background: rgba(245, 108, 108, 0.1);
  border-color: #F56C6C;
}

.quick-control-btn text {
  font-size: 24rpx;
  color: #666666;
}

/* 告警信息 */
.alerts-section {
  margin: 0 30rpx 30rpx;
  background-color: #FFFFFF;
  border-radius: 20rpx;
  padding: 30rpx 24rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
  border: 1rpx solid rgba(210, 237, 217, 0.5);
}

.alert-badge {
  background-color: #F56C6C;
  color: #FFFFFF;
  font-size: 22rpx;
  border-radius: 30rpx;
  padding: 2rpx 12rpx;
  margin-left: 12rpx;
  font-weight: normal;
  min-width: 32rpx;
  text-align: center;
}

.alerts-list {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
}

.alert-item {
  display: flex;
  align-items: center;
  padding: 24rpx 20rpx;
  border-radius: 12rpx;
  margin-bottom: 16rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  position: relative;
  border: 1rpx solid transparent;
  transition: transform 0.2s ease;
}

.alert-item:active {
  transform: scale(0.98);
}

.alert-urgent {
  background-color: #FEF3F3;
  border-left: 4rpx solid #F56C6C;
  border-color: rgba(245, 108, 108, 0.2);
}

.alert-warning {
  background-color: #FFF8E6;
  border-left: 4rpx solid #E6A23C;
  border-color: rgba(230, 162, 60, 0.2);
}

.alert-info {
  background-color: #F2FAF5;
  border-left: 4rpx solid #67C23A;
  border-color: rgba(103, 194, 58, 0.2);
}

.alert-item-icon {
  width: 50rpx;
  height: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16rpx;
}

.alert-item-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}

.alert-item-type {
  font-size: 28rpx;
  color: #333333;
  font-weight: 500;
  margin-bottom: 8rpx;
}

.alert-item-level {
  font-size: 24rpx;
  color: #999999;
}

.alert-item-time {
  font-size: 24rpx;
  color: #999999;
  margin-left: 16rpx;
  min-width: 100rpx;
  text-align: right;
}

.empty-alert {
  padding: 60rpx 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.empty-text {
  font-size: 28rpx;
  color: #999999;
}

/* 作业任务列表 */
.task-section {
  background: #ffffff;
  margin: 0 24rpx 20rpx;
  padding: 28rpx 24rpx 20rpx;
  border-radius: 20rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.06);
}

.task-list {
  margin-top: 8rpx;
}

.task-card {
  background: #f9fbfa;
  border-radius: 18rpx;
  padding: 20rpx 18rpx;
  margin-bottom: 16rpx;
  border: 1rpx solid rgba(0, 0, 0, 0.03);
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
}

.task-card.active {
  border-color: #3bb44a;
  background: #f2fff4;
  box-shadow: 0 4rpx 14rpx rgba(59, 180, 74, 0.12);
}

.task-header {
  display: flex;
  align-items: center;
}

.task-icon {
  width: 60rpx;
  height: 60rpx;
  border-radius: 16rpx;
  background: linear-gradient(135deg, #66cc6a, #3bb44a);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16rpx;
}

.task-icon-img {
  width: 34rpx;
  height: 34rpx;
}

.task-area-type {
  font-size: 22rpx;
  color: #888;
  margin-left: 8rpx;
  flex-shrink: 0;
}

.task-title-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.task-title {
  font-size: 30rpx;
  font-weight: 600;
  color: #2c3e50;
}

.task-sub {
  margin-top: 6rpx;
  font-size: 24rpx;
  color: #8c9396;
}

.task-radio {
  margin-left: 12rpx;
}

.radio-outer {
  width: 32rpx;
  height: 32rpx;
  border-radius: 16rpx;
  border: 2rpx solid #c0c4cc;
  display: flex;
  align-items: center;
  justify-content: center;
}

.radio-outer.checked {
  border-color: #3bb44a;
  background: rgba(59, 180, 74, 0.1);
}

.radio-inner {
  width: 18rpx;
  height: 18rpx;
  border-radius: 9rpx;
  background: #3bb44a;
  opacity: 0;
}

.radio-outer.checked .radio-inner {
  opacity: 1;
}

.task-meta {
  margin-top: 12rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 24rpx;
}

.task-time {
  color: #909399;
}

.task-meta-right {
  display: flex;
  align-items: center;
  gap: 12rpx;
}

.task-status {
  color: #3bb44a;
}

.task-delete-btn {
  height: 52rpx;
  line-height: 52rpx;
  padding: 0 16rpx;
  border-radius: 26rpx;
  background-color: rgba(245, 108, 108, 0.12);
  color: #F56C6C;
  font-size: 22rpx;
}

.task-empty {
  padding: 40rpx 0 10rpx;
  display: flex;
  justify-content: center;
}

.task-empty-text {
  font-size: 26rpx;
  color: #999999;
}

.task-footer {
  padding: 12rpx 30rpx 24rpx;
  background: #f8fcf9;
}

.start-btn {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  border-radius: 44rpx;
  background: linear-gradient(135deg, #3bb44a, #66cc6a);
  color: #ffffff;
  font-size: 30rpx;
  font-weight: 600;
}

.start-btn.disabled {
  background: #dcdfe6;
  color: #ffffff;
}

/* 作业标题行加号按钮（与新增作业页保持一致） */
.task-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

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

.task-title-text {
  font-size: 32rpx;
  font-weight: 600;
  color: #2c3e50;
}

.task-title-sub {
  margin-top: 4rpx;
  font-size: 22rpx;
  color: #8c9396;
}

.task-add-btn {
  width: 60rpx;
  height: 60rpx;
  border-radius: 30rpx;
  background: linear-gradient(135deg, #3bb44a, #66cc6a);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 12rpx rgba(59, 180, 74, 0.3);
}

.task-add-plus {
  font-size: 40rpx;
  color: #ffffff;
  line-height: 1;
}
</style> 