@extends('layout.admin')

@section('content')
<div class="container my-2">
  <div class="card border-0 shadow-lg rounded-4">
    <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center px-4 py-3">
      <h5 class="mb-0 d-flex align-items-center">
        <i class="bi bi-gear-fill me-2 fs-5"></i>
        Gmail বিক্রি সেটিংস
      </h5>
      <button id="toggleProject" class="btn btn-outline-light btn-sm rounded-pill d-flex align-items-center">
        <i class="bi bi-power me-1"></i> Toggle
      </button>
    </div>

    <div class="card-body bg-white px-4 py-4">
      <form action="{{ route('admin.gmail.setting.update') }}" method="POST" class="row g-4">
        @csrf

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-lock-fill me-1 text-muted"></i> Gmail Password
          </label>
          <input type="text" name="password" value="{{ $setting->password ?? '' }}"
                 class="form-control rounded-3 shadow-sm" required>
        </div>

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-envelope-at-fill me-1 text-muted"></i> রিকভারি তথ্য (ঐচ্ছিক)
          </label>
          <input type="text" name="recovery_email" value="{{ $setting->recovery_email ?? '' }}"
                 class="form-control rounded-3 shadow-sm" placeholder="যে কোনো তথ্য লিখতে পারেন (ঐচ্ছিক)">
        </div>

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-toggle2-on me-1 text-muted"></i> রিকভারি Gmail লাগবে?
          </label>
          <select name="require_recovery" class="form-select rounded-3 shadow-sm">
            <option value="1" {{ ($setting->require_recovery ?? 1) == 1 ? 'selected' : '' }}>ON (Required)</option>
            <option value="0" {{ ($setting->require_recovery ?? 1) == 0 ? 'selected' : '' }}>OFF (Not Required)</option>
          </select>
        </div>

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-calendar-check-fill me-1 text-muted"></i> Daily Limit
          </label>
          <input type="number" name="limit" value="{{ $setting->limit ?? 0 }}"
                 class="form-control rounded-3 shadow-sm" required>
        </div>

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-cash-coin me-1 text-muted"></i> Per Gmail Price (৳)
          </label>
          <input type="number" step="0.01" name="price" value="{{ $setting->price ?? 0 }}"
                 class="form-control rounded-3 shadow-sm" required>
        </div>

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-cash-stack me-1 text-muted"></i> Admin Price (৳)
          </label>
          <input type="number" step="0.01" min="0" name="admin_price" value="{{ $setting->admin_price ?? 0 }}"
                 class="form-control rounded-3 shadow-sm" required>
          <div class="form-text">এটা শুধু অ্যাডমিন হিসাবের জন্য। History table-এ Price নামে দেখাবে।</div>
        </div>

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-arrow-counterclockwise me-1 text-muted"></i> Gmail হিসাব Reset
          </label>
          <div class="border rounded-3 p-3 h-100 bg-light-subtle">
            <div class="small text-muted mb-2">
              পুরনো Gmail payment summary `0.00` থেকে শুরু করাতে চাইলে এই reset ব্যবহার করুন। Raw history মুছবে না, শুধু নতুন count reset time থেকে শুরু হবে।
            </div>
            <div class="small fw-semibold mb-3">
              Current reset time:
              {{ $setting->stats_reset_at ? $setting->stats_reset_at->timezone('Asia/Dhaka')->format('d M Y, h:i A') : 'Not reset yet' }}
            </div>
            <!--<button type="submit"-->
            <!--        class="btn btn-outline-danger rounded-pill"-->
            <!--        formaction="{{ route('admin.gmail.setting.reset_stats') }}"-->
            <!--        onclick="return confirm('পুরনো Gmail payment summary এখনই reset করতে চান?');">-->
            <!--  <i class="bi bi-arrow-clockwise me-1"></i> হিসাব Reset করুন-->
            <!--</button>-->
          </div>
        </div>

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-toggle-on me-1 text-muted"></i> Project Status
          </label>
          <select name="status" class="form-select rounded-3 shadow-sm">
            <option value="1" {{ ($setting->status ?? 0) == 1 ? 'selected' : '' }}>ON</option>
            <option value="0" {{ ($setting->status ?? 1) == 0 ? 'selected' : '' }}>OFF</option>
          </select>
        </div>

        <div class="col-md-6">
          <label class="form-label fw-semibold">
            <i class="bi bi-telegram me-1 text-muted"></i> OFF Notice Telegram Link
          </label>
          <input type="text" name="telegram_link" value="{{ old('telegram_link', $setting->telegram_link ?? '') }}"
                 class="form-control rounded-3 shadow-sm" placeholder="https://t.me/your_channel">
          <div class="form-text">Project OFF থাকলে ইউজার পেজের “টেলিগ্রামে আপডেট পান” বাটনে এই লিংক যাবে।</div>
        </div>

        {{-- ✅ Upline Commission Settings --}}
        <div class="col-12">
          <div class="border rounded-4 p-3">
            <div class="d-flex align-items-center justify-content-between flex-wrap gap-2 mb-2">
              <div class="fw-bold">
                <i class="bi bi-diagram-3-fill me-1 text-primary"></i>
                আপলাইন কমিশন (Level wise)
              </div>
              <button type="button" class="btn btn-sm btn-outline-primary rounded-pill" onclick="addCommissionRow()">
                <i class="bi bi-plus-circle me-1"></i> Add Level
              </button>
            </div>

            <div class="text-muted small mb-3">
              যত লেভেল চাই তত যোগ করুন। প্রতিটা লেভেলে (৳) কত কমিশন দিতে চান লিখুন।
            </div>

            <div id="commissionWrap" class="row g-2">
              @php
                $comm = $setting->upline_commissions ?? [0.50,0.25,0.15,0.05,0.05];
              @endphp

              @foreach($comm as $i => $amt)
                <div class="col-md-4 commission-row">
                  <div class="input-group">
                    <span class="input-group-text fw-bold">Level {{ $i+1 }}</span>
                    <input type="number" step="0.01" min="0" class="form-control"
                           name="upline_commissions[]" value="{{ $amt }}">
                    <button class="btn btn-outline-danger" type="button" onclick="removeRow(this)">
                      <i class="bi bi-x-lg"></i>
                    </button>
                  </div>
                </div>
              @endforeach
            </div>

            <div class="text-muted small mt-2">
              নোট: যদি ফাঁকা/শূন্য দেন, সেই লেভেলে কমিশন যাবে না।
            </div>
          </div>
        </div>

        <div class="col-12">
          <button type="submit" class="btn btn-success w-100 py-2 fw-bold rounded-pill shadow-sm">
            <i class="bi bi-save2-fill me-1"></i> আপডেট করুন
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

<script>
  document.getElementById('toggleProject')?.addEventListener('click', function () {
    fetch("{{ route('admin.gmail.setting.toggle') }}")
      .then(res => res.json())
      .then(data => {
        const statusText = data.status ? 'ON' : 'OFF';
        alert(`Project is now ${statusText}`);
      });
  });

  function relabelLevels(){
    const rows = document.querySelectorAll('#commissionWrap .commission-row');
    rows.forEach((row, idx) => {
      const label = row.querySelector('.input-group-text');
      if(label) label.textContent = 'Level ' + (idx + 1);
    });
  }

  function addCommissionRow(){
    const wrap = document.getElementById('commissionWrap');
    if(!wrap) return;

    const col = document.createElement('div');
    col.className = 'col-md-4 commission-row';
    col.innerHTML = `
      <div class="input-group">
        <span class="input-group-text fw-bold">Level</span>
        <input type="number" step="0.01" min="0" class="form-control" name="upline_commissions[]" value="0">
        <button class="btn btn-outline-danger" type="button" onclick="removeRow(this)">
          <i class="bi bi-x-lg"></i>
        </button>
      </div>
    `;
    wrap.appendChild(col);
    relabelLevels();
  }

  function removeRow(btn){
    const row = btn.closest('.commission-row');
    if(row) row.remove();
    relabelLevels();
  }

  relabelLevels();
</script>
@endsection
