<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
verify_csrf();
mj_listing_paid_upgrade_ensure_schema();
$adminTitle = 'Listing Upgrade Charges';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $keys = [
        'listing_renew_charge_enabled',
        'listing_feature_charge_enabled',
        'listing_sponsor_charge_enabled'
    ];
    foreach ($keys as $k) { q('INSERT INTO mj_settings(setting_key,setting_value) VALUES(?,?) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value)', [$k, isset($_POST[$k]) ? '1' : '0']); }

    $values = [
        'listing_renew_charge_amount' => number_format(max(0, (float)($_POST['listing_renew_charge_amount'] ?? 0)), 2, '.', ''),
        'listing_feature_charge_amount' => number_format(max(0, (float)($_POST['listing_feature_charge_amount'] ?? 10)), 2, '.', ''),
        'listing_sponsor_charge_amount' => number_format(max(0, (float)($_POST['listing_sponsor_charge_amount'] ?? 10)), 2, '.', ''),
        'listing_upgrade_currency' => strtoupper(substr(trim((string)($_POST['listing_upgrade_currency'] ?? 'SGD')), 0, 10)),
        'listing_upgrade_days' => (string)max(1, (int)($_POST['listing_upgrade_days'] ?? 30)),
        'listing_upgrade_payment_note' => trim((string)($_POST['listing_upgrade_payment_note'] ?? ''))
    ];
    foreach ($values as $k=>$v) { q('INSERT INTO mj_settings(setting_key,setting_value) VALUES(?,?) ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value)', [$k,$v]); }

    flash('success','Listing upgrade charges saved.');
    redirect('listing-upgrade-settings.php');
}

$s = mj_listing_upgrade_settings();
include __DIR__.'/_header.php';
?>
<div class="card card-primary card-outline">
  <div class="card-header">
    <h3 class="card-title"><i class="fas fa-tags mr-2"></i>Listing Upgrade Charges</h3>
    <div class="card-tools"><a href="listing-upgrade-requests.php" class="btn btn-sm btn-outline-primary">View Upgrade Requests</a></div>
  </div>
  <form method="post">
    <div class="card-body">
      <?php show_flash(); ?>
      <?=csrf_input()?>
      <div class="alert alert-info">
        <b>How this works:</b> Users can show interest in renewing, featuring or sponsoring a listing. The system only creates an admin review request. Users can upload manual payment proof from Billing & Payments. Admin approval activates the upgrade.
      </div>

      <div class="row">
        <div class="col-md-6 col-xl-3">
          <div class="form-group">
            <label>Currency</label>
            <input class="form-control" name="listing_upgrade_currency" value="<?=e($s['currency'])?>" maxlength="10">
          </div>
        </div>
        <div class="col-md-6 col-xl-3">
          <div class="form-group">
            <label>Default Days</label>
            <input class="form-control" type="number" min="1" name="listing_upgrade_days" value="<?=e($s['days'])?>">
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-lg-4">
          <div class="card bg-light">
            <div class="card-header"><b>Renew Listing</b></div>
            <div class="card-body">
              <div class="custom-control custom-switch mb-3">
                <input type="checkbox" class="custom-control-input" id="renew" name="listing_renew_charge_enabled" <?=setting('listing_renew_charge_enabled','1')==='1'?'checked':''?>>
                <label class="custom-control-label" for="renew">Charge enabled</label>
              </div>
              <label>Amount</label>
              <input class="form-control" type="number" min="0" step="0.01" name="listing_renew_charge_amount" value="<?=e(number_format((float)$s['renew_amount'],2,'.',''))?>">
            </div>
          </div>
        </div>

        <div class="col-lg-4">
          <div class="card bg-light">
            <div class="card-header"><b>Feature Listing</b></div>
            <div class="card-body">
              <div class="custom-control custom-switch mb-3">
                <input type="checkbox" class="custom-control-input" id="feature" name="listing_feature_charge_enabled" <?=setting('listing_feature_charge_enabled','1')==='1'?'checked':''?>>
                <label class="custom-control-label" for="feature">Charge enabled</label>
              </div>
              <label>Amount</label>
              <input class="form-control" type="number" min="0" step="0.01" name="listing_feature_charge_amount" value="<?=e(number_format((float)$s['feature_amount'],2,'.',''))?>">
            </div>
          </div>
        </div>

        <div class="col-lg-4">
          <div class="card bg-light">
            <div class="card-header"><b>Sponsored Listing</b></div>
            <div class="card-body">
              <div class="custom-control custom-switch mb-3">
                <input type="checkbox" class="custom-control-input" id="sponsor" name="listing_sponsor_charge_enabled" <?=setting('listing_sponsor_charge_enabled','1')==='1'?'checked':''?>>
                <label class="custom-control-label" for="sponsor">Charge enabled</label>
              </div>
              <label>Amount</label>
              <input class="form-control" type="number" min="0" step="0.01" name="listing_sponsor_charge_amount" value="<?=e(number_format((float)$s['sponsor_amount'],2,'.',''))?>">
            </div>
          </div>
        </div>
      </div>

      <div class="form-group mt-3">
        <label>User note shown on My Listings</label>
        <textarea class="form-control" rows="3" name="listing_upgrade_payment_note"><?=e($s['payment_note'])?></textarea>
      </div>
    </div>
    <div class="card-footer">
      <button class="btn btn-primary"><i class="fas fa-save mr-1"></i>Save Charges</button>
    </div>
  </form>
</div>
<?php include __DIR__.'/_footer.php'; ?>
