<?php
$pageTitle='Profil Saya';
require_once __DIR__.'/../../config/functions.php';
requireLogin();
$userId=currentUserId();
// Data user
$user=$pdo->prepare("SELECT u.*,p.nama,p.no_telefon,p.no_telefon_pejabat,p.bio,p.tugas_utama,p.gambar,p.facebook,p.twitter,p.linkedin,p.instagram,p.id as pegawai_id,j.nama_jawatan,un.nama_unit,un.warna FROM users u LEFT JOIN pegawai p ON u.pegawai_id=p.id LEFT JOIN jawatan j ON p.jawatan_id=j.id LEFT JOIN unit un ON p.unit_id=un.id WHERE u.id=?");
$user->execute([$userId]);$user=$user->fetch();
$hasPegawai=!empty($user['pegawai_id']);
// My success stories
$myStories=$hasPegawai?$pdo->prepare("SELECT * FROM success_stories WHERE unit_id=(SELECT unit_id FROM pegawai WHERE id=?) AND status=1 ORDER BY tarikh DESC LIMIT 5"):null;
if($myStories){$myStories->execute([$user['pegawai_id']]);$myStories=$myStories->fetchAll();}else{$myStories=[];}
// My informasi (dari view.php)
$myInfos=$hasPegawai?$pdo->prepare("SELECT * FROM informasi WHERE pegawai_id=? AND status=1 ORDER BY created_at DESC LIMIT 5"):null;
if($myInfos){$myInfos->execute([$user['pegawai_id']]);$myInfos=$myInfos->fetchAll();}else{$myInfos=[];}
$breadcrumb=[['label'=>'Utama','url'=>BASE_URL],['label'=>'Profil Saya','url'=>'#']];
require_once __DIR__.'/../../templates/header.php';
?>
<meta name="base-url" content="<?=BASE_URL?>">
<meta name="csrf-token" content="<?=$csrf?>">
<?php require_once __DIR__.'/../../templates/sidebar.php'; ?>
<div class="main-content">
<?php require_once __DIR__.'/../../templates/topbar.php'; ?>
<div style="padding:2rem">

<div class="row g-4">
<!-- KIRI: PROFIL CARD -->
<div class="col-lg-4">
  <div class="content-card text-center mb-3" style="border-top:3px solid <?=$user['warna']??'#cc0001'?>">
    <div style="position:relative;display:inline-block;margin:1rem auto">
      <img id="profilePic" src="<?=getPegawaiImage($user['gambar']??'')?>" style="width:110px;height:110px;border-radius:50%;object-fit:cover;border:3px solid <?=$user['warna']??'#cc0001'?>55">
      <?php if($hasPegawai): ?>
      <label for="uploadPicInput" style="position:absolute;bottom:0;right:0;width:32px;height:32px;background:#cc0001;border-radius:50%;border:2px solid #0a0a0f;display:flex;align-items:center;justify-content:center;cursor:pointer" title="Tukar Gambar">
        <i class="fas fa-camera" style="font-size:.7rem;color:#fff"></i>
      </label>
      <input type="file" id="uploadPicInput" accept="image/*" style="display:none" onchange="uploadGambar(this)">
      <?php endif; ?>
    </div>
    <h2 style="font-size:1.2rem;font-weight:700;color:#fff;margin-bottom:.25rem"><?=htmlspecialchars($user['nama']??$user['username'])?></h2>
    <?php if($user['nama_jawatan']): ?><div style="font-size:.85rem;color:<?=$user['warna']??'#cc0001'?>;font-weight:600;margin-bottom:.25rem"><?=htmlspecialchars($user['nama_jawatan'])?></div><?php endif; ?>
    <?php if($user['nama_unit']): ?><span style="background:<?=$user['warna']??'#888'?>22;color:<?=$user['warna']??'#888'?>;border:1px solid <?=$user['warna']??'#888'?>44;border-radius:50px;padding:.2rem .75rem;font-size:.75rem;font-weight:600"><?=htmlspecialchars($user['nama_unit'])?></span><?php endif; ?>
    <div style="font-size:.8rem;color:#555;margin-top:.75rem"><?=htmlspecialchars($user['email']??'')?></div>
    <?php if($user['last_login']): ?><div style="font-size:.72rem;color:#444;margin-top:.25rem"><i class="fas fa-clock me-1"></i>Log masuk: <?=formatDate($user['last_login'],'d M Y H:i')?></div><?php endif; ?>
  </div>

  <!-- Tukar Password -->
  <div class="content-card">
    <h3 style="font-size:.95rem;font-weight:700;color:#fff;margin-bottom:1rem"><i class="fas fa-lock me-2" style="color:#cc0001"></i>Tukar Kata Laluan</h3>
    <form id="pwForm">
      <input type="hidden" name="csrf_token" value="<?=$csrf?>">
      <input type="hidden" name="action" value="tukar_password">
      <div class="mb-2"><label class="form-label" style="font-size:.8rem">Kata Laluan Semasa</label><input type="password" name="current_password" class="form-control form-control-sm" required></div>
      <div class="mb-2"><label class="form-label" style="font-size:.8rem">Kata Laluan Baru</label><input type="password" name="new_password" class="form-control form-control-sm" required minlength="6"></div>
      <div class="mb-3"><label class="form-label" style="font-size:.8rem">Sahkan Kata Laluan</label><input type="password" name="confirm_password" class="form-control form-control-sm" required></div>
      <button type="submit" class="btn btn-sm btn-danger w-100"><i class="fas fa-key me-1"></i>Tukar Password</button>
    </form>
  </div>
</div>

<!-- KANAN: EDIT PROFIL + AKTIVITI -->
<div class="col-lg-8">

  <?php if($hasPegawai): ?>
  <!-- Edit Maklumat Pegawai -->
  <div class="content-card mb-4">
    <div class="d-flex align-items-center justify-content-between mb-3">
      <h3 style="font-size:1rem;font-weight:700;color:#fff;margin:0"><i class="fas fa-user-edit me-2" style="color:<?=$user['warna']??'#cc0001'?>"></i>Maklumat Saya</h3>
      <button onclick="toggleEdit()" id="editBtn" class="btn btn-sm" style="background:rgba(255,255,255,0.05);border:1px solid rgba(255,255,255,0.1);color:#ccc;border-radius:8px"><i class="fas fa-edit me-1"></i>Edit</button>
    </div>

    <!-- View Mode -->
    <div id="viewMode">
      <div class="row g-2">
        <div class="col-sm-6"><div style="font-size:.72rem;color:#555;margin-bottom:.2rem">No. Telefon Peribadi</div><div style="font-size:.9rem;color:#fff"><?=htmlspecialchars($user['no_telefon']??'-')?></div></div>
        <div class="col-sm-6"><div style="font-size:.72rem;color:#555;margin-bottom:.2rem">No. Telefon Pejabat</div><div style="font-size:.9rem;color:#fff"><?=htmlspecialchars($user['no_telefon_pejabat']??'-')?></div></div>
        <div class="col-12 mt-2"><div style="font-size:.72rem;color:#555;margin-bottom:.2rem">Bio</div><div style="font-size:.85rem;color:#888"><?=nl2br(htmlspecialchars($user['bio']??'-'))?></div></div>
        <div class="col-12 mt-2"><div style="font-size:.72rem;color:#555;margin-bottom:.2rem">Tugas Utama</div><div style="font-size:.85rem;color:#888"><?=nl2br(htmlspecialchars($user['tugas_utama']??'-'))?></div></div>
        <?php $socials=['facebook'=>'fab fa-facebook','twitter'=>'fab fa-twitter','linkedin'=>'fab fa-linkedin','instagram'=>'fab fa-instagram']; ?>
        <?php $hasSoc=array_filter($socials,fn($icon,$key)=>!empty($user[$key]),ARRAY_FILTER_USE_BOTH); ?>
        <?php if($hasSoc): ?>
        <div class="col-12 mt-2"><div style="font-size:.72rem;color:#555;margin-bottom:.5rem">Sosial Media</div>
          <div style="display:flex;gap:.5rem">
          <?php foreach($hasSoc as $key=>$icon): ?>
          <a href="<?=htmlspecialchars($user[$key])?>" target="_blank" style="width:34px;height:34px;background:rgba(255,255,255,0.05);border:1px solid rgba(255,255,255,0.1);border-radius:10px;display:flex;align-items:center;justify-content:center;color:#888;text-decoration:none"><i class="<?=$icon?>"></i></a>
          <?php endforeach; ?>
          </div>
        </div>
        <?php endif; ?>
      </div>
    </div>

    <!-- Edit Mode -->
    <div id="editMode" style="display:none">
      <form id="profilForm" enctype="multipart/form-data">
        <input type="hidden" name="csrf_token" value="<?=$csrf?>">
        <input type="hidden" name="action" value="update_profil">
        <div class="row g-2">
          <div class="col-sm-6"><label class="form-label">No. Tel Peribadi</label><input type="text" name="no_telefon" class="form-control" value="<?=htmlspecialchars($user['no_telefon']??'')?>" placeholder="01x-xxxxxxx"></div>
          <div class="col-sm-6"><label class="form-label">No. Tel Pejabat</label><input type="text" name="no_telefon_pejabat" class="form-control" value="<?=htmlspecialchars($user['no_telefon_pejabat']??'')?>"></div>
          <div class="col-12"><label class="form-label">Bio</label><textarea name="bio" class="form-control" rows="3"><?=htmlspecialchars($user['bio']??'')?></textarea></div>
          <div class="col-12"><label class="form-label">Tugas Utama</label><textarea name="tugas_utama" class="form-control" rows="3"><?=htmlspecialchars($user['tugas_utama']??'')?></textarea></div>
          <div class="col-sm-6"><label class="form-label"><i class="fab fa-facebook me-1"></i>Facebook</label><input type="url" name="facebook" class="form-control" value="<?=htmlspecialchars($user['facebook']??'')?>"></div>
          <div class="col-sm-6"><label class="form-label"><i class="fab fa-linkedin me-1"></i>LinkedIn</label><input type="url" name="linkedin" class="form-control" value="<?=htmlspecialchars($user['linkedin']??'')?>"></div>
          <div class="col-sm-6"><label class="form-label"><i class="fab fa-twitter me-1"></i>Twitter</label><input type="url" name="twitter" class="form-control" value="<?=htmlspecialchars($user['twitter']??'')?>"></div>
          <div class="col-sm-6"><label class="form-label"><i class="fab fa-instagram me-1"></i>Instagram</label><input type="url" name="instagram" class="form-control" value="<?=htmlspecialchars($user['instagram']??'')?>"></div>
        </div>
        <div style="display:flex;gap:.5rem;margin-top:1rem">
          <button type="submit" class="btn btn-sm btn-primary flex-fill"><i class="fas fa-save me-1"></i>Simpan</button>
          <button type="button" onclick="toggleEdit()" class="btn btn-sm btn-secondary">Batal</button>
        </div>
      </form>
    </div>
  </div>

  <!-- Tambah Informasi -->
  <div class="content-card mb-4">
    <div class="d-flex align-items-center justify-content-between mb-3">
      <a href="<?=BASE_URL?>modules/informasi/" style="text-decoration:none">
        <h3 style="font-size:1rem;font-weight:700;color:#fff;margin:0">
          <i class="fas fa-info-circle me-2" style="color:#0d6efd"></i>Informasi
        </h3>
      </a>
      <button onclick="showInfoModal()" class="btn btn-sm btn-outline-primary"><i class="fas fa-plus me-1"></i>Tambah</button>
    </div>
    <?php if($myInfos): ?>
    <?php foreach($myInfos as $info):
      $jCol=['umum'=>'#888','pengumuman'=>'#0d6efd','pencapaian'=>'#198754','aktiviti'=>'#ffc107','dokumen'=>'#6f42c1'][$info['jenis']]??'#888';
      $jIcon=['umum'=>'fas fa-info-circle','pengumuman'=>'fas fa-bullhorn','pencapaian'=>'fas fa-award','aktiviti'=>'fas fa-calendar','dokumen'=>'fas fa-file'][$info['jenis']]??'fas fa-info-circle';
    ?>
    <div style="display:flex;gap:.75rem;padding:.75rem 0;border-bottom:1px solid rgba(255,255,255,0.04);position:relative">
      <div style="width:36px;height:36px;background:<?=$jCol?>22;border:1px solid <?=$jCol?>44;border-radius:10px;display:flex;align-items:center;justify-content:center;flex-shrink:0"><i class="<?=$jIcon?>" style="color:<?=$jCol?>;font-size:.8rem"></i></div>
      <div style="min-width:0;flex:1">
        <div style="font-size:.85rem;font-weight:600;color:#fff"><?=htmlspecialchars($info['tajuk'])?></div>
        <div style="font-size:.72rem;color:#555"><?=timeAgo($info['created_at'])?> · <?=ucfirst($info['jenis'])?></div>
      </div>
      <?php if(isSuperAdmin() || (!empty($_SESSION['pegawai_id']) && $_SESSION['pegawai_id'] == $info['pegawai_id'])): ?>
      <div style="display:flex;gap:.3rem;align-items:center">
        <button class="btn btn-xs btn-outline-light" onclick='editInfo(<?=json_encode($info)?>)' style="padding:.15rem .3rem;font-size:.6rem"><i class="fas fa-edit"></i></button>
        <button class="btn btn-xs btn-outline-danger" onclick="deleteInfo(<?=$info['id']?>)" style="padding:.15rem .3rem;font-size:.6rem"><i class="fas fa-trash"></i></button>
      </div>
      <?php endif; ?>
    </div>
    <?php endforeach; ?>
    <a href="<?=BASE_URL?>modules/informasi/" class="btn btn-sm btn-outline-secondary w-100 mt-2">Lihat Semua <i class="fas fa-arrow-right ms-1"></i></a>
    <?php else: ?>
    <div style="text-align:center;padding:1.5rem;color:#444"><i class="fas fa-stream fa-2x mb-2"></i><p style="font-size:.85rem">Tiada maklumat lagi. Tambah sekarang!</p></div>
    <?php endif; ?>
  </div>

  <!-- Tambah Success Story -->
  <div class="content-card">
    <div class="d-flex align-items-center justify-content-between mb-3">
      <h3 style="font-size:1rem;font-weight:700;color:#fff;margin:0"><i class="fas fa-award me-2" style="color:#ffc107"></i>Success Stories Unit Saya</h3>
      <a href="<?=BASE_URL?>modules/success/" class="btn btn-sm btn-outline-warning"><i class="fas fa-plus me-1"></i>Tambah Story</a>
    </div>
    <?php if($myStories): ?>
    <?php foreach($myStories as $s): ?>
    <div style="display:flex;gap:.75rem;padding:.75rem 0;border-bottom:1px solid rgba(255,255,255,0.04);position:relative">
      <?php if($s['gambar']&&file_exists(UPLOAD_PATH.'gallery/'.$s['gambar'])): ?>
      <img src="<?=UPLOAD_URL.'gallery/'.$s['gambar']?>" style="width:48px;height:48px;border-radius:10px;object-fit:cover;flex-shrink:0">
      <?php else: ?><div style="width:48px;height:48px;background:rgba(255,193,7,0.1);border:1px solid rgba(255,193,7,0.2);border-radius:10px;display:flex;align-items:center;justify-content:center;flex-shrink:0"><i class="fas fa-award" style="color:#ffc107"></i></div><?php endif; ?>
      <div style="flex:1"><div style="font-size:.85rem;font-weight:600;color:#fff"><?=htmlspecialchars($s['tajuk'])?></div><div style="font-size:.72rem;color:#555"><?=$s['tarikh']?formatDate($s['tarikh']):'-'?></div></div>
      <?php if(isSuperAdmin() || (!empty($_SESSION['pegawai_id']) && $_SESSION['pegawai_id'] == $s['pegawai_id'])): ?>
      <div style="display:flex;gap:.3rem;align-items:center">
        <button class="btn btn-xs btn-outline-light" onclick='editStory(<?=json_encode($s)?>)' style="padding:.15rem .3rem;font-size:.6rem"><i class="fas fa-edit"></i></button>
        <button class="btn btn-xs btn-outline-danger" onclick="deleteStory(<?=$s['id']?>)" style="padding:.15rem .3rem;font-size:.6rem"><i class="fas fa-trash"></i></button>
      </div>
      <?php endif; ?>
    </div>
    <?php endforeach; ?>
    <?php else: ?>
    <div style="text-align:center;padding:1.5rem;color:#444"><i class="fas fa-award fa-2x mb-2"></i><p style="font-size:.85rem">Tiada success story. <a href="<?=BASE_URL?>modules/success/" style="color:#ffc107">Klik di sini untuk tambah</a></p></div>
    <?php endif; ?>
  </div>
  <?php else: ?>
  <div class="content-card text-center py-4" style="color:#555">
    <i class="fas fa-user-slash fa-2x mb-2"></i>
    <p>Akaun anda belum dikaitkan dengan profil pegawai.</p>
    <p style="font-size:.8rem">Hubungi admin untuk kemaskini profil anda.</p>
  </div>
  <?php endif; ?>
</div><!-- end col-lg-8 -->
</div><!-- end row -->
</div><!-- end padding -->
</div><!-- end main-content -->

<!-- Modal Tambah Informasi -->
<?php if($hasPegawai): ?>
<div class="modal fade" id="infoModal" tabindex="-1"><div class="modal-dialog modal-lg"><div class="modal-content">
<div class="modal-header"><h5 class="modal-title" style="color:#fff;font-weight:700"><i class="fas fa-bullhorn me-2" style="color:#0d6efd"></i>Tambah Informasi</h5><button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button></div>
<form id="infoForm" enctype="multipart/form-data">
<div class="modal-body">
  <input type="hidden" name="csrf_token" value="<?=$csrf?>">
  <input type="hidden" name="id" id="info_id" value="">
  <input type="hidden" name="pegawai_id" value="<?=$user['pegawai_id']?>">
  <div class="row g-2">
    <div class="col-md-8"><label class="form-label">Tajuk *</label><input type="text" name="tajuk" class="form-control" required placeholder="Tajuk informasi..."></div>
    <div class="col-md-4"><label class="form-label">Jenis</label>
      <select name="jenis" class="form-select">
        <option value="umum">Umum</option><option value="pengumuman">Pengumuman</option>
        <option value="pencapaian">Pencapaian</option><option value="aktiviti">Aktiviti</option>
        <option value="dokumen">Dokumen</option>
      </select>
    </div>
    <div class="col-12"><label class="form-label">Kandungan / Penerangan</label><textarea name="kandungan" class="form-control" rows="4" placeholder="Kandungan..."></textarea></div>
    <div class="col-12"><label class="form-label">Lampiran (Pilihan)</label><input type="file" name="lampiran" class="form-control"></div>
  </div>
</div>
<div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button><button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>Simpan</button></div>
</form></div></div></div>
<?php endif; ?>

<script>
function toggleEdit(){
  const v=document.getElementById('viewMode');const e=document.getElementById('editMode');const btn=document.getElementById('editBtn');
  if(v.style.display==='none'){v.style.display='';e.style.display='none';btn.innerHTML='<i class="fas fa-edit me-1"></i>Edit';}
  else{v.style.display='none';e.style.display='';btn.innerHTML='<i class="fas fa-times me-1"></i>Tutup';}
}
function showInfoModal(){
  document.getElementById('infoForm').reset();
  document.getElementById('info_id').value = '';
  document.querySelector('#infoModal .modal-title').innerHTML = '<i class="fas fa-bullhorn me-2" style="color:#0d6efd"></i>Tambah Informasi';
  new bootstrap.Modal(document.getElementById('infoModal')).show();
}
function editInfo(data){
  const form = document.getElementById('infoForm');
  form.reset();
  document.getElementById('info_id').value = data.id;
  form.querySelector('[name="tajuk"]').value = data.tajuk;
  form.querySelector('[name="jenis"]').value = data.jenis;
  form.querySelector('[name="kandungan"]').value = data.kandungan;
  document.querySelector('#infoModal .modal-title').innerHTML = '<i class="fas fa-edit me-2" style="color:#0d6efd"></i>Kemaskini Informasi';
  new bootstrap.Modal(document.getElementById('infoModal')).show();
}
function deleteInfo(id){
  Swal.fire({title:'Padam Informasi?',text:'Rekod ini akan dipadam secara kekal!',icon:'warning',showCancelButton:true,confirmButtonColor:'#dc3545',confirmButtonText:'Ya, Padam!',background:'#0f0f1a',color:'#e0e0e0'}).then(r=>{
    if(r.isConfirmed){
      const fd=new FormData();fd.append('id',id);fd.append('csrf_token','<?=$csrf?>');
      fetch('<?=BASE_URL?>modules/informasi/ajax_delete.php',{method:'POST',body:fd}).then(r=>r.json()).then(d=>{
        Swal.fire({toast:true,position:'top-end',icon:d.success?'success':'error',title:d.message,showConfirmButton:false,timer:2500,background:'#0f0f1a',color:'#e0e0e0'});
        if(d.success)setTimeout(()=>location.reload(),1000);
      });
    }
  });
}
function editStory(data){
  // Success stories usually have their own modal or page, but here we'll redirect or use a simple alert for now if no modal exists
  // But wait, success stories in profile.php don't have a modal. Let's redirect to success module with edit mode?
  // Actually, I should probably add a simple modal for success stories here too or just redirect.
  // The user wants edit/delete. I'll redirect to success module.
  window.location.href = '<?=BASE_URL?>modules/success/?edit=' + data.id;
}
function deleteStory(id){
  Swal.fire({title:'Padam Success Story?',text:'Rekod ini akan dipadam secara kekal!',icon:'warning',showCancelButton:true,confirmButtonColor:'#dc3545',confirmButtonText:'Ya, Padam!',background:'#0f0f1a',color:'#e0e0e0'}).then(r=>{
    if(r.isConfirmed){
      const fd=new FormData();fd.append('id',id);fd.append('csrf_token','<?=$csrf?>');
      fetch('<?=BASE_URL?>modules/success/ajax_delete.php',{method:'POST',body:fd}).then(r=>r.json()).then(d=>{
        Swal.fire({toast:true,position:'top-end',icon:d.success?'success':'error',title:d.message,showConfirmButton:false,timer:2500,background:'#0f0f1a',color:'#e0e0e0'});
        if(d.success)setTimeout(()=>location.reload(),1000);
      });
    }
  });
}
function uploadGambar(input){
  if(!input.files||!input.files[0])return;
  const r=new FileReader();r.onload=e=>document.getElementById('profilePic').src=e.target.result;r.readAsDataURL(input.files[0]);
  const fd=new FormData();
  fd.append('csrf_token','<?=$csrf?>');
  fd.append('action','update_profil');
  fd.append('gambar',input.files[0]);
  fd.append('no_telefon','<?=addslashes($user['no_telefon']??'')?>');
  fd.append('bio','<?=addslashes($user['bio']??'')?>');
  fetch('ajax_profil.php',{method:'POST',body:fd}).then(r=>r.json()).then(d=>{
    Swal.fire({toast:true,position:'top-end',icon:d.success?'success':'error',title:d.success?'Gambar dikemaskini!':d.message,showConfirmButton:false,timer:2000,background:'#0f0f1a',color:'#e0e0e0'});
  });
}
document.getElementById('profilForm')?.addEventListener('submit',function(e){
  e.preventDefault();const fd=new FormData(this);
  fetch('ajax_profil.php',{method:'POST',body:fd}).then(r=>r.json()).then(d=>{
    Swal.fire({toast:true,position:'top-end',icon:d.success?'success':'error',title:d.message,showConfirmButton:false,timer:2500,background:'#0f0f1a',color:'#e0e0e0'});
    if(d.success){toggleEdit();setTimeout(()=>location.reload(),1000);}
  });
});
document.getElementById('pwForm')?.addEventListener('submit',function(e){
  e.preventDefault();const fd=new FormData(this);
  fetch('ajax_profil.php',{method:'POST',body:fd}).then(r=>r.json()).then(d=>{
    Swal.fire({toast:true,position:'top-end',icon:d.success?'success':'error',title:d.message,showConfirmButton:false,timer:2500,background:'#0f0f1a',color:'#e0e0e0'});
    if(d.success)this.reset();
  });
});
document.getElementById('infoForm')?.addEventListener('submit',function(e){
  e.preventDefault();const fd=new FormData(this);
  fetch('<?=BASE_URL?>modules/informasi/ajax_add.php',{method:'POST',body:fd}).then(r=>r.json()).then(d=>{
    bootstrap.Modal.getInstance(document.getElementById('infoModal'))?.hide();
    Swal.fire({toast:true,position:'top-end',icon:d.success?'success':'error',title:d.message,showConfirmButton:false,timer:2500,background:'#0f0f1a',color:'#e0e0e0'});
    if(d.success)setTimeout(()=>location.reload(),1500);
  });
});
</script>
<?php require_once __DIR__.'/../../templates/footer.php'; ?>
