<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 active" @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>
        </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="processing"></el-option>
              <el-option label="已完成" value="completed"></el-option>
            </el-select>
          </div>
          <div class="filter-item">
            <el-select v-model="priority" placeholder="优先级">
              <el-option label="高优先级" value="high"></el-option>
              <el-option label="中优先级" value="medium"></el-option>
              <el-option label="低优先级" value="low"></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="liyang"></el-option>
              <el-option label="张明" value="zhangming"></el-option>
            </el-select>
          </div>
          <div class="filter-item">
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm"
            />
          </div>
          <div class="filter-actions">
            <el-button type="primary" class="search-btn" @click="handleSearch"> 搜索 </el-button>
          </div>
        </div>
      </div>

      <!-- 统计卡片区域 -->
      <div class="statistics-container">
        <div class="stat-card">
          <div class="stat-info">
            <p class="stat-label">本月报修数</p>
            <p class="stat-value">24</p>
            <p class="stat-trend up">较上月 +4.2%</p>
          </div>
          <div class="stat-icon">
            <i class="el-icon-file-text"></i>
          </div>
        </div>

        <div class="stat-card">
          <div class="stat-info">
            <p class="stat-label">平均处理时长</p>
            <p class="stat-value">3.5小时</p>
            <p class="stat-trend down">较上月 -0.6小时</p>
          </div>
          <div class="stat-icon">
            <i class="el-icon-clock"></i>
          </div>
        </div>

        <div class="stat-card">
          <div class="stat-info">
            <p class="stat-label">待处理报修</p>
            <p class="stat-value">15</p>
            <p class="stat-trend warning">需及时处理</p>
          </div>
          <div class="stat-icon warning">
            <i class="el-icon-alarm-clock"></i>
          </div>
        </div>

        <div class="stat-card">
          <div class="stat-info">
            <p class="stat-label">完成率</p>
            <p class="stat-value">92%</p>
            <p class="stat-trend up">较上月 +2.1%</p>
          </div>
          <div class="stat-icon success">
            <i class="el-icon-check-circle"></i>
          </div>
        </div>
      </div>

      <!-- 报修记录表格 -->
      <div class="table-container">
        <el-table :data="filteredRecords" border style="width: 100%" class="record-table">
          <el-table-column prop="reportNo" label="报修单号" width="140"></el-table-column>
          <el-table-column prop="content" label="报修内容"></el-table-column>
          <el-table-column prop="reporter" label="报修人" width="100"></el-table-column>
          <el-table-column prop="reportTime" label="报修时间" width="160"></el-table-column>
          <el-table-column prop="handler" label="处理人员" width="100"></el-table-column>
          <el-table-column prop="status" label="处理状态" width="100">
            <template #default="scope">
              <span :class="`status-tag ${scope.row.statusClass}`">{{ scope.row.status }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="handleTime" label="处理时间" width="160"></el-table-column>
          <el-table-column prop="result" label="维修结果"></el-table-column>
          <el-table-column label="操作" width="160">
            <template #default="scope">
              <el-button type="text" size="small" class="detail-btn" @click="handleDetail(scope.row)"> 详情 </el-button>
              <el-button type="text" size="small" :class="scope.row.actionClass" @click="handleAction(scope.row)">
                {{ scope.row.actionText }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

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

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

// 筛选条件
const taskStatus = ref('');
const priority = ref('');
const executor = ref('');
const dateRange = ref([]);

// 分页相关
const currentPage = ref(3);
const pageSize = ref(7);

// 报修记录数据
const repairRecords = ref([
  {
    reportNo: 'R-2025-0620-001',
    content: '服务器A1电源故障',
    reporter: '张明',
    reportTime: '2025-06-20 08:30',
    handler: '李阳',
    status: '处理中',
    statusClass: 'processing',
    handleTime: '--',
    result: '--',
    actionText: '跟进',
    actionClass: 'follow-btn'
  },
  {
    reportNo: 'R-2025-0620-002',
    content: '测试软件授权过期',
    reporter: '张明',
    reportTime: '2025-06-20 09:15',
    handler: '李阳',
    status: '已完成',
    statusClass: 'completed',
    handleTime: '2025-06-21 14:30',
    result: '已重新授权',
    actionText: '评价',
    actionClass: 'evaluate-btn'
  },
  {
    reportNo: 'R-2025-0620-003',
    content: '打印机卡纸故障',
    reporter: '张明',
    reportTime: '2025-06-20 10:45',
    handler: '李阳',
    status: '已完成',
    statusClass: 'completed',
    handleTime: '2025-06-21 10:15',
    result: '已清除卡纸，设备正常',
    actionText: '评价',
    actionClass: 'evaluate-btn'
  },
  {
    reportNo: 'R-2025-0620-004',
    content: '网络交换机端口故障',
    reporter: '张明',
    reportTime: '2025-06-20 13:20',
    handler: '李阳',
    status: '已完成',
    statusClass: 'completed',
    handleTime: '2025-06-20 15:40',
    result: '已更换端口',
    actionText: '评价',
    actionClass: 'evaluate-btn'
  },
  {
    reportNo: 'R-2025-0620-005',
    content: '服务器A1电源故障',
    reporter: '张明',
    reportTime: '2025-06-20 14:50',
    handler: '李阳',
    status: '已完成',
    statusClass: 'completed',
    handleTime: '2025-06-21 09:30',
    result: '已重新授权',
    actionText: '评价',
    actionClass: 'evaluate-btn'
  },
  {
    reportNo: 'R-2025-0620-006',
    content: '机房空调温度异常',
    reporter: '张明',
    reportTime: '2025-06-20 16:10',
    handler: '李阳',
    status: '已完成',
    statusClass: 'completed',
    handleTime: '2025-06-20 17:25',
    result: '已修复，温度正常',
    actionText: '评价',
    actionClass: 'evaluate-btn'
  },
  {
    reportNo: 'R-2025-0620-007',
    content: '监控系统无法连接',
    reporter: '张明',
    reportTime: '2025-06-20 18:05',
    handler: '李阳',
    status: '已完成',
    statusClass: 'completed',
    handleTime: '2025-06-21 08:45',
    result: '网络配置已重置',
    actionText: '评价',
    actionClass: 'evaluate-btn'
  }
]);

// 总记录数
const total = ref(54);

// 筛选后的记录
const filteredRecords = computed(() => {
  // 实际应用中这里会根据筛选条件过滤数据
  return repairRecords.value;
});

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

// 创建任务弹窗相关
const createTaskDialogVisible = ref(false);
const createTaskForm = ref({
  taskName: '',
  repairContent: '',
  timeRange: [],
  priority: '',
  handler: ''
});

const createTaskRules = {
  taskName: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
  repairContent: [{ required: true, message: '请输入报修内容', trigger: 'blur' }],
  timeRange: [{ required: true, message: '请选择时间范围', trigger: 'change' }]
};

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

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

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

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

// 查看详情
const handleDetail = (record) => {
  console.log('查看详情:', record);
};

// 处理操作
const handleAction = (record) => {
  console.log('执行操作:', record.actionText, record);
};

// 导航事件
const handleInspectionManagement1 = () => {
  router.push('/rili/baoxiuguanli');
};

const handleInspectionManagement2 = () => {
  router.push('/rili/baoxiujilu');
};

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);
}

/* 筛选栏样式 */
.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,
.filter-bar .el-date-picker {
  width: 180px;
  height: 36px;
}

.filter-bar .el-select .el-input__inner,
.filter-bar .el-date-picker .el-input__inner {
  border-radius: 4px;
  border-color: #dcdfe6;
  transition: all 0.2s ease;
}

.filter-bar .el-select .el-input__inner:focus,
.filter-bar .el-date-picker .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: 12px;
  flex-shrink: 0;
}

.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;
}

/* 统计卡片样式 */
.statistics-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-3px);
}

.stat-info {
  flex: 1;
}

.stat-label {
  font-size: 14px;
  color: #86909c;
  margin: 0 0 8px 0;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: #1d2129;
  margin: 0 0 4px 0;
}

.stat-trend {
  font-size: 12px;
  margin: 0;
  display: flex;
  align-items: center;
}

.stat-trend.up {
  color: #00b42a;
}

.stat-trend.up::before {
  content: '↑';
  margin-right: 4px;
}

.stat-trend.down {
  color: #ff4d4f;
}

.stat-trend.down::before {
  content: '↓';
  margin-right: 4px;
}

.stat-trend.warning {
  color: #fa8c16;
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background-color: #e8f3ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #165dff;
  font-size: 24px;
}

.stat-icon.warning {
  background-color: #fff7e6;
  color: #fa8c16;
}

.stat-icon.success {
  background-color: #f6ffed;
  color: #52c41a;
}

/* 表格样式 */
.table-container {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  padding: 16px;
  margin-bottom: 24px;
}

.record-table {
  border-collapse: separate;
  border-spacing: 0;
}

.record-table th {
  background-color: #f7f8fa;
  color: #4e5969;
  font-weight: 500;
  font-size: 14px;
}

.record-table td {
  color: #1d2129;
  font-size: 14px;
}

.status-tag {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.status-tag.processing {
  background-color: #fffbe6;
  color: #faad14;
  border: 1px solid #fff1b8;
}

.status-tag.completed {
  background-color: #f0f9eb;
  color: #52c41a;
  border: 1px solid #e1f3d8;
}

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

.follow-btn {
  color: #fa8c16;
}

.evaluate-btn {
  color: #52c41a;
}

/* 分页区域样式 */
.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;
}

/* 导航栏样式 */
.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;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .statistics-container {
    grid-template-columns: repeat(2, 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,
  .filter-bar .el-date-picker {
    width: 100%;
  }

  .statistics-container {
    grid-template-columns: 1fr;
  }

  .table-container {
    overflow-x: auto;
  }
}
</style>
