<?php foreach($kontak as $kontak) :   endforeach; ?>
<?php foreach($admin_login as $user) :   endforeach; ?> 
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark"> 
                 <?php echo $title1; ?>
            </h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active"><?php echo $title1; ?></li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
      
          
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
          <div class="card">
              <div class="card-header">
               
                <form method="get" action="<?php echo base_url('bukukas'); ?>">
                  <div class="row">
                    <div class="col-md-3">
                      <label>Tanggal Awal</label>
                      <input type="date" name="start_date" class="form-control" value="<?php echo isset($_GET['start_date']) ? $_GET['start_date'] : date('Y-m-01'); ?>">
                    </div>
                    <div class="col-md-3">
                      <label>Tanggal Akhir</label>
                      <input type="date" name="end_date" class="form-control" value="<?php echo isset($_GET['end_date']) ? $_GET['end_date'] : date('Y-m-t'); ?>">
                    </div>
                    <div class="col-md-3" style="margin-top: 25px">
                      <button type="submit" class="btn btn-primary">Filter</button>
                      <a href="<?php echo base_url('bukukas'); ?>" class="btn btn-default">Reset</a>
                    </div>
                  </div>
                </form>
                <br>
                <button type="button" class="btn btn-info" data-toggle="modal" data-target="#modal-default">
                  <i class="fa fa-plus"></i> Tambah Data 
                </button>


                <?php
                    // Ambil parameter tanggal
                    $start_date = $this->input->get('start_date') ? $this->input->get('start_date') : date('Y-m-01');
                    $end_date = $this->input->get('end_date') ? $this->input->get('end_date') : date('Y-m-t');

                    $query1 = $this->db->query("SELECT SUM(nominal) as pemasukan FROM tb_bukukas WHERE jenis='pemasukan' AND date BETWEEN '$start_date' AND '$end_date'"); 
                    $row1 = $query1->row_array();
                    $pemasukan = $row1['pemasukan'] ? $row1['pemasukan'] : 0;

                    $query2 = $this->db->query("SELECT SUM(nominal) as pengeluaran FROM tb_bukukas WHERE jenis='pengeluaran' AND date BETWEEN '$start_date' AND '$end_date'"); 
                    $row2 = $query2->row_array();
                    $pengeluaran = $row2['pengeluaran'] ? $row2['pengeluaran'] : 0;
                  ?>

              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <table   class="table table-bordered ">
                  <thead>
                    <tr style="background-color: #c6c2c2">
                      <th>No</th>
                      <th>Date</th>
                      <th>Petugas</th>
                      <th>Keterangan</th>
                      <th>Pemasukan </th>
                      <th>Pengeluaran</th>

                      <th>Aksi</th> 
                     
                      
                    </tr>
                  </thead>
                  <tbody>
                    <?php foreach($data->result()  as $rows) : 

                  
                     
                    ?>  
                      <tr>
                        <td><?php echo $no; ?></td>
                       
                        <td><?php echo $rows->date;  ?></td>
                         <td> 
                        <?php 
                        $id_admin = $rows->id_admin; 
                        $sql_menu = "SELECT * FROM tb_admin WHERE  id = '$id_admin' ";
                        $main_menu = $this->db->query($sql_menu)->result();
                        foreach ($main_menu as $maink) {
                          echo  $maink->nama_admin;

                        }
                        ?>
                      </td>
                        <td><?php echo $rows->keterangan;  ?></td>
                        <td style="text-align: right"><?php if($rows->jenis == 'pemasukan'){ echo nominal($rows->nominal); }else{ echo "-"; } ?></td> 
                        <td style="text-align: right"><?php if($rows->jenis == 'pengeluaran'){ echo nominal($rows->nominal); }else{ echo "-"; } ?></td>
                        <td>
                            <a href="" style="color: red;"  type="button"  data-toggle="modal" data-target="#modal-hapus<?php echo $rows->id_bukukas; ?>"><i class="fa fa-trash"></i></a>
                           
                              <form class="form-horizontal" action="<?php echo base_url('bukukas/code_hapus'); ?>" method="POST" enctype="multipart/form-data" />  
                                <div class="modal fade" id="modal-hapus<?php echo $rows->id_bukukas; ?>">
                                  <div class="modal-dialog">
                                    <div class="modal-content bg-danger">
                                      <div class="modal-header">
                                        <h4 class="modal-title">Hapus Data </h4>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                          <span aria-hidden="true">&times;</span>
                                        </button>
                                      </div>
                                      <div class="modal-body">
                                        <h4>Apakah Yakin Menghapus Data Ini, ?</h4>
                                        <!-- id kirim -->
                                        <input type="hidden" name="id_bukukas" value="<?php echo $rows->id_bukukas; ?>">


                                      </div>
                                      <div class="modal-footer justify-content-between">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-primary">Hapus</button>
                                      </div>
                                    </div>
                                    <!-- /.modal-content -->
                                  </div>
                                  <!-- /.modal-dialog -->
                                </div>
                              </form>
                        </td>
                          
                      </tr>
                    <?php $no++; endforeach; ?>

                    <?php

                    date_default_timezone_get('Asia/Jakarta');
                    $bulan = date('m');
                    $tahun = date('Y');
                    $this->db->order_by('date', 'desc');
                    // $this->db->where('month(tb_bukukas.date)',$bulan);
                    // $this->db->where('year(tb_bukukas.date)',$tahun); 
                    $query3 = $this->db->get('tb_bukukas');

                        $query1 = $this->db->query("SELECT SUM(nominal) as pemasukan FROM tb_bukukas where jenis='pemasukan' and month(tb_bukukas.date)='$bulan' and year(tb_bukukas.date) ='$tahun' "); 
                        $row1 = $query1->row_array();
                        $pemasukan = $row1['pemasukan'];
                        $query2 = $this->db->query("SELECT SUM(nominal) as pengeluaran FROM tb_bukukas where jenis='pengeluaran' and month(tb_bukukas.date)='$bulan' and year(tb_bukukas.date) ='$tahun' "); 
                        $row2 = $query2->row_array();
                        $pengeluaran = $row2['pengeluaran'];
                     ?>
                    <tr>
                      <td colspan="4">Total</td>
                      <td style="text-align: right"><strong><?php echo nominal($pemasukan);?></strong></td>
                      <td style="text-align: right"><strong><?php echo nominal($pengeluaran);?></strong></td>
                    </tr>
                    <tr>
                      <td colspan="4">Saldo</td>
                      <td style="text-align: right" colspan="2"> </td>
                      <td style="text-align: right"><strong><?php echo nominal($pemasukan - $pengeluaran);?></strong></td>
                    </tr>

                  </tbody>

                </table>
                <br>
                <div class="row">
                  <div class="col">
                    <!--Tampilkan pagination-->
                    <?php echo $pagination; ?>
                  </div>
                </div>
              </div>
              <!-- /.card-body -->
            </div>
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->



 <form class="form-horizontal" action="<?php echo base_url('bukukas/code_tambah'); ?>" method="POST" enctype="multipart/form-data" />  
        <div class="modal fade" id="modal-default">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Tambah Data</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                     
                        <div class="form-group">
                            <label for="exampleInputEmail1">Keterangan</label>
                            <input type="text" class="form-control" id="" name="keterangan" placeholder="Masukkan Keterangan">
                         
                        </div>
                        <div class="form-group">
                            <label for="exampleInputEmail1">Nominal</label>
                            <input type="number" class="form-control" id="" name="nominal" placeholder="Masukkan Nominal ">
                         
                        </div>
                        <div class="form-group">
                            <label for="exampleInputEmail1">Jenis Kas</label>
                            <select class="form-control" name="jenis">
                                <option value="pemasukan">Pemasukan</option>
                                <option value="pengeluaran">Pengeluaran</option>
                            </select>
                         
                        </div>
                        <div class="form-group">
                            <label for="exampleInputEmail1">Tanggal</label>
                            <input type="text" id="tbDate1"  class="form-control"   name="date" placeholder="Masukkan Nominal ">
                         
                        </div>
                        <input type="hidden" name="id_admin" value="<?php echo $id_admin_login; ?>">
                     
                    </div>
                        <div class="modal-footer justify-content-between">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="submit" class="btn btn-primary">Simpan</button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal-dialog -->
        </div>
    </form>

   