<?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> 
    ";
  }
}
//DELETE USER  =======================================================================================================
if(isset($_REQUEST['delete_acct_number'])){
        if($bankdata->delete_acct($_REQUEST['delete_acct_number'])){
          // print_r($_REQUEST['selected_cur']);
          echo "
          <script>
          Swal.fire({
          icon: 'success',
          title: 'Congratulations',
          text: 'Account Deleted Successfully!',
        })
        </script> 
          ";
       }else{
         echo "
         <script>
         Swal.fire({
         icon: 'error',
         title: 'Oops',
         text: 'Unable to Delete Account!',
       })
       </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">
        <div class="col-lg-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <h4 class="card-title">Bank Accounts</h4>
                  <p class="card-description">
                    Account List
                  </p>
                  <div class="table-responsive">
                    <table id="client_list" class="table table-striped">
                      <thead>
                        <tr>
                          <th>
                            photo
                          </th>
                          <th>
                            Full name
                          </th>
                          <th>
                            Email
                          </th>
                          <th>
                            Status
                          </th>
                        <th>
                            Balance
                          </th>
                          <th>
                            Account Number
                          </th>
                          <!--<th>-->
                          <!--  Action-->
                          <!--</th>-->
                        </tr>
                      </thead>
                      <tbody>
        <?php
            foreach($bankdata->get_acct_data() as $singleClient){
          ?>
                        <tr style="cursor:pointer;" onclick="location.href = '/admin/view_client/<?php echo $singleClient['acct_number']; ?>';">
                          <td class="py-1">
                            <img src="/img/upload/<?php echo $singleClient['passport_photo']; ?>" alt="image"/>
                          </td>
                          <td>
                            <?php echo $singleClient['acct_name']; ?>
                          </td>
                          <td>
                            <?php echo $singleClient['acct_email']; ?>
                          </td>
                          <td>
                        <?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>';
                      }
                      ?>
                          </td>
                          <td>
                              <?php echo number_format($singleClient['checking_balance'],2); ?>
                          </td>
                          <td>
                            <?php echo $singleClient['acct_number']; ?>
                          </td>
                          
                          <!--<td>-->
                          <!--  <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>-->
                          <!--</td>-->
                        </tr>
                        <?php } ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          
            
          </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="<?php echo $_SERVER['REQUEST_URI']; ?>" 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 Gender:</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">Main 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="account_type">Account type:</label>
												        <select name="account_type" class="form-control" id="account_type" required>
                                                          <option value="" selected>Select option</option>
                                                           <option value="Savings">Savings Account</option>
                                                          <option value="Checkings">Checkings Account</option>
                                                          <option value="Onshore">Onshore Account</option>
                                                          <option value="Offshore">Offshore Account</option>
                                                         <option value="Joint">Joint Account</option>
                                                         <option value="Corporate">Corporate Account</option>
                                                         </select>
                    </div>
                    <div class="form-group col-6">
                      <label for="active_currency">Currency:</label>
                    <select name="active_currency" class="form-control" id="active_currency" required>
                    <option value="" selected>Select option</option>
                    <option value="&#8364;">Euro</option>
                    <option value="&#163;">Pounds sterling</option>
                    <option value="&#36;">United States Dollar</option>
                    <option value="&yen">Chinese yuan</option>	
					</select>
                    </div>
                    <div class="form-group col-6">
                      <label for="transfer_code">Set Login Pin:</label>
                      <input type="number" class="form-control" id="transfer_code" name="transfer_code" placeholder="XXXX" 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-12">
                      <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 Client Passport:</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 -->