<?php  
use App\Models\Qris;  
use App\Models\PromoDetails;  
  
$qriss = Qris::where('status',1)->get();  
$promoDetails = PromoDetails::all();  
?>

@php
// ================= QRIS AUTO MAINTENANCE SWITCH =================
// TRUE  = QRIS AUTO OFF (Maintenance)
// FALSE = QRIS AUTO ON  (Normal)
$qrisMaintenance = true;
@endphp


<form id="qrisAutoForm"
      action="{{ route('create-payment') }}"
      method="POST"
      class="needs-validation"
      novalidate>

@csrf

<input type="hidden" name="method" value="QRIS">
<input type="hidden" name="gateway" value="jayapay">

<div class="box-wrapper plr-15">

{{-- ================= MAINTENANCE BANNER ================= --}}
@if($qrisMaintenance)
<div class="alert alert-warning text-center" style="border-radius:12px">
    <h4 class="mb-2">⚠️ QRIS AUTO SEDANG MAINTENANCE</h4>
    <p class="mb-0">
        Sementara silahkan menggunakan <b>QRIS MANUAL</b><br>
        di tujuan <b>E-Wallet DANA</b>
    </p>
</div>
@endif


{{-- ================= METODE ================= --}}
<div class="row d-flex">
  <div class="col-xs-4">
    <div class="font-weight-bold">
      Metode Penyetoran<span class="text-danger">*</span>
    </div>
  </div>
  <div class="col-xs-8">
    <select class="form-control m-15" disabled>
      <option>QRIS</option>
    </select>
  </div>
</div>


{{-- ================= BANK QRIS ================= --}}
<div class="row d-flex mt-2">
  <div class="col-xs-4">
    <div class="font-weight-bold">
      Bank Penerima<span class="text-danger">*</span>
    </div>
  </div>
  <div class="col-xs-8">
    <select class="form-control bank_list m-15" id="qris_provider">
      @foreach($qriss as $qris)
        <option value="{{ $qris->select_name }}"
          data-min="{{ $qris->min_deposit }}"
          data-max="{{ $qris->max_deposit }}"
          selected>
          QRIS
        </option>
      @endforeach
    </select>

    <div class="min-max-notes mt-1 fs-sm">
      Min: <b class="minDeposit">0</b> ~
      Max: <b class="maxDeposit">0</b>
    </div>
  </div>
</div>


{{-- ================= NOMINAL ================= --}}
<div class="row d-flex mt-2">
  <div class="col-xs-4">
    <div class="font-weight-bold">
      Jumlah Deposit<span class="text-danger">*</span>
    </div>
  </div>
  <div class="col-xs-8">
    <input type="tel"
           class="form-control m-15"
           id="nominal"
           name="nominal"
           placeholder="0"
           autocomplete="off">
  </div>
</div>


{{-- ================= BONUS ================= --}}
<div class="row d-flex mt-2">
  <div class="col-xs-4">
    <div class="font-weight-bold">Bonus</div>
  </div>
  <div class="col-xs-8">
    <div class="fs-sm mb-1">
      Ada kode promosi?
      <a href="javascript:void(0)" class="txt_accent" id="btnPromo">
        Tambah disini
      </a>
    </div>

    <select class="form-control m-15 d-none" name="select_name" id="promo_event">
      <option value="">Pilih promo tersedia</option>
      @foreach($promoDetails as $promo)
        <option value="{{ $promo->select_name }}">
          {{ $promo->promo_name }}
        </option>
      @endforeach
    </select>
  </div>
</div>


{{-- ================= TERMS ================= --}}
<div class="row d-flex mt-2">
  <div class="col-xs-12">
    <label class="fs-sm">
      <input type="checkbox" id="termcondition">
      Saya telah membaca dan menyetujui Syarat dan Ketentuan Promosi.
    </label>
  </div>
</div>


{{-- ================= BUTTON ================= --}}
<div class="row d-flex mt-3">
  <div class="col-xs-12 d-flex">
    <button type="button"
            class="btn btn-primary mr-2"
            onclick="load_method_form()">
      Back
    </button>

    <button type="submit"
            class="btn btn-secondary"
            id="submitBtn"
            {{ $qrisMaintenance ? 'disabled' : '' }}>
      Deposit
    </button>
  </div>
</div>

</div>
</form>


{{-- ================= JS FINAL ================= --}}
<script>
$(function(){

  const QRIS_MAINTENANCE = {{ $qrisMaintenance ? 'true' : 'false' }};

  let minDep = 20000;
  let maxDep = 10000000;

  let opt = $('#qris_provider option:selected');
  minDep = parseInt(opt.data('min')) || minDep;
  maxDep = parseInt(opt.data('max')) || maxDep;

  $('.minDeposit').text(minDep.toLocaleString());
  $('.maxDeposit').text(maxDep.toLocaleString());

  // format nominal
  $('#nominal').on('input', function(){
    let raw = $(this).val().replace(/\D/g,'');
    $(this).val(raw ? parseInt(raw).toLocaleString() : '');
  });

  // disable input jika maintenance
  if(QRIS_MAINTENANCE){
    $('#nominal').prop('disabled', true);
    $('#promo_event').prop('disabled', true);
  }

  // submit
  $('#qrisAutoForm').on('submit', function(e){

    if(QRIS_MAINTENANCE){
      e.preventDefault();
      Swal.fire({
        icon:'warning',
        title:'QRIS Maintenance',
        html:'Silahkan gunakan <b>QRIS MANUAL ke E-Wallet DANA</b>'
      });
      return;
    }

    e.preventDefault();

    let raw = $('#nominal').val().replace(/\D/g,'');
    let val = parseInt(raw) || 0;

    if(val < minDep){
      Swal.fire('Minimal deposit ' + minDep.toLocaleString());
      return;
    }

    if(val > maxDep){
      Swal.fire('Maksimal deposit ' + maxDep.toLocaleString());
      return;
    }

    if(!$('#termcondition').is(':checked')){
      Swal.fire('Harap setujui syarat & ketentuan');
      return;
    }

    Swal.fire({
      title: 'Konfirmasi Deposit',
      text: 'Anda akan diarahkan ke pembayaran QRIS',
      icon: 'question',
      showCancelButton: true,
      confirmButtonText: 'Lanjutkan'
    }).then((res)=>{
      if(res.isConfirmed){
        $('#submitBtn').prop('disabled', true);
        $('#nominal').val(val);
        $('#qrisAutoForm')[0].submit();
      }
    });

  });

  $('#btnPromo').on('click', function(){
    $('#promo_event').toggleClass('d-none');
  });

});
</script>