<template>
  <div class="drive-page">
    <Header />

    <main class="drive-main">
      <section class="drive-hero">
        <div class="drive-hero__copy pl-5">
          <p class="drive-hero__eyebrow">DASHBOARD PORTAL</p>
          <p class="drive-hero__title">驾驶舱门户</p>
          <p class="drive-hero__desc">选择一个驾驶舱，快速进入对应业务看板。</p>
        </div>
        <div class="drive-hero__visual">
          <el-image :src="banner" class="drive-hero__image" />
        </div>
      </section>

      <section class="drive-tabs px-10">
        <button
          v-for="tab in tabs"
          :key="tab.value"
          type="button"
          class="drive-tab"
          :class="{ 'drive-tab--active': activeTab === tab.value }"
          @click="activeTab = tab.value"
        >
          <div class="flex items-center gap-2">
            <img :src="tab.icon" alt="tab icon" class="w-6 h-6" />
            {{ tab.label }}
          </div>
        </button>
      </section>

      <section class="drive-grid md:px-20">
        <button
          v-for="card in filteredCards"
          :key="card.title"
          type="button"
          class="drive-card overflow-hidden"
          :style="{
            '--bg-image': `url(${card.pcBg})`,
            '--mobile-bg-image': `url(${card.mobileBg})`,
          }"
          @click="openDrive(card)"
        ></button>

        <div
          v-if="filteredCards.length === 0"
          class="text-center py-10 text-gray-400 col-span-2"
        >
          暂无相关驾驶舱
        </div>
      </section>
    </main>

    <div class="mt-[500px] md:mt-[300px]">
      <Footer />
    </div>
  </div>
</template>

<script setup lang="ts">
import Header from "@components/home/header.vue";
import Footer from "@components/home/Footer.vue";
import { ref, computed } from "vue";

import { getMCSsoToken } from "@/api/user";
import { useUserStore } from "@/stores/useUserStore";
import { getAccessToken } from "@/utils/auth";
import banner from "@/assets/images/drivebannere.png";

import drivePc1 from "@/assets/images/dr1.png"; // 经营驾驶舱pc端背景图
import drivePc2 from "@/assets/images/生产pc.png"; // 生产驾驶舱pc端背景图
import drivePc3 from "@/assets/images/财务pc.png"; // 财务驾驶舱pc端背景图
import drivePc4 from "@/assets/images/供应链pc.png"; // 供应链驾驶舱pc端背景图
import drivePc5 from "@/assets/images/市场pc.png"; // 市场驾驶舱pc端背景图
import drivePc6 from "@/assets/images/qhsepc.png"; // QHSE驾驶舱pc端背景图
import driveMobile1 from "@/assets/images/mobile.png"; // 经营驾驶舱移动端背景图
import driveMobile2 from "@/assets/images/生产驾驶舱.png"; // 生产驾驶舱移动端背景图
import driveMobile3 from "@/assets/images/财务驾驶舱.png"; // 财务驾驶舱移动端背景图
import driveMobile4 from "@/assets/images/供应链驾驶舱.png"; // 供应链驾驶舱移动端背景图
import driveMobile5 from "@/assets/images/市场驾驶舱.png"; // 市场驾驶舱移动端背景图
import driveMobile6 from "@/assets/images/QHSE驾驶舱.png"; // QHSE驾驶舱移动端背景图

import all from "@/assets/images/driveall.png";
import jingying from "@/assets/images/jingying.png";
import shengc from "@/assets/images/shengc.png";
import cai from "@/assets/images/cai.png";
import gong from "@/assets/images/gong.png";
import city from "@/assets/images/city.png";

const userStore = useUserStore();

const tabs = [
  { label: "全部驾驶舱", value: "all", icon: all },
  { label: "经营管理", value: "management", icon: jingying },
  { label: "生产运营", value: "production", icon: shengc },
  { label: "财务管理", value: "finance", icon: cai },
  { label: "供应链管理", value: "supply", icon: gong },
  { label: "市场营销", value: "marketing", icon: city },
  { label: "QHSE", value: "qhse", icon: gong },
];
const activeTab = ref("all");
type DriveCard = {
  title: string;
  description: string;
  url: string;
  bgColor: string;
  pcBg: string;
  mobileBg: string;
  category: string; // 新增分类字段
};

const driveCards: DriveCard[] = [
  {
    title: "经营驾驶舱",
    description: "查看经营分析、经营指标与执行情况。",
    url: "https://report.deepoil.cc/webroot/decision/v10/entry/access/9fb42908-894a-4373-a6be-ce046a42851d?preview=true&page_number=1",
    bgColor: "#3876e0",
    pcBg: drivePc1,
    mobileBg: driveMobile1,
    category: "management", // 对应经营管理
  },
  {
    title: "生产驾驶舱",
    description: "查看生产运营态势、核心指标与执行情况。",
    url: "https://report.deepoil.cc/webroot/decision/v10/entry/access/dbc9cf73-81ce-43f1-9923-45cdfa5d5d3a?preview=true&page_number=1",
    bgColor: "#0f766e",
    pcBg: drivePc2,
    mobileBg: driveMobile2,
    category: "production", // 对应生产运营
  },
  {
    title: "财务驾驶舱",
    description: "查看财务分析、预算执行与经营数据表现。",
    url: "https://report.deepoil.cc/webroot/decision/v10/entry/access/e836fb5b-092c-4d64-a324-3beeb4fac0cc?preview=true&page_number=1",
    bgColor: "#ca8a04",
    pcBg: drivePc3,
    mobileBg: driveMobile3,
    category: "finance", // 对应财务管理
  },
  {
    title: "供应链驾驶舱",
    description: "查看供应链分析、物料需求与执行。",
    url: "#",
    bgColor: "#7c3aed",
    pcBg: drivePc4,
    mobileBg: driveMobile4,
    category: "supply", // 对应供应链管理
  },
  {
    title: "市场驾驶舱",
    description: "查看市场分析、销售数据与执行情况。",
    url: "#",
    bgColor: "#16a34a",
    pcBg: drivePc5,
    mobileBg: driveMobile5,
    category: "marketing", // 对应市场营销
  },
  {
    title: "QHSE驾驶舱",
    description: "查看安全、健康、环境与质量数据。",
    url: "#",
    bgColor: "#dc2626",
    pcBg: drivePc6,
    mobileBg: driveMobile6,
    category: "qhse", // 对应 QHSE
  },
];

const filteredCards = computed(() => {
  if (activeTab.value === "all") {
    return driveCards;
  }
  return driveCards.filter((card) => card.category === activeTab.value);
});

const openDrive = async (option: DriveCard) => {
  if (userStore.getUser.username && getAccessToken()) {
    const res = await getMCSsoToken();
    if (res) {
      window.open(`${option.url}&ssoToken=${res}`, "_blank");
    }
  }
};
</script>

<style scoped>
.drive-page {
  min-height: 100vh;
  background:
    radial-gradient(circle at 50% 0%, rgba(30, 74, 255, 0.18), transparent 28%),
    radial-gradient(
      circle at 82% 18%,
      rgba(151, 66, 255, 0.16),
      transparent 18%
    ),
    linear-gradient(180deg, #000613 0%, #000613 100%);
}

.drive-main {
  max-width: 100vw;
  margin: 0 auto;
  padding-top: 50px;
}

.drive-hero {
  position: relative;
  min-height: 220px;

  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(2, 8, 25, 0.55), rgba(2, 8, 25, 0.86)), #000613;
  box-shadow:
    0 24px 60px rgba(0, 0, 0, 0.42),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.drive-hero::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(
    90deg,
    rgba(0, 6, 19, 0.8) 0%,
    rgba(0, 6, 19, 0.24) 40%,
    rgba(0, 6, 19, 0.08) 100%
  );
  z-index: 1;
}

.drive-hero__copy {
  position: absolute;
  z-index: 2;
  left: 56px;
  top: 54px;
  max-width: 560px;
}

.drive-hero__eyebrow {
  margin: 0 0 16px;
  color: #6d77ff;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.22em;
}

.drive-hero__title {
  margin: 0;
  color: #f5f8ff;
  font-size: clamp(20px, 5vw, 50px);
  font-weight: 900;
  line-height: 1.08;
  text-shadow: 0 10px 24px rgba(0, 0, 0, 0.45);
}

.drive-hero__desc {
  margin: 20px 0 0;
  color: #5c6dc8;
  font-size: 18px;
  line-height: 1.8;
}

.drive-hero__visual {
  position: absolute;
  inset: 0;
}

.drive-hero__image {
  width: 100%;
  height: 100%;
}

.drive-hero__image :deep(img) {
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: saturate(1.1) contrast(1.05);
}

.drive-tabs {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0;
  margin: 0 auto;
  padding: 0px 0px;
  width: 90%;
  border: 1px solid rgba(79, 110, 208, 0.16);
  border-radius: 10px;
  background: #070e24;
  box-shadow: 0 16px 34px rgba(0, 0, 0, 0.2);
}

.drive-tab {
  height: 58px;
  border: 0;
  border-right: 1px solid rgba(255, 255, 255, 0.06);
  background: transparent;
  color: rgba(220, 229, 255, 0.78);
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
}

.drive-tab:last-child {
  border-right: 0;
}

.drive-tab--active {
  color: #fff !important;

  background: linear-gradient(
    360deg,
    #003be0 0%,
    rgba(10, 65, 227, 0.6) 10%,
    #001c71 40%,
    #000c33 80%
  ) !important;

  padding-bottom: 22px;
  padding-top: 22px;
  border-radius: 0;

  position: relative;
}

.drive-tab--active::before {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -2px;
  width: 100%;
  height: 2px;
  transform: translateX(-50%);
  border-radius: 999px;
  background: linear-gradient(
    to right,
    #5887f8 0%,
    #69b5f8 30%,
    #fff 50%,
    #69b5f8 70%,
    #5887f8 100%
  );
  box-shadow: 0 0 12px rgba(112, 120, 255, 0.95);
}

.drive-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 22px;
  margin-top: 30px;
}

.drive-card {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;

  width: 100%;
  height: 140px;

  /* 使用 CSS 变量动态绑定背景图 */
  background-image: var(--bg-image);
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;

  text-align: center;
  cursor: pointer;
  transition:
    transform 0.2s ease,
    box-shadow 0.2s ease,
    border-color 0.2s ease;

  overflow: hidden;
  /* margin-top: -50px; */
}

.drive-card:hover {
  transform: translateY(-4px);
  border-color: rgba(115, 145, 255, 0.42);
  box-shadow: 0 28px 54px rgba(0, 0, 0, 0.42);
}

/* 补充缺失的内容样式，确保文字能显示在背景图上 */
.drive-card__content {
  position: relative;
  z-index: 1;
  min-width: 0;
  flex: 1;
  /* 根据原设计可能需要调整文字颜色或背景遮罩以确保可读性 */
}

.drive-card__title {
  margin: 0;
  color: #f4f7ff;
  font-size: 28px;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.drive-card__text {
  margin: 10px 0 0;
  color: rgba(191, 203, 235, 0.82);
  font-size: 16px;
  line-height: 1.7;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.drive-card__arrow {
  position: relative;
  z-index: 1;
  color: rgba(210, 220, 248, 0.82);
  font-size: 34px;
  flex-shrink: 0;
  transition:
    transform 0.2s ease,
    color 0.2s ease;
}

.drive-card:hover .drive-card__arrow {
  color: #ffffff;
  transform: translate(2px, -2px);
}

@media (max-width: 768px) {
  .drive-main {
    padding: 50px 0px 56px;
    margin-top: 0;
  }

  .drive-hero {
    min-height: 260px;
  }

  .drive-hero__copy {
    left: 20px;
    right: 20px;
    top: 24px;
  }

  .drive-tabs {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .drive-grid {
    grid-template-columns: 1fr;
    gap: 16px;
    padding-left: 0;
  }

  .drive-card {
    min-height: 100px;
    height: 100px; /* 修正原代码中 height: 0px 的问题，确保卡片有高度 */
    width: 100%; /* 修正原代码中 width: 100vw 可能导致溢出问题，通常用 100% */
    padding: 0;
    margin-top: 0;

    /* 使用 CSS 变量动态绑定移动端背景图 */
    background-image: var(--mobile-bg-image);
    background-size: contain;
    background-position: center center;
  }

  .drive-card__title {
    font-size: 20px;
  }

  .drive-hero__image :deep(img) {
    object-fit: cover;
    object-position: 68% center;
  }
}

@media (max-width: 768px) {
  .drive-tabs {
    display: flex; /* 改为 flex 布局 */
    overflow-x: auto; /* 允许横向滚动 */
    white-space: nowrap; /* 防止文字换行 */
    padding: 12px;
    gap: 10px; /* 增加间距 */
    /* 移除原有的 grid 相关属性 */
    grid-template-columns: unset;
    width: 100%;
  }

  .drive-tab {
    flex-shrink: 0; /* 防止按钮被压缩 */
    border-right: none; /* 移除右侧边框，改用间距或底部指示器 */
    padding: 0 16px; /* 增加左右内边距 */
  }
}

/* 优化横向滚动条 */
.drive-tabs::-webkit-scrollbar {
  height: 1px;
}
.drive-tabs::-webkit-scrollbar-thumb {
  background-color: var(--color-border-default);
  border-radius: 2px;
}
</style>
