<?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
$dPJU = mysqli_fetch_array(mysqli_query($conn,"SELECT SUM(penerimaan) AS totalMasuk, SUM(pengeluaran) AS totalKeluar FROM jurnal_umum"));
$totalPemasukan = $dPJU['totalMasuk'];
$totalPengeluaran = $dPJU['totalKeluar'];

$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 -->
    <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>
        <!-- /.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 -->
        <?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_jadwal_pelajaran where nip='$_SESSION[nips]'");
        $num_jadwal = mysqli_num_rows($query_jadwal);




        ?>
        <!-- fix for small devices only -->
        <div class="clearfix visible-sm-block"></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>
        <!-- /.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>
        <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">jadwal </span>
              <span class="info-box-number"><?php echo $num_jadwal; ?></span>
            </div>
            <!-- /.info-box-content -->
          </div>
          <!-- /.info-box -->
        </div>
        <!-- /.info-box -->

        <!-- /.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-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>
        <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 -->

        <!-- /.col -->

        <!-- /.col -->

      </div><br>

      <div class="alert alert-info">
        <!-- panggil file "content" untuk menampilkan content -->

        <p>Ini adalah halaman Dashboard yang digunakan untuk mengelola Seluruh akses guru
          halaman ini adalah bukti bahwa anda adalah pengguna resmi aplikasi ini.
          Silahkan digunakan dengan baik, dan sampaikan jika ada keluhan dalam masalah penggunaan aplikasi ini. Terima kasih atas kepercayaan anda!</p>
      </div>