<template>
  <header></header>
  <main class="p-[80px] mx-auto" style="max-width: 100vw; min-width: 350px">
    <div class="page-title mb-[24px]">
      {{ t("financialinformation.quarterlyreports.title") }}
    </div>
    <div class="search-container">
      <input
        type="text"
        v-model="searchQuery"
        class="search-input"
        :placeholder="
          t('financialinformation.quarterlyreports.search.placeholder')
        "
      />
      <button @click="handleSearch" class="search-button">
        {{ t("financialinformation.quarterlyreports.search.button") }}
      </button>
    </div>

    <div class="results-list">
      <div
        v-for="(item, index) in filteredList"
        :key="index"
        class="result-item flex items-center mt-[20px] mb-[20px]"
      >
        <img
          src="@/assets/image/pdf.png"
          alt="PDF"
          style="width: 20px; height: 20px"
        />
        <div class="content">
          <div class="result-title">{{ item.title }}</div>
        </div>

        <img
          src="@/assets/image/download.svg"
          style="width: 20px; height: 20px"
          @click="downloadPdf(item.url)"
        />
      </div>
    </div>
  </main>
  <footer></footer>
</template>
<script setup>
import { ref, watch, onMounted, computed, reactive } from "vue";
import { useI18n } from "vue-i18n";
import quarterlyPdf2024Q3 from "@/assets/file/quarterly/10Q 2024-Q3.pdf";
import quarterlyPdf2024Q2 from "@/assets/file/quarterly/10Q 2024-Q2.pdf";
import quarterlyPdf2024Q1 from "@/assets/file/quarterly/10Q 2024-Q1.pdf";
import quarterlyPdf2023Q3 from "@/assets/file/quarterly/10Q 2023-Q3.pdf";
import quarterlyPdf2023Q2 from "@/assets/file/quarterly/10Q 2023-Q2.pdf";
import quarterlyPdf2023Q1 from "@/assets/file/quarterly/10Q 2023-Q1.pdf";
import quarterlyPdf2022Q3 from "@/assets/file/quarterly/10Q 2022-Q3.pdf";
import quarterlyPdf2022Q2 from "@/assets/file/quarterly/10Q 2022-Q2.pdf";
import quarterlyPdf2022Q1 from "@/assets/file/quarterly/10Q 2022-Q1.pdf";
import quarterlyPdf2021Q3 from "@/assets/file/quarterly/10Q 2021-Q3.pdf";
import quarterlyPdf2021Q2 from "@/assets/file/quarterly/10Q 2021-Q2.pdf";
import quarterlyPdf2021Q1 from "@/assets/file/quarterly/10Q 2021-Q1.pdf";
import quarterlyPdf2020Q3 from "@/assets/file/quarterly/10Q 2020-Q3.pdf";
import quarterlyPdf2020Q2 from "@/assets/file/quarterly/10Q 2020-Q2.pdf";
import quarterlyPdf2020Q1 from "@/assets/file/quarterly/10Q 2020-Q1.pdf";
import quarterlyPdf2019Q3 from "@/assets/file/quarterly/10Q 2019-Q3.pdf";
import quarterlyPdf2019Q2 from "@/assets/file/quarterly/10Q 2019-Q2.pdf";
import quarterlyPdf2019Q1 from "@/assets/file/quarterly/10Q 2019-Q1.pdf";
import quarterlyPdf2018Q3 from "@/assets/file/quarterly/10Q 2018-Q3.pdf";
import quarterlyPdf2018Q2 from "@/assets/file/quarterly/10Q 2018-Q2.pdf";
import quarterlyPdf2018Q1 from "@/assets/file/quarterly/10Q 2018-Q1.pdf";
import quarterlyPdf2017Q3 from "@/assets/file/quarterly/10Q 2017-Q3.pdf";
import quarterlyPdf2017Q2 from "@/assets/file/quarterly/10Q 2017-Q2.pdf";
import quarterlyPdf2017Q1 from "@/assets/file/quarterly/10Q 2017-Q1.pdf";
import quarterlyPdf2016Q3 from "@/assets/file/quarterly/10Q 2016-Q3.pdf";
import quarterlyPdf2016Q2 from "@/assets/file/quarterly/10Q 2016-Q2.pdf";
import quarterlyPdf2016Q1 from "@/assets/file/quarterly/10Q 2016-Q1.pdf";
import quarterlyPdf2015Q3 from "@/assets/file/quarterly/10Q 2015-Q3.pdf";
import quarterlyPdf2015Q2 from "@/assets/file/quarterly/10Q 2015-Q2.pdf";
import quarterlyPdf2015Q1 from "@/assets/file/quarterly/10Q 2015-Q1.pdf";
import quarterlyPdf2014Q3 from "@/assets/file/quarterly/10Q 2014-Q3.pdf";
import quarterlyPdf2014Q2 from "@/assets/file/quarterly/10Q 2014-Q2.pdf";
import quarterlyPdf2014Q1 from "@/assets/file/quarterly/10Q 2014-Q1.pdf";
import quarterlyPdf2013Q3 from "@/assets/file/quarterly/10Q 2013-Q3.pdf";
import quarterlyPdf2013Q2 from "@/assets/file/quarterly/10Q 2013-Q2.pdf";
import quarterlyPdf2013Q1 from "@/assets/file/quarterly/10Q 2013-Q1.pdf";
import quarterlyPdf2012Q3 from "@/assets/file/quarterly/10Q 2012-Q3.pdf";
import quarterlyPdf2012Q2 from "@/assets/file/quarterly/10Q 2012-Q2.pdf";
import quarterlyPdf2012Q1 from "@/assets/file/quarterly/10Q 2012-Q1.pdf";
import quarterlyPdf2011Q3 from "@/assets/file/quarterly/10Q 2011-Q3.pdf";
import quarterlyPdf2011Q2 from "@/assets/file/quarterly/10Q 2011-Q2.pdf";
import quarterlyPdf2011Q1 from "@/assets/file/quarterly/10Q 2011-Q1.pdf";
import quarterlyPdf2010Q3 from "@/assets/file/quarterly/10Q 2010-Q3.pdf";
import quarterlyPdf2010Q2 from "@/assets/file/quarterly/10Q 2010-Q2.pdf";
import quarterlyPdf2010Q1 from "@/assets/file/quarterly/10Q 2010-Q1.pdf";
import quarterlyPdf2009Q3 from "@/assets/file/quarterly/10Q 2009-Q3.pdf";

const { t } = useI18n();
const searchQuery = ref("");

const state = reactive({
  list: [
    {
      title: "2024 Q3 Quarterly Reports",
      description: "Third Quarter 2024 Financial Results",
      url: quarterlyPdf2024Q3,
    },
    {
      title: "2024 Q2 Quarterly Reports",
      description: "Second Quarter 2024 Financial Results",
      url: quarterlyPdf2024Q2,
    },
    {
      title: "2024 Q1 Quarterly Reports",
      description: "First Quarter 2024 Financial Results",
      url: quarterlyPdf2024Q1,
    },
    {
      title: "2023 Q3 Quarterly Reports",
      description: "Third Quarter 2023 Financial Results",
      url: quarterlyPdf2023Q3,
    },
    {
      title: "2023 Q2 Quarterly Reports",
      description: "Second Quarter 2023 Financial Results",
      url: quarterlyPdf2023Q2,
    },
    {
      title: "2023 Q1 Quarterly Reports",
      description: "First Quarter 2023 Financial Results",
      url: quarterlyPdf2023Q1,
    },
    {
      title: "2022 Q3 Quarterly Reports",
      description: "Third Quarter 2022 Financial Results",
      url: quarterlyPdf2022Q3,
    },
    {
      title: "2022 Q2 Quarterly Reports",
      description: "Second Quarter 2022 Financial Results",
      url: quarterlyPdf2022Q2,
    },
    {
      title: "2022 Q1 Quarterly Reports",
      description: "First Quarter 2022 Financial Results",
      url: quarterlyPdf2022Q1,
    },
    {
      title: "2021 Q3 Quarterly Reports",
      description: "Third Quarter 2021 Financial Results",
      url: quarterlyPdf2021Q3,
    },
    {
      title: "2021 Q2 Quarterly Reports",
      description: "Second Quarter 2021 Financial Results",
      url: quarterlyPdf2021Q2,
    },
    {
      title: "2021 Q1 Quarterly Reports",
      description: "First Quarter 2021 Financial Results",
      url: quarterlyPdf2021Q1,
    },
    {
      title: "2020 Q3 Quarterly Reports",
      description: "Third Quarter 2020 Financial Results",
      url: quarterlyPdf2020Q3,
    },
    {
      title: "2020 Q2 Quarterly Reports",
      description: "Second Quarter 2020 Financial Results",
      url: quarterlyPdf2020Q2,
    },
    {
      title: "2020 Q1 Quarterly Reports",
      description: "First Quarter 2020 Financial Results",
      url: quarterlyPdf2020Q1,
    },
    {
      title: "2019 Q3 Quarterly Reports",
      description: "Third Quarter 2019 Financial Results",
      url: quarterlyPdf2019Q3,
    },
    {
      title: "2019 Q2 Quarterly Reports",
      description: "Second Quarter 2019 Financial Results",
      url: quarterlyPdf2019Q2,
    },
    {
      title: "2019 Q1 Quarterly Reports",
      description: "First Quarter 2019 Financial Results",
      url: quarterlyPdf2019Q1,
    },
    {
      title: "2018 Q3 Quarterly Reports",
      description: "Third Quarter 2018 Financial Results",
      url: quarterlyPdf2018Q3,
    },
    {
      title: "2018 Q2 Quarterly Reports",
      description: "Second Quarter 2018 Financial Results",
      url: quarterlyPdf2018Q2,
    },
    {
      title: "2018 Q1 Quarterly Reports",
      description: "First Quarter 2018 Financial Results",
      url: quarterlyPdf2018Q1,
    },
    {
      title: "2017 Q3 Quarterly Reports",
      description: "Third Quarter 2017 Financial Results",
      url: quarterlyPdf2017Q3,
    },
    {
      title: "2017 Q2 Quarterly Reports",
      description: "Second Quarter 2017 Financial Results",
      url: quarterlyPdf2017Q2,
    },
    {
      title: "2017 Q1 Quarterly Reports",
      description: "First Quarter 2017 Financial Results",
      url: quarterlyPdf2017Q1,
    },
    {
      title: "2016 Q3 Quarterly Reports",
      description: "Third Quarter 2016 Financial Results",
      url: quarterlyPdf2016Q3,
    },
    {
      title: "2016 Q2 Quarterly Reports",
      description: "Second Quarter 2016 Financial Results",
      url: quarterlyPdf2016Q2,
    },
    {
      title: "2016 Q1 Quarterly Reports",
      description: "First Quarter 2016 Financial Results",
      url: quarterlyPdf2016Q1,
    },
    {
      title: "2015 Q3 Quarterly Reports",
      description: "Third Quarter 2015 Financial Results",
      url: quarterlyPdf2015Q3,
    },
    {
      title: "2015 Q2 Quarterly Reports",
      description: "Second Quarter 2015 Financial Results",
      url: quarterlyPdf2015Q2,
    },
    {
      title: "2015 Q1 Quarterly Reports",
      description: "First Quarter 2015 Financial Results",
      url: quarterlyPdf2015Q1,
    },
    {
      title: "2014 Q3 Quarterly Reports",
      description: "Third Quarter 2014 Financial Results",
      url: quarterlyPdf2014Q3,
    },
    {
      title: "2014 Q2 Quarterly Reports",
      description: "Second Quarter 2014 Financial Results",
      url: quarterlyPdf2014Q2,
    },
    {
      title: "2014 Q1 Quarterly Reports",
      description: "First Quarter 2014 Financial Results",
      url: quarterlyPdf2014Q1,
    },
    {
      title: "2013 Q3 Quarterly Reports",
      description: "Third Quarter 2013 Financial Results",
      url: quarterlyPdf2013Q3,
    },
    {
      title: "2013 Q2 Quarterly Reports",
      description: "Second Quarter 2013 Financial Results",
      url: quarterlyPdf2013Q2,
    },
    {
      title: "2013 Q1 Quarterly Reports",
      description: "First Quarter 2013 Financial Results",
      url: quarterlyPdf2013Q1,
    },
    {
      title: "2012 Q3 Quarterly Reports",
      description: "Third Quarter 2012 Financial Results",
      url: quarterlyPdf2012Q3,
    },
    {
      title: "2012 Q2 Quarterly Reports",
      description: "Second Quarter 2012 Financial Results",
      url: quarterlyPdf2012Q2,
    },
    {
      title: "2012 Q1 Quarterly Reports",
      description: "First Quarter 2012 Financial Results",
      url: quarterlyPdf2012Q1,
    },
    {
      title: "2011 Q3 Quarterly Reports",
      description: "Third Quarter 2011 Financial Results",
      url: quarterlyPdf2011Q3,
    },
    {
      title: "2011 Q2 Quarterly Reports",
      description: "Second Quarter 2011 Financial Results",
      url: quarterlyPdf2011Q2,
    },
    {
      title: "2011 Q1 Quarterly Reports",
      description: "First Quarter 2011 Financial Results",
      url: quarterlyPdf2011Q1,
    },
    {
      title: "2010 Q3 Quarterly Reports",
      description: "Third Quarter 2010 Financial Results",
      url: quarterlyPdf2010Q3,
    },
    {
      title: "2010 Q2 Quarterly Reports",
      description: "Second Quarter 2010 Financial Results",
      url: quarterlyPdf2010Q2,
    },
    {
      title: "2010 Q1 Quarterly Reports",
      description: "First Quarter 2010 Financial Results",
      url: quarterlyPdf2010Q1,
    },
    {
      title: "2009 Q3 Quarterly Reports",
      description: "Third Quarter 2009 Financial Results",
      url: quarterlyPdf2009Q3,
    },
  ],
});
const filteredList = computed(() => {
  if (!searchQuery.value) return state.list;
  const query = searchQuery.value.toLowerCase();
  return state.list.filter(
    (item) =>
      item.title.toLowerCase().includes(query) ||
      item.description.toLowerCase().includes(query)
  );
});

const handleSearch = () => {
  // 搜索处理逻辑
  console.log("搜索:", searchQuery.value);
};

const downloadPdf = (url) => {
  // 创建一个隐藏的a元素
  const link = document.createElement("a");
  link.href = url;

  // 修复文件名提取逻辑
  let fileName = url.split("/").pop();
  // 移除可能存在的查询参数
  if (fileName.includes("?") || fileName.includes("_t=")) {
    fileName = fileName.split(/[?_]/)[0];
  }
  link.download = fileName;

  // 对于移动设备，我们需要特殊处理
  const isMobile =
    /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
      navigator.userAgent
    );

  if (isMobile) {
    // 在移动设备上，使用fetch下载文件并直接触发下载，不使用blob URL进行预览
    fetch(url)
      .then((response) => response.blob())
      .then((blob) => {
        const objectUrl = URL.createObjectURL(blob);
        link.href = objectUrl;
        // 确保设置download属性并且不设置target="_blank"以避免预览
        link.download = fileName;
        link.target = "_self"; // 避免在新窗口打开
        document.body.appendChild(link);
        link.click();

        // 清理
        setTimeout(() => {
          document.body.removeChild(link);
          URL.revokeObjectURL(objectUrl);
        }, 100);
      })
      .catch((error) => {
        console.error("下载文件时出错:", error);
        // 如果fetch失败，尝试直接下载
        link.href = url;
        link.download = fileName;
        link.target = "_self";
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
  } else {
    // 桌面设备上直接点击链接下载
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }
};
</script>
<style scoped lang="scss">
.page-title {
  font-size: 85px;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-top: 8px;
}

.search-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 20px;
}

.search-input {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  outline: none;
  transition: border-color 0.3s;
}

.search-input:focus {
  border-color: #2979ff;
  box-shadow: 0 0 0 2px rgba(41, 121, 255, 0.2);
}

.search-button {
  width: 100%;
  padding: 10px 0;
  background: #2979ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.3s;
}

.search-button:hover {
  background: #1e6de8;
}

.results-list {
  margin-top: 46px;
  max-height: 3000px;
  overflow-y: auto;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  background-color: #fff;
}

.result-item {
  display: flex;
  justify-content: space-between;
  padding: 46px;
  border-bottom: 1px solid #f0f0f0;
  transition: background-color 0.2s;

  &:hover {
    background-color: #f9fafc;
  }

  &:last-child {
    border-bottom: none;
  }
}

.result-title {
  color: #2979ff;
  text-decoration: none;
  display: block;
  margin-bottom: 8px;
  font-size: 50px;
  font-weight: 600;
}

.result-description {
  color: #666;
  margin: 0;
  font-size: 40px;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.pdf-icon {
  margin-left: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;

  .pdf-link {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-decoration: none;
    color: #ff5252;
    font-size: 40px;

    &:hover {
      opacity: 0.8;
    }
  }
}
</style>
