<?php
require_once("../layouts/members/head.php");
if ($user_data['aadhar_fingerPrint_service'] == "YES") {
if (in_array($user_data['usertype'], ['Master Distributor', 'Distributor', 'Retailer'])) {
?>
<div class="row">
    <div class="col-md-4 mb-2">
      <div class="card text-danger">
        <div class="card-header bg-primary text-white py-2">
          <div class="d-flex align-items-center">
            <span class="mr-auto"><?= file_name() ?></span>
             <button class="btn btn-dark btn-sm ml-auto" onclick="history.back()" > <i class="fas fa-arrow-left"></i> Back</button>
          </div>
        </div>
        <div class="p-3 text-dark">
        <?php
          $amount = $user_data['aadhar_advance_fee'];
          
              if (isset($_POST['submit'])) {
                  $mobileNumber = $_POST['mobileNumber'];
                  if (!empty($mobileNumber)) {
                      if ($user_data['balance'] >= $amount) {

                          $responseApi = aadhaar_otpSend_mobile_api_run($mobileNumber);
                          $response = json_decode($responseApi, true) ?? '';
                          
                      if (isset($response['status']) && $response['status'] == '100') {
                          alert_msg($response['message'] ?? "Something Wrong !", 'success', 0);
                      }else{
                          alert_msg($response['error'], 'danger');
                      }
                      } else {
                          awal_alert_msg("Insufficient Wallet Balance!", 'error');
                      }
                  } else {
                      awal_alert_msg("Mobile Number cannot be empty!", 'error');
                  }
              }
          
          if (isset($_POST['submitotp'])) {
              $mobileNumber = $_POST['mobileNumber'] ?? '';
              $otp = $_POST['otp'] ?? '';

              if (!empty($mobileNumber) && !empty($otp)) {

                  if ($user_data['balance'] >= $amount) {
                      $responseApi = aadhaar_otpVerify_mobile_api_run($mobileNumber, $otp);
                      $response = json_decode($responseApi, true) ?? '';
                      if (isset($response['status']) && $response['status'] == '100') {
                          $_SESSION['jwtoken']    = $response['token'];
                          $_SESSION['enMobileNo'] = $response['mobileNumber'];
                          awal_alert_msg("Redirecting... please dont go back or Refrsh.", 'warning');
                          redirect('aadhar_Fingerprint1', 3000);
                      } else {
                          awal_alert_msg($response['error'] ?? 'Server Down', 'error');
                      }
                  } else {
                      awal_alert_msg("Insufficient Wallet Balance!", 'error');
                  }
              } else {
                  awal_alert_msg("Please Fill All Valid Details!", 'error');
              }
          }
          ?>
          <?php if (!isset($response['mobileNumber']) || $response['mobileNumber'] == '') { ?>
          <div class="d-flex justify-content-center gap-2 mb-3 flex-wrap">
            <div class="border border-warning rounded-pill px-3 py-1 d-flex align-items-center bg-white">
              <i class="fas fa-mobile-alt text-warning me-2"></i>
              <span class="ms-1 small text-dark"><strong> Step 1 :</strong> Mobile OTP Verify</span>
            </div>
            <div class="border border-warning rounded-pill px-3 py-1 d-flex align-items-center bg-white">
              <i class="fas fa-fingerprint text-warning me-2"></i>
              <span class="ms-1 small text-dark"><strong> Step 1 :</strong> Biometric Verify</span>
            </div>
          </div>
          <form action="" method="POST" class="row">
            <div class="col-md-12 mb-3">
              <label> Mobile Number</label>
              <input type="number" name="mobileNumber" placeholder="Enter Mobile Number" maxlength="10" class="form-control" onkeypress="if(this.value.length==10) return false;" required>
            </div>
            <div class="col-md-12 mb-3 d-flex justify-content-between">
              <button type="button" class="btn btn-secondary" ><i class="fas fa-inr"></i> <?php echo $user_data['aadhar_advance_fee'];?></button>
              <button type="submit" name="submit" class="btn btn-primary" onclick="return confirm('Are you sure?');">Submit</button>
            </div>
          </form>
          
          <?php }else{ ?>
          <form action="" method="POST" class="row">
            <div class="col-md-12 mb-3">
              <label>Secret Code</label>
              <input type="number" name="otp" placeholder="Enter 8 Digit OTP" maxlength="8" class="form-control" onkeypress="if(this.value.length==8) return false;" required>
              <input type="hidden" name="mobileNumber" placeholder="Enter Mobile Number" value="<?php echo $response['mobileNumber'] ?? ''; ?>" maxlength="10" minlength="10" class="form-control" onkeypress="if(this.value.length==10) return false;" required>
            </div>
            <div class="col-md-12 mb-3 d-flex justify-content-between">
              <button type="button" class="btn btn-secondary" ><i class="fas fa-inr"></i> <?php echo $user_data['aadhar_advance_fee'];?></button>
              <button type="submit" name="submitotp" class="btn btn-primary" onclick="return confirm('Are you sure?');">Submit</button>
            </div>
          </form>
         <?php }?>
        </div>
      </div>
    </div>
</div>
<?php
} else {
    require_once("404.php");
}
} else {
      awal_alert_msg('You Do Not Have This Service Active!', 'info', 'dashboard');
}
require_once("../layouts/members/foot.php");
?>
