<template>
  <div v-if="visible" class="team-dialog-overlay" @click="handleOverlayClick">
    <div class="team-dialog" @click.stop>
      <!-- 标题栏 -->
      <div class="dialog-header">
        <div class="header-title">潼南护路团队成员</div>
        <div class="close-btn" @click="handleClose">
          <el-icon><Close /></el-icon>
        </div>
      </div>

      <!-- 数据表格 -->
      <div class="table-section">
        <div class="table-header">
          <div
            v-for="(column, index) in tableColumns"
            :key="index"
            class="th"
            :style="{ width: column.width, flex: column.flex || 'none' }"
          >
            {{ column.label }}
          </div>
        </div>
        <div class="table-body">
          <div
            v-for="(item, index) in tableData"
            :key="item.id"
            class="table-row"
            :class="{ 'row-even': index % 2 === 1 }"
          >
            <div class="td" style="width: 60px">{{ item.id }}</div>
            <div class="td" style="width: 100px">{{ item.district }}</div>
            <div class="td" style="width: 80px">{{ item.totalCount }}</div>
            <div class="td" style="width: 140px">{{ item.trafficDept }}</div>
            <div class="td" style="width: 120px">{{ item.roadOrg }}</div>
            <div class="td" style="width: 140px">{{ item.maintenance }}</div>
            <div class="td" style="width: 80px">{{ item.roadKeeper }}</div>
            <div class="td" style="flex: 1">
              <span class="view-link" @click="handleView(item)">查看</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination">
        <div class="page-btn" :class="{ disabled: currentPage === 1 }" @click="prevPage">
          上一个
        </div>
        <div class="page-numbers">
          <div
            v-for="page in visiblePages"
            :key="page"
            class="page-num"
            :class="{ active: currentPage === page }"
            @click="goToPage(page)"
          >
            {{ page }}
          </div>
        </div>
        <div class="page-btn" :class="{ disabled: currentPage === totalPages }" @click="nextPage">
          下一个
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from "vue";
import { Close } from "@element-plus/icons-vue";

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(["update:visible", "close", "view"]);

// 表格列配置
const tableColumns = ref([
  { label: "序号", width: "60px" },
  { label: "区县", width: "100px" },
  { label: "总人数", width: "80px" },
  { label: "交通主管部门责任人", width: "140px" },
  { label: "公路机构责任人", width: "120px" },
  { label: "养护站道班责任人", width: "140px" },
  { label: "护路员", width: "80px" },
  { label: "操作", flex: "1" },
]);

// 表格数据
const tableData = ref([
  {
    id: 1,
    district: "潼南",
    totalCount: 128,
    trafficDept: 2,
    roadOrg: 3,
    maintenance: 6,
    roadKeeper: 117,
  },
  {
    id: 2,
    district: "万州",
    totalCount: 96,
    trafficDept: 1,
    roadOrg: 2,
    maintenance: 4,
    roadKeeper: 89,
  },
  {
    id: 3,
    district: "沙坪坝",
    totalCount: 156,
    trafficDept: 3,
    roadOrg: 4,
    maintenance: 8,
    roadKeeper: 141,
  },
  {
    id: 4,
    district: "渝中",
    totalCount: 64,
    trafficDept: 1,
    roadOrg: 2,
    maintenance: 3,
    roadKeeper: 58,
  },
]);

// 分页
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(36);

const totalPages = computed(() => Math.ceil(total.value / pageSize.value));

const visiblePages = computed(() => {
  const pages = [];
  const maxVisible = 5;
  let start = Math.max(1, currentPage.value - Math.floor(maxVisible / 2));
  let end = Math.min(totalPages.value, start + maxVisible - 1);

  if (end - start + 1 < maxVisible) {
    start = Math.max(1, end - maxVisible + 1);
  }

  for (let i = start; i <= end; i++) {
    pages.push(i);
  }
  return pages;
});

// 关闭对话框
const handleClose = () => {
  emit("update:visible", false);
  emit("close");
};

// 点击遮罩关闭
const handleOverlayClick = () => {
  handleClose();
};

// 查看详情
const handleView = (item) => {
  emit("view", item);
};

// 分页操作
const prevPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--;
    fetchData();
  }
};

const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++;
    fetchData();
  }
};

const goToPage = (page) => {
  currentPage.value = page;
  fetchData();
};

// 获取数据
const fetchData = () => {
  console.log("获取第", currentPage.value, "页数据");
  // 实际项目中调用API获取数据
};

// 监听visible变化
watch(
  () => props.visible,
  (newVal) => {
    if (newVal) {
      currentPage.value = 1;
      fetchData();
    }
  }
);
</script>

<style lang="scss" scoped>
.team-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.team-dialog {
  width: 80vw;
  max-width: 900px;
  background: linear-gradient(135deg, rgba(20, 50, 90, 0.95) 0%, rgba(10, 30, 60, 0.98) 100%);
  border: 1px solid rgba(64, 169, 255, 0.3);
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

// 标题栏
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin-bottom: 20px;

  .header-title {
    font-size: 20px;
    font-weight: 600;
    color: #fff;
    padding: 8px 40px;
    background: linear-gradient(90deg, transparent 0%, rgba(64, 169, 255, 0.2) 20%, rgba(64, 169, 255, 0.2) 80%, transparent 100%);
    border-bottom: 2px solid #40a9ff;
  }

  .close-btn {
    position: absolute;
    right: 0;
    top: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.7);
    cursor: pointer;
    font-size: 20px;
    transition: color 0.3s;

    &:hover {
      color: #fff;
    }
  }
}

// 表格区域
.table-section {
  background-color: rgba(30, 70, 120, 0.3);
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 20px;

  .table-header {
    display: flex;
    background-color: rgba(64, 169, 255, 0.2);
    padding: 12px 16px;

    .th {
      font-size: 14px;
      font-weight: 500;
      color: #fff;
      text-align: center;
    }
  }

  .table-body {
    max-height: 40vh;
    overflow-y: auto;

    .table-row {
      display: flex;
      padding: 14px 16px;
      align-items: center;
      transition: background-color 0.3s;

      &:hover {
        background-color: rgba(64, 169, 255, 0.1);
      }

      &.row-even {
        background-color: rgba(30, 70, 120, 0.2);
      }

      .td {
        font-size: 13px;
        color: rgba(255, 255, 255, 0.85);
        text-align: center;

        // 查看链接
        .view-link {
          color: #40a9ff;
          cursor: pointer;
          font-size: 13px;
          transition: color 0.3s;

          &:hover {
            color: #69c0ff;
            text-decoration: underline;
          }
        }
      }
    }
  }
}

// 分页
.pagination {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;

  .page-btn {
    padding: 6px 16px;
    background-color: rgba(64, 169, 255, 0.1);
    border: 1px solid rgba(64, 169, 255, 0.3);
    border-radius: 4px;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.8);
    cursor: pointer;
    transition: all 0.3s;

    &:hover:not(.disabled) {
      background-color: rgba(64, 169, 255, 0.2);
      border-color: rgba(64, 169, 255, 0.5);
    }

    &.disabled {
      opacity: 0.4;
      cursor: not-allowed;
    }
  }

  .page-numbers {
    display: flex;
    gap: 8px;

    .page-num {
      min-width: 28px;
      height: 28px;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: rgba(64, 169, 255, 0.1);
      border: 1px solid rgba(64, 169, 255, 0.3);
      border-radius: 4px;
      font-size: 13px;
      color: rgba(255, 255, 255, 0.8);
      cursor: pointer;
      transition: all 0.3s;

      &:hover:not(.active) {
        background-color: rgba(64, 169, 255, 0.2);
        border-color: rgba(64, 169, 255, 0.5);
      }

      &.active {
        background-color: #40a9ff;
        border-color: #40a9ff;
        color: #fff;
      }
    }
  }
}

// 滚动条样式
.table-body::-webkit-scrollbar {
  width: 6px;
}

.table-body::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 3px;
}

.table-body::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, #40a9ff 0%, #1890ff 100%);
  border-radius: 3px;
}

.table-body::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(180deg, #69c0ff 0%, #40a9ff 100%);
}
</style>
