<?php $page_title = "KYC" ?>
<?php include 'user_header.php'; ?>

<?php
  if (isset($_POST['submit_kyc'])) {
      $kyc_fname = $_POST['fullname'];
      $dob = $_POST['dob'];
      $kyc_address = $_POST['address'];
      $kyc_phone = $_POST['phone'];
      $gov_id_type = $_POST['gov_id_type'];

      $gov_id_file = $_FILES["gov_id_file"]["name"];
      $tempname1 = $_FILES["gov_id_file"]["tmp_name"];
      $folder1 = "../images/kyc/$gov_id_file";

      $proof_address_file = $_FILES["proof_address_file"]["name"];
      $tempname2 = $_FILES["proof_address_file"]["tmp_name"];
      $folder2 = "../images/kyc/$proof_address_file";

      $date_created = date("Y-m-d h:i:s");

      if (empty($kyc_fname) || empty($dob) || empty($kyc_address) || empty($kyc_phone) || empty($gov_id_type) || empty($gov_id_file) || empty($proof_address_file)) {
        $error = "<div class='alert alert-danger d-flex align-items-center' role='alert'>
          <i class='fa fa-warning'></i> &nbsp; All fields are required!!!
        </div>";
      }else {
        if (move_uploaded_file($tempname1, $folder1) && move_uploaded_file($tempname2, $folder2)) {
            $sql_kyc = "INSERT INTO kyc (kyc_user_id, fullName, dob, address, phone, id_type, gov_id_file, proof_address_file, date_created)
            VALUES ('$user_id', '$kyc_fname', '$dob', '$kyc_address', '$kyc_phone', '$gov_id_type', '$gov_id_file', '$proof_address_file', '$date_created')";

            $acct_update = "UPDATE users SET kyc_status = '1' WHERE user_id = '$user_id'";

            if ($connection->query($sql_kyc)===TRUE && $connection->query($acct_update)===TRUE) {
              echo "<script>
                window.location.href='kyc.php?status=success'
              </script>";
            }else {
              echo "<script>
                window.location.href='kyc.php?status=error'
              </script>";
            }
        }else {
          echo "<script>
            window.location.href='kyc.php?status=image_error'
          </script>";
        }
      }
  }

 ?>

 <?php
   $dep_status = $_GET['status']
  ?>
 <?php if ($dep_status == "success"): ?>
   <div class='alert alert-success d-flex align-items-center' role='alert'>
     <i class='fa fa-check'></i> &nbsp; KYC information submitted successfully!
   </div>

 <?php elseif ($dep_status == "error"): ?>
   <div class='alert alert-danger d-flex align-items-center' role='alert'>
     <i class='fa fa-warning'></i> &nbsp; Sorry an error occurred. Please try again later or contact support
   </div>
 <?php elseif ($dep_status == "image_error"): ?>
   <div class='alert alert-danger d-flex align-items-center' role='alert'>
     <i class='fa fa-warning'></i> &nbsp; Only JPEG, PNG, and GIF images are allowed.
   </div>
 <?php else: ?>
     <?php echo $error; ?>
 <?php endif; ?>

<div class="container-fluid content-inner pb-0">
    <div>
        <div class="row">

            <div class="col-xl-9 col-lg-8">
                <div class="card">
                    <div class="card-header d-flex justify-content-between">
                        <div class="header-title">
                            <h4 class="card-title">KYC Verification</h4><button class="badge rounded-pill bg-danger"></button>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="new-user-info">
                          <?php if ($kyc_status == 0): ?>
                            <form action="" method="post" enctype="multipart/form-data">
                              <div class="row">
                                    <div class="form-group col-md-6">
                                        <label class="form-label" for="fname">Fullname:</label>
                                        <input type="text" class="form-control" name="fullname" id="fname" value="<?php echo $full_name ?>" readonly>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label class="form-label" for="lname">Date of Birth:</label>
                                        <input type="date" class="form-control" name="dob" id="lname" placeholder="Enter DOB">
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label class="form-label" for="add1">House Address:</label>
                                        <input type="text" name="address" class="form-control" id="add1" placeholder="Enter House Address">
                                    </div>

                                    <div class="form-group col-md-6">
                                        <label class="form-label" for="mobno">Phone Number:</label>
                                        <input type="text" name="phone" class="form-control" id="mobno" placeholder="Phone Number">
                                    </div>

                                    <div class="form-group col-md-6">
                                        <label class="form-label" for="email">Goverment Issued ID:</label>
                                        <select name="gov_id_type" class="form-control">
                                            <option value="">-- select --</option>
                                            <option value="Driver License">Driver's License</option>
                                            <option value="International Passport">International Passport</option>
                                            <option value="National ID">National ID</option>

                                        </select>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <label class="form-label" for="id">Upload Government Issued ID:</label>
                                        <input type="file" class="form-control" name="gov_id_file" id="id" required>
                                    </div>
                                    <div class="form-group col-md-12">
                                        <label class="form-label" for="pro">Upload Proof of Address:</label>
                                        <input type="file" class="form-control" name="proof_address_file" id="pro" required>
                                    </div>
                                </div>


                                <button name="submit_kyc" type="submit" class="btn btn-primary">
                                    Proceed
                                </button>
                            </form>

                          <?php elseif ($kyc_status == 1): ?>
                            <center>
                              <img src="../images/pending_kyc.png" alt="">
                            </center>
                          <?php elseif ($kyc_status == 2): ?>
                            <center>
                              <img src="../images/approve.jpg" alt="">
                            </center>
                          <?php endif; ?>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

 <?php include 'user_footer.php'; ?>
