<template>
  <div>
    <div class="inspection-tasks">
      <div class="navigation-tabs">
        <div class="nav-tab" @click="handleInspection1">待办事项</div>
        <div class="nav-tab active" @click="handleInspection2">巡检管理</div>
        <div class="nav-tab" @click="handleInspection3">试验管理</div>
        <div class="nav-tab" @click="handleInspection4">报修管理</div>
        <div class="nav-tab" @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>
          <el-button type="primary" @click="handleInspectionManagement3">巡检记录</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="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">搜索</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">
          <div class="task-header">
            <div class="task-title">
              {{ task.title }}
            </div>
            <div class="task-status" :class="task.statusClass">
              {{ task.statusText }}
            </div>
          </div>

          <div class="task-details">
            <div class="detail-item">
              <span class="detail-label">计划时间</span>
              <span class="detail-value">{{ task.planTime }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">巡检对象</span>
              <span class="detail-value">{{ task.target }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">执行人</span>
              <span class="detail-value">{{ task.executor }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">关联计划</span>
              <span class="detail-value">{{ task.relatedPlan }}</span>
            </div>

            <!-- 特定状态的额外信息 -->
            <div v-if="task.status === 'delayed'" class="delay-reason">
              <span class="detail-label">延期原因</span>
              <span class="detail-value">{{ task.delayReason }}</span>
            </div>

            <div v-if="task.status === 'executing'" class="progress-container">
              <span class="detail-label">完成进度</span>
              <div class="progress-bar">
                <el-progress :percentage="task.progress" stroke-width="6" :stroke-color="task.progressColor"></el-progress>
              </div>
            </div>

            <div v-if="task.status === 'completed'" class="task-result">
              <span class="detail-label">结果</span>
              <span class="detail-value" :class="task.resultClass">{{ task.result }}</span>
            </div>
          </div>

          <div class="task-actions">
            <el-button type="text" class="action-btn view-btn" @click="handleView(task)"> 详情 </el-button>
            <el-button type="primary" :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="500px" :before-close="handleCancelCreateTask">
        <el-form ref="createTaskFormRef" :model="createTaskForm" :rules="createTaskRules" label-width="80px">
          <el-form-item label="任务名称" prop="taskName">
            <el-input v-model="createTaskForm.taskName" placeholder="输入任务名称" />
          </el-form-item>

          <el-form-item label="巡检对象" prop="inspectionTarget">
            <el-input v-model="createTaskForm.inspectionTarget" placeholder="输入巡检内容" />
          </el-form-item>

          <el-form-item label="时间" prop="timeRange">
            <el-date-picker
              v-model="createTaskForm.timeRange"
              type="datetimerange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm"
              placeholder="选择时间范围"
            />
          </el-form-item>

          <el-form-item label="关联计划">
            <el-select v-model="createTaskForm.relatedPlan" placeholder="选择关联计划">
              <el-option label="全部计划" value="all" />
              <el-option label="每日巡检计划" value="daily" />
              <el-option label="每周巡检计划" value="weekly" />
              <el-option label="每月巡检计划" value="monthly" />
              <el-option label="每季度巡检计划" value="quarterly" />
            </el-select>
          </el-form-item>

          <el-form-item label="执行人">
            <el-select v-model="createTaskForm.executor" placeholder="选择执行人">
              <el-option label="全部人员" value="all" />
              <el-option label="张明" value="zhangming" />
              <el-option label="李华" value="lihua" />
              <el-option label="王强" value="wangqiang" />
              <el-option label="赵伟" value="zhaowei" />
            </el-select>
          </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 './TitleComponent.vue';

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

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

// 任务数据
const tasks = ref([
  {
    title: '生产服务器日常巡检',
    status: 'pending',
    statusText: '待执行',
    statusClass: 'status-pending',
    planTime: '2025-06-16 08:30',
    target: '生产服务器集群(12台)',
    executor: '张明',
    relatedPlan: '每日巡检计划',
    actionText: '开始执行',
    actionClass: 'start-btn'
  },
  {
    title: '机房环境检查',
    status: 'delayed',
    statusText: '已延期',
    statusClass: 'status-delayed',
    planTime: '2025-06-16 08:30',
    target: '机房温度、湿度、电源',
    executor: '李华',
    relatedPlan: '每周巡检计划',
    delayReason: '设备维修处理中',
    actionText: '重新安排',
    actionClass: 'reschedule-btn'
  },
  {
    title: '网络设备安全巡检',
    status: 'executing',
    statusText: '执行中',
    statusClass: 'status-executing',
    planTime: '2025-06-16 09:30',
    target: '核心交换机、防火墙',
    executor: '王强',
    relatedPlan: '每周巡检计划',
    progress: 60,
    progressColor: '#FF7D00',
    actionText: '完成',
    actionClass: 'complete-btn'
  },
  {
    title: '数据库性能巡检',
    status: 'completed',
    statusText: '已完成',
    statusClass: 'status-completed',
    planTime: '2025-06-16 10:30',
    target: '生产数据库集群(2组)',
    executor: '赵伟',
    relatedPlan: '每月巡检计划',
    result: '正常',
    resultClass: 'result-normal',
    actionText: '查看报告',
    actionClass: 'report-btn'
  },
  {
    title: '生产服务器日常巡检',
    status: 'pending',
    statusText: '待执行',
    statusClass: 'status-pending',
    planTime: '2025-06-16 13:30',
    target: '生产服务器集群(12台)',
    executor: '张明',
    relatedPlan: '每日巡检计划',
    actionText: '开始执行',
    actionClass: 'start-btn'
  },
  {
    title: '机房环境检查',
    status: 'delayed',
    statusText: '已延期',
    statusClass: 'status-delayed',
    planTime: '2025-06-16 14:00',
    target: '机房温度、湿度、电源',
    executor: '李华',
    relatedPlan: '每周巡检计划',
    delayReason: '设备维修处理中',
    actionText: '重新安排',
    actionClass: 'reschedule-btn'
  },
  {
    title: '网络设备安全巡检',
    status: 'executing',
    statusText: '执行中',
    statusClass: 'status-executing',
    planTime: '2025-06-16 15:00',
    target: '核心交换机、防火墙',
    executor: '王强',
    relatedPlan: '每周巡检计划',
    progress: 35,
    progressColor: '#FF7D00',
    actionText: '完成',
    actionClass: 'complete-btn'
  },
  {
    title: '数据库性能巡检',
    status: 'completed',
    statusText: '已完成',
    statusClass: 'status-completed',
    planTime: '2025-06-16 16:00',
    target: '生产数据库集群(2组)',
    executor: '赵伟',
    relatedPlan: '每月巡检计划',
    result: '正常',
    resultClass: 'result-normal',
    actionText: '查看报告',
    actionClass: 'report-btn'
  }
]);

// 分页相关
const currentPage = ref(1);
const pageSize = ref(8);
const total = ref(tasks.value.length);

// 状态排序映射
const statusOrder = {
  pending: 0, // 待完成
  delayed: 1, // 已延期
  executing: 2, // 执行中
  completed: 3 // 已完成
};

// 分页处理后的数据（含排序）
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({
  taskName: '',
  inspectionTarget: '',
  timeRange: [],
  relatedPlan: 'all',
  executor: 'all'
});

const createTaskRules = {
  taskName: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
  inspectionTarget: [{ required: true, message: '请输入巡检对象', trigger: 'blur' }],
  timeRange: [{ required: true, message: '请选择时间范围', trigger: 'change' }]
};

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

// 保存任务
const handleSaveTask = () => {
  // 模拟保存任务逻辑
  console.log('保存任务:', createTaskForm.value);
  // 关闭弹窗
  createTaskDialogVisible.value = false;
  // 重置表单
  createTaskForm.value = {
    taskName: '',
    inspectionTarget: '',
    timeRange: [],
    relatedPlan: 'all',
    executor: 'all'
  };
  // 这里可以添加成功提示和刷新任务列表的逻辑
};

// 取消创建任务
const handleCancelCreateTask = () => {
  createTaskDialogVisible.value = false;
  // 重置表单
  createTaskForm.value = {
    taskName: '',
    inspectionTarget: '',
    timeRange: [],
    relatedPlan: 'all',
    executor: 'all'
  };
};

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

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

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

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

const handleInspectionManagement2 = () => {
  router.push('/rili/xunjianrenwu');
};
const handleInspectionManagement3 = () => {
  router.push('/rili/xunjianjihua');
};
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;
}

/* 选项卡样式 */
.tabs-wrapper {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.custom-tabs {
  border-bottom: none;
  padding-top: 20px;
}

.custom-tabs .el-tabs__header {
  margin: 0 -20px 0 -20px;
  padding: 0 20px;
  border-bottom: 1px solid #e4e7ed;
}

.custom-tabs .el-tabs__nav-wrap::after {
  height: 0;
}

.custom-tabs .el-tabs__item {
  font-size: 14px;
  color: #606266;
  padding: 12px 20px;
  margin-right: 20px;
  border-bottom: 2px solid transparent;
}

.custom-tabs .el-tabs__item.is-active {
  color: #409eff;
  border-bottom-color: #409eff;
}

.custom-tabs .el-tabs__item:hover {
  color: #409eff;
}
/* 筛选栏样式 */
.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-bar .el-select .el-input__inner {
  border-radius: 4px;
  border-color: #dcdfe6;
  transition: all 0.2s ease;
}

.filter-bar .el-select .el-input__inner:focus {
  border-color: #165dff;
  box-shadow: 0 0 0 2px rgba(22, 93, 255, 0.1);
}

.filter-actions {
  margin-left: auto;
  display: flex;
  gap: 10px;
}

.search-btn {
  background-color: #f2f3f5;
  color: #303133;
  border-color: #f2f3f5;
  transition: all 0.2s ease;
}

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

.create-btn {
  background-color: #165dff;
  border-color: #165dff;
  transition: all 0.2s ease;
}

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

/* 任务卡片样式 */
.task-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.task-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 16px 16px 60px 16px; /* 底部留出更多空间给按钮 */
  transition: box-shadow 0.2s ease;
  position: relative;
  overflow: hidden;
  min-height: 280px; /* 确保有足够高度显示所有内容 */
}

.task-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-top: 12px;
  border-top: 1px solid #f0f2f5;
  position: absolute;
  bottom: 16px;
  right: 16px;
  left: 16px;
  background-color: #fff;
  padding: 12px 0 0 0;
  z-index: 10;
}

.task-actions .el-button {
  border-radius: 16px;
  padding: 6px 16px;
}
.task-card::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
}

.task-card.status-pending::before {
  background-color: #1677ff;
}

.task-card.status-delayed::before {
  background-color: #ff4d4f;
}

.task-card.status-executing::before {
  background-color: #fa8c16;
}

.task-card.status-completed::before {
  background-color: #52c41a;
}

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

.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: 16px;
  font-weight: 500;
  color: #1d2129;
  line-height: 1.4;
}

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

/* 待执行状态 - 蓝色 */
.status-pending {
  background-color: #e6f7ff;
  color: #1677ff;
  border-color: #91d5ff;
}

/* 已延期状态 - 红色 */
.status-delayed {
  background-color: #fff2f0;
  color: #ff4d4f;
  border-color: #ffccc7;
}

/* 执行中状态 - 黄色 */
.status-executing {
  background-color: #fffbe6;
  color: #fa8c16;
  border-color: #ffe58f;
}

/* 已完成状态 - 绿色 */
.status-completed {
  background-color: #f6ffed;
  color: #52c41a;
  border-color: #b7eb8f;
}

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

.detail-item {
  display: flex;
  margin-bottom: 10px;
  font-size: 13px;
}

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

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

.delay-reason {
  display: flex;
  margin: 10px 0;
  font-size: 13px;
  padding-top: 8px;
  border-top: 1px dashed #f0f2f5;
}

.progress-container {
  display: flex;
  flex-direction: column;
  margin: 10px 0;
  padding-top: 8px;
  border-top: 1px dashed #f0f2f5;
  z-index: 1;
}

.progress-bar {
  flex: 1;
  padding-left: 80px;
  margin-top: 4px;
  position: relative;
  z-index: 1;
}

.progress-text {
  position: absolute;
  right: 0;
  top: 0;
  font-size: 12px;
  color: #86909c;
}

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

.result-normal {
  color: #00b42a;
}

.result-abnormal {
  color: #f53f3f;
}

.task-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-top: 12px;
  border-top: 1px solid #f0f2f5;
  position: absolute;
  bottom: 16px;
  right: 16px;
  left: 16px;
  background-color: #fff;
  padding: 12px 0 0 0;
  z-index: 10;
}

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

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

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

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

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

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

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

.complete-btn {
  background-color: #00b42a;
  border-color: #00b42a;
}

.complete-btn:hover {
  background-color: #008718;
  border-color: #008718;
}

.report-btn {
  background-color: #86909c;
  border-color: #86909c;
}

.report-btn:hover {
  background-color: #6b7785;
  border-color: #6b7785;
}

/* 分页区域样式 */
.pagination-section {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.pagination-controls .el-pagination {
  margin: 0;
}

.pagination-controls .el-pagination button,
.pagination-controls .el-pagination .el-pager li {
  min-width: 36px;
  height: 36px;
  line-height: 36px;
  border-radius: 4px;
}

.pagination-controls .el-pagination .el-pager li.active {
  background-color: #165dff;
  color: #fff;
}

/* 响应式设计 */
@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;
  }
}
/* 导航栏样式 */
.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);
}

.nav-tab {
  cursor: pointer;
  user-select: none;
}
</style>
