<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="themeStore.theme === 'light' ? banner_white : banner"
            class="drive-hero__image"
          />
        </div>
      </section>

      <section class="drive-tabs px-10 mt-2!">
        <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, watch } from "vue";

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

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 drivePc1_white from "@/assets/images/dr1_white.png"; // 经营驾驶舱pc端背景图
import drivePc2_white from "@/assets/images/生产pc_white.png"; // 生产驾驶舱pc端背景图
import drivePc3_white from "@/assets/images/财务pc_white.png"; // 财务驾驶舱pc端背景图
import drivePc4_white from "@/assets/images/gong_white.png";
import drivePc5_white from "@/assets/images/市场pc_white.png";
import drivePc6_white from "@/assets/images/qhsepc_white.png";

import driveMobile1_white from "@/assets/images/mobile_white.png"; // 经营驾驶舱移动端背景图
import driveMobile2_white from "@/assets/images/生产驾驶舱_white.png"; // 生产驾驶舱移动端背景图
import driveMobile3_white from "@/assets/images/财务驾驶舱_white.png"; // 生产驾驶舱移动端背景图
import driveMobile4_white from "@/assets/images/供应链驾驶舱_white.png"; // 生产驾驶舱移动端背景图
import driveMobile5_white from "@/assets/images/市场驾驶舱_white.png"; // 生产驾驶舱移动端背景图
import driveMobile6_white from "@/assets/images/QHSE驾驶舱_white.png"; // 生产驾驶舱移动端背景图

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 themeStore = useThemeStore();

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; // 新增分类字段
};

watch(
  () => themeStore.theme,
  () => {
    if (themeStore.theme === "light") {
      driveCards.value = [
        {
          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_white,
          mobileBg: driveMobile1_white,
          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_white,
          mobileBg: driveMobile2_white,
          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_white,
          mobileBg: driveMobile3_white,
          category: "finance", // 对应财务管理
        },
        {
          title: "供应链驾驶舱",
          description: "查看供应链分析、物料需求与执行。",
          url: "#",
          bgColor: "#7c3aed",
          pcBg: drivePc4_white,
          mobileBg: driveMobile4_white,
          category: "supply", // 对应供应链管理
        },
        {
          title: "市场驾驶舱",
          description: "查看市场分析、销售数据与执行情况。",
          url: "#",
          bgColor: "#16a34a",
          pcBg: drivePc5_white,
          mobileBg: driveMobile5_white,
          category: "marketing", // 对应市场营销
        },
        {
          title: "QHSE驾驶舱",
          description: "查看安全、健康、环境与质量数据。",
          url: `${import.meta.env.VITE_PMS_URL}/portalLogin?username=${userStore.getUser.username}&source=qhse`,
          bgColor: "#dc2626",
          pcBg: drivePc6_white,
          mobileBg: driveMobile6_white,
          category: "qhse", // 对应 QHSE
        },
      ];
    } else {
      driveCards.value = [
        {
          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: `${import.meta.env.VITE_PMS_URL}/portalLogin?username=${userStore.getUser.username}&source=qhse`,
          bgColor: "#dc2626",
          pcBg: drivePc6,
          mobileBg: driveMobile6,
          category: "qhse", // 对应 QHSE
        },
      ];
    }
  },
);

const driveCards = ref([
  {
    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: themeStore.theme === "light" ? drivePc1_white : drivePc1,
    mobileBg: themeStore.theme === "light" ? driveMobile1_white : 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: themeStore.theme === "light" ? drivePc2_white : drivePc2,
    mobileBg: themeStore.theme === "light" ? driveMobile2_white : 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: themeStore.theme === "light" ? drivePc3_white : drivePc3,
    mobileBg: themeStore.theme === "light" ? driveMobile3_white : driveMobile3,
    category: "finance", // 对应财务管理
  },
  {
    title: "供应链驾驶舱",
    description: "查看供应链分析、物料需求与执行。",
    url: "#",
    bgColor: "#7c3aed",
    pcBg: themeStore.theme === "light" ? drivePc4_white : drivePc4,
    mobileBg: themeStore.theme === "light" ? driveMobile4_white : driveMobile4,
    category: "supply", // 对应供应链管理
  },
  {
    title: "市场驾驶舱",
    description: "查看市场分析、销售数据与执行情况。",
    url: "#",
    bgColor: "#16a34a",
    pcBg: themeStore.theme === "light" ? drivePc5_white : drivePc5,
    mobileBg: themeStore.theme === "light" ? driveMobile5_white : driveMobile5,
    category: "marketing", // 对应市场营销
  },
  {
    title: "QHSE驾驶舱",
    description: "查看安全、健康、环境与质量数据。",
    url: `${import.meta.env.VITE_PMS_URL}/portalLogin?username=${userStore.getUser.username}&source=qhse`,
    bgColor: "#dc2626",
    pcBg: themeStore.theme === "light" ? drivePc6_white : drivePc6,
    mobileBg: themeStore.theme === "light" ? driveMobile6_white : driveMobile6,
    category: "qhse", // 对应 QHSE
  },
]);

const filteredCards = computed(() => {
  if (activeTab.value === "all") {
    return driveCards.value;
  }
  return driveCards.value.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 {
  --portal-text: #17345f;
  --portal-text-muted: #5f6f83;
  --portal-text-soft: #7f8fa6;
  --portal-title: #163867;
  --portal-subtitle: rgba(61, 92, 135, 0.86);
  --portal-line: rgba(126, 156, 201, 0.24);
  --portal-card: rgba(255, 255, 255, 0.82);
  --portal-card-2: rgba(248, 251, 255, 0.94);
  --portal-card-3: rgba(240, 246, 255, 0.88);
  --portal-card-4: rgba(231, 239, 251, 0.92);
  --portal-nav-bg: rgba(255, 255, 255, 0.72);
  --portal-nav-hover: rgba(219, 232, 252, 0.8);
  --portal-input-bg: rgba(255, 255, 255, 0.7);
  --portal-input-hover: rgba(255, 255, 255, 0.92);
  --portal-shadow: 0 18px 40px rgba(23, 52, 95, 0.12);
  --portal-shadow-strong: 0 24px 60px rgba(23, 52, 95, 0.16);
  --portal-accent: #245edb;
  --portal-accent-2: #4e8cff;
  --portal-accent-soft: rgba(36, 94, 219, 0.14);
  --portal-todo-bg: rgba(242, 247, 255, 0.94);
  --portal-todo-hover: rgba(228, 238, 252, 0.95);
  --portal-number-todo: #e15a5a;
  --portal-number-done: #2da04d;
  color: var(--portal-text);
  background:
    radial-gradient(
      circle at 18% 12%,
      rgba(83, 126, 255, 0.14),
      transparent 22%
    ),
    radial-gradient(
      circle at 82% 20%,
      rgba(71, 148, 255, 0.14),
      transparent 20%
    ),
    radial-gradient(
      circle at 50% 100%,
      rgba(97, 142, 247, 0.12),
      transparent 28%
    ),
    linear-gradient(180deg, #eef3f9 0%, #f7faff 46%, #eef3f9 100%);
}

:global([data-theme="dark"] .drive-page) {
  --portal-text: #eaf1ff;
  --portal-text-muted: rgba(234, 241, 255, 0.95);
  --portal-text-soft: #8a9ab0;
  --portal-title: #f4f7ff;
  --portal-subtitle: rgba(188, 205, 255, 0.82);
  --portal-line: rgba(97, 129, 206, 0.28);
  --portal-card: rgba(10, 19, 43, 0.8);
  --portal-card-2: rgba(12, 24, 52, 0.92);
  --portal-card-3: rgba(17, 25, 48, 0.8);
  --portal-card-4: rgba(15, 24, 45, 0.82);
  --portal-nav-bg: rgba(10, 19, 43, 0.8);
  --portal-nav-hover: rgba(28, 40, 72, 0.8);
  --portal-input-bg: rgba(255, 255, 255, 0.08);
  --portal-input-hover: rgba(255, 255, 255, 0.12);
  --portal-shadow: 0 16px 34px rgba(0, 0, 0, 0.22);
  --portal-shadow-strong: 0 24px 60px rgba(0, 0, 0, 0.38);
  --portal-accent: #6e7dff;
  --portal-accent-2: #8d4dff;
  --portal-accent-soft: rgba(110, 125, 255, 0.16);
  --portal-todo-bg: #070e20;
  --portal-todo-hover: rgba(28, 40, 72, 0.8);
  --portal-number-todo: #f56c6c;
  --portal-number-done: #ffffff;
  color: var(--portal-text);
  background:
    radial-gradient(
      circle at 18% 12%,
      rgba(79, 82, 221, 0.34),
      transparent 22%
    ),
    radial-gradient(circle at 82% 20%, rgba(28, 95, 255, 0.2), transparent 20%),
    radial-gradient(
      circle at 50% 100%,
      rgba(103, 46, 255, 0.16),
      transparent 28%
    ),
    linear-gradient(180deg, #040814 0%, #060d1d 46%, #040814 100%);
}

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

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

  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: var(--bg-tab);
  box-shadow: 0 16px 34px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.drive-tab {
  height: 58px;
  border: 0;
  border-right: 1px solid rgba(255, 255, 255, 0.06);
  background: transparent;
  color: var(--tab-text);
  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: var(--bg-drive-tab);

  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;
  background-color: transparent !important;

  /* 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;
}

.drive-card:hover {
  transform: scale(1.02);
  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>
