<?php
    function rupiah($angka){
	
        $hasil_rupiah = "Rp " . number_format($angka,2,',','.');
        return $hasil_rupiah;
     
    }
?>
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="<?php echo base_url()?>assets/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="<?php echo base_url()?>assets/css/dataTables.bootstrap4.min.css">
    <link data-require="sweet-alert@*" data-semver="0.4.2" rel="stylesheet" href="<?php echo base_url()?>assets/css/sweetalert.min.css" />

    <link rel="icon" type="image/png" href="../../assets/images/favicon.png" />

    <title>Aplikasi UMKM</title>
  </head>
  <body>

        <nav class="navbar navbar-expand-lg navbar-dark bg-primary">    
            <div class="container">
                <a class="navbar-brand" href="#">
                    <img src="<?php echo base_url()?>assets/images/logo-white.png" width="100" alt="">
                </a>
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>

                    <div class="collapse navbar-collapse" id="navbarSupportedContent">
                        <ul class="navbar-nav mr-auto">
                        
                        </ul>
                        <form methode="POST" action="../login/logout" class="form-inline my-2 my-lg-0" >
                        <button class="btn btn-outline-warning btn-sm my-2 my-sm-0" type="submit">Logout</button>
                        </form>
                    </div>
             </div>
        </nav>

    <div class="container">
       <div class="row">
           <div class="col-md-3" style="cursor:pointer;">
                <div class="card" style="width: 18rem;">
                <div class="card-header text-center">
                    <img src="<?php echo base_url()?>assets/images/<?php echo $user_logged->foto_user ?>" width="125" /><br>
                    <strong><?php echo $user_logged->nama ?></strong>
                </div>
                    <ul class="list-group list-group-flush">
                    <a href="<?php echo base_url('index.php/')?>admin/dashboard" style="text-decoration:none;">
                        <li class="list-group-item text-dark border-bottom">Data Pemilik UMKM</li>
                    </a>
                    <a href="<?php echo base_url('index.php/')?>admin/umkm" style="text-decoration:none;">
                        <li class="list-group-item text-dark border-bottom">Data UMKM</li>
                    </a>
                    <a href="<?php echo base_url('index.php/')?>admin/absensi" style="text-decoration:none;">
                        <li class="list-group-item bg-primary text-white border-bottom">Absensi</li>
                    </a>
                    <a href="<?php echo base_url('index.php/')?>admin/laporan" style="text-decoration:none;">
                        <li class="list-group-item text-dark border-bottom">Laporan</li>
                    </a>
                    <a href="<?php echo base_url('index.php/')?>admin/setting" style="text-decoration:none;">
                        <li class="list-group-item text-dark">Setting</li>
                    </a>
                    </ul>
                </div>
           </div>
           <div class="col-md-9">
                <div class="card">
                    <h5 class="card-header">Absensi</h5>
                    <div class="card-body">
                        <div>
                        <ul class="nav nav-tabs">
                            <li class="nav-item">
                                <a href="<?php echo base_url('index.php/')?>admin/absensi" class="nav-link">Absen Aktif</a>
                            </li>
                            <li class="nav-item">
                                <a href="<?php echo base_url('index.php/')?>admin/daftarabsen" class="nav-link active">Data Absensi</a>
                            </li>
                            <li class="nav-item">
                                <a href="<?php echo base_url('index.php/')?>admin/progresabsen" class="nav-link">Progres Absensi</a>
                            </li>
                            
                        </ul>

                        <div class="mt-3">
                            <div>
                                <form action="" method="POST" class="needs-validation">
                                    <div class="form-group">
                                        <label for="tahun">Pilih Tahun</label>
                                        <select name="tahun" class="form-control" id="tahun" onchange="this.form.submit()" required>
                                            <?php foreach($tahun_list as $list) {?>
                                                <option value="<?php echo $list->tahun ?>" <?php if($list->tahun===$tahun){echo "selected";}?>>
                                                    <?php echo $list->tahun ?>
                                                </option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </form>
                            </div>
                            <?php 
                                if (isset($del_msg)){
                                    if($del_msg=='error'){
                            ?>
                            <div class="alert alert-danger" role="alert">Tidak bisa menghapus absen bulan ke <strong><?php echo $del_absen ?></strong>
                            tahun <strong><?php echo $tahun ?></strong>, kerena terdapat data absensi UMKM yang aktif !</div>
                            <?php } elseif($del_msg=='errorlaporan'){ ?>
                                <div class="alert alert-danger" role="alert">
                                    Tidak bisa menghapus absen bulan ke <strong><?php echo $del_absen ?></strong> tahun  <strong><?php echo $tahun ?></strong>
                                    , kerena absen untuk tahun  <strong><?php echo $tahun ?></strong> sudah ditutup !
                                </div>
                            <?php } elseif($del_msg=='sucess'){ ?>
                            <div class="alert alert-success" role="alert">Data berhasil terhapus !</div>
                            <?php }}?>

                            <?php 
                                $jumlahlaporan = $laporanmodel->getLaporanCountByYear($tahun);
                                if($jumlahlaporan>0){
                                    $notifikasi = true;
                                } else {
                                    $notifikasi = false;
                                }
                            ?>

                            <?php if($notifikasi){?>
                                <div class="alert alert-warning" role="alert">
                                    Absensi untuk tahun <strong><?php echo $tahun ?></strong> sudah <strong>DITUTUP</strong>, 
                                    Anda <strong>tidak bisa</strong> melakukan operasi apapun terhadap absensi yang sudah ditutup !
                                </div>
                            <?php } ?>
                            
                            <table id="tabeldata" class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>Bln</th>
                                        <th>Jml Absensi</th>
                                        <th>Jml UMKM</th>
                                        <th>Rata-rata Aset</th>
                                        <th>Rata-rata Omset/Bln</th>
                                        <?php if(!$notifikasi){?>
                                            <th>Status</th>
                                        <?php } ?>
                                        <th>Opsi</th>

                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                     foreach($data_absen as $absen) {
                                     
                                    ?>
                                    <tr>
                                        <?php 
                                        
                                        if($absen->bulan==1){
                                            $bulan = 'Januari';
                                        } elseif($absen->bulan==2){
                                            $bulan = 'Februari';
                                        } elseif($absen->bulan==3){
                                            $bulan = 'Maret';
                                        } elseif($absen->bulan==4){
                                            $bulan = 'April';
                                        } elseif($absen->bulan==5){
                                            $bulan = 'Mei';
                                        } elseif($absen->bulan==6){
                                            $bulan = 'Juni';
                                        } elseif($absen->bulan==7){
                                            $bulan = 'Juli';
                                        } elseif($absen->bulan==8){
                                            $bulan = 'Agustus';
                                        } elseif($absen->bulan==9){
                                            $bulan = 'September';
                                        } elseif($absen->bulan==10){
                                            $bulan = 'Oktober';
                                        } elseif($absen->bulan==11){
                                            $bulan = 'November';
                                        } elseif($absen->bulan==12){
                                            $bulan = 'Desember';
                                        } else {
                                            $bulan = 'a/n';
                                        }
                                        
                                        ?>
                                        <td><?php echo $bulan?></td>

                                        <?php 
                                            
                                            $jumlahabsensi = $model->getSignedAbsensiCountByAbsenId($absen->id_absen);
                                        ?>


                                        <td><?php echo $jumlahabsensi?></td>
                                        
                                        <td><?php echo $absen->jumlah_umkm ?></td>
                                        <?php
                                            if($absen->jumlah_umkm > 0){
                                                $ratarataaset = ($absen->aset/$absen->jumlah_umkm);
                                            } else {
                                                $ratarataaset = 0;
                                            }
                                        ?>
                                        <td><?php echo rupiah($ratarataaset) ?></td>
                                        <?php
                                            if($absen->jumlah_umkm > 0){
                                                $ratarataomset =($absen->omset/$absen->jumlah_umkm);
                                            } else {
                                                $ratarataomset = 0;
                                            }
                                        ?>
                                        <td><?php echo rupiah($ratarataomset) ?></td>
                                        <?php if(!$notifikasi){?>
                                            <td>
                                                <?php if($absen->status_absen=='open'){?>
                                                    <span class="badge badge-pill badge-success">Active</span>
                                                <?php } else {?>
                                                    <span class="badge badge-pill badge-secondary">Inactive</span>
                                                <?php } ?>
    
                                            </td>
                                        <?php } ?>
                                        <td>
                                            <a href="<?php echo base_url('index.php/')?>admin/detailabsen?id_absen=<?php echo $absen->id_absen ?>" class="btn btn-block btn-info btn-sm">Detail</a>
                                            <?php if(!$notifikasi){?>
                                                <a id="btn_hapus" href="<?php echo base_url('index.php/')?>admin/hapusabsen?id_absen=<?php echo $absen->id_absen ?>&tahun=<?php echo $absen->tahun ?>" class="btn btn-block btn-danger btn-sm">Hapus</a>
                                                <?php if($absen->status_absen!=='open'){?>
                                                    <a href="<?php echo base_url('index.php/')?>admin/aktifabsen?id_absen=<?php echo $absen->id_absen ?>" class="btn btn-block btn-success btn-sm">Aktif</a>
                                                <?php } else {?>
                                                    <a href="<?php echo base_url('index.php/')?>admin/nonaktifabsen?id_absen=<?php echo $absen->id_absen ?>" class="btn btn-block btn-warning btn-sm">Nonaktif</a>
                                                <?php } ?>
                                            <?php } ?>  
                                            
                                        </td>
                                    </tr>
                                    <?php 
                                     }
                                    ?>
                                
                                </tbody>
                            
                            </table>

                        </div>
                        

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

       </div>
    </div>
    

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="<?php echo base_url()?>assets/js/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="<?php echo base_url()?>assets/js/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="<?php echo base_url()?>assets/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <script src="<?php echo base_url()?>assets/js/jquery.dataTables.min.js"></script>
    <script src="<?php echo base_url()?>assets/js/dataTables.bootstrap4.min.js"></script>
    <script src="<?php echo base_url()?>assets/js/sweetalert.min.js"></script>

    <script>
        $(document).ready(function() {
            $('#tabeldata').DataTable();
        } );
    </script>

<script>
    const element = document.querySelectorAll('#btn_hapus')
    element.forEach(function(el){
  
      el.addEventListener('click', function (e) {  
      
      e.preventDefault();

      var urlToRedirect = e.currentTarget.getAttribute('href'); //use currentTarget because the click may be on the nested i tag and not a tag causing the href to be empty
      console.log(urlToRedirect); // verify if this is the right URL
      
      swal({
          title: "Kamu yakin ?",
          text: "Data yang dihapus tidak bisa dikembalikan.",
          icon: "warning",
          buttons: [
            'Batal',
            'Hapus'
          ],
          dangerMode: true,
        }).then(function(isConfirm) {
          if (isConfirm) {
            swal({
              title: 'Berhasil',
              text: 'Data berhasil terhapus',
              icon: 'success'
            }).then(function() {
                window.location.href = urlToRedirect;
            });
          } else {
            swal("Batal", "Data tidak terhapus.)", "error");
          }
        });
    });
});
  </script>

  </body>
</html>