<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Dashboard extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('User_model');
    $this->load->model('Form_batubara_masuk_model');
    $this->load->model('Form_batubara_produksi_model');
    $this->load->model('Form_batubara_kirim_model');
    $this->load->model('Stockpile_batubara_masuk_model');
    $this->load->model('Site_model');
    $this->load->model('Checker_model');

    $this->data['module'] = 'User';
    if ($this->session->userdata('status') != "login") {
      redirect(base_url("login"));
    }
  }

  public function index()
  {
    $hari_ini = date("Y-m-d");
    $tgl_pertama = date('Y-m-01', strtotime($hari_ini));
    $tgl_terakhir = date('Y-m-t', strtotime($hari_ini));

    $this->data['title'] = 'Dashboard';
    $this->data['active'] = 'dashboard';
    $this->data['jumlah_form_batubara_masuk'] = $this->Form_batubara_masuk_model->total_rows_by_date($tgl_pertama, $tgl_terakhir);
    $this->data['jumlah_tonase_masuk'] = $this->Form_batubara_masuk_model->sum_tonase_by_table($tgl_pertama, $tgl_terakhir);
    //$this->data['jumlah_tonase_masuk'] = $this->Form_batubara_produksi_model->sum_tonase_by_table();
    $this->data['jumlah_form_batubara_kirim'] = $this->Form_batubara_kirim_model->total_rows_by_date($tgl_pertama, $tgl_terakhir);
    $this->data['jumlah_tonase_keluar'] = $this->Form_batubara_kirim_model->sum_tonase_by_table($tgl_pertama, $tgl_terakhir);

    // tampilkan site
    $site =  $this->Site_model->get_all();

    $result = array();
    $total_ritase_masuk = 0;
    $total_tonase_masuk = 0;
    $total_tonase_proses = 0;
    $total_ritase_kirim = 0;
    $total_tonase_kirim = 0;
    foreach($site as $row) {
      $id = $row->id_site;
      $nama = $row->name_site;
      // tampilkan stokpile
      $stockpile = $this->Stockpile_batubara_masuk_model->get_all_by_id_side($row->id_site);

      $result_masuk = array();
      $jumlah_semua_ritase_masuk = 0;
      $jumlah_semua_tonase_masuk = 0;
      foreach ($stockpile as $sp) {
        $jumlah_ritase_masuk = $this->Checker_model->get_row_table_by_id('form_batubara_masuk', 'id_stockpile_batubara_masuk', $sp->id_stockpile_batubara_masuk,'tanggal_batubara_masuk', $tgl_pertama, $tgl_terakhir);

        // menampilkan data dari tabel form_batubara_masuk
        $getDataMasukByIdSP = $this->Form_batubara_masuk_model->get_data_table_by_id($sp->id_stockpile_batubara_masuk, $tgl_pertama, $tgl_terakhir);

        $jumlah_tonase_masuk = 0;
        foreach ($getDataMasukByIdSP as $row) {
          $jumlah_tonase_masuk += $row['bruto'] - $row['tarra'];
        }

        // menampilkan data dari tabel form_batubara_produksi
        $getDataProsesByIdSP = $this->Form_batubara_produksi_model->get_data_table_by_id($sp->id_stockpile_batubara_masuk, $tgl_pertama, $tgl_terakhir);

        $jumlah_tonase_proses = 0;
        foreach ($getDataProsesByIdSP as $row) {
          $jumlah_tonase_proses += $row['jumlah_batubara_produksi'];
        }

        $sisa_tonase = $jumlah_tonase_masuk - $jumlah_tonase_proses;

        $result_masuk[] = array(
          'name_sp' => $sp->nama_stockpile_batubara_masuk,
          'ritase' => $jumlah_ritase_masuk,  //jumlah surat batubara masuk
          'tonase' => number_format($jumlah_tonase_masuk, 3),   //jumlah muatan
          'sisa_tonase' => number_format($sisa_tonase, 3),   //sisa muatan
        );

        $jumlah_semua_ritase_masuk += $jumlah_ritase_masuk;
        $jumlah_semua_tonase_masuk += $jumlah_tonase_masuk;
      }

      // jumlah batu bara proses
      $result_proses = array();
      $jumlah_semua_tonase_proses = 0;
      foreach ($stockpile as $sp) {
        $jumlah_ritase_masuk = $this->Checker_model->get_row_table_by_id('form_batubara_masuk', 'id_stockpile_batubara_masuk', $sp->id_stockpile_batubara_masuk,'tanggal_batubara_masuk', $tgl_pertama, $tgl_terakhir);

        // menampilkan data dari tabel form_batubara_produksi
        $getDataProsesByIdSP = $this->Form_batubara_produksi_model->get_data_table_by_id($sp->id_stockpile_batubara_masuk, $tgl_pertama, $tgl_terakhir);

        $jumlah_tonase_proses = 0;
        foreach ($getDataProsesByIdSP as $row) {
          $jumlah_tonase_proses += $row['jumlah_batubara_produksi'];
        }

        // menampilkan data dari tabel form_batubara_kirim
        $getDataByIdSP = $this->Form_batubara_kirim_model->get_data_table_by_id($sp->id_stockpile_batubara_masuk, $tgl_pertama, $tgl_terakhir);

        $jumlah_tonase = 0;
        foreach ($getDataByIdSP as $row) {
          $jumlah_tonase += $row['bruto'] - $row['tarra'];
        }

        $sisa_tonase = $jumlah_tonase_proses - $jumlah_tonase;

        $result_proses[] = array(
          'name_sp' => $sp->nama_stockpile_batubara_masuk,
          'tonase_proses' => number_format($jumlah_tonase_proses, 3),   //jumlah muatan
          'sisa_tonase' => number_format($sisa_tonase, 3),   //sisa muatan
        );

        $jumlah_semua_tonase_proses += $sisa_tonase;
      }


      // jumlah batu bara keluar
      $result_keluar = array();
      $jumlah_semua_ritase_kirim = 0;
      $jumlah_semua_tonase_kirim = 0;
      foreach ($stockpile as $sp) {
        $jumlah_ritase_kirim = $this->Form_batubara_kirim_model->get_row_table_by_id('form_batubara_kirim', 'id_asal_batubara_kirim', $sp->id_stockpile_batubara_masuk, $tgl_pertama, $tgl_terakhir);

        // menampilkan data dari tabel form_batubara_kirim
        $getDataByIdSP = $this->Form_batubara_kirim_model->get_data_table_by_id($sp->id_stockpile_batubara_masuk, $tgl_pertama, $tgl_terakhir);

        $jumlah_tonase = 0;
        foreach ($getDataByIdSP as $row) {
          $jumlah_tonase += $row['bruto'] - $row['tarra'];
        }

        $result_keluar[] = array(
          'name_sp' => $sp->nama_stockpile_batubara_masuk,
          'ritase' => $jumlah_ritase_kirim,  //jumlah surat batubara masuk
          'tonase' => number_format($jumlah_tonase, 3),   //jumlah muatan
        );


        $jumlah_semua_ritase_kirim += $jumlah_ritase_kirim;
        $jumlah_semua_tonase_kirim += $jumlah_tonase;
      }

      $result[] = array(
          'id_site' => $id,
          'name_site' => $nama,
          'masuk' => $result_masuk,
          'proses' => $result_proses,
          'keluar' => $result_keluar,
          'jumlah_ritase_masuk' => $jumlah_semua_ritase_masuk,
          'jumlah_tonase_masuk' => number_format($jumlah_semua_tonase_masuk, 3),
          'jumlah_tonase_proses' => number_format($jumlah_semua_tonase_proses, 3),
          'jumlah_ritase_kirim' => $jumlah_semua_ritase_kirim,
          'jumlah_tonase_kirim' => number_format($jumlah_semua_tonase_kirim, 3),
       );

      $total_ritase_masuk += $jumlah_semua_ritase_masuk;
      $total_tonase_masuk += $jumlah_semua_tonase_masuk;
      $total_tonase_proses += $jumlah_semua_tonase_proses;
      $total_ritase_kirim += $jumlah_semua_ritase_kirim;
      $total_tonase_kirim += $jumlah_semua_tonase_kirim;
    }

    $this->data['data_site'] = $result;


    $this->data['total_ritase_masuk'] = $total_ritase_masuk;
    $this->data['total_tonase_masuk'] = $total_tonase_masuk;
    $this->data['total_tonase_proses'] = $total_tonase_proses;
    $this->data['total_ritase_kirim'] = $total_ritase_kirim;
    $this->data['total_tonase_kirim'] = $total_tonase_kirim;



    $tanggal_report_awal = date('Y-m-d');
    $tanggal_report_akhir = date('Y-m-d');


    $this->load->view('dashboard/admin', $this->data);
  }
}
