<?php
if (!isset($_SESSION)) session_start();

$extplayer = $_SESSION['extplayer'] ?? '';
$qUser = mysqli_query(
  $koneksi,
  "SELECT username FROM tb_user WHERE extplayer = '$extplayer' LIMIT 1"
);
$user = mysqli_fetch_assoc($qUser);
$username_login = $user['username'] ?? '';
?>

<!-- BOTTOM SHEET CLAIM SCATTER -->
<div id="scaterSheet" class="modal-sheet-overlay" onclick="closeScaterSheet(event)">
  <div class="modal-sheet-container" onclick="event.stopPropagation()">
    <div class="modal-sheet-header">
      <div class="modal-sheet-drag"></div>
      <span>Claim Scatter</span>
      <button onclick="closeScaterSheet()">✕</button>
    </div>

    <div class="modal-sheet-body">
      <div class="note-info warning">
        <div class="note-icon">
          <i class="fas fa-shield-halved"></i>
        </div>
        <div class="note-content">
          <strong>Perhatian</strong>
          <p>
            Sistem memiliki mekanisme deteksi otomatis terhadap segala bentuk manipulasi, pemalsuan data, maupun klaim bonus yang tidak valid.
          </p>
          <p class="note-danger">
            Setiap pelanggaran akan dikenakan sanksi berupa
            <b>pemblokiran akun secara permanen tanpa toleransi.</b>.
          </p>
        </div>
      </div>

      <form id="scaterForm" method="post" enctype="multipart/form-data">
        <!-- UPLOAD IMAGE -->
        <label class="upload-box">
          <input type="file" name="bukti_ss" id="bukti_ss" accept="image/*" required hidden>
          <div class="upload-placeholder" id="uploadPlaceholder">
            <i class="fa-solid fa-camera"></i>
            <span>Klik untuk upload bukti scatter</span>
            <small>PNG / JPG</small>
          </div>
          <img id="previewImage" class="preview-image" style="display:none;">
        </label>

        <label>Username</label>
        <input
          type="text"
          name="username"
          value="<?= htmlspecialchars($username_login) ?>"
          readonly
        >

        <label>Play ID</label>
        <input type="text" name="play_id" required>

        <label>Total Bet</label>
        <input type="number" name="total_bet" required>

        <label>Jumlah Scatter</label>
        <select name="jumlah_scater" required>
          <option value="">-- Pilih --</option>
          <option>3 SCATER</option>
          <option>4 SCATER</option>
          <option>5 SCATER</option>
          <option>5++ SCATER</option>
        </select>

        <div id="bonusInfo" class="bonus-info">
          Bonus yang akan didapatkan: <b id="bonusText">Rp 0</b>
        </div>

        <input type="hidden" name="total_bonus" id="total_bonus">

        <button type="submit" class="btn-submit">Process Claim</button>
      </form>
    </div>
  </div>
</div>

<!-- ================= SHEET SC ================= -->
<script>
document.addEventListener('DOMContentLoaded', () => {

  const scaterForm = document.getElementById('scaterForm');

  scaterForm.addEventListener('submit', function(e){
    e.preventDefault();

    const formData = new FormData(scaterForm);

    fetch('../../main/function/claim_scater_process.php', {
      method: 'POST',
      body: formData
    })
    .then(res => res.json())
    .then(res => {
      if (res.status === 'success') {
        showToast(res.message, 'success');

        scaterForm.reset();
        document.getElementById('previewImage').style.display = 'none';
        document.getElementById('uploadPlaceholder').style.display = 'block';

        setTimeout(() => closeScaterSheet(), 2000);
      } else {
        showToast(res.message, 'error');
      }
    })
    .catch(err => {
      console.error(err);
      showToast('Terjadi kesalahan sistem','error');
    });
  });

});
</script>

<script>
function openScaterModal(){
  document.getElementById('scaterSheet').classList.add('show');
}

function closeScaterSheet(e){
  if(e) e.stopPropagation();
  document.getElementById('scaterSheet').classList.remove('show');
}
</script>

<script>
const rules = [
  {min: 400,  bonus: {3:10000, 4:20000, 5:50000}},
  {min: 1000, bonus: {3:20000, 4:40000, 5:100000}},
  {min: 2000, bonus: {3:30000, 4:60000, 5:200000}},
  {min: 5000, bonus: {3:50000, 4:100000,5:300000}},
  {min: 10000,bonus: {3:80000, 4:150000,5:300000}}
];

const betInput     = document.querySelector('input[name="total_bet"]');
const scatterInput = document.querySelector('select[name="jumlah_scater"]');
const bonusText    = document.getElementById('bonusText');
const bonusHidden  = document.getElementById('total_bonus');

function formatRupiah(num){
  return 'Rp ' + num.toLocaleString('id-ID');
}

function hitungBonus(){
  const bet = parseInt(betInput.value || 0);
  let scatter = scatterInput.value;

  if(!bet || !scatter){
    bonusText.innerText = 'Rp 0';
    bonusHidden.value = 0;
    return;
  }

  // ambil angka scatter (1-3,2-4,3-5,5++)
  if(scatter.includes('5')) scatter = 5;
  else if(scatter.includes('4')) scatter = 4;
  else scatter = 3;

  let bonus = 0;

  for(let i = rules.length-1; i >= 0; i--){
    if(bet >= rules[i].min){
      bonus = rules[i].bonus[scatter];
      break;
    }
  }

  bonusText.innerText = formatRupiah(bonus);
  bonusHidden.value = bonus;
}

betInput.addEventListener('input', hitungBonus);
scatterInput.addEventListener('change', hitungBonus);
</script>
<script>
const fileInput = document.getElementById('bukti_ss');
const previewImg = document.getElementById('previewImage');
const placeholder = document.getElementById('uploadPlaceholder');

fileInput.addEventListener('change', function(){
  const file = this.files[0];
  if(!file) return;

  if(!file.type.startsWith('image/')){
    alert('File harus berupa gambar!');
    this.value = '';
    return;
  }

  const reader = new FileReader();
  reader.onload = function(e){
    previewImg.src = e.target.result;
    previewImg.style.display = 'block';
    placeholder.style.display = 'none';
  }
  reader.readAsDataURL(file);
});
</script>
<script>
let toastTimer;

(function() {
  const style = document.createElement("style");
  style.innerHTML = `
    .toast-notif {
      position: fixed;
      bottom: 24px;
      left: 50%;
      transform: translateX(-50%) translateY(20px);
      z-index: 40000;
      opacity: 0;
      pointer-events: none;
      transition: all .3s ease;
      font-family: Arial, sans-serif;
    }

    .toast-notif.show {
      opacity: 1;
      pointer-events: auto;
      transform: translateX(-50%) translateY(0);
    }

    .toast-box {
      min-width: 260px;
      max-width: 90vw;
      padding: 14px 16px;
      border-radius: 14px;
      color: #fff;
      display: flex;
      align-items: center;
      gap: 12px;
      box-shadow: 0 12px 30px rgba(0,0,0,.45);
    }

    .toast-success { background: #198754; }
    .toast-error { background: #dc3545; }
    .toast-warning { background: #f39c12; }
    .toast-info { background: #3498db; }

    .toast-close {
      background: none;
      border: none;
      color: #fff;
      font-size: 18px;
      cursor: pointer;
    }
  `;
  document.head.appendChild(style);
  
  window.showToast = function(message, type = "info", duration = 3000) {
    let toast = document.querySelector(".toast-notif");
    
    if (!toast) {
      toast = document.createElement("div");
      toast.className = "toast-notif";
      document.body.appendChild(toast);
    }
    
    toast.innerHTML = `
      <div class="toast-box toast-${type}">
        <span>${message}</span>
        <button class="toast-close">✕</button>
      </div>
    `;
    
    toast.classList.add("show");
    
    toast.querySelector(".toast-close").onclick = () => {
      toast.classList.remove("show");
    };
    
    clearTimeout(window.__toastTimer);
    window.__toastTimer = setTimeout(() => {
      toast.classList.remove("show");
    }, duration);
  };
})();
</script>
<!-- ================= SHEET CS ================= -->

<style>
  /* ===============================
   BOTTOM SHEET OVERLAY
================================ */
.modal-sheet-overlay{
  position:fixed;
  inset:0;
  background:rgba(0,0,0,.65);
  backdrop-filter:blur(6px);
  display:none;
  z-index:20000;
}

.modal-sheet-overlay.show{
  display:block;
}

/* ===============================
   BOTTOM SHEET CONTAINER
================================ */
.modal-sheet-container{
  position:absolute;
  bottom:0;
  width:100%;
  max-height:85vh;
  background:#1b1b1b;
  border-radius:20px 20px 0 0;
  display:flex;
  flex-direction:column;
  animation:sheetUp .35s ease;
}

@keyframes sheetUp{
  from{transform:translateY(100%)}
  to{transform:translateY(0)}
}

/* ===============================
   HEADER (STICKY)
================================ */
.modal-sheet-header{
  position:sticky;
  top:0;
  background:#222;
  padding:10px 16px 10px;
  display:flex;
  align-items:center;
  justify-content:space-between;
  color:#f5c16c;
  font-weight:700;
  z-index:10;
  border-bottom:1px solid #333;
}

.modal-sheet-drag{
  position:absolute;
  top:6px;
  left:50%;
  transform:translateX(-50%);
  width:42px;
  height:4px;
  border-radius:3px;
  background:#666;
}

.modal-sheet-header button{
  background:none;
  border:none;
  color:#f5c16c;
  font-size:22px;
  cursor:pointer;
}

/* ===============================
   BODY (SCROLL AREA)
================================ */
.modal-sheet-body{
  padding:16px;
  overflow-y:auto;
}

/* ===============================
   FORM ELEMENT
================================ */
.modal-sheet-body label{
  display:block;
  font-size:13px;
  color:#ccc;
  margin-top:14px;
}

.modal-sheet-body input,
.modal-sheet-body select{
  width:100%;
  padding:12px;
  margin-top:6px;
  background:#111;
  border:1px solid #333;
  border-radius:12px;
  color:#fff;
  font-size:14px;
}

.modal-sheet-body input:focus,
.modal-sheet-body select:focus{
  outline:none;
  border-color:#f5c16c;
}

/* ===============================
   SUBMIT BUTTON
================================ */
.btn-submit{
  margin-top:20px;
  padding:14px;
  width:100%;
  border:none;
  border-radius:14px;
  background:linear-gradient(45deg,#f5c16c,#ff9900);
  font-weight:700;
  font-size:15px;
  cursor:pointer;
  box-shadow:0 6px 16px rgba(245,193,108,.4);
}

/* ===============================
   BONUS INFO
================================ */
.bonus-info{
  margin-top:14px;
  padding:12px;
  background:#0f0f0f;
  border:1px dashed #f5c16c;
  border-radius:12px;
  color:#f5c16c;
  font-size:14px;
  text-align:center;
}

/* ===============================
   CUSTOM IMAGE UPLOAD
================================ */
.upload-box{
  width:100%;
  cursor:pointer;
  margin-top:6px;
}

.upload-placeholder{
  border:2px dashed #f5c16c;
  border-radius:14px;
  padding:15px 8px;
  text-align:center;
  color:#f5c16c;
  transition:.25s;
  transform: translateZ(0);
  will-change: transform;
}

.upload-placeholder i{
  font-size:28px;
  margin-bottom:10px;
}

.upload-placeholder span{
  display:block;
  font-weight:600;
  margin-bottom:4px;
}

.upload-placeholder small{
  font-size:12px;
  color:#aaa;
}

.upload-box:hover .upload-placeholder{
  background:rgba(245,193,108,.08);
}

/* ===============================
   IMAGE PREVIEW
================================ */
.preview-image{
  width:100%;
  max-height:240px;
  object-fit:contain;
  border-radius:14px;
  border:1px solid #333;
  background:#000;
  margin-top:6px;
}

/* ===============================
   NOTE INFO (WARNING)
================================ */
.note-info{
  display:flex;
  gap:12px;
  padding:14px;
  border-radius:14px;
  margin-bottom:16px;
  background:linear-gradient(
    135deg,
    rgba(255, 77, 77, 0.12),
    rgba(245, 193, 108, 0.08)
  );
  border:1px solid rgba(255, 77, 77, 0.35);
  box-shadow:0 6px 18px rgba(0,0,0,.25);
}

/* ICON */
.note-icon{
  min-width:36px;
  height:36px;
  border-radius:50%;
  background:rgba(255,77,77,.15);
  display:flex;
  align-items:center;
  justify-content:center;
  color:#ff6b6b;
  font-size:16px;
}

/* CONTENT */
.note-content strong{
  display:block;
  font-size:14px;
  color:#ffb3b3;
  margin-bottom:4px;
  letter-spacing:.3px;
}

.note-content p{
  font-size:12.5px;
  line-height:1.6;
  color:#f1dcdc;
  margin:0 0 6px;
}

.note-content p:last-child{
  margin-bottom:0;
}

/* DANGER TEXT */
.note-danger{
  color:#ff9d9d;
}

.toast-icon{
  font-size:22px;
}

.toast-box.success .toast-icon::before{
  content:'✔';
}

.toast-box.error .toast-icon::before{
  content:'✖';
}

.toast-text{
  flex:1;
  font-size:14px;
  line-height:1.4;
}

.toast-close{
  background:none;
  border:none;
  font-size:18px;
  color:inherit;
  cursor:pointer;
}

@keyframes toastPop{
  from{transform:scale(.85);opacity:0}
  to{transform:scale(1);opacity:1}
}


</style>

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css?v=2.6.0.0" rel="stylesheet">