<template>
  <base-dialog
    v-model:visible="props.visible"
    title="调度区县情况"
    :table-data="tableData"
    :table-columns="tableColumns"
    :table-height="tableHeight"
    :total="total"
    :current-page="currentPage"
    :page-size="pageSize"
    :z-index="2200"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    @close="handleClose"
  >
    <!-- 筛选区域 -->
    <template #filter>
      <div class="filter-row">
        <div class="filter-item">
          <span class="filter-label">影响区域</span>
          <el-select :teleported="false"
            v-model="filterForm.region"
            placeholder="请选择"
            class="filter-select"
            clearable
          >
            <el-option
              v-for="item in regionOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
    </template>

    <!-- 调度数列插槽 -->
    <template #dispatchCount="{ row }">
      <span class="dispatch-count" @click="handleDispatchClick(row)">{{
        row.dispatchCount
      }}</span>
    </template>
  </base-dialog>
</template>

<script setup>
import { ref, computed, watch } from "vue";
import { Close } from "@element-plus/icons-vue";
import { regionOptions } from "../component/index.js";
import BaseDialog from "../component/baseDialog.vue";

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

const emit = defineEmits({
  "update:visible": (value) => typeof value === "boolean",
  close: () => true,
  dispatchClick: (item) => item !== undefined,
});

// 筛选表单
const filterForm = ref({
  region: "",
});

// 影响区域选项
// 已从 index.js 导入

// 表格高度
const tableHeight = ref(300);

// 表格列配置
const tableColumns = ref([
  { prop: "id", label: "序号", width: "" },
  { prop: "region", label: "影响区域", width: "" },
  { prop: "dispatchCount", label: "调度数", width: "", slot: "dispatchCount" },
  { prop: "lastDispatchTime", label: "最近调度时间", width: "" },
]);

// 表格数据
const tableData = ref([
  {
    id: 1,
    region: "重庆市",
    dispatchCount: 1,
    lastDispatchTime: "2025-08-11 04:53:42",
  },
  {
    id: 2,
    region: "万州区",
    dispatchCount: 1,
    lastDispatchTime: "2025-08-11 04:53:42",
  },
  {
    id: 3,
    region: "沙坪坝区",
    dispatchCount: 3,
    lastDispatchTime: "2025-08-10 16:20:15",
  },
  {
    id: 4,
    region: "渝中区",
    dispatchCount: 2,
    lastDispatchTime: "2025-08-09 11:45:30",
  },
]);

// 分页
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 handleDispatchClick = (item) => {
  emit("dispatchClick", item);
};

// 分页操作
const handleSizeChange = (val) => {
  pageSize.value = val;
  fetchData();
};

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

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

// 监听visible变化
watch(
  () => props.visible,
  (newVal) => {
    if (newVal) {
      currentPage.value = 1;
      // 重置筛选表单
      filterForm.value.region = "";
      fetchData();
    }
  },
);
</script>

<style lang="scss" scoped>
// 筛选区域
.filter-section {
  margin-bottom: 16px;

  .filter-row {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
  }

  .filter-item {
    display: flex;
    align-items: center;
    gap: 8px;

    .filter-label {
      font-size: 13px;
      color: rgba(255, 255, 255, 0.8);
      white-space: nowrap;
    }

    .filter-select {
      width: 140px;

      :deep(.el-input__wrapper) {
        background-color: rgba(30, 70, 120, 0.4);
        border: 1px solid rgba(64, 169, 255, 0.3);
        box-shadow: none;
        border-radius: 4px;

        .el-input__inner {
          color: #fff;
          font-size: 13px;

          &::placeholder {
            color: rgba(255, 255, 255, 0.4);
          }
        }

        .el-input__suffix {
          .el-icon {
            color: rgba(255, 255, 255, 0.6);
          }
        }
      }
    }
  }
}

// 调度数字
.dispatch-count {
  color: #40a9ff;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.3s;

  &:hover {
    color: #69c0ff;
    text-shadow: 0 0 8px rgba(105, 192, 255, 0.6);
  }
}
</style>
