<?php
include 'config/rupiah.php';
$query_saldo = mysqli_query($conn,"SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi");
$row_saldo = mysqli_fetch_array($query_saldo);
$saldo_keseluruhan = $row_saldo['jumlah_debit'] - $row_saldo['jumlah_kredit'];

$total = 0;
//$sqlJU = mysqli_query($conn,"SELECT * FROM jurnal_umum WHERE DATE(tgl) BETWEEN '$tgl1' AND '$tgl2' ORDER BY tgl ASC");

//hitung pemasukan dan pengeluaran dari jurnal umum


$dPJUs = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(penerimaan) AS totalMasuk, SUM(pengeluaran) AS totalKeluar FROM saldoawal"));
$totalPemasukans = $dPJUs['totalMasuk'];
$totalPengeluarans = $dPJUs['totalKeluar'];

// Hitung Pembayaran Bulanan
$dBul = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBul FROM tagihan_bulanan WHERE statusBayar='1'"));
$totalPendapatanBulanan = $dBul['totalBul'];

// Hitung Pembayaran Bebas
$dBeb = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBeb FROM tagihan_bebas_bayar"));
$totalPendapatanBebas = $dBeb['totalBeb'];

$query_saldo = mysqli_query($conn,"SELECT SUM(sisa) as jumlah_debit FROM hutangtoko");
$row = mysqli_fetch_array($query_saldo);
$saldo_keseluruhans = $row['jumlah_debit'];

$bulan = date('m');
$query_saldo = mysqli_query($conn,"SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE DATE_FORMAT((tanggal),'%m') like '%$bulan%'");
$saldo = mysqli_fetch_array($query_saldo);
$saldo_bulan = $saldo['jumlah_debit'] - $saldo['jumlah_kredit'];


$hari = date('d');
$query_hari = mysqli_query($conn,"SELECT SUM(debit) as jumlah_debit, SUM(kredit) as jumlah_kredit FROM transaksi WHERE DATE_FORMAT((tanggal),'%m') like '%$bulan%'");
$saldo_h = mysqli_fetch_array($query_hari);
$saldo_hari = $saldo_h['jumlah_debit'] - $saldo_h['jumlah_kredit'];


$edit = mysqli_query($conn,"SELECT * FROM identitas ");
$record = mysqli_fetch_array($edit);
?>

<div class="col-xs-12">
  <?php if (isset($_GET['alert']) && $_GET['alert'] == 'updb') { ?>
    <div class="alert alert-success alert-dismissible text-center" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <p><b>Update Datbase Berhasil..</b></p>
    </div>
  <?php } ?>

  <div class="box box-primary box-solid">
    <div class="box-header with-border">
      <h3 class="box-title">

        <SCRIPT language=JavaScript>
          var d = new Date();
          var h = d.getHours();
          if (h < 11) {
            document.write('Selamat pagi, ');
          } else {
            if (h < 15) {
              document.write('Selamat siang, ');
            } else {
              if (h < 19) {
                document.write('Selamat sore, ');
              } else {
                if (h <= 23) {
                  document.write('Selamat malam, ');
                }
              }
            }
          }
        </SCRIPT> <?php echo $_SESSION['namalengkap']; ?> ||<div class="col" role="main">
      </h3>
      <h3 class="box-title">

        <?php
        $hari = date('l');
        /*$new = date('l, F d, Y', strtotime($Today));*/
        if ($hari == "Sunday") {
          echo "Minggu";
        } elseif ($hari == "Monday") {
          echo "Senin";
        } elseif ($hari == "Tuesday") {
          echo "Selasa";
        } elseif ($hari == "Wednesday") {
          echo "Rabu";
        } elseif ($hari == "Thursday") {
          echo ("Kamis");
        } elseif ($hari == "Friday") {
          echo "Jum'at";
        } elseif ($hari == "Saturday") {
          echo "Sabtu";
        }
        ?>,
        <?php
        $tgl = date('d');
        echo $tgl;
        $bulan = date('F');
        if ($bulan == "January") {
          echo " Januari ";
        } elseif ($bulan == "February") {
          echo " Februari ";
        } elseif ($bulan == "March") {
          echo " Maret ";
        } elseif ($bulan == "April") {
          echo " April ";
        } elseif ($bulan == "May") {
          echo " Mei ";
        } elseif ($bulan == "June") {
          echo " Juni ";
        } elseif ($bulan == "July") {
          echo " Juli ";
        } elseif ($bulan == "August") {
          echo " Agustus ";
        } elseif ($bulan == "September") {
          echo " September ";
        } elseif ($bulan == "October") {
          echo " Oktober ";
        } elseif ($bulan == "November") {
          echo " November ";
        } elseif ($bulan == "December") {
          echo " Desember ";
        }
        $tahun = date('Y');
        echo $tahun;
        ?> ||<script type="text/javascript">
          //fungsi displayTime yang dipanggil di bodyOnLoad dieksekusi tiap 1000ms = 1detik
          function tampilkanwaktu() {
            //buat object date berdasarkan waktu saat ini
            var waktu = new Date();
            //ambil nilai jam, 
            //tambahan script + "" supaya variable sh bertipe string sehingga bisa dihitung panjangnya : sh.length
            var sh = waktu.getHours() + "";
            //ambil nilai menit
            var sm = waktu.getMinutes() + "";
            //ambil nilai detik
            var ss = waktu.getSeconds() + "";
            //tampilkan jam:menit:detik dengan menambahkan angka 0 jika angkanya cuma satu digit (0-9)
            document.getElementById("clock").innerHTML = (sh.length == 1 ? "0" + sh : sh) + ":" + (sm.length == 1 ? "0" + sm : sm) + ":" + (ss.length == 1 ? "0" + ss : ss);
          }
        </script>

        <body onload="tampilkanwaktu();setInterval('tampilkanwaktu()', 1000);">
          <span id="clock"></span>
      </h3>
    </div><!-- /.box-header -->
    <?php
    $query_pegawai = mysqli_query($conn,"SELECT *  FROM rb_guru");
    $num_pegawai = mysqli_num_rows($query_pegawai);
    $query_kelas = mysqli_query($conn,"SELECT *  FROM kelas_siswa");
    $num_kelas = mysqli_num_rows($query_kelas);

    $query_nasabah = mysqli_query($conn,"SELECT *  FROM siswa");
    $num_nasabah = mysqli_num_rows($query_nasabah);
    $tanggal = date("Y/m/d ");
    $query_absen = mysqli_query($conn,"SELECT *  FROM rb_absensi_siswa where tanggal='$tanggal' ");
    $num_absen = mysqli_num_rows($query_absen);
    $num_tidakabsen = mysqli_num_rows(mysqli_query($conn,"SELECT * FROM siswa where nisnSiswa NOT IN (SELECT nisn  FROM rb_absensi_siswa where tanggal='$tanggal') "));
    $query_jadwal = mysqli_query($conn,"SELECT *  FROM rb_mata_pelajaran ");
    $num_jadwal = mysqli_num_rows($query_jadwal);




    ?>
    <section class="content">
      <div class="row">
        <div class="col-md-3 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-aqua"><i class="fa fa-bank"></i></span>

            <div class="info-box-content">
              <span class="info-box-text dash-text">Nama Sekolah</span>
              <span class="info-box-number"><?php echo $record['nmSekolah']; ?> </span>
            </div>
            <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>
        <div class="col-md-3 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-green"><i class="fa fa-user-plus"></i></span>

            <div class="info-box-content">

              <span class="info-box-text dash-text">Guru Aktif</span>
              <span class="info-box-number"><?php echo $num_pegawai; ?></span>
            </div>
            <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>
        <div class="col-md-3 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-red"><i class="fa fa-tags"></i></span>

            <div class="info-box-content">
              <span class="info-box-text dash-text">pelajaran </span>
              <span class="info-box-number"><?php echo $num_jadwal; ?></span>
            </div>
            <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>
        <div class="col-md-3 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-aqua"><i class="fa fa-bell "></i></span>

            <div class="info-box-content">
              <span class="info-box-text dash-text">Siswa sudah absen hari ini</span>
              <span class="info-box-number"><?php echo $num_absen; ?> Orang</span>
            </div>
            <!-- /.info-box-content -->
          </div>
        </div>
        <!-- /.col -->
        <div class="col-md-3 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-red"><i class="fa fa-user"></i></span>


            <div class="info-box-content">
              <span class="info-box-text dash-text">Nama Kepala Sekolah</span>
              <span class="info-box-number"><?php echo $record['nmKepsek']; ?> </span>
            </div>
            <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>
        <!-- /.col -->

        <!-- fix for small devices only -->
        <div class="clearfix visible-sm-block"></div>


        <!-- /.col -->

        <div class="col-md-3 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-yellow"><i class="fa fa-users"></i></span>

            <div class="info-box-content">
              <span class="info-box-text dash-text">Siswa Aktif</span>
              <span class="info-box-number"><?php echo $num_nasabah; ?></span>
            </div>
            <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>
        <!-- /.col -->
        <div class="col-md-3 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-aqua"><i class="fa fa-home"></i></span>

            <div class="info-box-content">
              <span class="info-box-text dash-text">kelas aktif</span>
              <span class="info-box-number"><?php echo $num_kelas; ?></span>
            </div>
            <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>



        <!-- /.col -->

        <div class="col-md-3 col-sm-6 col-xs-12">
          <div class="info-box">
            <span class="info-box-icon bg-red"><i class="fa fa-bell-slash "></i></span>

            <div class="info-box-content">
              <span class="info-box-text dash-text">Siswa belum Absen hari ini</span>
              <span class="info-box-number"><?php echo $num_tidakabsen; ?> Orang</span>
            </div>
            <!-- /.info-box-content -->
          </div>
        </div>
        <!-- /.col -->

        <!--
       <div class="col-md-6 col-sm-6 col-xs-12">
        <div class="info-box">
          <span class="info-box-icon bg-yellow"><i class="fa fa-money "></i></span>

          <div class="info-box-content">
            <span class="info-box-text dash-text">total kas umum</span>
            <span class="info-box-number">Rp. <?php echo rupiah($totalPemasukans + $totalPendapatanBulanan + $totalPendapatanBebas + $totalPemasukan - $totalPengeluaran); ?></span>
          </div>
          <!-- /.info-box-content -->
      </div>
      <!-- /.info-box -->

      <!-- /.col 
       <div class="col-md-6 col-sm-6 col-xs-12">
        <div class="info-box">
          <span class="info-box-icon bg-green"><i class="fa fa-toggle-down text-azure"></i></span>

          <div class="info-box-content">
            <span class="info-box-text dash-text">Total Pemasukan </span>
            <span class="info-box-number">Rp. <?php echo rupiah($totalPendapatanBulanan + $totalPendapatanBebas + $totalPemasukan); ?></span>
          </div>
          <!-- /.info-box-content 
        </div>
        <!-- /.info-box
      </div>
      <!-- /.col 
	  <div class="col-md-6 col-sm-6 col-xs-12">
        <div class="info-box">
          <span class="info-box-icon bg-red"><i class="fa fa-toggle-up text-azure"></i></span>

          <div class="info-box-content">
            <span class="info-box-text dash-text">Total Pengeluaran </span>
            <span class="info-box-number">Rp. <?php echo rupiah($totalPengeluaran); ?></span>
          </div>
          <!-- /.info-box-content 
        </div>
        <!-- /.info-box 
      </div>
      <!-- /.col 
	 
	  	  <div class="col-md-6 col-sm-6 col-xs-12">
        <div class="info-box">
          <span class="info-box-icon bg-blue"><i class="fa fa-usd"></i></span>

          <div class="info-box-content">
            <span class="info-box-text dash-text">total Tabungan </span>
            <span class="info-box-number">Rp. <?php echo rupiah($saldo_keseluruhan); ?></span>
          </div>
          <!-- /.info-box-content 
        </div>
        <!-- /.info-box 
      </div>
      <!-- /.col 
	   <div class="col-md-6 col-sm-6 col-xs-12">
        <div class="info-box">
          <span class="info-box-icon bg-white"><i class="fa fa-balance-scale text-azure"></i></span>

          <div class="info-box-content">
            <span class="info-box-text dash-text">Total Hutang Piutang</span>
            <span class="info-box-number">Rp. <?php echo rupiah($saldo_keseluruhans); ?></span>
          </div>
          <!-- /.info-box-content
        </div>
        <!-- /.info-box
      </div>
      <!-- /.col 
	   
    </div><br> -->




      <?php
      $hari  = date('d');
      $bulan = date('m');
      $tahun = date('Y');

      // Hitung pemasukan hari ini
      $dHah = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBul FROM tagihan_bulanan WHERE statusBayar='1' AND day(tglBayar)='$hari' AND month(tglBayar)='$bulan' AND year(tglBayar)='$tahun'"));
      $dBebs = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBebs FROM tagihan_bebas_bayar WHERE day(tglBayar)='$hari' AND month(tglBayar)='$bulan' AND year(tglBayar)='$tahun'"));
      $totalPemasukanHariInis = $dHah['totalBul'] ;
      $totalPemasukanHariIniss = $dBebs['totalBeb'] ;
      $totalPemasukanHariIni = $totalPemasukanHariInis  + $totalPemasukanHariIniss;
      // Hitung pemasukan bulan ini
      $dTah = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBul FROM tagihan_bulanan WHERE statusBayar='1' AND month(tglBayar)='$bulan' AND year(tglBayar)='$tahun'"));
      $dBeb = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBebs FROM tagihan_bebas_bayar WHERE month(tglBayar)='$bulan' AND year(tglBayar)='$tahun'"));;
      $totalPemasukanBulanInis = $dTah['totalBul'] ;
      $totalPemasukanBulanIniss = $dBeb['totalBeb'] ;
      $totalPemasukanBulanIni = $totalPemasukanBulanInis + $totalPemasukanBulanIniss;
      // Hitung pemasukan tahun ini
      $dBul = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBul  FROM tagihan_bulanan WHERE statusBayar='1' AND year(tglBayar)='$tahun'"));
      $dBeb = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBebs FROM tagihan_bebas_bayar WHERE year(tglBayar)='$tahun'"));
      $totalPemasukanTahunInis = $dBul['totalBul'] ;
      $totalPemasukanTahunIniss = $dBeb['totalBeb'] ;
      $totalPemasukanTahunIni =  $totalPemasukanTahunInis  +  $totalPemasukanTahunIniss;
      // Hitung seluruh pemasukan
      $dTahS = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBul FROM tagihan_bulanan WHERE statusBayar='1' "));
      $dBebS = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(jumlahBayar) AS totalBebs FROM tagihan_bebas_bayar "));
      $totalPemasukanSeluruhnyas = $dTahS['totalBul'] - $dTahS['dispen'];
      $totalPemasukanSeluruhnyass = $dBebS['totalBeb'] - $dBebS['dispens'];
      $totalPemasukanSeluruhnya = $totalPemasukanSeluruhnyas + $totalPemasukanSeluruhnyass;
      // Hitung pengeluaran hari ini
      $dPJU = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(pengeluaran) AS totalKeluar FROM jurnal_umum WHERE day(tgl)='$hari' AND month(tgl)='$bulan' AND year(tgl)='$tahun'"));
      $totalPengeluaranHariIni = $dPJU['totalKeluar'];

      // Hitung pengeluaran bulan ini
      $dPJUss = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(pengeluaran) AS totalKeluar FROM jurnal_umum WHERE month(tgl)='$bulan' AND year(tgl)='$tahun'"));
      $totalPengeluaranBulanIni = $dPJUss['totalKeluar'];

      // Hitung pengeluaran tahun ini
      $dPJUs = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(pengeluaran) AS totalKeluar FROM jurnal_umum WHERE year(tgl)='$tahun'"));
      $totalPengeluaranTahunIni = $dPJUs['totalKeluar'];

      // Hitung seluruh pengeluaran

      $dPJUa = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(pengeluaran) AS totalKeluar FROM jurnal_umum "));
      $totalPengeluaranSeluruhnya = $dPJUa['totalKeluar'];

      ?>
  </div>
  <div class="box box-primary box-solid">
    <div class="box-header with-border">
      <h3 class="box-title">
        Rekapitulasi Keuangan Saat Ini
      </h3>
    </div>
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-green">
              <div class="inner">
                <h3 style="font-size: 2.5rem;"><?php echo buatRp($totalPemasukanHariIni); ?></h3>

                <p class="dash-text">Pemasukan Hari Ini </p>
              </div>
              <div class="icon">
                <i class="ion ion-stats-bars"></i>
              </div>
              <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
            </div>
          </div>
          <!-- ./col -->
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-blue">
              <div class="inner">
                <h3 style="font-size: 2.5rem;"><?php echo buatRp($totalPemasukanBulanIni); ?></h3>

                <p class="dash-text">Pemasukan Bulan Ini </p>
              </div>
              <div class="icon">
                <i class="ion ion-stats-bars"></i>
              </div>
              <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
            </div>
          </div>
          <!-- ./col -->
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-yellow">
              <div class="inner">
                <h3 style="font-size: 2.5rem;"><?php echo buatRp($totalPemasukanTahunIni); ?></h3>

                <p class="dash-text">Pemasukan Tahun Ini</p>
              </div>
              <div class="icon">
                <i class="ion ion-stats-bars"></i>
              </div>
              <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
            </div>
          </div>
          <!-- ./col -->
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-black">
              <div class="inner">
                <h3 style="font-size: 2.5rem;"><?php echo buatRp($totalPemasukanSeluruhnya); ?></h3>

                <p class="dash-text">Seluruh Pemasukan</p>
              </div>
              <div class="icon">
                <i class="ion ion-pie-graph"></i>
              </div>
              <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
            </div>
          </div>
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-red">
              <div class="inner">
                <h3 style="font-size: 2.5rem;"><?php echo buatRp($totalPengeluaranHariIni); ?></h3>

                <p class="dash-text">Pengeluaran Hari Ini</p>
              </div>
              <div class="icon">
                <i class="ion ion-stats-bars"></i>
              </div>
              <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
            </div>
          </div>
          <!-- ./col -->
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-red">
              <div class="inner">
                <h3 style="font-size: 2.5rem;"><?php echo buatRp($totalPengeluaranBulanIni); ?></h3>

                <p class="dash-text">Pengeluaran Bulan Ini</p>
              </div>
              <div class="icon">
                <i class="ion ion-stats-bars"></i>
              </div>
              <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
            </div>
          </div>
          <!-- ./col -->
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-red">
              <div class="inner">
                <h3 style="font-size: 2.5rem;"><?php echo buatRp($totalPengeluaranTahunIni); ?></h3>

                <p class="dash-text">Pengeluaran Tahun Ini</p>
              </div>
              <div class="icon">
                <i class="ion ion-stats-bars"></i>
              </div>
              <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
            </div>
          </div>
          <!-- ./col -->
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-black">
              <div class="inner">
                <h3 style="font-size: 2.5rem;"><?php echo buatRp($totalPengeluaranSeluruhnya); ?></h3>

                <p class="dash-text">Seluruh Pengeluaran</p>
              </div>
              <div class="icon">
                <i class="ion ion-pie-graph"></i>
              </div>
              <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>
            </div>
          </div>
          <!-- ./col -->
        </div>
      </div>
      <div class="row">
        <div class="col-md-8">
          <div class="box box-success">
            <div class="box-header with-border">
              <div class="col-md-8">
                <h3 class="box-title">Grafik</h3>
              </div>
              <div class="col-md-4">
                <select name="tahun_ajaran" class="form-control input-sm" id="comboGrafik">
                  <?php
                  $tampil = mysqli_query($conn,"SELECT * FROM tahun_ajaran ORDER BY idTahunAjaran DESC");
                  while ($r = mysqli_fetch_array($tampil)) {
                    if ($r['aktif'] == 'Y') {
                      echo '<option value="' . $r['idTahunAjaran'] . '" selected>' . $r['nmTahunAjaran'] . '</option>';
                    } else {
                      echo '<option value="' . $r['idTahunAjaran'] . '">' . $r['nmTahunAjaran'] . '</option>';
                    }
                  }
                  ?>
                </select>
              </div>
            </div>
            <div class="box-body no-padding">
              <center><label>Grafik Data Pemasukan & Pengeluaran Per Bulan</label></center>
              <div class="chart" id="bar-chart" style="height: 300px;"></div>
            </div>
          </div>
        </div>


        <div class="col-md-4">
          <div class="box box-solid bg-green-gradient">
            <div class="box-header" style="color: #fff !important">
              <i class="fa fa-calendar"></i>
              <h3 class="box-title">Kalender</h3>

              <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                </button>
                <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
              </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body no-padding">
              <div id="calendar" style="width: 100%"></div>
            </div>
          </div>
          <!-- /.box -->
        </div>


      </div>

    </section>
    <script>
      $(document).ready(function() {

        $('#calendar').fullCalendar({
          header: {
            left: 'prev,next today',
            center: 'title',
            right: 'prevYear,nextYear',
          },
          defaultDate: '<?= date('Y-m-d'); ?>',
          editable: true,
          eventLimit: true, // allow "more" link when too many events
          selectable: true,
          selectHelper: true,
          select: function(tgl_mulai, tgl_selesai) {
            $("#ModalAdd #labelDate_mulai").text(moment(tgl_mulai).format('DD-MM-YYYY'));
            $("#ModalAdd #labelDate_selesai").text(moment(tgl_selesai).format('DD-MM-YYYY'));
            $('#ModalAdd #tgl_mulai').val(moment(tgl_mulai).format('YYYY-MM-DD HH:mm:ss'));
            $('#ModalAdd #tgl_selesai').val(moment(tgl_selesai).format('YYYY-MM-DD HH:mm:ss'));
            $('#ModalAdd').modal('show');
          },
          eventRender: function(event, element) {
            element.bind('dblclick', function() {
              $('#ModalEdit #id').val(event.id);
              $("#ModalEdit #labelDate_mulai").text(event.start.format('DD-MM-YYYY'));
              $("#ModalEdit #labelDate_selesai").text(event.end.format('DD-MM-YYYY'));
              $('#ModalEdit #keterangan').val(event.title);
              $('#ModalEdit #warna').val(event.color);
              $('#ModalEdit').modal('show');
            });
          },
          eventDrop: function(event, delta, revertFunc) { // si changement de position

            edit(event);

          },
          eventResize: function(event, dayDelta, minuteDelta, revertFunc) { // si changement de longueur

            edit(event);

          },

          events: [
            <?php
            $sql_agenda = mysqli_query($koneksi, "SELECT id, nama, tgl_mulai, tgl_selesai, warna FROM agenda WHERE stdel='0'");
            while ($agenda = mysqli_fetch_array($sql_agenda)) {

              $start = explode(" ", $agenda['tgl_mulai']);
              $end = explode(" ", $agenda['tgl_selesai']);
              if ($start[1] == '00:00:00') {
                $start = $start[0];
              } else {
                $start = $agenda['tgl_mulai'];
              }
              if ($end[1] == '00:00:00') {
                $end = $end[0];
              } else {
                $end = $agenda['tgl_selesai'];
              }
            ?> {
                id: '<?php echo $agenda['id']; ?>',
                title: '<?php echo $agenda['nama']; ?>',
                start: '<?php echo $start; ?>',
                end: '<?php echo $end; ?>',
                color: '<?php echo $agenda['warna']; ?>',
              },
            <?php } ?>
          ]
        });

        function edit(event) {
          start = event.start.format('YYYY-MM-DD HH:mm:ss');
          if (event.end) {
            end = event.end.format('YYYY-MM-DD HH:mm:ss');
          } else {
            end = start;
          }

          id = event.id;

          Event = [];
          Event[0] = id;
          Event[1] = start;
          Event[2] = end;

          $.ajax({
            url: 'admin/agenda/agenda.php?id=<?= $idUsers ?>',
            type: "POST",
            data: {
              Event: Event,
              tipe: 'edit_tanggal'
            },
            success: function(rep) {
              if (rep == 'berhasil') {
                toastr["success"]("Data berhasil diupdate.", "Sukses!");
              } else {
                toastr["error"]("Data gagal diupdate.", "Gagal!");
              }
            }
          });
        }

      });
    </script>