<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
verify_csrf();
mj291_payment_ensure_schema();
$adminTitle = 'Upgrade Payment Methods';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $bools = [
        'upgrade_payment_bank_enabled',
        'upgrade_payment_paynow_enabled',
        'upgrade_payment_paypal_enabled',
        'upgrade_payment_cash_enabled',
        'upgrade_payment_proof_upload_enabled',
        'listing_renew_charge_enabled',
        'listing_feature_charge_enabled',
        'listing_sponsor_charge_enabled',
        'payment_cart_tax_enabled'
    ];
    foreach($bools as $k){ mj_setting_set($k, isset($_POST[$k]) ? '1' : '0'); }

    foreach([
        'upgrade_payment_bank_label',
        'upgrade_payment_bank_details',
        'upgrade_payment_paynow_label',
        'upgrade_payment_paynow_details',
        'upgrade_payment_paypal_label',
        'upgrade_payment_paypal_details',
        'upgrade_payment_cash_label',
        'upgrade_payment_cash_details',
        'extra_listing_slot_charge_amount',
        'extra_company_slot_charge_amount',
        'extra_slot_currency',
        'listing_upgrade_currency',
        'listing_upgrade_days',
        'listing_renew_charge_amount',
        'listing_feature_charge_amount',
        'listing_sponsor_charge_amount',
        'extra_slot_min_qty',
        'extra_slot_max_qty',
        'payment_cart_tax_label',
        'payment_cart_default_tax_rate'
    ] as $k){
        mj_setting_set($k, trim((string)($_POST[$k] ?? '')));
    }

    flash('success','Upgrade payment methods saved.');
    redirect('upgrade-payment-settings.php');
}

include __DIR__.'/_header.php';
?>
<div class="card card-primary card-outline">
  <div class="card-header">
    <h3 class="card-title"><i class="fas fa-money-check-alt mr-2"></i>Upgrade Payment Methods</h3>
    <div class="card-tools">
      <a href="payment-center.php?tab=review" class="btn btn-sm btn-outline-primary">Payment Proofs</a>
    </div>
  </div>
  <form method="post">
    <div class="card-body">
      <?php show_flash(); ?>
      <?=csrf_input()?>

      <div class="alert alert-info">
        Activate only the payment methods you want users to see for renew, featured, sponsored and extra slot payments.
        This does not enable automatic online checkout; users upload proof and admin reviews it.
      </div>

      <div class="custom-control custom-switch mb-3">
        <input type="checkbox" class="custom-control-input" id="proof" name="upgrade_payment_proof_upload_enabled" <?=setting('upgrade_payment_proof_upload_enabled','1')==='1'?'checked':''?>>
        <label class="custom-control-label" for="proof">Allow users to upload payment proof</label>
      </div>

      <div class="row">
        <?php
        $cards = [
          'bank' => ['title'=>'Bank Transfer','icon'=>'fas fa-university'],
          'paynow' => ['title'=>'PayNow / QR','icon'=>'fas fa-qrcode'],
          'paypal' => ['title'=>'PayPal','icon'=>'fab fa-paypal'],
          'cash' => ['title'=>'Cash / Pay at Shop','icon'=>'fas fa-cash-register']
        ];
        foreach($cards as $key=>$cfg):
        ?>
          <div class="col-lg-6">
            <div class="card bg-light">
              <div class="card-header">
                <b><i class="<?=e($cfg['icon'])?> mr-1"></i><?=e($cfg['title'])?></b>
              </div>
              <div class="card-body">
                <div class="custom-control custom-switch mb-3">
                  <input type="checkbox" class="custom-control-input" id="<?=e($key)?>" name="upgrade_payment_<?=e($key)?>_enabled" <?=setting('upgrade_payment_'.$key.'_enabled','0')==='1'?'checked':''?>>
                  <label class="custom-control-label" for="<?=e($key)?>">Enable <?=e($cfg['title'])?></label>
                </div>
                <label>Display label</label>
                <input class="form-control mb-2" name="upgrade_payment_<?=e($key)?>_label" value="<?=e(setting('upgrade_payment_'.$key.'_label',$cfg['title']))?>">
                <label>Instructions / details shown to user</label>
                <textarea class="form-control" rows="4" name="upgrade_payment_<?=e($key)?>_details"><?=e(setting('upgrade_payment_'.$key.'_details',''))?></textarea>
              </div>
            </div>
          </div>
        <?php endforeach; ?>
      </div>

      <div class="card mt-3">
        <div class="card-header"><b>Listing Renew / Featured / Sponsored Charges</b></div>
        <div class="card-body">
          <div class="alert alert-light border">Default requested amount for <b>Featured Listing</b> and <b>Sponsored Listing</b> is <b>SGD 10.00 for 30 days</b>. You can change it here anytime. User proof upload remains pending until admin approves.</div>
          <div class="row">
            <div class="col-md-3"><label>Currency</label><input class="form-control" name="listing_upgrade_currency" value="<?=e(setting('listing_upgrade_currency','SGD'))?>"></div>
            <div class="col-md-3"><label>Default days</label><input class="form-control" type="number" min="1" name="listing_upgrade_days" value="<?=e(setting('listing_upgrade_days','30'))?>"></div>
            <div class="col-md-2"><label>Renew amount</label><input class="form-control" type="number" min="0" step="0.01" name="listing_renew_charge_amount" value="<?=e(setting('listing_renew_charge_amount','10.00'))?>"><div class="custom-control custom-switch mt-2"><input type="checkbox" class="custom-control-input" id="renewCharge" name="listing_renew_charge_enabled" <?=setting('listing_renew_charge_enabled','1')==='1'?'checked':''?>><label class="custom-control-label" for="renewCharge">Enable</label></div></div>
            <div class="col-md-2"><label>Featured amount</label><input class="form-control" type="number" min="0" step="0.01" name="listing_feature_charge_amount" value="<?=e(setting('listing_feature_charge_amount','10.00'))?>"><div class="custom-control custom-switch mt-2"><input type="checkbox" class="custom-control-input" id="featureCharge" name="listing_feature_charge_enabled" <?=setting('listing_feature_charge_enabled','1')==='1'?'checked':''?>><label class="custom-control-label" for="featureCharge">Enable</label></div></div>
            <div class="col-md-2"><label>Sponsored amount</label><input class="form-control" type="number" min="0" step="0.01" name="listing_sponsor_charge_amount" value="<?=e(setting('listing_sponsor_charge_amount','10.00'))?>"><div class="custom-control custom-switch mt-2"><input type="checkbox" class="custom-control-input" id="sponsorCharge" name="listing_sponsor_charge_enabled" <?=setting('listing_sponsor_charge_enabled','1')==='1'?'checked':''?>><label class="custom-control-label" for="sponsorCharge">Enable</label></div></div>
          </div>
        </div>
      </div>

      <div class="card mt-3">
        <div class="card-header"><b>Extra Slot Charges</b></div>
        <div class="card-body">
          <div class="row">
            <div class="col-md-4"><label>Currency</label><input class="form-control" name="extra_slot_currency" value="<?=e(setting('extra_slot_currency','SGD'))?>"></div>
            <div class="col-md-4"><label>Extra listing slot amount</label><input class="form-control" type="number" step="0.01" min="0" name="extra_listing_slot_charge_amount" value="<?=e(setting('extra_listing_slot_charge_amount','5.00'))?>"></div>
            <div class="col-md-4"><label>Extra company slot amount</label><input class="form-control" type="number" step="0.01" min="0" name="extra_company_slot_charge_amount" value="<?=e(setting('extra_company_slot_charge_amount','5.00'))?>"></div>
          </div>
          <div class="row mt-3">
            <div class="col-md-3"><label>Minimum quantity</label><input class="form-control" type="number" min="1" name="extra_slot_min_qty" value="<?=e(setting('extra_slot_min_qty','1'))?>"></div>
            <div class="col-md-3"><label>Maximum quantity per checkout</label><input class="form-control" type="number" min="1" name="extra_slot_max_qty" value="<?=e(setting('extra_slot_max_qty','100'))?>"></div>
            <div class="col-md-6"><div class="alert alert-light border mb-0"><b>Users can buy multiple slots together.</b><br>Example: 3 listing slots × <?=e(setting('extra_slot_currency','SGD'))?> <?=e(setting('extra_listing_slot_charge_amount','5.00'))?>.</div></div>
          </div>
        </div>
      </div>
    </div>

      <div class="card mt-3">
        <div class="card-header"><b>Bulk Checkout / Invoice Tax</b></div>
        <div class="card-body">
          <div class="custom-control custom-switch mb-3"><input type="checkbox" class="custom-control-input" id="cartTax" name="payment_cart_tax_enabled" <?=setting('payment_cart_tax_enabled', setting('billing_tax_enabled','0'))==='1'?'checked':''?>><label class="custom-control-label" for="cartTax">Add tax/GST/VAT to checkout invoice</label></div>
          <div class="row"><div class="col-md-6"><label>Tax label</label><input class="form-control" name="payment_cart_tax_label" value="<?=e(setting('payment_cart_tax_label',setting('billing_tax_label','GST/VAT')))?>"></div><div class="col-md-6"><label>Default tax rate %</label><input class="form-control" type="number" min="0" step="0.01" name="payment_cart_default_tax_rate" value="<?=e(setting('payment_cart_default_tax_rate',setting('billing_default_tax_rate','0')))?>"></div></div>
        </div>
      </div>

    <div class="card-footer"><button class="btn btn-primary"><i class="fas fa-save mr-1"></i>Save Payment Methods</button></div>
  </form>
</div>
<?php include __DIR__.'/_footer.php'; ?>
