<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_grafik extends CI_Model {

    protected $idGroup = null;
    protected $iduser = null;

    public function __construct() {
        parent::__construct();
        $this->idGroup = $this->session->userdata("r_idGroup");
        $this->idUser = $userId = $this->session->userdata('r_userId');
    }

    function get_jenis_layanan(){
        $jenis_layanan =  $this->db->query("SELECT id_jenis, nama_jenis FROM jenis_layanan ORDER BY id_jenis ASC")->result_array();
        $color_chart = array("3e95cd", "8e5ea2", "3cba9f", "e8c3b9", "c45850", "66cc00",  );

        $tb = (int) date('m');
        $cb = intval($tb-5+1);

        $result = array();
        $x=0;
        foreach($jenis_layanan as $r){
            $result['jl_'.$r['id_jenis']]['nama_layanan'] = $r['nama_jenis'];
            $result['jl_'.$r['id_jenis']]['color'] = $color_chart[$x];
            for($i=$cb;$i<=$tb;$i++){
                $param = array();
                $param[] = $r['id_jenis'];
                $param[] = date('Y-m-01', strtotime(date('Y').'-'.(int)date('m', strtotime('+'.$i.' months',date('m'))).'-'.date('d')));
                $param[] = date('Y-m-t', strtotime(date('Y').'-'.(int)date('m', strtotime('+'.$i.' months',date('m'))).'-'.date('d')));
                $query = "SELECT COUNT(id_uji) as total_uji
                    FROM permintaan_uji
                    WHERE id_jenis_layanan = ? AND tgl_pukul_registrasi BETWEEN ? AND ? AND is_active = '1'
                ";
                $result['jl_'.$r['id_jenis']]['data_jl']['bl_jl_'.date('m', strtotime('+'.$i.' months',date('m')))] = $this->db->query($query, $param)->row_array()['total_uji'];
            }
            $x++;
        }
        //print("<pre>".print_r($result, true)."</pre>"); die();
        return $result;
    }

    function get_pad(){

        for($i=1; $i<=12;$i++){
            $param = array();
            $param[] = date("Y");
            $param[] = $i;
            $query =  "SELECT SUM(nominal) as total_nominal, bulan FROM data_pad WHERE tahun = ? AND bulan = ? AND is_active = '1'";

            $result[$i] =  $this->db->query($query, $param)->row_array();

        }

        return $result;
    }

    function get_uji_sampel(){
        for($i=1; $i<=12;$i++){
            $param = array();
            $param[] = date('Y-m-01', strtotime(date('Y').'-'.$i.'-'.date('d')));
            $param[] = date('Y-m-t', strtotime(date('Y').'-'.$i.'-'.date('d')));
            $query =  "SELECT count(id_uji) as total_uji FROM permintaan_uji WHERE tgl_pukul_registrasi BETWEEN ? AND ? AND is_active = '1'";

            $result[$i] =  $this->db->query($query, $param)->row_array();

        }

        return $result;
    }

    function get_jenis_pengujian() {
        $jenis_layanan =  $this->db->query("SELECT id_jenis, nama_jenis FROM parameter_uji ORDER BY id_jenis ASC")->result_array();
        $color_chart = array( "3366cc", "00ccff", "ffccff", "333300", "ffff00", "ff3300", "66ff33", "ff3399", "990099", "666699", "006600", "00e6e6", "", "3566cc", "6666cc");

        $tb = (int) date('m');
        $cb = intval($tb-5+1);

        $result = array();
        $x=0;
        foreach($jenis_layanan as $r){
            $result['jl_'.$r['id_jenis']]['nama_parameter'] = $r['nama_jenis'];
            $result['jl_'.$r['id_jenis']]['color'] = $color_chart[$x];
            for($i=$cb;$i<=$tb;$i++){
                $param = array();
                $param[] = '%'.$r['id_jenis'].'|%';
                $param[] = date('Y-m-01', strtotime(date('Y').'-'.(int)date('m', strtotime('+'.$i.' months',date('m'))).'-'.date('d')));
                $param[] = date('Y-m-t', strtotime(date('Y').'-'.(int)date('m', strtotime('+'.$i.' months',date('m'))).'-'.date('d')));
                $query = "SELECT COUNT(ds.id_sampel) as total_sampel
                    FROM data_sampel ds
                    JOIN permintaan_uji pu ON pu.id_uji = ds.id_uji
                    WHERE ds.jenis_pengujian LIKE ? AND pu.tgl_pukul_registrasi BETWEEN ? AND ? AND ds.is_active = '1'
                ";
                $result['jl_'.$r['id_jenis']]['data_jl']['bl_jl_'.date('m', strtotime('+'.$i.' months',date('m')))] = $this->db->query($query, $param)->row_array()['total_sampel'];
            }
            $x++;
        }
        //print("<pre>".print_r($result, true)."</pre>"); die();
        return $result;
    }

}
