---
/**
 * PainpointSection - Interactive tab section showing customer pain points
 * Features: Tab switching, icon display, hover effects
 */
import type { HomeData } from '@/lib/api/home'

interface Props {
  homeData?: HomeData | null
}

const { homeData } = Astro.props

// Pain points data - matching reference HTML exactly
const painpoints = [
  {
    id: 'company',
    title: '行銷公司難找',
    icon: '🔍',
    description: '市場上有太多行銷公司了，每家都跟我說他們家的服務可以掛保證。但是我應該要找誰合作呢？',
  },
  {
    id: 'methods',
    title: '宣傳方法太多難選擇',
    icon: '🤔',
    description: '網路行銷方法這麼多，Google、FB、IG、YT...哪種才適合我的產品？',
  },
  {
    id: 'digital',
    title: '數位轉型太難',
    icon: '💻',
    description: '想要做數位轉型，但不知道從哪裡開始？技術門檻太高？',
  },
  {
    id: 'burning',
    title: '廣告行銷像燒錢',
    img: '💸',
    description: '廣告預算投入很多，但看不到實際效果？感覺像在燒錢？',
  },
]
---

<section class="section-painpoint" aria-labelledby="painpoint-heading">
  <div class="max-w-6xl mx-auto px-4">
    <!-- Section Header -->
    <div class="section-header-w-line">
      <h2 id="painpoint-heading" class="header-subtitle-head">
        你可能會遇到的煩惱
      </h2>
      <div class="divider-line"></div>
    </div>

    <!-- Tabs Container -->
    <div class="painpoint-tabs" id="painpoint-tabs">
      <!-- Tab Headers -->
      <div class="tab-headers">
        {
          painpoints.map((point, index) => (
            <button
              class={`painpoint-text ${index === 0 ? 'active' : ''}`}
              data-tab={point.id}
              aria-selected={index === 0 ? 'true' : 'false'}
              role="tab"
            >
              {point.title}
            </button>
          ))
        }
      </div>

      <!-- Tab Panels -->
      <div class="tab-panels">
        {
          painpoints.map((point, index) => (
            <div
              id={`panel-${point.id}`}
              class={`tab-panel-frame ${index === 0 ? 'active' : ''}`}
              role="tabpanel"
              aria-labelledby={`tab-${point.id}`}
              hidden={index !== 0}
            >
              <!-- Icon -->
              <div class="painpoint-icon">
                <div class="icon-holder">
                  <span class="text-6xl" aria-hidden="true">{point.icon}</span>
                </div>
              </div>

              <!-- Description -->
              <p class="painpoint-description">
                {point.description}
              </p>

              <!-- Solution Link -->
              <div class="painpoint-solution">
                <span class="solution-text">我們有解決方法</span>
                <a href="/about-enchun" class="solution-link">
                  <span class="sr-only">了解更多</span>
                </a>
              </div>
            </div>
          ))
        }
      </div>
    </div>
  </div>
</section>

<style>
  /* Painpoint Section Styles - Pixel-perfect from Webflow */
  .section-painpoint {
    height: 80vh;
    max-height: 80vh;
    display: flex;
    align-items: center;
  }

  /* Section Header */
  .section-header-w-line {
    text-align: center;
    margin-bottom: 2rem;
  }

  .header-subtitle-head {
    font-size: 1.8rem;
    text-align: center;
    color: var(--color-dark-blue);
    font-weight: 500;
  }

  .divider-line {
    background-color: var(--color-enchunblue);
    height: 1px;
    width: 60px;
    margin: 16px auto;
  }

  /* Tabs Container */
  .painpoint-tabs {
    padding-top: 32px;
    padding-bottom: 70px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 32px;
  }

  /* Tab Headers */
  .tab-headers {
    display: flex;
    flex-direction: column;
    gap: 24px;
    justify-content: center;
  }

  .painpoint-text {
    font-size: 2.08em;
    font-weight: 500;
    line-height: 1;
    color: var(--color-dark-blue);
    font-family: "Noto Sans TC", sans-serif;
    cursor: pointer;
    transition: text-shadow 0.3s ease;
    background: none;
    border: none;
    text-align: left;
    padding: 8px 0;
  }

  .painpoint-text:hover {
    text-shadow: 7px 4px 13px rgba(0, 0, 0, 0.5);
  }

  .painpoint-text.active {
    color: var(--color-enchunblue);
    font-weight: 600;
  }

  /* Tab Panels */
  .tab-panels {
    position: relative;
  }

  .tab-panel-frame {
    border: 1px solid var(--color-enchunblue);
    border-radius: 10px;
    position: relative;
    top: -47px;
    padding: 32px;
    background: white;
    opacity: 0;
    transform: translateY(10px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    display: none;
  }

  .tab-panel-frame.active {
    opacity: 1;
    transform: translateY(0);
    display: block;
  }

  /* Icon */
  .painpoint-icon {
    height: 110px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 24px;
  }

  .icon-holder {
    width: 130px;
    height: 115px;
    background-color: var(--color-grey6);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* Description */
  .painpoint-description {
    font-size: 1.2rem;
    line-height: 1.6;
    color: var(--color-gray-700);
    text-align: center;
    margin-bottom: 1rem;
  }

  /* Solution Section */
  .painpoint-solution {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }

  .solution-text {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--color-enchunblue);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }

  .solution-link {
    width: 48px;
    height: 48px;
    background-color: var(--color-notification-red);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    text-decoration: none;
    transition: transform 0.2s ease, background-color 0.2s ease;
    position: relative;
  }

  .solution-link:hover {
    transform: scale(1.1);
    background-color: #b92d1a;
  }

  .solution-link::after {
    content: '→';
    font-size: 1.5rem;
    font-weight: bold;
  }

  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  /* Responsive Adjustments */
  @media (max-width: 991px) {
    .section-painpoint {
      height: auto;
      max-height: none;
      padding: 64px 0;
    }

    .painpoint-tabs {
      grid-template-columns: 1fr;
      gap: 24px;
    }

    .tab-headers {
      flex-direction: row;
      overflow-x: auto;
      padding-bottom: 8px;
    }

    .painpoint-text {
      font-size: 1.5em;
      white-space: nowrap;
    }

    .tab-panel-frame {
      top: 0;
    }

    .header-subtitle-head {
      font-size: 1.5rem;
    }
  }

  @media (max-width: 767px) {
    .painpoint-text {
      font-size: 1.2em;
    }

    .icon-holder {
      width: 100px;
      height: 90px;
    }
  }
</style>

<script>
  // Tab switching functionality
  function initPainpointTabs() {
    const tabs = document.querySelectorAll('.painpoint-text')
    const panels = document.querySelectorAll('.tab-panel-frame')

    tabs.forEach((tab) => {
      tab.addEventListener('click', () => {
        const tabId = tab.getAttribute('data-tab')

        // Remove active class from all tabs
        tabs.forEach(t => {
          t.classList.remove('active')
          t.setAttribute('aria-selected', 'false')
        })

        // Hide all panels
        panels.forEach(p => {
          p.classList.remove('active')
          ;(p as HTMLElement).hidden = true
        })

        // Activate clicked tab
        tab.classList.add('active')
        tab.setAttribute('aria-selected', 'true')

        // Show corresponding panel
        const activePanel = document.getElementById(`panel-${tabId}`)
        if (activePanel) {
          activePanel.classList.add('active')
          ;(activePanel as HTMLElement).hidden = false
        }
      })
    })
  }

  // Initialize when DOM is ready
  document.addEventListener('DOMContentLoaded', initPainpointTabs)
  if (document.readyState !== 'loading') {
    initPainpointTabs()
  }
</script>
