<?php
class View extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('dashboard_model');
        $this->load->helper('text');
        $this->load->helper('content');
    }

    public function index() {
         redirect('profil');
        /*
        $view['menuhome'] = true;
        $view['menu'] = false;
        $view['content'] = 'list';
        $view['bed'] = $this->infobed();

        $view['chart_besar_irja'] = $this->dashboard_model->sepuluh_besar_irja()->result();
        $view['chart_besar_irna'] = $this->dashboard_model->sepuluh_besar_irna()->result();
        $view['chart_kunjungan_irja'] = $this->dashboard_model->kunjungan_irja_blnini()->result();
        $view['chart_kunjungan_irna_kelas'] = $this->dashboard_model->kunjungan_irna_kelas()->result();
        $view['chart_alkes'] = $this->dashboard_model->alkes()->result();
        $view['chart_obat'] = $this->dashboard_model->obat()->result();

        $this->load->view('index', $view);
        */
    }
    
    public function chart_besar_irja() {
        $view['chart_besar_irja'] = $this->dashboard_model->sepuluh_besar_irja()->result();
        $view['content'] = 'chart/chart_besar_irja';

        $this->load->view('index', $view);
    }
    
    public function chart_besar_irna() {
        $view['chart_besar_irna'] = $this->dashboard_model->sepuluh_besar_irna()->result();
        $view['content'] = 'chart/chart_besar_irna';

        $this->load->view('index', $view);
    }

    public function chart_alkes() {
        $view['chart_alkes'] = $this->dashboard_model->alkes()->result();
        $view['content'] = 'chart/chart_alkes';

        $this->load->view('index', $view);
    }
    
    public function chart_obat() {
        $view['chart_obat'] = $this->dashboard_model->obat()->result();
        $view['content'] = 'chart/chart_obat';

        $this->load->view('index', $view);
    }

/* ===== INFORMASI BED ===== */
    public function bed() {
        $view['bed'] = $this->infobed();
        $view['content'] = 'chart/bed';

        $this->load->view('index', $view);
    }

    public function infobed() {
        $nip = $this->dashboard_model->get_ip_local2()->row();
        $nomorip = $nip->nomorip;
        $nomorport = $nip->port;

        $t_bed = 0;
        $t_terpakai = 0;
        $t_sisa = 0;
        $t_wl = 0;

        $html= '<table class="table table-bordered table-striped">
                  <thead>
                  <tr>
                    <td align="center"><b>Kelas</b></td>
                    <td align="center"><b>Jumlah</b></td>
                    <td align="center"><b>Terpakai</b></td>
                    <td align="center"><b>Sisa</b></td>
                    <td align="center"><b>Daftar Tunggu</b></td>
                  </tr>
                  </thead>
                  <tbody>';

        if (false !== ($contents = @file_get_contents('http://'.$nomorip.':'.$nomorport.'/dashboard_rsdk/bed/'))) {
            //echo 'true';
            $filexml = simplexml_load_string($contents);
            //print_r($filexml);
                foreach ($filexml as $content) 
                {
                    $html.='<tr>
                                <td align="center" valign="middle">'.$content->kelas.'</td>
                                <td align="center" valign="middle">'.$content->total_TT.'</td>
                                <td align="center">'.$content->terpakai.'</td>
                                <td align="center">'.$content->sisa.'</td>
                                <td align="center">'.$content->waitinglist.'</td>
                            </tr>';
                    //update to database
                    $this->db->set('total_TT',$content->total_TT);
                    $this->db->set('terpakai',$content->terpakai);
                    $this->db->set('sisa',$content->sisa);
                    $this->db->set('waitinglist',$content->waitinglist);
                    $this->db->set('tgl_update',$content->tgl_update);
                    $this->db->where('kelas',$content->kelas);
                    $this->db->update('bednew');
                
                    $t_bed = $t_bed + $content->total_TT; 
                    $t_terpakai = $t_terpakai + $content->terpakai; 
                    $t_sisa = $t_sisa + $content->sisa; 
                    $t_wl = $t_wl + $content->waitinglist; 
                }
                $html.='<tr>
                            <td align="center"><b>Total</b></td>
                            <td align="center"><b>'.$t_bed.'</b></td>
                            <td align="center"><b>'.$t_terpakai.'</b></td>
                            <td align="center"><b>'.$t_sisa.'</b></td>
                            <td align="center"><b>'.$t_wl.'</b></td>
                        </tr>
                        </tbody>
                        </table><br><p align="center"><b>Update terakhir: '.date('d-m-Y H:i',strtotime($content->tgl_update)).'</b></p>';
        } else {
            //echo 'false';
            $DB1 = $this->load->database('webrsdk', TRUE);
            $data = $DB1->get('dev_bednew')->result();
            foreach ($data as $content) {
                $html.='<tr>
                            <td align="center">'.$content->kelas.'</td>
                            <td align="center">'.$content->total_TT.'</td>
                            <td align="center">'.$content->terpakai.'</td>
                            <td align="center">'.$content->sisa.'</td>
                            <td align="center">'.$content->waitinglist.'</td>
                        </tr>';
                $t_bed = $t_bed + $content->total_TT; 
                $t_terpakai = $t_terpakai + $content->terpakai; 
                $t_sisa = $t_sisa + $content->sisa; 
                $t_wl = $t_wl + $content->waitinglist; 
            }
            $html.='<tr>
                        <td align="center"><b>Total</b></td>
                        <td align="center"><b>'.$t_bed.'</b></td>
                        <td align="center"><b>'.$t_terpakai.'</b></td>
                        <td align="center"><b>'.$t_sisa.'</b></td>
                        <td align="center"><b>'.$t_wl.'</b></td>
                    </tr>
                    </tbody>
                    </table><br><p align="center"><b>Update terakhir: '.date('d-m-Y H:i',strtotime($content->tgl_update)).'</b></p>';
        }
        return $html;
    }

/* ===== KUNJUNGAN RAWAT JALAN ===== */
    public function kunjunganirja() {
        $view['menuhome'] = false;
        $view['menu'] = true;
        $view['content'] = 'chart/list_irja';

        $view['chart_kunjungan_irja'] = $this->dashboard_model->kunjungan_irja_hrini()->result();

        $this->load->view('index', $view);
    }
    
    public function kunjunganirjabln() {
        $view['menuhome'] = false;
        $view['menu'] = true;
        $view['content'] = 'chart/list_irja';

        $view['chart_kunjungan_irja'] = $this->dashboard_model->kunjungan_irja_blnini()->result();

        $this->load->view('index', $view);
    }

    public function kunjunganirjathn() {
        $view['menuhome'] = false;
        $view['menu'] = true;
        $view['content'] = 'chart/list_irja';

        $view['chart_kunjungan_irja'] = $this->dashboard_model->kunjungan_irja_thnini()->result();

        $this->load->view('index', $view);
    }

/* ===== KUNJUNGAN RAWAT INAP ===== */
    public function kunjunganirna() {
        $view['menuhome'] = false;
        $view['menu'] = true;
        $view['content'] = 'chart/list_irna';

        $view['chart_kunjungan_irna'] = $this->dashboard_model->kunjungan_irna_hrini()->result();
        $view['chart_kunjungan_irna_kelas'] = $this->dashboard_model->kunjungan_irna_kelas()->result();

        $this->load->view('index', $view);
        
    }

    public function kunjunganirnabln() {
        $view['menuhome'] = false;
        $view['menu'] = true;
        $view['content'] = 'chart/list_irna';

        $view['chart_kunjungan_irna'] = $this->dashboard_model->kunjungan_irna_blnini()->result();
        $view['chart_kunjungan_irna_kelas'] = $this->dashboard_model->kunjungan_irna_kelas()->result();

        $this->load->view('index', $view);
    }

    public function kunjunganirnathn() {
        $view['menuhome'] = false;
        $view['menu'] = true;
        $view['content'] = 'chart/list_irna';

        $view['chart_kunjungan_irna'] = $this->dashboard_model->kunjungan_irna_thnini()->result();
        $view['chart_kunjungan_irna_kelas'] = $this->dashboard_model->kunjungan_irna_kelas()->result();

        $this->load->view('index', $view);
    }

}
