File size: 8,901 Bytes
e84a62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4176dc5
 
 
 
 
 
 
 
 
 
 
23ab40b
 
 
e84a62a
23ab40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4176dc5
23ab40b
 
 
4176dc5
 
 
 
e84a62a
23ab40b
 
 
 
 
 
 
 
4176dc5
e84a62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401c0af
 
 
 
 
 
 
 
 
 
e84a62a
 
401c0af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e84a62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
document.addEventListener('DOMContentLoaded', function() {
  // Initialize Feather Icons
  feather.replace();
  
  // Form Navigation
  const formSteps = document.querySelectorAll('.form-step');
  const stepIndicators = document.querySelectorAll('.step-indicator');
  
  document.querySelectorAll('.next-step').forEach(button => {
    button.addEventListener('click', function() {
      const currentStep = document.querySelector('.form-step.active');
      const nextStep = currentStep.nextElementSibling;
      
      if (validateStep(currentStep)) {
        currentStep.classList.remove('active');
        currentStep.classList.add('hidden');
        
        nextStep.classList.remove('hidden');
        nextStep.classList.add('active');
        
        updateStepIndicator(nextStep.dataset.step);
      }
    });
  });
  
  document.querySelectorAll('.prev-step').forEach(button => {
    button.addEventListener('click', function() {
      const currentStep = document.querySelector('.form-step.active');
      const prevStep = currentStep.previousElementSibling;
      
      currentStep.classList.remove('active');
      currentStep.classList.add('hidden');
      
      prevStep.classList.remove('hidden');
      prevStep.classList.add('active');
      
      updateStepIndicator(prevStep.dataset.step);
    });
  });
  
  // Handle "Other" values checkbox
  document.querySelectorAll('input[name="values"]').forEach(checkbox => {
    checkbox.addEventListener('change', function() {
      if (this.value === '其他' && this.checked) {
        document.getElementById('otherValues').classList.remove('hidden');
      } else if (this.value === '其他' && !this.checked) {
        document.getElementById('otherValues').classList.add('hidden');
      }
    });
  });
  
  // Image Upload Preview
  const imageUpload = document.getElementById('imageUpload');
  const previewContainer = document.getElementById('previewContainer');
  
  if (imageUpload) {
    imageUpload.addEventListener('change', function() {
      previewContainer.innerHTML = '';
      
      if (this.files.length > 0) {
        previewContainer.classList.remove('hidden');
        
        Array.from(this.files).forEach(file => {
          const reader = new FileReader();
          
          reader.onload = function(e) {
            const img = document.createElement('img');
            img.src = e.target.result;
            previewContainer.appendChild(img);
          }
          
          reader.readAsDataURL(file);
        });
      } else {
        previewContainer.classList.add('hidden');
      }
    });
  }
  
  // Form Submission
  const sloganForm = document.getElementById('sloganForm');
  const resultsModal = document.getElementById('resultsModal');
  const closeModal = document.getElementById('closeModal');
  
  if (sloganForm) {
    sloganForm.addEventListener('submit', function(e) {
      e.preventDefault();
      // Show loading state
      const sloganResults = document.getElementById('sloganResults');
      sloganResults.innerHTML = `
        <div class="flex flex-col items-center justify-center py-12">
          <div class="loading-spinner mb-4"></div>
          <p class="text-gray-600">正在为您精心生成标语方案...</p>
        </div>
      `;
      
      resultsModal.classList.remove('hidden');
      document.body.style.overflow = 'hidden';
      // Get form data
      const formData = new FormData(sloganForm);
      const data = Object.fromEntries(formData.entries());
      
      // Add selected values from checkboxes/radios
      data.values = Array.from(document.querySelectorAll('input[name="values"]:checked')).map(el => el.value);
      data.usage = Array.from(document.querySelectorAll('input[name="usage"]:checked')).map(el => el.value);
      
      try {
        // Call logo generation API
        const response = await axios.post('https://api.logo-generator.com/v1/design', {
          brandName: data.brandName,
          businessDesc: data.businessDesc,
          coreEmotion: data.coreEmotion,
          logoStyle: data.logoStyle,
          requiredElements: data.requiredElements,
          avoidElements: data.avoidElements,
          logoSize: data.logoSize,
          brandStory: data.brandStory,
          existingSlogans: data.existingSlogans
        });

        const slogans = response.data.slogans.map((slogan, i) => `
          <div class="bg-gray-50 p-4 rounded-lg transition-all duration-300 hover:bg-white hover:shadow-sm">
            <h3 class="font-bold text-lg text-primary mb-2">方案${i+1}</h3>
            <p class="text-gray-700">"${slogan.text}"</p>
            ${slogan.logoUrl ? `<img src="${slogan.logoUrl}" alt="Logo Option ${i+1}" class="mt-2 max-h-32">` : ''}
            <div class="mt-3 flex space-x-2">
              <button class="text-xs bg-primary text-white px-2 py-1 rounded">复制</button>
              <button class="text-xs bg-gray-200 text-gray-700 px-2 py-1 rounded">编辑</button>
            </div>
          </div>
        `).join('');

        sloganResults.innerHTML = slogans;
      } catch (error) {
        console.error('API Error:', error);
        // Fallback to default slogans
        sloganResults.innerHTML = `
`;
resultsModal.classList.remove('hidden');
        document.body.style.overflow = 'hidden';
      }, 1000);
    });
  }
  
  if (closeModal) {
    closeModal.addEventListener('click', function() {
      resultsModal.classList.add('hidden');
      document.body.style.overflow = 'auto';
    });
  }
  // Download Results
  const downloadResults = document.getElementById('downloadResults');
  if (downloadResults) {
    downloadResults.addEventListener('click', function() {
      const sloganTexts = Array.from(document.querySelectorAll('#sloganResults p')).map(p => p.textContent);
      const blob = new Blob([sloganTexts.join('\n\n')], { type: 'text/plain' });
      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = '品牌标语方案.txt';
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
    });
  }

  // Share Results
  const shareResults = document.getElementById('shareResults');
  if (shareResults && navigator.share) {
    shareResults.addEventListener('click', async function() {
      try {
        const sloganTexts = Array.from(document.querySelectorAll('#sloganResults p')).map(p => p.textContent);
        await navigator.share({
          title: '品牌标语方案',
          text: sloganTexts.join('\n\n'),
          url: window.location.href
        });
      } catch (err) {
        console.log('分享取消:', err);
      }
    });
  } else if (shareResults) {
    shareResults.style.display = 'none';
  }

  // Drag and drop for image upload
  const dropZone = document.querySelector('#imageUpload + label');
  if (dropZone) {
    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
      dropZone.addEventListener(eventName, preventDefaults, false);
    });

    function preventDefaults(e) {
      e.preventDefault();
      e.stopPropagation();
    }

    ['dragenter', 'dragover'].forEach(eventName => {
      dropZone.addEventListener(eventName, highlight, false);
    });

    ['dragleave', 'drop'].forEach(eventName => {
      dropZone.addEventListener(eventName, unhighlight, false);
    });

    function highlight() {
      dropZone.classList.add('border-primary', 'bg-blue-50');
    }

    function unhighlight() {
      dropZone.classList.remove('border-primary', 'bg-blue-50');
    }

    dropZone.addEventListener('drop', handleDrop, false);

    function handleDrop(e) {
      const dt = e.dataTransfer;
      const files = dt.files;
      imageUpload.files = files;
      
      // Trigger change event for preview
      const event = new Event('change');
      imageUpload.dispatchEvent(event);
    }
  }
// Helper Functions
  function validateStep(step) {
    const requiredFields = step.querySelectorAll('[required]');
    let isValid = true;
    
    requiredFields.forEach(field => {
      if (!field.value.trim()) {
        field.classList.add('border-red-500');
        isValid = false;
        
        field.addEventListener('input', function() {
          if (this.value.trim()) {
            this.classList.remove('border-red-500');
          }
        });
      }
    });
    
    if (!isValid) {
      alert('请填写所有必填字段');
    }
    
    return isValid;
  }
  
  function updateStepIndicator(activeStep) {
    stepIndicators.forEach(indicator => {
      indicator.classList.remove('active');
      
      if (indicator.dataset.step === activeStep) {
        indicator.classList.add('active');
      } else if (parseInt(indicator.dataset.step) < parseInt(activeStep)) {
        indicator.classList.add('completed');
      }
    });
  }
});