File size: 5,487 Bytes
3c778ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* ==========================================================================
   Apple Design System Interactive Logic
   Handles nav toggles, configurator chip selection, accessory search & sticky bar
   ========================================================================== */

document.addEventListener('DOMContentLoaded', () => {

  /* ------------------------------------------------------------------------
     1. Configurator Storage & Color Chip Selection
     ------------------------------------------------------------------------ */
  const storageChips = document.querySelectorAll('.configurator-chips-storage .configurator-option-chip');
  const colorChips = document.querySelectorAll('.configurator-chips-color .configurator-option-chip');
  const totalPriceEl = document.getElementById('configurator-total-price');
  const stickyPriceEl = document.getElementById('sticky-price');
  const configuratorImage = document.getElementById('configurator-preview-img');

  let basePrice = 48900;
  let storageExtra = 0;

  // Storage selection
  storageChips.forEach(chip => {
    chip.addEventListener('click', () => {
      storageChips.forEach(c => c.classList.remove('selected'));
      chip.classList.add('selected');

      storageExtra = parseInt(chip.dataset.priceExtra || '0', 10);
      updatePrice();
    });
  });

  // Color selection
  colorChips.forEach(chip => {
    chip.addEventListener('click', () => {
      colorChips.forEach(c => c.classList.remove('selected'));
      chip.classList.add('selected');

      // Subtle feedback animation on preview image
      if (configuratorImage) {
        configuratorImage.style.opacity = '0.7';
        setTimeout(() => {
          configuratorImage.style.opacity = '1';
        }, 150);
      }
    });
  });

  function updatePrice() {
    const total = basePrice + storageExtra;
    const formatted = 'NT$ ' + total.toLocaleString();
    if (totalPriceEl) totalPriceEl.textContent = formatted;
    if (stickyPriceEl) stickyPriceEl.textContent = formatted;
  }

  /* ------------------------------------------------------------------------
     2. Accessories Grid Search Filtering
     ------------------------------------------------------------------------ */
  const searchInput = document.getElementById('accessory-search-input');
  const accessoryCards = document.querySelectorAll('.store-utility-card');

  if (searchInput) {
    searchInput.addEventListener('input', (e) => {
      const term = e.target.value.toLowerCase().trim();

      accessoryCards.forEach(card => {
        const title = card.querySelector('.t-body-strong')?.textContent.toLowerCase() || '';
        const desc = card.querySelector('.t-caption')?.textContent.toLowerCase() || '';

        if (title.includes(term) || desc.includes(term)) {
          card.style.display = 'flex';
        } else {
          card.style.display = 'none';
        }
      });
    });
  }

  /* ------------------------------------------------------------------------
     3. Floating Sticky Bar Scroll Trigger
     ------------------------------------------------------------------------ */
  const stickyBar = document.getElementById('floating-sticky-bar');
  const configuratorSection = document.getElementById('configurator-section');

  if (stickyBar && configuratorSection) {
    window.addEventListener('scroll', () => {
      const rect = configuratorSection.getBoundingClientRect();
      // Show sticky bar when user scrolls past top of configurator section
      if (rect.top <= window.innerHeight && rect.bottom >= 0) {
        stickyBar.classList.remove('hidden');
      } else {
        stickyBar.classList.add('hidden');
      }
    });
  }

  /* ------------------------------------------------------------------------
     4. Mobile Hamburger Menu Toggle
     ------------------------------------------------------------------------ */
  const hamburgerBtn = document.getElementById('hamburger-btn');
  const globalNavLinks = document.querySelector('.global-nav-links');

  if (hamburgerBtn && globalNavLinks) {
    hamburgerBtn.addEventListener('click', () => {
      const isExpanded = globalNavLinks.style.display === 'flex';
      if (isExpanded) {
        globalNavLinks.style.display = 'none';
      } else {
        globalNavLinks.style.display = 'flex';
        globalNavLinks.style.flexDirection = 'column';
        globalNavLinks.style.position = 'absolute';
        globalNavLinks.style.top = '44px';
        globalNavLinks.style.left = '0';
        globalNavLinks.style.width = '100%';
        globalNavLinks.style.backgroundColor = '#000000';
        globalNavLinks.style.padding = '24px';
        globalNavLinks.style.gap = '16px';
      }
    });
  }

  /* ------------------------------------------------------------------------
     5. Add to Bag Interactive Feedback
     ------------------------------------------------------------------------ */
  const buyButtons = document.querySelectorAll('.btn-primary, #add-to-bag-btn');
  buyButtons.forEach(btn => {
    btn.addEventListener('click', (e) => {
      if (btn.tagName === 'A' && btn.getAttribute('href') !== '#') return;
      e.preventDefault();
      
      const originalText = btn.textContent;
      btn.textContent = '已加入購物袋 ✓';
      btn.style.backgroundColor = '#34c759'; // Apple Success Green
      
      setTimeout(() => {
        btn.textContent = originalText;
        btn.style.backgroundColor = '';
      }, 2000);
    });
  });
});