<?php session_start(); include"../connect/db.php" ?>    
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Admin| Users</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Font Awesome -->
 <!-- Font Awesome -->
 <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- DataTables -->
  <link rel="stylesheet" href="../plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="../plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">
  <!-- Google Font: Source Sans Pro -->
  <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
  <!-- Navbar -->
  <nav class="main-header navbar navbar-expand navbar-white navbar-light">
    <!-- Left navbar links -->
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
      </li>
      <li class="nav-item d-none d-sm-inline-block">
       
      </li>
      <li class="nav-item d-none d-sm-inline-block">
   
      </li>
    </ul>

    <!-- SEARCH FORM -->
    
    <!-- Right navbar links -->
    <ul class="navbar-nav ml-auto">
      <!-- Messages Dropdown Menu -->
      
      <!-- Notifications Dropdown Menu -->
      
      <li class="nav-item">
        <a class="nav-link" data-widget="control-sidebar" data-slide="true" href="#" role="button">
          <i class="fas fa-th-large"></i>
        </a>
      </li>
    </ul>
  </nav>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->


  <?php           


?> 
  <aside class="main-sidebar sidebar-dark-primary elevation-4">
    <!-- Brand Logo -->
    <a href="index3.html" class="brand-link">
      <img src="dist/img/AdminLTELogo.png"
           alt="AdminLTE Logo"
           class="brand-image img-circle elevation-3"
           style="opacity: .8">
      <span class="brand-text font-weight-light">Admin</span>
    </a>

    <!-- Sidebar -->

    <?php include"includes/sidebar.php" ?>        
 
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Transactions</h1>
            
          <div class="col-sm-6">
        
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
   
      <div class="container-fluid">
        
     
        <div class="row">
        
       
          
          <div class="col-12">
        
          <span  data-toggle="modal" data-target="#depositModal" class="btn btn btn-success btn-md">Create Deposit <i class="fa fa-plus"></i></span>
          <span  data-toggle="modal" data-target="#withdrawModal" class="btn btn btn-success btn-md">Create Withdrawals <i class="fa fa-plus"></i></span>
        <span  data-toggle="modal" data-target="#generateModal" class="btn btn btn-danger btn-md">Generate Transactions <i class="fa fa-plus"></i></span>

            <!-- /.card -->
            <!-- /.card -->

            <div class="card">
              <div class="card-header">
                <h3 class="card-title"><?php
                $maga = mysqli_real_escape_string($con, $_GET['user'] ?? '');
                echo htmlspecialchars($maga ?: 'All') ?> Transactions</h3>
              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <table id="example1" class="table table-bordered table-striped ">
                  <thead>
                  <tr>
                                                        <th scope="col">#</th>
                                                        <th scope="col">Trans ID</th>
                                                        <th scope="col">Date</th>
                                                        <th scope="col">DESC</th>
                                                        
                                                        <th scope="col">Status</th>
                                                        <th scope="col">View Receipt</th>

                                                         <th scope="col">Edit</th>
                                                      
                                                        <th scope="col">Delete</th>
                                                    </tr>     
                  </thead>
                  <tbody> 
                  <?php
                                                        $tosin = mysqli_query($con,"SELECT * FROM transactions WHERE user = '$maga' ORDER BY id DESC");
                                                        $transactions = mysqli_num_rows($tosin);
                                                        if( $transactions == 0 ){
                                                            echo'
                                                                <tr>
                                                                    <td colspan="8">Oops! No Transaction have been made</td>
                                                                </tr>
                                                            '
                                                            ;
                                                        }
                                                        else{
                                                            $i = 1;
                                                            while( $tosin_req = mysqli_fetch_assoc($tosin) ){
                                                                echo'
                                                                <tr>
                                                                <td>'.$i.'</td>
                                                                <td>'.$tosin_req['trans_id'].'</td>
                                                                <td>'.$tosin_req['date'].'</td>
                                                                <td>'.$tosin_req['description'].'</td>
                                                                <td scope="row">'.$tosin_req['status'].'</td>
                                                                                                                                  <td><a href="receipt.php?id='.$tosin_req['trans_id'].'" ><span class="btn btn-md btn-info" ><i class="fa fa-eye" >Generate Receipt</i></span></a></td> 

                                                                  <td><a href="edittrans.php?id='.$tosin_req['trans_id'].'" ><span class="btn btn-md btn-info" ><i class="fa fa-plus" >Edit</i></span></a></td> 
                                                                <td><span onclick={deleteTrans("'.$tosin_req['trans_id'].'")} class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></span></td>
                                                                </tr>
                                                                '
                                                                ;
                                                                $i++ ;
                                                            }                                                    
                                                        }
                                                    ?>                   
                                                </tbody>
                  <tfoot>
                  <tr>
                  <th scope="col">#</th>
                                                        <th scope="col">Trans ID</th>
                                                        <th scope="col">Date</th>
                                                        <th scope="col">DESC</th>
                                                      
                                                        <th scope="col">Status</th>
                                                      
                                                        <th scope="col">View Receipt</th>
                                                       <th scope="col">Edit</th>
                                                       
                                                        <th scope="col">Delete</th>
                                                    </tr>
                  </tfoot>
                </table>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>
        <!-- /.row -->
      </div>
      <!-- /.container-fluid -->
    </section>
      <!-- MODAL CREATE TRANSACTION -->
      <div class="modal fade" id="withdrawModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <?php

              $maga = mysqli_real_escape_string($con, $_GET['user'] ?? '');
          $vanila = mysqli_query($con,"SELECT * FROM transactions  WHERE user = '$maga' ");
          $crow = mysqli_fetch_assoc($vanila);
          ?>
              <div class="modal-body">
              <div class="card">
                <div class="card-body">
                    <div class="login-content">
                        <div class="login-logo">
                            <a href="#">
                            </a>
                        </div>


                        <div class="login-form">
                            <form>
                                <div class="form-group">
                                    <label for="acct_num">Account Number</label>
                                    <input   class="form-control" value="<?=$maga?>"  type="text" name="acct_num" readonly>
                                </div>
                                <div class="form-group">
                                    <label for="amount">Amount</label>
                                    <input class="form-control" type="text" name="amount">
                                </div>
                                <div class="form-group">
                                    <label for="depositname">Transaction Description</label>

                                    <textarea name="depositname" class="form-control" id="depositname" cols="30" rows="10"></textarea>


                                </div>

                                         <div class="form-group">

                                    <input  class="form-control" type="hidden" name="currency" id="currency" value="null" placeholder="2020-10-25">
                                </div>
                                <div class="form-group">
                                    <label for="d_o_t">Date</label>
                                    <input  class="form-control" type="date" name="d_o_t" placeholder="2020-10-25">
                                </div>


                                <div class="form-group">
                                    <label for="status">Status</label>

                                    <select name="status" id="status" class="form-control">
                                        <option value="Completed">Completed</option>
                                                                                <option value="Pending">Pending</option>

                                        <option value="Declined">Declined</option>

                                    </select>
                                  
                                    
                                    
                                     
                                <div class="form-group">
    <label>
        <input type="checkbox" name="alert_settings1" id="alert_settings1"> Enable Email Alerts
    </label>
</div>
<script>
document.getElementById("alert_settings1").addEventListener("change", function () {
    if (this.checked) {
        Swal.fire({
            title: "Email Alert Enabled",
            html: "This action will make the user receive an email alert.<br>If the alert is not found in the inbox, please check the spam folder.",
            icon: "info",
            showConfirmButton: false, // no button
            timer: 5000 // auto close after 5 seconds
        });
    }
});
</script>                       
                                </div>                                
                                <span onclick="createwithdraw()" style="text-align:center; background-color:#03d0ea;" class="btn btn-sucess">Submit</span>
                            </form>    
                        </div>
                    </div>                            
                </div>
            </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              </div>
            </div>
            
          </div>
          
        </div>  
        
        
        
          </div>  
           <!-- MODAL CREATE TRANSACTION -->
      <div class="modal fade" id="depositModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <?php           

              $maga = mysqli_real_escape_string($con, $_GET['user'] ?? '');
          $vanila = mysqli_query($con,"SELECT * FROM transactions  WHERE user = '$maga' ");
          $crow = mysqli_fetch_assoc($vanila);
          ?> 
              <div class="modal-body">
              <div class="card">
                <div class="card-body">
                    <div class="login-content">
                        <div class="login-logo">
                            <a href="#">
                            </a>
                        </div>
                                                     
                                                        
                        <div class="login-form">
                            <form>                                
                                <div class="form-group">
                                    <label for="acct_num">Account Number</label>
                                    <input   class="form-control" value="<?=$maga?>"  type="text" name="acct_numx" readonly>
                                </div>
                                <div class="form-group">
                                    <label for="amount">Amount</label>
                                    <input class="form-control" type="text" name="amountx">
                                </div>
                                <div class="form-group">
                                    <label for="depositname">Transaction Description</label>
                                    
                               <textarea name="depositnamex" class="form-control" id="depositnamex" cols="30" rows="10"></textarea>

                                    
                                    
                                 
                                </div>
                                       <div class="form-group">
                                    
                                    <input  class="form-control" type="hidden" name="currencyx" id="currencyx" value="null" placeholder="2020-10-25">
                                </div>    
                                                            
                                <div class="form-group">
                                    <label for="d_o_t">Date</label>
                                    <input  class="form-control" type="date" name="d_o_tx" placeholder="2020-10-25">
                                </div>
                                            
                   
                                <div class="form-group">
                                    <label for="status">Status</label>
                                  <select name="statusx" id="statusx" class="form-control">
                                        <option value="Completed">Completed</option>
                                                                                <option value="Pending">Pending</option>

                                        <option value="Declined">Declined</option>

                                    </select>
                                </div>         
                                             <div class="form-group">
    <label>
        <input type="checkbox" name="alert_settings" id="alert_settings"> Enable Email Alerts
    </label>
</div>
<script>
document.getElementById("alert_settings").addEventListener("change", function () {
    if (this.checked) {
        Swal.fire({
            title: "Email Alert Enabled",
            html: "This action will make the user receive an email alert.<br>If the alert is not found in the inbox, please check the spam folder.",
            icon: "info",
            showConfirmButton: false, // no button
            timer: 5000 // auto close after 5 seconds
        });
    }
});
</script>                       
                                <span onclick="createdeposit()" style="text-align:center; background-color:#03d0ea;" class="btn btn-sucess">Submit</span>
                            </form>    
                        </div>
                    </div>                            
                </div>
            </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              </div>
            </div>
          </div>
        </div> 
        </div>
  <!-- /.content-wrapper -->

   <div class="modal fade" id="generateModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <?php           

              $maga = mysqli_real_escape_string($con, $_GET['user'] ?? '');
          $vanila = mysqli_query($con,"SELECT * FROM transactions  WHERE user = '$maga' ");
          $crow = mysqli_fetch_assoc($vanila);
          ?> 
              <div class="modal-body">
              <div class="card">
                <div class="card-body">
                    <div class="login-content">
                        <div class="login-logo">
                            <a href="#">
                            </a>
                        </div>
                                                     
                                                        
                        <div class="login-form">
                            <form>                                
                                <div class="form-group">
                                    <label for="acct_num">Account Number</label>
                                    <input   class="form-control" value="<?=$maga?>"  type="text" name="acct_numx" readonly>
                                </div>
                              
                             
                                                            
                                <div class="form-group">
    <label for="date-range">Date Range</label>
    <div style="display: flex; gap: 10px;">
        <input class="form-control" type="date" name="date_from" placeholder="From Date">
        <input class="form-control" type="date" name="date_to" placeholder="To Date">
    </div>
</div>

                                                           
                               <div class="form-group">
    <label for="status_number">Number of Transactions</label>
    <input type="number" name="status_number" id="status_number" class="form-control" min="0" step="1" value="0">
</div>
<div class="form-group">
    <label>Transaction Types</label><br>

    <!-- Popular Stores -->
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Walmart" id="walmart">
        <label class="form-check-label" for="walmart">Walmart</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Amazon" id="amazon">
        <label class="form-check-label" for="amazon">Amazon</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Target" id="target">
        <label class="form-check-label" for="target">Target</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Costco" id="costco">
        <label class="form-check-label" for="costco">Costco</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Best Buy" id="bestbuy">
        <label class="form-check-label" for="bestbuy">Best Buy</label>
    </div>

    <!-- Transaction Descriptions -->
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Utilities Bill" id="utilities">
        <label class="form-check-label" for="utilities">Utilities Bill</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Credit Card Payment" id="credit_card">
        <label class="form-check-label" for="credit_card">Credit Card Payment</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Mortgage Payment" id="mortgage">
        <label class="form-check-label" for="mortgage">Mortgage Payment</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Car Loan" id="car_loan">
        <label class="form-check-label" for="car_loan">Car Loan</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Medical Expense" id="medical">
        <label class="form-check-label" for="medical">Medical Expense</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Dining Out" id="dining">
        <label class="form-check-label" for="dining">Dining Out</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Subscription Services" id="subscription">
        <label class="form-check-label" for="subscription">Subscription Services</label>
    </div>

    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Gas Station" id="gas">
        <label class="form-check-label" for="gas">Gas Station</label>
    </div>
    <div class="form-check">
    <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Machine Maintenance" id="machine_maintenance">
    <label class="form-check-label" for="machine_maintenance">Machine Maintenance</label>
</div>

<div class="form-check">
    <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Machine Purchase" id="machine_purchase">
    <label class="form-check-label" for="machine_purchase">Machine Purchase</label>
</div>

<div class="form-check">
    <input class="form-check-input" type="checkbox" name="transaction_types[]" value="Company Expenses" id="company_expenses">
    <label class="form-check-label" for="company_expenses">Company Expenses</label>
</div>

</div>

                                
                                <span onclick="clonetransactions()" style="text-align:center; background-color:#03d0ea;" class="btn btn-sucess">Submit</span>
                            </form>    
                            <script>
function clonetransactions() {
    let acct_numx = document.querySelector('[name="acct_numx"]').value;
    let date_from = document.querySelector('[name="date_from"]').value;
    let date_to = document.querySelector('[name="date_to"]').value;
    let status_number = document.querySelector('[name="status_number"]').value;

    // Get all checked transaction types
    let checkedBoxes = document.querySelectorAll('input[name="transaction_types[]"]:checked');
    let types = [];
    checkedBoxes.forEach(cb => types.push(cb.value));

    if (types.length === 0) {
        alert('Please select at least one transaction type.');
        return;
    }

    if (!date_from || !date_to || !status_number || status_number <= 0) {
        alert('Fill all required fields.');
        return;
    }

    const formData = new FormData();
    formData.append('acct_numx', acct_numx);
    formData.append('date_from', date_from);
    formData.append('date_to', date_to);
    formData.append('status_number', status_number);
    formData.append('transaction_types', JSON.stringify(types));

    fetch('../apps/man/create_transaction.php', {
        method: 'POST',
        body: formData
    })
    .then(res => res.text())
    .then(data => {
        alert(data); 
            setTimeout(function(){ location.reload()} , 3200);
        // Show confirmation or response
    })
    .catch(err => {
        console.error(err);
        alert("An error occurred");
    });
}
</script>

                        </div>
                    </div>                            
                </div>
            </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              </div>
            </div>
          </div>
        </div> 
        </div>
  <!-- /.content-wrapper -->




  <footer class="main-footer">
    <div class="float-right d-none d-sm-block">
      <b>Version</b> 1.0.0
    </div>
    <strong>Copyright &copy; 2021 <a href="#">Swagalomo's Admin</a>.</strong> All rights
    reserved.
  </footer>

  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- DataTables -->
<script src="../plugins/datatables/jquery.dataTables.min.js"></script>
<script src="../plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="../plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="../plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
<!-- page script -->

<script>
  $(function () {
    $("#example1").DataTable({
      "responsive": true,
      "autoWidth": false,
    });
    $('#example2').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": true,
      "autoWidth": false,
      "responsive": true,
    });
  });
</script>

<script src="js/sweetalert2.all.min.js" ></script>

<!-- Main JS-->
<script src="js/main.js"></script>
<script src="js/custom.js?v=1.93348"></script>
<script>

        function readURL(input) {
          if (input.files && input.files[0]) {
            var reader = new FileReader();
            
            reader.onload = function(e) {
              $('#blah').attr('src', e.target.result);
            }
            
            reader.readAsDataURL(input.files[0]);
          }
        }

        $("#profile-pic").change(function() {
          readURL(this);
        });

        function readURL1(input) {
          if (input.files && input.files[0]) {
            var reader = new FileReader();
            
            reader.onload = function(e) {
              $('#blah1').attr('src', e.target.result);
            }
            
            reader.readAsDataURL(input.files[0]);
          }
        }

        $("#profile-pic-update").change(function() {
          readURL(this);
        });
    function sly(x){
        x.type='date';
    }
</script>    
</body>
</html>
