File size: 5,878 Bytes
e39d48e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c60baa5
 
 
 
 
 
 
 
 
 
 
 
e39d48e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

// ── HELP PROOF SCREEN ──────────────────────────────────────────────────────
(function() {
  let helpProofAnimalId = null;
  let helpProofSelType  = 'fed';

  window.openHelpProofScreen = function() {
    helpProofAnimalId = window.profileAnimalId;
    helpProofSelType  = 'fed';

    const preview = document.getElementById('help-proof-preview');
    preview.src = ''; preview.classList.add('hidden');
    document.getElementById('help-proof-placeholder').style.display = '';
    document.getElementById('help-proof-ai-badge').classList.add('hidden');
    document.getElementById('help-proof-verified-badge').classList.add('hidden');
    document.getElementById('help-proof-notes').value = '';
    document.getElementById('help-proof-input').value = '';
    document.querySelectorAll('.help-type-chip').forEach(c => c.classList.remove('active'));
    document.querySelector('.help-type-chip[data-type="fed"]').classList.add('active');

    const name = document.getElementById('profile-title').textContent || 'Animal';
    const sub  = document.getElementById('profile-status-text').textContent || '';
    document.getElementById('help-proof-animal-name').textContent = name;
    document.getElementById('help-proof-animal-sub').textContent  = sub;
    const heroSrc = document.getElementById('profile-hero-img').src;
    const thumb   = document.getElementById('help-proof-animal-photo');
    thumb.style.backgroundImage = heroSrc ? 'url(' + heroSrc + ')' : '';
    thumb.style.backgroundSize  = 'cover';

    window.showScreen('help-proof');
  };

  document.getElementById('help-type-chips').addEventListener('click', e => {
    const chip = e.target.closest('.help-type-chip');
    if (!chip) return;
    document.querySelectorAll('.help-type-chip').forEach(c => c.classList.remove('active'));
    chip.classList.add('active');
    helpProofSelType = chip.dataset.type;
  });

  document.getElementById('help-proof-viewfinder').addEventListener('click', () => {
    document.getElementById('help-proof-input').click();
  });

  document.getElementById('help-proof-input').addEventListener('change', async (e) => {
    const file = e.target.files[0];
    if (!file) return;

    const preview = document.getElementById('help-proof-preview');
    preview.src = URL.createObjectURL(file);
    preview.classList.remove('hidden');
    document.getElementById('help-proof-placeholder').style.display = 'none';

    const aiBadge       = document.getElementById('help-proof-ai-badge');
    const verifiedBadge = document.getElementById('help-proof-verified-badge');
    aiBadge.classList.remove('hidden');
    document.getElementById('help-proof-ai-text').textContent = 'Verifying with AI…';
    verifiedBadge.classList.add('hidden');

    try {
      const { client, handleFile } = await window.getGradioClient();
      const res  = await client.predict('/analyze_image', { image_path: handleFile(file) });
      const data = res.data[0] || {};

      aiBadge.classList.add('hidden');
      verifiedBadge.classList.remove('hidden');
      const verifiedText = document.getElementById('help-proof-verified-text');

      if (data.error) {
        verifiedBadge.style.background = 'rgba(220,53,69,0.85)';
        verifiedText.textContent = 'No animal detected in photo';
      } else {
        const match = data.similar && data.similar.find(s => s.id === helpProofAnimalId);
        const score = match ? match.score_pct : 0;
        if (score >= 60) {
          verifiedBadge.style.background = 'rgba(56,140,89,0.9)';
          verifiedText.textContent = 'Same animal confirmed';
        } else if (score >= 35) {
          verifiedBadge.style.background = 'rgba(56,140,89,0.75)';
          verifiedText.textContent = 'Likely the same animal';
        } else {
          verifiedBadge.style.background = 'rgba(180,130,0,0.85)';
          verifiedText.textContent = 'Photo added β€” animal not matched';
        }
      }
    } catch(err) {
      console.warn('AI verify failed:', err);
      document.getElementById('help-proof-ai-badge').classList.add('hidden');
    }
  });

  document.getElementById('help-proof-submit').addEventListener('click', async () => {
    const btn   = document.getElementById('help-proof-submit');
    const notes = document.getElementById('help-proof-notes').value.trim();
    const file  = document.getElementById('help-proof-input').files[0];

    btn.disabled = true;
    btn.innerHTML = '<div class="spinner"></div> Saving…';

    try {
      const { client, handleFile } = await window.getGradioClient();
      const res = await client.predict('/submit_help_proof', {
        animal_id:  helpProofAnimalId,
        help_type:  helpProofSelType,
        notes:      notes,
        image_path: file ? handleFile(file) : null,
      });
      const result = res.data[0] || {};

      if (result.ok) {
        window.showScreen('profile');
        await window.openProfile(helpProofAnimalId);
        const confirmEl = document.getElementById('helped-confirm');
        confirmEl.classList.remove('hidden');
        requestAnimationFrame(() => {
          confirmEl.classList.add('open');
          if (typeof lucide !== 'undefined') lucide.createIcons();
        });
      } else {
        alert('Erro ao registrar. Tente novamente.');
      }
    } catch(err) {
      console.error(err);
      alert('Erro ao registrar. Tente novamente.');
    } finally {
      btn.disabled = false;
      btn.innerHTML = '<svg viewBox="0 0 24 24"><path d="M20.84 4.61a5.5 5.5 0 00-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 00-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 000-7.78z"/></svg> Register Help';
    }
  });

  document.getElementById('help-proof-back').addEventListener('click', () => window.showScreen('profile'));
})();