<template>
  <div>
    <div class="inspection-tasks">
      <div class="navigation-tabs">
        <div class="nav-tab" @click="handleInspection1">待办事项</div>
        <div class="nav-tab" @click="handleInspection2">巡检管理</div>
        <div class="nav-tab" @click="handleInspection3">试验管理</div>
        <div class="nav-tab" @click="handleInspection4">报修管理</div>
        <div class="nav-tab active" @click="handleInspection5">抢修管理</div>
        <div class="nav-tab" @click="handleInspection6">工单管理</div>
        <div class="nav-tab" @click="handleInspection7">运维组织</div>
      </div>
      <!-- 页面标题 -->
      <TitleComponent title="抢修管理模块" subtitle="处理紧急抢修任务，跟踪抢修进度和记录"></TitleComponent>

      <!-- 选项卡 -->
      <div class="tabs-wrapper">
        <div style="display: flex; align-items: center; gap: 10px">
          <el-button type="primary" @click="handleInspectionManagement1">抢修任务</el-button>
          <el-button type="primary" @click="handleInspectionManagement2">抢修记录</el-button>
        </div>
      </div>

      <!-- 筛选栏 -->
      <div class="filter-bar">
        <div class="filter-container">
          <div class="filter-item">
            <el-select v-model="taskStatus" placeholder="任务状态">
              <el-option label="待执行" value="pending"></el-option>
              <el-option label="执行中" value="executing"></el-option>
              <el-option label="已延期" value="delayed"></el-option>
              <el-option label="已完成" value="completed"></el-option>
            </el-select>
          </div>
          <div class="filter-item">
            <el-select v-model="planType" placeholder="全部计划">
              <el-option label="全部计划" value="all"></el-option>
              <el-option label="每日巡检计划" value="daily"></el-option>
              <el-option label="每周巡检计划" value="weekly"></el-option>
              <el-option label="每月巡检计划" value="monthly"></el-option>
              <el-option label="每季度巡检计划" value="quarterly"></el-option>
            </el-select>
          </div>
          <div class="filter-item">
            <el-select v-model="executor" placeholder="执行人">
              <el-option label="全部人员" value="all"></el-option>
              <el-option label="张明" value="zhangming"></el-option>
              <el-option label="李华" value="lihua"></el-option>
              <el-option label="王强" value="wangqiang"></el-option>
            </el-select>
          </div>
          <div class="filter-actions">
            <el-button type="primary" class="search-btn" @click="handleSearch"> 搜索 </el-button>
            <el-button type="primary" icon="el-icon-plus" class="create-btn" @click="handleCreateTask"> 手动创建任务 </el-button>
          </div>
        </div>
      </div>

      <!-- 任务卡片列表 -->
      <div class="task-cards">
        <div class="task-card" v-for="(task, index) in pagedTasks" :key="index" :class="task.statusClass">
          <!-- 顶部信息区域 -->
          <div class="task-header">
            <div class="task-title">
              {{ task.title }}
            </div>
            <div class="task-type-tag" :class="getFaultTypeClass(task.faultType)">
              {{ task.faultType }}
            </div>
          </div>

          <!-- 新增滚动内容容器 -->
          <div class="task-content-scroll">
            <div class="task-details">
              <div class="detail-item">
                <span class="detail-label">报修时间</span>
                <span class="detail-value">{{ task.reportTime }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">报修人</span>
                <span class="detail-value">{{ task.reporter }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">维修人</span>
                <span class="detail-value">{{ task.maintainer }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">预计完成</span>
                <span class="detail-value">{{ task.expectedCompleteTime }}</span>
              </div>
              <div class="detail-item">
                <span class="detail-label">状态</span>
                <span class="detail-value">{{ task.statusText }}</span>
              </div>

              <!-- 已完成状态的额外信息 -->
              <div v-if="task.status === 'completed'" class="task-result">
                <span class="detail-label">完成时间</span>
                <span class="detail-value">{{ task.completeTime }}</span>
              </div>

              <!-- 增加一些示例数据来演示滚动效果 -->
              <div v-if="task.status === 'executing'" class="detail-item">
                <span class="detail-label">开始时间</span>
                <span class="detail-value">{{ task.startTime || '30分钟前' }}</span>
              </div>
              <div v-if="task.status === 'executing'" class="detail-item">
                <span class="detail-label">进度</span>
                <span class="detail-value">{{ task.progress || '60%' }}</span>
              </div>
            </div>
          </div>

          <div class="task-actions">
            <el-button type="text" size="small" class="action-btn view-btn" @click="handleView(task)"> 详情 </el-button>
            <el-button type="primary" size="small" :class="task.actionClass" @click="handleAction(task)">
              {{ task.actionText }}
            </el-button>
          </div>
        </div>
      </div>

      <!-- 分页区域 -->
      <div class="pagination-section">
        <div class="pagination-controls">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[8, 12, 16, 20]"
            :page-size="pageSize"
            layout="prev, pager, next, jumper"
            :total="total"
            background
          >
          </el-pagination>
        </div>
      </div>

      <!-- 新建紧急抢修任务弹窗 -->
      <el-dialog
        v-model="createTaskDialogVisible"
        title="创建紧急抢修任务"
        width="800px"
        :before-close="handleCancelCreateTask"
        custom-class="beautiful-dialog"
        center
      >
        <el-form ref="createTaskFormRef" :model="createTaskForm" :rules="createTaskRules" label-width="100px" class="elegant-form">
          <el-form-item label="抢修名称*" prop="repairName">
            <el-input v-model="createTaskForm.repairName" placeholder="简要描述抢修内容" />
          </el-form-item>

          <el-row :gutter="16">
            <el-col :span="12">
              <el-form-item label="抢修类型*" prop="repairType">
                <el-select v-model="createTaskForm.repairType" placeholder="请选择类型">
                  <el-option label="设备故障" value="device" />
                  <el-option label="软件故障" value="software" />
                  <el-option label="网络故障" value="network" />
                  <el-option label="环境问题" value="environment" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="紧急程度*" prop="priority">
                <el-select v-model="createTaskForm.priority" placeholder="请选择紧急程度">
                  <el-option label="致命" value="fatal" />
                  <el-option label="紧急" value="urgent" />
                  <el-option label="较危险" value="dangerous" />
                  <el-option label="一般" value="normal" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="故障描述*" prop="detailedDescription">
            <el-input
              v-model="createTaskForm.detailedDescription"
              type="textarea"
              :rows="4"
              placeholder="详细描述故障现象、影响范围、潜在威胁等信息"
            />
          </el-form-item>

          <el-form-item label="故障位置*" prop="faultLocation">
            <el-input v-model="createTaskForm.faultLocation" placeholder="例如：A区102" />
          </el-form-item>

          <el-form-item label="上传图片(可选)*">
            <div class="upload-container">
              <div class="upload-box">
                <i class="el-icon-plus avatar-uploader-icon"></i>
                <div class="upload-text">点击或拖拽图片至此处上传</div>
                <div class="upload-hint">支持JPG、PNG格式，最多3张</div>
              </div>
            </div>
          </el-form-item>

          <el-row :gutter="16">
            <el-col :span="12">
              <el-form-item label="联系人*" prop="contactPerson">
                <el-input v-model="createTaskForm.contactPerson" placeholder="您的姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话*" prop="contactPhone">
                <el-input v-model="createTaskForm.contactPhone" placeholder="您的联系电话" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="是否需要立即现场支持*">
            <div>
              <el-radio v-model="createTaskForm.needSupport" label="yes">是，需要立即派人现场</el-radio>
              <el-radio v-model="createTaskForm.needSupport" label="no">否，可远程指导或延后处理</el-radio>
            </div>
          </el-form-item>
        </el-form>

        <template #footer>
          <span class="dialog-footer">
            <el-button @click="handleCancelCreateTask">取消</el-button>
            <el-button type="primary" @click="handleSaveTask">提交抢修计划</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import router from '@/router';
import TitleComponent from '@/views/demo/components/TitleComponent.vue';

// 激活的选项卡
const activeTab = ref('task');

// 根据故障类型获取对应的CSS类
const getFaultTypeClass = (faultType) => {
  const typeMap = {
    // 电力，设备故障为红色
    '电力故障': 'electric',
    '设备故障': 'equipment',

    // 供水，设备损坏为黄色
    '供水问题': 'water',
    '设备损坏': 'damage',

    // 其余默认使用基本样式（绿色）
    '网络中断': '',
    '制冷系统故障': '',
    '安全问题': ''
  };
  return typeMap[faultType] || '';
};

// 筛选条件
const repairType = ref('all');
const taskStatus = ref('all');
const emergencyLevel = ref('all');
const planType = ref('all');
const executor = ref('all');

// 任务数据 - 添加了更多字段以展示滚动效果
const tasks = ref([
  {
    title: '主配电室短路故障',
    status: 'executing',
    statusText: '抢修中',
    statusClass: 'status-high',
    priority: '致命',
    reportTime: '10分钟前',
    reporter: '李阳',
    maintainer: '张明',
    expectedCompleteTime: '30分钟内',
    faultType: '电力故障',
    faultLocation: '地下一层主配电室',
    startTime: '10分钟前',
    progress: '40%',
    remarks: '已切断该区域电源，正在排查短路点',
    actionText: '实时跟进',
    actionClass: 'follow-btn'
  },
  {
    title: '主配电室短路故障',
    status: 'executing',
    statusText: '抢修中',
    statusClass: 'status-medium',
    priority: '紧急',
    reportTime: '45分钟前',
    reporter: '李阳',
    maintainer: '张明',
    expectedCompleteTime: '1小时内',
    faultType: '供水问题',
    faultLocation: '三楼东侧卫生间',
    startTime: '40分钟前',
    progress: '70%',
    remarks: '已找到漏水点，正在进行修复',
    actionText: '实时跟进',
    actionClass: 'follow-btn'
  },
  {
    title: '网络主干交换机故障',
    status: 'completed',
    statusText: '已完成',
    statusClass: 'status-completed',
    priority: '较危险',
    reportTime: '1小时前',
    reporter: '李阳',
    maintainer: '张明',
    expectedCompleteTime: '1小时内',
    faultType: '网络中断',
    faultLocation: '二楼机房',
    completeTime: '2小时前',
    remarks: '交换机电源模块故障，已更换备用模块',
    actionText: '查看报告',
    actionClass: 'view-btn'
  },
  {
    title: '安全出口指示牌损坏',
    status: 'pending',
    statusText: '待处理',
    statusClass: 'status-medium',
    priority: '较危险',
    reportTime: '3小时前',
    reporter: '李阳',
    maintainer: '未分配',
    expectedCompleteTime: '今天以内',
    faultType: '设备损坏',
    faultLocation: '一楼东侧安全通道',
    remarks: '指示牌不亮，可能是线路问题',
    actionText: '分配任务',
    actionClass: 'assign-btn'
  },
  {
    title: '制冷系统主压缩机故障',
    status: 'executing',
    statusText: '抢修中',
    statusClass: 'status-high',
    priority: '致命',
    reportTime: '1小时前',
    reporter: '李阳',
    maintainer: '张明',
    expectedCompleteTime: '45分钟内',
    faultType: '设备故障',
    faultLocation: '楼顶制冷机房',
    startTime: '50分钟前',
    progress: '30%',
    remarks: '压缩机无法启动，正在检查电路和 refrigerant 压力',
    actionText: '实时跟进',
    actionClass: 'follow-btn'
  },
  {
    title: '主配电室短路故障',
    status: 'executing',
    statusText: '抢修中',
    statusClass: 'status-medium',
    priority: '紧急',
    reportTime: '45分钟前',
    reporter: '李阳',
    maintainer: '张明',
    expectedCompleteTime: '1小时内',
    faultType: '供水问题',
    faultLocation: '地下室水泵房',
    startTime: '40分钟前',
    progress: '60%',
    remarks: '水泵压力不足，正在更换滤网和检查管道',
    actionText: '实时跟进',
    actionClass: 'follow-btn'
  }
]);

// 分页相关
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(6);

// 状态排序映射
const statusOrder = {
  pending: 0, // 待处理
  executing: 1, // 处理中
  completed: 2 // 已完成
};

// 分页处理后的数据（含排序）
const pagedTasks = computed(() => {
  // 先按状态排序
  const sortedTasks = [...tasks.value].sort((a, b) => {
    return statusOrder[a.status] - statusOrder[b.status];
  });

  // 再进行分页
  const startIndex = (currentPage.value - 1) * pageSize.value;
  const endIndex = startIndex + pageSize.value;
  return sortedTasks.slice(startIndex, endIndex);
});

// 搜索处理
const handleSearch = () => {
  currentPage.value = 1; // 重置到第一页
  // 实际应用中这里会根据筛选条件过滤数据
};

// 创建紧急抢修任务弹窗相关
const createTaskDialogVisible = ref(false);
const createTaskForm = ref({
  repairName: '',
  repairType: '',
  priority: '',
  detailedDescription: '',
  faultLocation: '',
  contactPerson: '',
  contactPhone: '',
  needSupport: 'yes'
});

const createTaskRules = {
  repairName: [{ required: true, message: '请输入抢修名称', trigger: 'blur' }],
  repairType: [{ required: true, message: '请选择抢修类型', trigger: 'change' }],
  priority: [{ required: true, message: '请选择紧急程度', trigger: 'change' }],
  detailedDescription: [{ required: true, message: '请输入故障描述', trigger: 'blur' }],
  faultLocation: [{ required: true, message: '请输入故障位置', trigger: 'blur' }],
  contactPerson: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
  contactPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }]
};

// 创建任务
const handleCreateTask = () => {
  createTaskDialogVisible.value = true;
};

// 提交抢修计划
const handleSaveTask = () => {
  // 模拟提交抢修计划逻辑
  console.log('提交抢修计划:', createTaskForm.value);
  // 关闭弹窗
  createTaskDialogVisible.value = false;
  // 重置表单
  createTaskForm.value = {
    repairName: '',
    repairType: '',
    priority: '',
    detailedDescription: '',
    faultLocation: '',
    contactPerson: '',
    contactPhone: '',
    needSupport: 'yes'
  };
  // 这里可以添加成功提示和刷新任务列表的逻辑
};

// 取消创建紧急抢修任务
const handleCancelCreateTask = () => {
  createTaskDialogVisible.value = false;
  // 重置表单
  createTaskForm.value = {
    repairName: '',
    repairType: '',
    priority: '',
    detailedDescription: '',
    faultLocation: '',
    contactPerson: '',
    contactPhone: '',
    needSupport: 'yes'
  };
};

// 分页事件
const handleSizeChange = (val) => {
  pageSize.value = val;
  currentPage.value = 1;
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
};

// 查看任务详情
const handleView = (task) => {
  console.log('查看任务详情:', task);
};

// 处理选项卡点击
const handleTabClick = () => {
  currentPage.value = 1;
};

const handleInspectionManagement1 = () => {
  router.push('/rili/qiangxiuguanli');
};

const handleInspectionManagement2 = () => {
  router.push('/rili/qiangxiujilu');
};
const handleInspection1 = () => {
  router.push('/rili/rili');
};
const handleInspection2 = () => {
  router.push('/rili/InspectionManagement');
};
const handleInspection3 = () => {
  router.push('/rili/shiyanguanli');
};
const handleInspection4 = () => {
  router.push('/rili/baoxiuguanli');
};
const handleInspection5 = () => {
  router.push('/rili/qiangxiuguanli');
};
const handleInspection6 = () => {
  router.push('/rili/gongdanliebiao');
};
const handleInspection7 = () => {
  router.push('/rili/renyuanzhuangtai');
};
</script>

<style scoped>
.inspection-tasks {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

/* 任务卡片样式修改 - 增加滚动功能 */
.task-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  margin-bottom: 30px;
}

.task-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  padding: 16px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  /* 固定卡片高度 */
  min-height: 280px;
  display: flex;
  flex-direction: column;
}

/* 顶部装饰条样式 */
.task-card.status-high::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
}

.task-card.status-medium::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
}

.task-card.status-low::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
}

.task-card.status-completed::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
}

/* 添加滚动内容容器 */
.task-content-scroll {
  flex: 1;
  overflow-y: auto;
  padding-right: 8px; /* 为滚动条预留空间 */
  margin-bottom: 10px;
  /* 限制最大高度，超出则显示滚动条 */
  max-height: 180px;
}

/* 滚动条样式保持不变 */
.task-content-scroll::-webkit-scrollbar {
  width: 6px;
}

.task-content-scroll::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.task-content-scroll::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.task-content-scroll::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

.task-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f2f5;
}

.task-title {
  font-size: 14px;
  font-weight: 500;
  color: #1d2129;
  line-height: 1.4;
  flex: 1;
  margin-right: 8px;
}

.task-type-tag {
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid transparent;
}

/* 不同故障类型的颜色 */
/* 电力，设备故障为红色 */
.task-type-tag.electric,
.task-type-tag.equipment {
  background-color: #fff2f0;
  color: #ff4d4f;
  border-color: #ffccc7;
}

/* 供水，设备损坏为黄色 */
.task-type-tag.water,
.task-type-tag.damage {
  background-color: #fffbe6;
  color: #fa8c16;
  border-color: #ffe58f;
}

/* 其余为绿色 */
.task-type-tag {
  background-color: #f6ffed;
  color: #52c41a;
  border-color: #b7eb8f;
}

.task-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.task-details {
  margin-bottom: 16px;
}

.detail-item {
  display: flex;
  margin-bottom: 8px;
  font-size: 12px;
  line-height: 1.5;
}

.detail-label {
  flex: 0 0 70px;
  color: #86909c;
}

.detail-value {
  flex: 1;
  color: #4e5969;
  word-break: break-all;
}

.task-result {
  display: flex;
  margin: 8px 0;
  font-size: 12px;
  padding-top: 8px;
  border-top: 1px dashed #f0f2f5;
}

.task-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-top: 16px;
  border-top: 1px solid #f0f2f5;
  margin-top: auto; /* 自动推到最底部 */
  gap: 8px;
}

.action-btn {
  font-size: 12px;
  padding: 4px 12px;
}

.view-btn {
  color: #165dff;
}

.view-btn:hover {
  color: #0e42d2;
  background-color: #e8f3ff;
}

.follow-btn {
  background-color: #165dff;
  border-color: #165dff;
}

.follow-btn:hover {
  background-color: #0e42d2;
  border-color: #0e42d2;
}

.assign-btn {
  background-color: #ff7d00;
  border-color: #ff7d00;
}

.assign-btn:hover {
  background-color: #e86a00;
  border-color: #e86a00;
}

/* 其他样式保持不变 */
.tabs-wrapper {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.filter-bar {
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 16px 24px;
}

.filter-container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
  width: 100%;
}

.filter-item {
  flex-shrink: 0;
}

.filter-bar .el-select {
  width: 180px;
  height: 36px;
}

.filter-actions {
  margin-left: auto;
  display: flex;
  gap: 12px;
  flex-shrink: 0;
}

.search-btn {
  background-color: #f2f3f5;
  color: #303133;
  border-color: #f2f3f5;
}

.create-btn {
  background-color: #165dff;
  border-color: #165dff;
}

.pagination-section {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.navigation-tabs {
  display: flex;
  margin-bottom: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  padding: 2px;
}

.nav-tab {
  padding: 12px 24px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 4px;
  font-size: 14px;
  color: #606266;
  border-right: 1px solid #f0f0f0;
  flex: 1;
  text-align: center;
}

.nav-tab:last-child {
  border-right: none;
}

.nav-tab:hover {
  color: #409eff;
  background-color: #ecf5ff;
}

.nav-tab.active {
  background-color: #409eff;
  color: #fff;
  box-shadow: 0 2px 4px rgba(64, 158, 255, 0.3);
}

/* 上传图片区域样式 */
.upload-container {
  width: 100%;
}

.upload-box {
  border: 2px dashed #dcdfe6;
  border-radius: 8px;
  padding: 40px 20px;
  text-align: center;
  cursor: pointer;
  background-color: #f8f9fa;
}

.avatar-uploader-icon {
  font-size: 40px;
  color: #c0c4cc;
  margin-bottom: 12px;
}

.upload-text {
  font-size: 14px;
  color: #606266;
  font-weight: 500;
  margin-bottom: 6px;
}

.upload-hint {
  font-size: 12px;
  color: #909399;
}

/* 弹窗和表单样式保持不变 */
.beautiful-dialog {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
  background-color: #fff;
}

.elegant-form .el-form-item {
  margin-bottom: 24px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .task-cards {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
}

@media (max-width: 768px) {
  .inspection-tasks {
    padding: 16px;
  }

  .filter-container {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-actions {
    margin-left: 0;
    justify-content: flex-end;
  }

  .filter-bar .el-select {
    width: 100%;
  }

  .task-cards {
    grid-template-columns: 1fr;
  }
}
</style>
