<?php
if(isset($_POST['create_acct'])){
if($bankdata->create_acct($_POST,$_FILES['passport_photo'])){
     echo "
     <script>
     Swal.fire({
     icon: 'success',
     title: 'Congratulations',
     text: 'New Client Added successfully!',
   })
   </script> 
     ";
  }else{
    echo "
    <script>
    Swal.fire({
    icon: 'error',
    title: 'Oops',
    text: 'Unable to add Client, try again later',
  })
  </script> 
    ";
  }
}
// print_r($_POST);
if(isset($_POST['updateStats'])){
  $acct_number = $_POST['acct_number'];
  $stat_data = $_POST['stat_data'];

                      if($stat_data == 'active'){
                        $stat_data = 'inactive';
                      }else{
                        $stat_data = 'active';
                      }
                      
  if($bankdata->acct_stats_func($acct_number,$stat_data)){
    echo "
    <script>
    Swal.fire({
    icon: 'success',
    title: 'Congratulations',
    text: 'Account Status Updated Successfully!',
  })
  </script> 
    ";
  }else{
    echo "
    <script>
    Swal.fire({
    icon: 'error',
    title: 'Oops',
    text: 'Unable to update Account Status, try again later',
  })
  </script> 
    ";
  }


}
if(isset($_POST['updateBal'])){
  $acct_number = $_POST['acct_number'];
  if(isset($_POST['checking_balance'])){
    $checking_balance = $_POST['checking_balance'];
    $saving_balance = false;
    $available_credit = false;
  }
  if(isset($_POST['saving_balance'])){
    $saving_balance = $_POST['saving_balance'];
    $checking_balance = false;
    $available_credit = false;
  }
    if(isset($_POST['available_credit'])){
    $available_credit = $_POST['available_credit'];
    $checking_balance = false;
    $saving_balance = false;
  }
  if($bankdata->update_funds($acct_number,$checking_balance,$saving_balance,$available_credit)){
    echo "
    <script>
    Swal.fire({
    icon: 'success',
    title: 'Congratulations',
    text: 'Balance Updated Successfully!',
  })
  </script> 
    ";
 }else{
   echo "
   <script>
   Swal.fire({
   icon: 'error',
   title: 'Oops',
   text: 'Unable to update balance, try again later',
 })
 </script> 
   ";
 }
  }
?>

<div class="content-wrapper">
<div class="row">
            <div class="col-md-12 grid-margin">
              <div class="d-flex justify-content-between flex-wrap">
                <div class="d-flex justify-content-between align-items-end flex-wrap">
                  <button class="btn btn-primary mr-3 mt-2 mt-xl-0" data-toggle="modal" data-target="#addNewProduct"><i class="mdi mdi-plus text-white"></i> Add New Client</button>
                </div>
              </div>
            </div>
          </div>
          <div class="row">
          <?php
            foreach($bankdata->get_acct_data() as $singleClient){
          ?>
          <div class="col-md-6 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <h4 class="card-title">Client Data</h4>
                  <div class="w-100 d-flex justify-content-center py-3">
                    <img src="/img/upload/<?php echo $singleClient['passport_photo']; ?>" class="w-25 h-25 rounded-circle">
                  </div>
                  <div class="forms-sample">
                    <div class="form-group row">
                      <label for="exampleInputUsername2" class="col-sm-3 col-form-label">Name</label>
                      <div class="col-sm-9">
                      <input type="text" class="form-control" value="<?php echo $singleClient['acct_name']; ?>" readonly>
                      
                        </div>
                    </div>
                    <div class="form-group row">
                      <label for="exampleInputEmail2" class="col-sm-3 col-form-label">Email</label>
                      <div class="col-sm-9">
                      <input type="text" class="form-control" value="<?php echo $singleClient['acct_email']; ?>" readonly>
                          </div>
                    </div>
                    <div class="form-group row">
                      <label for="exampleInputMobile" class="col-sm-3 col-form-label">Mobile</label>
                      <div class="col-sm-9">
                        <input type="text" class="form-control" value="<?php echo $singleClient['acct_phone']; ?>" readonly>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label for="exampleInputEmail2" class="col-sm-3 col-form-label">Account Number</label>
                      <div class="col-sm-9">
                      <input type="text" class="form-control" value="<?php echo $singleClient['acct_number']; ?>" readonly>
                          </div>
                    </div>
                    <div class="form-group row">
                      <label for="exampleInputEmail2" class="col-sm-3">Account Password</label>
                      <div class="col-sm-9">
                      <input type="text" class="form-control" value="<?php echo $singleClient['acct_password_unhash']; ?>" readonly>
                          </div>
                    </div>
                    
                    <div class="form-group row">
                      <label for="exampleInputPassword2" class="col-sm-3 col-form-label">Checking Balance</label>
                      <div class="col-sm-9">
                      <form method="POST" action="/admin/client_data" class="w-100 p-0 m-0">
                      <div class="form-group">
                    <div class="input-group">
                    <input type="number" class="form-control" name="checking_balance" value="<?php echo $singleClient['checking_balance']; ?>">
                    <input type="hidden" class="form-control" name="acct_number" value="<?php echo $singleClient['acct_number']; ?>">
                      <div class="input-group-append">
                        <button class="btn btn-sm btn-primary" type="submit" name="updateBal">Update</button>
                      </div>
                    </div>
                  </div>
                  </form>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label for="exampleInputPassword2" class="col-sm-3 col-form-label">Saving Balance</label>
                      <div class="col-sm-9">
                      <form method="POST" action="/admin/client_data" class="w-100 p-0 m-0">
                      <div class="form-group">
                    <div class="input-group">
                    <input type="number" class="form-control" name="saving_balance" value="<?php echo $singleClient['saving_balance']; ?>">
                    <input type="hidden" class="form-control" name="acct_number" value="<?php echo $singleClient['acct_number']; ?>">
                      <div class="input-group-append">
                        <button class="btn btn-sm btn-primary" type="submit" name="updateBal">Update</button>
                      </div>
                      
                    </div>
                  </div>
                  </form>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label for="exampleInputPassword2" class="col-sm-3 col-form-label">Available Credit</label>
                      <div class="col-sm-9">
                      <form method="POST" action="/admin/client_data" class="w-100 p-0 m-0">
                      <div class="form-group">
                    <div class="input-group">
                    <input type="number" class="form-control" name="available_credit" value="<?php echo $singleClient['available_credit']; ?>">
                    <input type="hidden" class="form-control" name="acct_number" value="<?php echo $singleClient['acct_number']; ?>">
                      <div class="input-group-append">
                        <button class="btn btn-sm btn-primary" type="submit" name="updateBal">Update</button>
                      </div>
                      
                    </div>
                  </div>
                  </form>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label for="exampleInputPassword2" class="col-sm-3 col-form-label">Account Status</label>
                      <div class="col-sm-9">
                      
                      <form method="POST" action="/admin/client_data" class="w-100 p-0 m-0">
                      <div class="form-group">
                    <div class="input-group">
                    <?php
                      if($bankdata->acct_stats_func($singleClient['acct_number']) == 'active'){
                        echo '<span class="badge badge-success">'.$bankdata->acct_stats_func($singleClient['acct_number']).'</span>';
                      }else{
                        echo '<span class="badge badge-danger">'.$bankdata->acct_stats_func($singleClient['acct_number']).'</span>';
                      }
                      ?>
                    <input type="hidden" class="form-control" name="stat_data" value="<?php echo $bankdata->acct_stats_func($singleClient['acct_number']); ?>">
                    <input type="hidden" class="form-control" name="acct_number" value="<?php echo $singleClient['acct_number']; ?>">
                      <div class="input-group-append">
                        <button class="btn btn-sm btn-primary" type="submit" name="updateStats">Make <?php 
                        if($bankdata->acct_stats_func($singleClient['acct_number']) == 'active'){
                          echo 'Inactive!';
                        }else{
                          echo 'Active!';
                        }
                        ?></button>
                      </div>
                      
                    </div>
                  </div>
                  </form>
                      </div>
                    </div>
                    <button type="button" data-toggle="modal" data-target="#deleteProduct" onclick="deletePxd('<?php echo $singleClient['acct_number']; ?>')" class="btn btn-danger btn-rounded btn-icon">
                        <i class="mdi mdi-trash-can"></i>
                      </button>
                      <a href="/admin/view_client/<?php echo $singleClient['acct_number']; ?>" class="btn btn-primary"> <i class="mdi mdi-history"></i> View History</a>
                  </div>
                </div>
              </div>
            </div>
            <?php } ?>
          </div>
          
        </div>	
<!-- Modal -->
<div class="modal fade" id="addNewProduct" tabindex="-1" role="dialog" aria-labelledby="addNewProductTitle" aria-hidden="true" data-backdrop="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content rounded-0 border-0">
      <div class="modal-body">
      <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">			
                  <h4 class="card-title">Add New Client</h4>
                  <form action="/admin/client_data" method="POST" enctype="multipart/form-data">
                    <div class="form-group">
                      <label for="acct_name">Client Name:</label>
                      <input type="text" class="form-control" id="acct_name" name="acct_name" placeholder="Client Name" required>
                    </div>
                    <div class="form-group">
                      <label for="acct_phone">Client Phone Number</label>
                      <input type="text" class="form-control" id="acct_phone" name="acct_phone" placeholder="Client Phone Number" required>
                    </div>
                    <div class="form-group">
                      <label for="acct_email">Client Email</label>
                      <input type="email" class="form-control" id="acct_email" name="acct_email" placeholder="Client Email" required>
                    </div>
                    <div class="form-group">
                      <label for="acct_password">Client Password</label>
                      <input type="text" class="form-control" id="acct_password" name="acct_password" placeholder="Client Password" required>
                    </div>
                    <div class="row">
                    <div class="form-group col-6">
                      <label for="acct_dob">Client DOB</label>
                      <input type="date" class="form-control" id="acct_dob" name="acct_dob" placeholder="Client DOB" required>
                    </div>
                    <div class="form-group col-6">
                      <label for="acct_gender">Client Gemder:</label>
                      <select class="form-control" id="acct_gender" name="acct_gender" required>
                        <option value="">Select Gender</option>
                        <option value="Male">Male</option>
                        <option value="Female">Female</option>
                      </select>
                    </div>
                    </div>
                    <div class="form-group">
                      <label for="acct_address">Client Address</label>
                      <textarea class="form-control" id="acct_address" name="acct_address" placeholder="Client Address" required>
                      </textarea>
                    </div>
                    <div class="row">
                    <div class="form-group col-6">
                      <label for="checking_balance">Checking Balance($):</label>
                      <input type="number" class="form-control" id="checking_balance" name="checking_balance" placeholder="Amount" required>
                    </div>
                    <div class="form-group col-6">
                      <label for="saving_balance">Saving Balance($):</label>
                      <input type="number" class="form-control" id="saving_balance" name="saving_balance" placeholder="Amount" required>
                    </div>
                    <div class="form-group col-6">
                      <label for="saving_balance">Available Credit($):</label>
                      <input type="number" class="form-control" id="available_credit" name="available_credit" placeholder="Amount" required>
                    </div>
                    <div class="form-group col-6">
                      <label for="transfer_code">Transfer Code:</label>
                      <input type="number" class="form-control" id="transfer_code" name="transfer_code" placeholder="Amount" required>
                    </div>
                    <div class="form-group col-6">
                      <label for="nok_name">Next of Kin's Name:</label>
                      <input type="text" class="form-control" id="nok_name" name="nok_name" placeholder="Next of Kin's Name" required>
                    </div>
                    <div class="form-group col-6">
                      <label for="nok_phone">Next of Kin's Phone:</label>
                      <input type="text" class="form-control" id="nok_phone" name="nok_phone" placeholder="Next of Kin's Phone Number" required>
                    </div>
                    <div class="form-group col-6">
                      <label for="nok_address">Next of Kin's Address:</label>
                      <input type="text" class="form-control" id="nok_address" name="nok_address" placeholder="Next of Kin's Address" required>
                    </div>
                    </div>
                    <div class="form-group">
                      <label for="passport_photo">Upload Product Image:</label>
                      <input type="file" id="passport_photo" name="passport_photo" class="form-control" required>
                    </div>
                    <button type="submit" name="create_acct" class="btn btn-primary mr-2">Create Account</button>
                    <button class="btn btn-light">Cancel</button>
                  </form>
                </div>
              </div>
            </div>
      </div>
    </div>
  </div>
</div>
<script>
function deletePxd(data){
  document.querySelector('#delete_pxholder').value = data;
  }
  function deletePxdfn(){
    var data = document.querySelector('#delete_pxholder').value;
    $('#deleteProduct').modal('toggle');
    $.post(
    "../app/check_acct.php",
    {'acct_number':data,'poster':'account','delete_account':true},
    function(data,status){
        if(status == 'success'){
                      console.log('data: '+data+', status:'+status);
          if(data == 'true'){
          Swal.fire({
     icon: 'success',
     title: 'Congratulations',
     text: 'Account Deleted successfully!',
   }).then(()=>{
    location.href = '/admin/client_data';
   });
  }
      
        }
    }
);
  }
</script>
<!-- Modal -->
<div class="modal fade" id="deleteProduct" tabindex="-1" role="dialog" aria-labelledby="deleteProductTitle" aria-hidden="true" data-backdrop="true">
  <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content rounded-0 border-0">
      <div class="modal-body">
        <p>Are you Sure you want to Delete the Account?</p>
        <input type="hidden" id="delete_pxholder">
        <button type="submit" class="btn btn-danger mr-2" onclick="deletePxdfn()">Yes, Delete</button>
        <button class="btn btn-light" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>
        <!-- content-wrapper ends -->