<?php
/***************************
 * ADS CENTER – AUTO LOAD
 ***************************/
function loadFiles($dir, $exts = []) {
  if (!is_dir($dir)) return [];
  return array_values(array_filter(scandir($dir), function($f) use ($dir,$exts){
    if ($f === '.' || $f === '..') return false;
    $e = strtolower(pathinfo($f, PATHINFO_EXTENSION));
    return empty($exts) || in_array($e, $exts);
  }));
}

$banners = loadFiles('../uploads/ads/banner', ['jpg','jpeg','png','webp']);
$videos  = loadFiles('../uploads/ads/vidio', ['mp4','webm']);
$textAds = [];

$textFile = '../uploads/ads/text.txt';
if (file_exists($textFile)) {
  $raw = file_get_contents($textFile);

  // Normalisasi line ending (Windows/Linux)
  $raw = str_replace("\r\n", "\n", $raw);

  // Pecah berdasarkan === (PEMBATAS IKLAN)
  $parts = explode("===\n", $raw);

  foreach ($parts as $t) {
    $t = trim($t);
    if ($t !== '') {
      $textAds[] = $t;
    }
  }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Bahan Iklan</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
:root{
  --gold:#cfa14a;
  --bg:#0e0e0e;
  --card:#1b1b1b;
  --soft:0 6px 18px rgba(0,0,0,.45);
}
*{box-sizing:border-box}
body{
  margin:0;
  background:var(--bg);
  color:#fff;
  font-family:Inter,system-ui,sans-serif;
  padding-bottom: 90px; /* sesuaikan tinggi footer */
}

/* HEADER */
.header{
  padding:14px 16px;
  background:#141414;
  font-weight:700;
  font-size:15px;
  box-shadow:var(--soft);
}

/* TABS */
.tabs{
  display:flex;
  background:#1a1a1a;
  margin:12px;
  padding:4px;
  border-radius:14px;
}
.tab-btn{
  flex:1;
  padding:10px;
  background:transparent;
  border:none;
  color:#888;
  border-radius:12px;
  font-weight:600;
}
.tab-btn.active{
  background:linear-gradient(135deg,#f5c16c,var(--gold));
  color:#000;
}

/* CONTENT */
.tab-content{display:none;padding:12px}
.tab-content.active{display:block}

/* GRID */
.grid{
  display:grid;
  grid-template-columns:repeat(auto-fill,minmax(150px,1fr));
  gap:12px;
}

/* CARD */
.card{
  background:var(--card);
  border-radius:14px;
  padding:10px;
  box-shadow:var(--soft);
}
.card img,
.card video{
  width:100%;
  border-radius:10px;
  display:block;
}

/* ACTION */
.action{
  margin-top:8px;
  display:flex;
  gap:6px;
}
.btn{
  flex:1;
  background:var(--gold);
  border:none;
  padding:6px;
  border-radius:10px;
  font-weight:700;
  text-align:center;
  color:#000;
  cursor:pointer;
  font-size:12px;
}
.btn.dark{
  background:#222;
  color:#fff;
}

/* TEXT CARD */
.text-card p{
  font-size:12px;
  line-height:1.5;
  color:#ddd;
}

/* TOAST */
.toast{
  position:fixed;
  bottom:100px;
  left:50%;
  transform:translateX(-50%);
  background:#111;
  padding:10px 16px;
  border-radius:12px;
  opacity:0;
  transition:.3s;
  box-shadow:var(--soft);
}
.toast.show{opacity:1}

.text-card {
  background: #121212;
  border-radius: 14px;
  padding: 12px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .45);
}

.text-box {
  width: 100%;
  min-height: 220px;
  background: #0b0b0b;
  color: #f5c16c;
  font-family: monospace;
  font-size: 13px;
  line-height: 1.6;
  padding: 12px;
  border-radius: 10px;
  border: none;
  resize: none;
  box-sizing: border-box;
}

.text-box:focus {
  outline: none;
}

.action {
  text-align: right;
  margin-top: 8px;
}

.btn {
  background: linear-gradient(135deg, #f5c16c, #cfa14a);
  color: #000;
  border: none;
  padding: 6px 14px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

/* ================= VIDEO FIX RESPONSIVE ================= */

.video-card {
  display: flex;
  flex-direction: column;
}

/* Bungkus video */
.video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 9 / 16; /* video tiktok / reels */
  background: #000;
  border-radius: 12px;
  overflow: hidden;
}

/* Video isi penuh */
.video-wrap video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover; /* FULL TANPA PENYOK */
  border-radius: 12px;
}

/* ================= FORM SUBMIT VIDEO ================= */

.submit-card {
  margin-bottom: 14px;
  background: linear-gradient(180deg, #1a1a1a, #121212);
}

.submit-title {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 800;
  color: #f5c16c;
  text-align: center;
}

.form-group {
  margin-bottom: 10px;
}

.form-group label {
  font-size: 12px;
  color: #aaa;
  display: block;
  margin-bottom: 4px;
}

.form-group input,
.form-group textarea {
  width: 100%;
  background: #0b0b0b;
  border: 1px solid #222;
  border-radius: 10px;
  padding: 10px;
  color: #fff;
  font-size: 13px;
}

.form-group textarea {
  min-height: 80px;
  resize: none;
}

.submit-btn {
  width: 100%;
  padding: 10px;
  font-size: 13px;
  font-weight: 800;
}

</style>
</head>
<body>

<!-- HEADER -->
<div class="none-header">
  <div class="left" onclick="window.location.href='?page=home'">
    <i class="fas fa-chevron-left"></i> Bahan Iklan
  </div>

  <div class="right">
  <a href="javascript:void(0)" onclick="openLiveChat()" class="button-nav">
      <i class="fas fa-headset m-auto"></i>
    </a>
  </div>
</div>


<div class="tabs">
  <button class="tab-btn active" onclick="openTab('banner',this)">Banner</button>
  <button class="tab-btn" onclick="openTab('video',this)">Video</button>
  <button class="tab-btn" onclick="openTab('text',this)">Kata-kata</button>
</div>

<!-- BANNER -->
<div class="tab-content active" id="banner">
  <div class="grid">
    <?php if(empty($banners)): ?>
      <div class="card">Belum ada banner</div>
    <?php endif; ?>
    <?php foreach($banners as $b): ?>
    <div class="card">
      <img src="../uploads/ads/banner/<?= $b ?>">
      <div class="action">
        <a class="btn" download href="../uploads/ads/banner/<?= $b ?>">Download</a>
      </div>
    </div>
    <?php endforeach; ?>
  </div>
</div>

<!-- VIDEO -->
<div class="tab-content" id="video">
  <!-- FORM SUBMIT VIDEO -->
<div class="card submit-card">
  <h3 class="submit-title">JUAL SKILL EDIT DI SINI</h3>
                        <div class="note-info">
                        <i class="fas fa-info-circle"></i>
                        <span>
                          <label>
                            Punya video iklan KOIWIN? Tukarkan sekarang dan dapatkan uang hingga 700K.
                          </label>
                        </span>
                      </div>

  <div class="form-group">
    <label>Kode UID</label>
    <input type="url" id="kode_UID" placeholder="UID">
  </div>

  <div class="form-group">
    <label>Keterangan Video</label>
    <textarea id="video_desc" placeholder="Contoh: Video slot KOIWIN maxwin..."></textarea>
  </div>

  <div class="form-group">
    <label>Link YouTube Video</label>
    <input type="url" id="yt_link" placeholder="https://youtube.com/...">
  </div>

  <button class="btn submit-btn" onclick="sendToWA()">Kirim ke WhatsApp</button>
</div>
  <div class="grid">
    <?php if(empty($videos)): ?>
      <div class="card">Belum ada video</div>
    <?php endif; ?>
    <?php foreach($videos as $v): ?>
<div class="card video-card">
  <div class="video-wrap">
    <video src="../uploads/ads/vidio/<?= $v ?>" controls></video>
  </div>

  <div class="action">
    <a class="btn" download href="../uploads/ads/vidio/<?= $v ?>">Download</a>
  </div>
</div>
    <?php endforeach; ?>
  </div>
</div>

<!-- TEXT -->
<div class="tab-content" id="text">
  <div class="grid">

    <?php if (empty($textAds)): ?>
      <div class="card">Belum ada kata-kata iklan</div>
    <?php endif; ?>

    <?php foreach ($textAds as $i => $t): ?>
      <div class="card text-card">

        <textarea
          id="txt<?= $i ?>"
          class="text-box"
          readonly
        ><?= htmlspecialchars($t) ?></textarea>

        <div class="action">
          <button class="btn" onclick="copyText('txt<?= $i ?>')">
            Copy
          </button>
        </div>

      </div>
    <?php endforeach; ?>

  </div>
</div>

<div class="toast" id="toast">Tersalin</div>

<script>
function openTab(id, el){
  document.querySelectorAll('.tab-content').forEach(t=>t.classList.remove('active'));
  document.querySelectorAll('.tab-btn').forEach(b=>b.classList.remove('active'));
  document.getElementById(id).classList.add('active');
  el.classList.add('active');
}

function copyText(id){
  navigator.clipboard.writeText(
    document.getElementById(id).innerText
  ).then(()=>showToast());
}

function showToast(){
  const t=document.getElementById('toast');
  t.classList.add('show');
  setTimeout(()=>t.classList.remove('show'),1800);
}
</script>

</body>
</html>

<script>
function sendToWA() {
  const waUser = document.getElementById('kode_UID').value.trim();
  const desc   = document.getElementById('video_desc').value.trim();
  const yt     = document.getElementById('yt_link').value.trim();

  if (!waUser || !desc || !yt) {
    alert('Lengkapi semua data terlebih dahulu');
    return;
  }


  const adminWA = '<?php echo $whatsapp ?>'; // 083874277588

  const message =
`📢 SUBMIT VIDEO PROMO KOIWIN
 user UID:
${waUser}
 Keterangan Video:
${desc}
 Link YouTube:
${yt}
💰 Klaim Reward`;

  const url = `https://wa.me/${adminWA}?text=${encodeURIComponent(message)}`;
  window.open(url, '_blank');
}
</script>