<?php include 'header.php'; ?>

<div class="content-wrapper">

  <section class="content-header">
    <h1>
      Dashboard
      <small>Control panel</small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
      <li class="active">Dashboard</li>
    </ol>
  </section>


  <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="glyphicon glyphicon-circle-arrow-down"></i></span>
          <div class="info-box-content">
            <?php
            $tanggal = date('Y-m-d');
            $pemasukan = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pemasukan FROM transaksi WHERE transaksi_jenis='Pemasukan' and transaksi_tanggal='$tanggal'");
            $p = mysqli_fetch_assoc($pemasukan);
            ?>
            <span class="info-box-text">Pemasukan Hari Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($p['total_pemasukan'] ?? 0) . " ,-" ?></span>

          </div>

        </div>
      </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="glyphicon glyphicon-circle-arrow-down"></i></span>
          <div class="info-box-content">
            <?php
            $bulan = date('m');
            $pemasukan = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pemasukan FROM transaksi WHERE transaksi_jenis='Pemasukan' and month(transaksi_tanggal)='$bulan'");
            $p = mysqli_fetch_assoc($pemasukan);
            ?>
            <span class="info-box-text">Pemasukan Bulan Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($p['total_pemasukan'] ?? 0) . " ,-" ?></span>
          </div>

        </div>
      </div>

      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box">
          <span class="info-box-icon bg-yellow"><i class="glyphicon glyphicon-circle-arrow-down"></i></span>
          <div class="info-box-content">
            <?php
            $tahun = date('Y');
            $pemasukan = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pemasukan FROM transaksi WHERE transaksi_jenis='Pemasukan' and year(transaksi_tanggal)='$tahun'");
            $p = mysqli_fetch_assoc($pemasukan);
            ?>
            <span class="info-box-text">Pemasukan Tahun Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($p['total_pemasukan'] ?? 0) . " ,-" ?></span>
          </div>

        </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="glyphicon glyphicon-floppy-save"></i></span>
          <div class="info-box-content">
            <?php
            $pemasukan = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pemasukan FROM transaksi WHERE transaksi_jenis='Pemasukan'");
            $p = mysqli_fetch_assoc($pemasukan);
            ?>
            <span class="info-box-text">Seluruh Pemasukan</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($p['total_pemasukan'] ?? 0) . " ,-" ?></span>
          </div>

        </div>

      </div>
    </div>

    <div class="row">
    <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-aqua">
            <span class="info-box-icon"><i class="glyphicon glyphicon-send"></i></span>
            <div class="info-box-content">
                <?php 
                $tanggal = date('Y-m-d');
                $pengeluaran = mysqli_query($koneksi,"SELECT sum(transaksi_nominal) as total_pengeluaran FROM transaksi WHERE transaksi_jenis='pengeluaran' and transaksi_tanggal='$tanggal'");
                $p = mysqli_fetch_assoc($pengeluaran);
                ?>
                <span class="info-box-text">PENGELUARAN HARI INI</span>
                <span class="info-box-number"><?php echo "Rp. ".number_format($p['total_pengeluaran'] ?? 0)." ,-" ?></span>
                
            </div>

        </div>

    </div>

    <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-green">
            <span class="info-box-icon"><i class="glyphicon glyphicon-send"></i></span>
            <div class="info-box-content">
            <?php
                $bulan = date('m');
                $pengeluaran = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pengeluaran FROM transaksi WHERE transaksi_jenis='pengeluaran' and month(transaksi_tanggal)='$bulan'");
                $p = mysqli_fetch_assoc($pengeluaran);
                ?>
                <span class="info-box-text">PENGELUARAN BULAN INI</span>
                <span class="info-box-number"><?php echo "Rp. " . number_format($p['total_pengeluaran'] ?? 0) . " ,-" ?></span>
                
            </div>

        </div>

    </div>

    <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-yellow">
            <span class="info-box-icon"><i class="glyphicon glyphicon-send"></i></span>
            <div class="info-box-content">
                <?php 
                $tahun = date('Y');
                $pengeluaran = mysqli_query($koneksi,"SELECT sum(transaksi_nominal) as total_pengeluaran FROM transaksi WHERE transaksi_jenis='pengeluaran' and year(transaksi_tanggal)='$tahun'");
                $p = mysqli_fetch_assoc($pengeluaran);
                ?>
                <span class="info-box-text">PENGELUARAN TAHUN INI</span>
                <span class="info-box-number"><?php echo "Rp. ".number_format($p['total_pengeluaran'] ?? 0)." ,-" ?></span>
                
            </div>

        </div>

    </div>

    <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-red">
            <span class="info-box-icon"><i class="glyphicon glyphicon-send"></i></span>
            <div class="info-box-content">
            <?php 
            $pengeluaran = mysqli_query($koneksi,"SELECT sum(transaksi_nominal) as total_pengeluaran FROM transaksi WHERE transaksi_jenis='pengeluaran'");
            $p = mysqli_fetch_assoc($pengeluaran);
            ?>
                <span class="info-box-text">SELURUH PENGELUARAN</span>
                <span class="info-box-number"><?php echo "Rp. ".number_format($p['total_pengeluaran'] ?? 0)." ,-" ?></span>
                
            </div>

        </div>

    </div>

</div>

    <!-- Section Arus Kas (Saldo = Pemasukan - Pengeluaran) -->
    <div class="row">
      <div class="col-md-12">
        <h4 class="box-title" style="margin: 15px 0 10px 0;"><i class="fa fa-money"></i> <b>Arus Kas</b> <small>(Saldo per periode)</small></h4>
      </div>
      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-teal">
          <span class="info-box-icon"><i class="fa fa-arrow-right"></i></span>
          <div class="info-box-content">
            <?php
            $tanggal = date('Y-m-d');
            $pm = mysqli_query($koneksi, "SELECT COALESCE(SUM(transaksi_nominal),0) as total FROM transaksi WHERE (transaksi_jenis='Pemasukan' OR transaksi_jenis='pemasukan') AND transaksi_tanggal='$tanggal'");
            $pl = mysqli_query($koneksi, "SELECT COALESCE(SUM(transaksi_nominal),0) as total FROM transaksi WHERE (transaksi_jenis='Pengeluaran' OR transaksi_jenis='pengeluaran') AND transaksi_tanggal='$tanggal'");
            $v_pm = mysqli_fetch_assoc($pm)['total'] ?? 0;
            $v_pl = mysqli_fetch_assoc($pl)['total'] ?? 0;
            $saldo_hari = $v_pm - $v_pl;
            ?>
            <span class="info-box-text">Saldo Arus Kas Hari Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($saldo_hari, 0, ',', '.') . " ,-"; ?></span>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-teal">
          <span class="info-box-icon"><i class="fa fa-arrow-right"></i></span>
          <div class="info-box-content">
            <?php
            $bulan = date('m');
            $thn = date('Y');
            $pm = mysqli_query($koneksi, "SELECT COALESCE(SUM(transaksi_nominal),0) as total FROM transaksi WHERE (transaksi_jenis='Pemasukan' OR transaksi_jenis='pemasukan') AND MONTH(transaksi_tanggal)='$bulan' AND YEAR(transaksi_tanggal)='$thn'");
            $pl = mysqli_query($koneksi, "SELECT COALESCE(SUM(transaksi_nominal),0) as total FROM transaksi WHERE (transaksi_jenis='Pengeluaran' OR transaksi_jenis='pengeluaran') AND MONTH(transaksi_tanggal)='$bulan' AND YEAR(transaksi_tanggal)='$thn'");
            $v_pm = mysqli_fetch_assoc($pm)['total'] ?? 0;
            $v_pl = mysqli_fetch_assoc($pl)['total'] ?? 0;
            $saldo_bulan = $v_pm - $v_pl;
            ?>
            <span class="info-box-text">Saldo Arus Kas Bulan Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($saldo_bulan, 0, ',', '.') . " ,-"; ?></span>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-teal">
          <span class="info-box-icon"><i class="fa fa-arrow-right"></i></span>
          <div class="info-box-content">
            <?php
            $tahun = date('Y');
            $pm = mysqli_query($koneksi, "SELECT COALESCE(SUM(transaksi_nominal),0) as total FROM transaksi WHERE (transaksi_jenis='Pemasukan' OR transaksi_jenis='pemasukan') AND YEAR(transaksi_tanggal)='$tahun'");
            $pl = mysqli_query($koneksi, "SELECT COALESCE(SUM(transaksi_nominal),0) as total FROM transaksi WHERE (transaksi_jenis='Pengeluaran' OR transaksi_jenis='pengeluaran') AND YEAR(transaksi_tanggal)='$tahun'");
            $v_pm = mysqli_fetch_assoc($pm)['total'] ?? 0;
            $v_pl = mysqli_fetch_assoc($pl)['total'] ?? 0;
            $saldo_tahun = $v_pm - $v_pl;
            ?>
            <span class="info-box-text">Saldo Arus Kas Tahun Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($saldo_tahun, 0, ',', '.') . " ,-"; ?></span>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box bg-teal">
          <span class="info-box-icon"><i class="fa fa-arrow-right"></i></span>
          <div class="info-box-content">
            <?php
            $pm = mysqli_query($koneksi, "SELECT COALESCE(SUM(transaksi_nominal),0) as total FROM transaksi WHERE (transaksi_jenis='Pemasukan' OR transaksi_jenis='pemasukan')");
            $pl = mysqli_query($koneksi, "SELECT COALESCE(SUM(transaksi_nominal),0) as total FROM transaksi WHERE (transaksi_jenis='Pengeluaran' OR transaksi_jenis='pengeluaran')");
            $v_pm = mysqli_fetch_assoc($pm)['total'] ?? 0;
            $v_pl = mysqli_fetch_assoc($pl)['total'] ?? 0;
            $saldo_total = $v_pm - $v_pl;
            ?>
            <span class="info-box-text">Total Saldo Arus Kas</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($saldo_total, 0, ',', '.') . " ,-"; ?></span>
          </div>
        </div>
      </div>
    </div>

    <!-- Section Laba Rugi (Pendapatan - Beban) -->
    <div class="row">
      <div class="col-md-12">
        <h4 class="box-title" style="margin: 15px 0 10px 0;"><i class="fa fa-line-chart"></i> <b>Laba Rugi</b> <small>(Pendapatan - Beban per periode)</small></h4>
      </div>
      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box <?php echo $saldo_hari >= 0 ? 'bg-green' : 'bg-red'; ?>">
          <span class="info-box-icon"><i class="fa fa-<?php echo $saldo_hari >= 0 ? 'plus' : 'minus'; ?>-circle"></i></span>
          <div class="info-box-content">
            <span class="info-box-text">Laba/Rugi Hari Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($saldo_hari, 0, ',', '.') . " ,-"; ?></span>
            <small><?php echo $saldo_hari >= 0 ? 'Laba' : 'Rugi'; ?></small>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box <?php echo $saldo_bulan >= 0 ? 'bg-green' : 'bg-red'; ?>">
          <span class="info-box-icon"><i class="fa fa-<?php echo $saldo_bulan >= 0 ? 'plus' : 'minus'; ?>-circle"></i></span>
          <div class="info-box-content">
            <span class="info-box-text">Laba/Rugi Bulan Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($saldo_bulan, 0, ',', '.') . " ,-"; ?></span>
            <small><?php echo $saldo_bulan >= 0 ? 'Laba' : 'Rugi'; ?></small>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box <?php echo $saldo_tahun >= 0 ? 'bg-green' : 'bg-red'; ?>">
          <span class="info-box-icon"><i class="fa fa-<?php echo $saldo_tahun >= 0 ? 'plus' : 'minus'; ?>-circle"></i></span>
          <div class="info-box-content">
            <span class="info-box-text">Laba/Rugi Tahun Ini</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($saldo_tahun, 0, ',', '.') . " ,-"; ?></span>
            <small><?php echo $saldo_tahun >= 0 ? 'Laba' : 'Rugi'; ?></small>
          </div>
        </div>
      </div>
      <div class="col-md-3 col-sm-6 col-xs-12">
        <div class="info-box <?php echo $saldo_total >= 0 ? 'bg-green' : 'bg-red'; ?>">
          <span class="info-box-icon"><i class="fa fa-<?php echo $saldo_total >= 0 ? 'plus' : 'minus'; ?>-circle"></i></span>
          <div class="info-box-content">
            <span class="info-box-text">Total Laba/Rugi</span>
            <span class="info-box-number"><?php echo "Rp. " . number_format($saldo_total, 0, ',', '.') . " ,-"; ?></span>
            <small><?php echo $saldo_total >= 0 ? 'Laba' : 'Rugi'; ?></small>
          </div>
        </div>
      </div>
    </div>

        <!-- <div class="row">

          <div class="col-lg-3 col-xs-6">
            <div class="small-box bg-green">
              <div class="inner">
                <?php
                $tanggal = date('Y-m-d');
                $pemasukan = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pemasukan FROM transaksi WHERE transaksi_jenis='Pemasukan' and transaksi_tanggal='$tanggal'");
                $p = mysqli_fetch_assoc($pemasukan);
                ?>
                <h4 style="font-weight: bolder"><?php echo "Rp. " . number_format($p['total_pemasukan']) . " ,-" ?></h4>
                <p>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>

          <div class="col-lg-3 col-xs-6">
            <div class="small-box bg-blue">
              <div class="inner">
                <?php
                $bulan = date('m');
                $pemasukan = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pemasukan FROM transaksi WHERE transaksi_jenis='Pemasukan' and month(transaksi_tanggal)='$bulan'");
                $p = mysqli_fetch_assoc($pemasukan);
                ?>
                <h4 style="font-weight: bolder"><?php echo "Rp. " . number_format($p['total_pemasukan']) . " ,-" ?></h4>
                <p>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>

          <div class="col-lg-3 col-xs-6">
            <div class="small-box bg-orange">
              <div class="inner">
                <?php
                $tahun = date('Y');
                $pemasukan = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pemasukan FROM transaksi WHERE transaksi_jenis='Pemasukan' and year(transaksi_tanggal)='$tahun'");
                $p = mysqli_fetch_assoc($pemasukan);
                ?>
                <h4 style="font-weight: bolder"><?php echo "Rp. " . number_format($p['total_pemasukan']) . " ,-" ?></h4>
                <p>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>

          <div class="col-lg-3 col-xs-6">
            <div class="small-box bg-black">
              <div class="inner">
                <?php
                $pemasukan = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pemasukan FROM transaksi WHERE transaksi_jenis='Pemasukan'");
                $p = mysqli_fetch_assoc($pemasukan);
                ?>
                <h4 style="font-weight: bolder"><?php echo "Rp. " . number_format($p['total_pemasukan']) . " ,-" ?></h4>
                <p>Seluruh Pemasukan</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>



          <div class="col-lg-3 col-xs-6">
            <div class="small-box bg-red">
              <div class="inner">
                <?php
                $tanggal = date('Y-m-d');
                $pengeluaran = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pengeluaran FROM transaksi WHERE transaksi_jenis='pengeluaran' and transaksi_tanggal='$tanggal'");
                $p = mysqli_fetch_assoc($pengeluaran);
                ?>

                <h4 style="font-weight: bolder"><?php echo "Rp. " . number_format($p['total_pengeluaran']) . " ,-" ?></h4>
                <p>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>

          <div class="col-lg-3 col-xs-6">
            <div class="small-box bg-red">
              <div class="inner">
                <?php
                $bulan = date('m');
                $pengeluaran = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pengeluaran FROM transaksi WHERE transaksi_jenis='pengeluaran' and month(transaksi_tanggal)='$bulan'");
                $p = mysqli_fetch_assoc($pengeluaran);
                ?>

                <h4 style="font-weight: bolder"><?php echo "Rp. " . number_format($p['total_pengeluaran']) . " ,-" ?></h4>
                <p>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>

          <div class="col-lg-3 col-xs-6">
            <div class="small-box bg-red">
              <div class="inner">
                <?php
                $tahun = date('Y');
                $pengeluaran = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pengeluaran FROM transaksi WHERE transaksi_jenis='pengeluaran' and year(transaksi_tanggal)='$tahun'");
                $p = mysqli_fetch_assoc($pengeluaran);
                ?>

                <h4 style="font-weight: bolder"><?php echo "Rp. " . number_format($p['total_pengeluaran']) . " ,-" ?></h4>
                <p>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>

          <div class="col-lg-3 col-xs-6">
            <div class="small-box bg-black">
              <div class="inner">
                <?php
                $pengeluaran = mysqli_query($koneksi, "SELECT sum(transaksi_nominal) as total_pengeluaran FROM transaksi WHERE transaksi_jenis='pengeluaran'");
                $p = mysqli_fetch_assoc($pengeluaran);
                ?>
                <h4 style="font-weight: bolder"><?php echo "Rp. " . number_format($p['total_pengeluaran']) . " ,-" ?></h4>
                <p>Seluruh Pengeluaran</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>

        </div> -->























        <!-- /.row -->
        <!-- Main row -->
        <div class="row">

          <!-- Left col -->
          <section class="col-lg-8">

            <div class="nav-tabs-custom">

              <ul class="nav nav-tabs pull-right">
                <!-- <li><a href="#tab2" data-toggle="tab">Pemasukan</a></li> -->
                <li class="active"><a href="#tab1" data-toggle="tab">Pemasukan & Pengeluaran</a></li>
                <li class="pull-left header">Grafik</li>
              </ul>

              <div class="tab-content" style="padding: 20px">

                <div class="chart tab-pane active" id="tab1">


                  <h4 class="text-center">Grafik Data Pemasukan & Pengeluaran Per <b>Bulan</b></h4>
                  <canvas id="grafik1" style="position: relative; height: 300px;"></canvas>

                  <br />
                  <br />
                  <br />

                  <h4 class="text-center">Grafik Data Pemasukan & Pengeluaran Per <b>Tahun</b></h4>
                  <canvas id="grafik2" style="position: relative; height: 300px;"></canvas>

                </div>
                <div class="chart tab-pane" id="tab2" style="position: relative; height: 300px;">
                  b
                </div>
              </div>

            </div>

          </section>
          <!-- /.Left col -->


          <section class="col-lg-4">


            <!-- Calendar -->
            <div class="box box-solid bg-green-gradient">
              <div class="box-header">
                <i class="fa fa-calendar"></i>
                <h3 class="box-title">Kalender</h3>
              </div>
              <!-- /.box-header -->
              <div class="box-body no-padding">
                <!--The calendar -->
                <div id="calendar" style="width: 100%"></div>
              </div>
              <!-- /.box-body -->
            </div>


          </section>
          <!-- right col -->
        </div>
        <!-- /.row (main row) -->










  </section>

</div>

















<?php include 'footer.php'; ?>