File size: 5,669 Bytes
3754bec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Profile Actions - Handle inline editing, modals, and avatar upload
 */

// Avatar Upload
document.getElementById('avatarUpload')?.addEventListener('change', async function(e) {
  const file = e.target.files[0];
  if (!file) return;

  const btn = document.querySelector('.avatar-upload-btn');
  const originalHtml = btn.innerHTML;
  btn.innerHTML = '<span style="width:14px;height:14px;border:2px solid transparent;border-top-color:#fff;border-radius:50%;animation:spin 1s linear infinite;display:inline-block;"></span>';
  btn.disabled = true;

  const formData = new FormData();
  formData.append('avatar', file);

  try {
    const response = await fetch('/auth/profile/upload-avatar', {
      method: 'POST',
      body: formData
    });
    
    const data = await response.json();
    if (response.ok) {
      // Reload to show new avatar everywhere
      window.location.reload();
    } else {
      showProfileMessage(data.error || 'Failed to upload avatar', 'error');
      btn.innerHTML = originalHtml;
      btn.disabled = false;
    }
  } catch (err) {
    showProfileMessage('Network error occurred', 'error');
    btn.innerHTML = originalHtml;
    btn.disabled = false;
  }
});

// Modals
function openEditModal(field, currentValue) {
  document.getElementById('editFieldType').value = field;
  document.getElementById('editFieldValue').value = currentValue;
  
  let label = 'New Value';
  let title = 'Edit Field';
  if (field === 'username') { label = 'New Username'; title = 'Change Username'; }
  if (field === 'email') { label = 'New Email Address'; title = 'Change Email'; }
  if (field === 'full_name') { label = 'New Full Name'; title = 'Update Name'; }
  
  document.getElementById('editFieldLabel').innerText = label;
  document.getElementById('editModalTitle').innerText = title;
  
  document.getElementById('editStep1').style.display = 'block';
  document.getElementById('editStep2').style.display = 'none';
  document.getElementById('editFieldOtpToken').value = '';
  document.getElementById('editFieldOtp').value = '';
  
  document.getElementById('editModal').style.display = 'flex';
}

function closeEditModal() {
  document.getElementById('editModal').style.display = 'none';
}

function openDeleteModal() {
  document.getElementById('deleteModal').style.display = 'flex';
}

function closeDeleteModal() {
  document.getElementById('deleteModal').style.display = 'none';
}

// Edit Form Submission
document.getElementById('editFieldForm')?.addEventListener('submit', async function(e) {
  e.preventDefault();
  
  const field = document.getElementById('editFieldType').value;
  const value = document.getElementById('editFieldValue').value.trim();
  const btn = document.getElementById('btnRequestChange');
  
  if (!value) return;
  
  const originalText = btn.innerText;
  btn.innerText = 'Saving...';
  btn.disabled = true;
  
  let endpoint = '';
  let payload = {};
  
  if (field === 'full_name') {
    endpoint = '/auth/profile/update-name';
    payload = { full_name: value };
  } else if (field === 'username') {
    endpoint = '/auth/profile/request-username-change';
    payload = { new_username: value };
  } else if (field === 'email') {
    endpoint = '/auth/profile/request-email-change';
    payload = { new_email: value };
  }
  
  try {
    const res = await fetch(endpoint, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(payload)
    });
    const data = await res.json();
    
    if (res.ok) {
      if (field === 'full_name') {
        document.getElementById('val-full_name').innerText = data.full_name;
        closeEditModal();
        showProfileMessage('Name updated successfully', 'success');
      } else {
        // Show OTP step
        document.getElementById('editStep1').style.display = 'none';
        document.getElementById('editStep2').style.display = 'block';
        document.getElementById('editFieldOtpToken').value = data.otp_token;
      }
    } else {
      showProfileMessage(data.error || 'Failed to request change', 'error');
    }
  } catch (err) {
    showProfileMessage('Network error occurred', 'error');
  } finally {
    btn.innerText = originalText;
    btn.disabled = false;
  }
});

// Confirm OTP (Username/Email)
document.getElementById('btnConfirmChange')?.addEventListener('click', async function() {
  const otp = document.getElementById('editFieldOtp').value.trim();
  const token = document.getElementById('editFieldOtpToken').value;
  const field = document.getElementById('editFieldType').value;
  const btn = this;
  
  if (otp.length !== 6) return;
  
  const originalText = btn.innerText;
  btn.innerText = 'Verifying...';
  btn.disabled = true;
  
  const purpose = field === 'username' ? 'change_username' : 'change_email';
  
  try {
    const res = await fetch('/auth/profile/confirm-change', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ otp: otp, otp_token: token, purpose: purpose })
    });
    const data = await res.json();
    
    if (res.ok) {
      // Reload to reflect changes
      window.location.reload();
    } else {
      showProfileMessage(data.error || 'Failed to verify OTP', 'error');
    }
  } catch (err) {
    showProfileMessage('Network error occurred', 'error');
  } finally {
    btn.innerText = originalText;
    btn.disabled = false;
  }
});

function showProfileMessage(msg, type) {
  const container = document.getElementById('profileMessage');
  if (!container) return;
  container.innerHTML = `<div class="alert alert-${type}">${msg}</div>`;
  setTimeout(() => { container.innerHTML = ''; }, 5000);
}