<?php
require 'db.php';
require 'functions.php';
requireRole(['panchayatuser']);

$pagetitle = 'Register a Villager';
$activemenu = 'registrations';
$fee = getSetting($pdo, 'registrationfee', '100.00');
$isfree = bccomp(moneyStr($fee), '0.00', 2) === 0;
require 'header.php';
?>

<div class="dfp-card" style="max-width:640px;">
  <h3>Assisted Registration</h3>
  <p style="font-size:var(--fontsize-sm);color:var(--clr-gray-mid);">
    Use this to register villagers who cannot use the mobile app themselves (e.g. elderly residents).
    Enter their details below on their behalf.
  </p>

  <form id="frmAssisted" autocomplete="off">
    <div class="dfp-formgroup"><label>Full Name</label><input type="text" name="fullname" required></div>
    <div class="dfp-formgroup"><label>Phone Number</label><input type="text" name="phone" maxlength="10" required></div>
    <div class="dfp-formgroup"><label>Photo</label><input type="file" name="photo" accept="image/*" required></div>
    <div class="dfp-formgroup"><label>Date of Birth</label><input type="date" name="dob" required></div>

    <div class="dfp-formgroup">
      <label>Verification Method</label>
      <select name="verifymethod" id="verifymethod" required>
        <option value="aadhaar">Aadhaar</option>
        <option value="house">House Number (no Aadhaar)</option>
      </select>
    </div>

    <div id="aadhaarFields">
      <div class="dfp-formgroup"><label>Aadhaar Number</label><input type="text" name="aadhaarnumber" maxlength="12"></div>
      <div class="dfp-formgroup"><label>Aadhaar Card Photo</label><input type="file" name="aadhaarphoto" accept="image/*"></div>
      <div class="dfp-formgroup"><label>Village (vtc field from Aadhaar card)</label><input type="text" name="village"></div>
      <div class="dfp-formgroup"><label>Address</label><input type="text" name="addressaadhaar"></div>
    </div>

    <div id="houseFields" style="display:none;">
      <div class="dfp-formgroup"><label>House Number</label><input type="text" name="houseno"></div>
      <div class="dfp-formgroup"><label>Address</label><input type="text" name="addresshouse"></div>
    </div>

    <div class="dfp-formgroup">
      <label>Vehicle Numbers (optional)</label>
      <div id="vehicleRows"><input type="text" name="vehicles[]" placeholder="e.g. GA-05-AB-1234" style="margin-bottom:6px;"></div>
      <button type="button" class="btn btn-sm btn-outline" id="btnAddVehicle">+ Add another vehicle</button>
    </div>

    <?php if ($isfree): ?>
      <div class="dfp-formgroup">
        <p style="font-weight:700;">Registration fee is currently Rs. 0 - no payment needed. The pass will be activated immediately.</p>
      </div>
    <?php else: ?>
      <div class="dfp-formgroup">
        <label>Payment (Rs. <?php echo safeOut(moneyStr($fee)); ?>)</label>
        <select name="paymentmethod" required>
          <option value="cash">Collected in Cash</option>
          <option value="upi">Collected via UPI</option>
          <option value="unpaid">Not Collected Now (villager pays later via app)</option>
        </select>
      </div>
    <?php endif; ?>

    <button type="submit" class="btn">Register Villager</button>
  </form>
</div>

<?php require 'footer.php'; ?>
<script>
$('#verifymethod').on('change', function () {
  if (this.value === 'aadhaar') {
    $('#aadhaarFields').show();
    $('#houseFields').hide();
  } else {
    $('#aadhaarFields').hide();
    $('#houseFields').show();
  }
});

$('#btnAddVehicle').on('click', function () {
  $('#vehicleRows').append('<input type="text" name="vehicles[]" placeholder="e.g. GA-05-AB-1234" style="margin-bottom:6px;margin-top:6px;">');
});

$('#frmAssisted').on('submit', function (e) {
  e.preventDefault();
  var btn = $(this).find('button[type=submit]');
  if (btn.prop('disabled')) { return; }
  btn.prop('disabled', true);

  var formdata = new FormData(this);
  $.ajax({
    url: 'ajax_panchayat_registerassisted.php',
    type: 'POST',
    data: formdata,
    processData: false,
    contentType: false,
    dataType: 'json',
    success: function (resp) {
      btn.prop('disabled', false);
      dfpToast(resp.message, resp.success ? 'success' : 'error');
      if (resp.success) {
        setTimeout(function () { window.location.href = 'panchayat_registrationview.php?id=' + resp.data.id; }, 1000);
      }
    },
    error: function () {
      btn.prop('disabled', false);
      dfpToast('Something went wrong. Please try again.', 'error');
    }
  });
});
</script>
<?php require 'footer2.php'; ?>
