<?php
class View extends CI_Controller {

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

        if ($this->user_model->is_logged_in() == FALSE)
            redirect(base_url(), 'refresh');

    }

    public function index() {
        redirect('admin/home');
    }

/* ===== IKT RS ===== */
    public function bor_harian() {
        $view['content'] = 'chart/bor_harian';
        $view['title'] = 'Indikator RS - BOR HARIAN';
        $view['data'] = $this->live_model->get_bor_harian();
        $this->load->view('admin/index', $view);
    }
    
    public function bor() {
        $view['content'] = 'chart/indikator_rs';
        $view['title'] = 'Indikator RS - BOR BULANAN';
        $view['data'] = $this->live_model->get_bor();
        $this->load->view('admin/index', $view);
    }

    public function avlos() {
        $view['content'] = 'chart/indikator_rs';
        $view['title'] = 'Indikator RS - AVLOS';
        $view['data'] = $this->live_model->get_avlos();
        $this->load->view('admin/index', $view);
    }

    public function bto() {
        $view['content'] = 'chart/indikator_rs';
        $view['title'] = 'Indikator RS - BTO';
        $view['data'] = $this->live_model->get_bto();
        $this->load->view('admin/index', $view);
    }

    public function ndr() {
        $view['content'] = 'chart/indikator_rs';
        $view['data'] = $this->live_model->get_ndr();
        $view['title'] = 'Indikator RS - NDR';
        $this->load->view('admin/index', $view);
    }

    public function gdr() {
        $view['content'] = 'chart/indikator_rs';
        $view['data'] = $this->live_model->get_ndr();
        $view['title'] = 'Indikator RS - GDR';
        $this->load->view('admin/index', $view);
    }

    public function toi() {
        $view['content'] = 'chart/indikator_rs';
        $view['title'] = 'Indikator RS - TOI';
        $view['data'] = $this->live_model->get_toi();
        $this->load->view('admin/index', $view);
    }

/* ===== KUNJUNGAN ===== */
// rawat jalan
    public function kunjunganirja() {
        // if(!isset($this->uri->segment(4))) {
            $view['title'] = 'Jalan';
            $view['periode'] = 'Periode Bulan '.ConvertBulan(date('m'));
            $view['content'] = 'chart/list_kunj_rawat';
            $view['data'] = $this->live_model->get_kunj_irja();
            $this->load->view('admin/index', $view);
        // }
        // else {
        //     redirect(base_url('admin/view/kunjunganirja_periode/'.$this->uri->segment(4)));
        // }
    }

    public function kunjunganirja_periode() {
        $tgl = explode("%20to%20", $this->uri->segment(4));
        $start = $tgl[0];
        $end = $tgl[1];
        $view['title'] = 'Jalan';
        $view['periode'] = 'Periode '.$start.' s.d. '.$end;
        $view['content'] = 'chart/list_kunj_rawat';
        $view['data'] = $this->live_model->get_kunj_irja_periode($start, $end);
        $this->load->view('admin/index', $view);
    }

// rawat inap
    public function kunjunganirna() {
        // if(empty($this->uri->segment(4))) {
            $view['title'] = 'Inap';
            $view['periode'] = 'Periode Bulan '.ConvertBulan(date('m'));
            $view['content'] = 'chart/list_kunj_rawat';
            $view['data'] = $this->live_model->get_kunj_irna();
            $this->load->view('admin/index', $view);
        // }
        // else {
        //     redirect(base_url('admin/view/kunjunganirna_periode/'.$this->uri->segment(4)));
        // }
    }

    public function kunjunganirna_periode() {
        $tgl = explode("%20to%20", $this->uri->segment(4));
        $start = $tgl[0];
        $end = $tgl[1];
        $view['title'] = 'Inap';
        $view['periode'] = 'Periode '.$start.' s.d. '.$end;
        $view['content'] = 'chart/list_kunj_rawat';
        $view['data'] = $this->live_model->get_kunj_irna_periode($start, $end);
        $this->load->view('admin/index', $view);
    }

// rawat darurat
    public function kunjunganigd() {
        // if(empty($this->uri->segment(4))) {
            $view['title'] = 'Darurat';
            $view['periode'] = 'Periode Bulan '.ConvertBulan(date('m'));
            $view['content'] = 'chart/list_kunj_rawat';
            $view['data'] = $this->live_model->get_kunj_igd();
            $this->load->view('admin/index', $view);
        // }
        // else {
        //     redirect(base_url('admin/view/kunjunganigd_periode/'.$this->uri->segment(4)));
        // }
    }

    public function kunjunganigd_periode() {
        $tgl = explode("%20to%20", $this->uri->segment(4));
        $start = $tgl[0];
        $end = $tgl[1];
        $view['title'] = 'Darurat';
        $view['periode'] = 'Periode '.$start.' s.d. '.$end;
        $view['content'] = 'chart/list_kunj_rawat';
        $view['data'] = $this->live_model->get_kunj_igd_periode($start, $end);
        $this->load->view('admin/index', $view);
    }
    
// pasien umum
    public function kunjunganumum() {
        $view['title'] = 'Kunjungan Pasien Umum';
        $view['content'] = 'chart/indikator_rs';
        $view['data'] = $this->live_model->get_kunj_umum();
        $this->load->view('admin/index', $view);
    }

// pasien jaminan
    public function kunjunganjkn() {
        $view['title'] = 'Kunjungan Pasien JKN';
        $view['content'] = 'chart/indikator_rs';
        $view['data'] = $this->live_model->get_kunj_jkn();
        $this->load->view('admin/index', $view);
    }

// pasien total
    public function kunjungantotal() {
        $view['title'] = 'Kunjungan Pasien Total';
        $view['content'] = 'chart/indikator_rs';
        $view['data'] = $this->live_model->get_kunj_total();
        $this->load->view('admin/index', $view);
    }
    
    // public function kunjunganirjabln() {
    //     $view['content'] = 'chart/list_irja';
    //     $view['chart_kunjungan_irja'] = $this->live_model->kunjungan_irja_bulan()->result();
    //     $this->load->view('admin/index', $view);
    // }

    // public function kunjunganirjathn() {
    //     $view['content'] = 'chart/list_irja';
    //     $view['chart_kunjungan_irja'] = $this->live_model->kunjungan_irja_tahun()->result();
    //     $this->load->view('admin/index', $view);
    // }

/* ===== KUNJUNGAN RAWAT INAP ===== */
    public function kunjunganirnabln() {
        $view['content'] = 'chart/list_irna';
        $view['chart_kunjungan_irna'] = $this->dashboard_model->kunjungan_irna_blnini()->result();
        $view['chart_kunjungan_irna_kelas'] = $this->live_model->kunjungan_irna_kelas()->result();
        $this->load->view('admin/index', $view);
    }

    public function kunjunganirnathn() {
        $view['content'] = 'chart/list_irna';
        $view['chart_kunjungan_irna'] = $this->dashboard_model->kunjungan_irna_thnini()->result();
        $view['chart_kunjungan_irna_kelas'] = $this->live_model->kunjungan_irna_kelas()->result();
        $this->load->view('admin/index', $view);
    }

/* ===== PENGUNJUNG ===== */
// rawat jalan
    public function pengunjungirja() {
        // if(empty($this->uri->segment(4))) {
            $view['title'] = 'Pengunjung Pasien Rawat Jalan';
            $view['periode'] = 'Periode Bulan '.ConvertBulan(date('m'));
            $view['content'] = 'chart/indikator_rs';
            $view['data'] = $this->live_model->get_pengunjung_irja();
            $this->load->view('admin/index', $view);
        // }
        // else {
        //     redirect(base_url('admin/view/pengunjungirja_periode/'.$this->uri->segment(4)));
        // }
    }

// rawat inap
    public function pengunjungirna() {
        // if(empty($this->uri->segment(4))) {
            $view['title'] = 'Pengunjung Pasien Rawat Inap';
            $view['periode'] = 'Periode Bulan '.ConvertBulan(date('m'));
            $view['content'] = 'chart/indikator_rs';
            $view['data'] = $this->live_model->get_pengunjung_irna();
            $this->load->view('admin/index', $view);
        // }
        // else {
        //     redirect(base_url('admin/view/pengunjungirna_periode/'.$this->uri->segment(4)));
        // }
    }

// rawat darurat
    public function pengunjungigd() {
        // if(empty($this->uri->segment(4))) {
            $view['title'] = 'Pengunjung Pasien Rawat Darurat';
            $view['periode'] = 'Periode Bulan '.ConvertBulan(date('m'));
            $view['content'] = 'chart/indikator_rs';
            $view['data'] = $this->live_model->get_pengunjung_igd();
            $this->load->view('admin/index', $view);
        // }
        // else {
        //     redirect(base_url('admin/view/pengunjungigd_periode/'.$this->uri->segment(4)));
        // }
    }
    
// pasien umum
    public function pengunjungumum() {
        $view['title'] = 'Pengunjung Pasien Umum';
        $view['content'] = 'chart/indikator_rs';
        $view['data'] = $this->live_model->get_pengunjung_umum();
        $this->load->view('admin/index', $view);
    }

// pasien jaminan
    public function pengunjungjkn() {
        $view['title'] = 'Pengunjung Pasien JKN';
        $view['content'] = 'chart/indikator_rs';
        $view['data'] = $this->live_model->get_pengunjung_jkn();
        $this->load->view('admin/index', $view);
    }

// pasien total
    public function pengunjungtotal() {
        $view['title'] = 'Pengunjung Pasien Total';
        $view['content'] = 'chart/indikator_rs';
        $view['data'] = $this->live_model->get_pengunjung_total();
        $this->load->view('admin/index', $view);
    }
    
/* ===== 10 BESAR ===== */
    public function chart_besar_irja() {
        //$view['adminmenu'] = true;
        $view['chart_besar_irja'] = $this->live_model->sepuluh_besar_irja()->result();
        $view['content'] = 'chart/chart_besar_irja';
        $this->load->view('admin/index', $view);
    }
    
    public function chart_besar_irna() {
        //$view['adminmenu'] = true;
        $view['chart_besar_irna'] = $this->live_model->sepuluh_besar_irna()->result();
        $view['content'] = 'chart/chart_besar_irna';
        $this->load->view('admin/index', $view);
    }
    
    public function chart_alkes() {
        //$view['adminmenu'] = true;
        $view['chart_alkes'] = $this->live_model->alkes()->result();
        $view['content'] = 'chart/chart_alkes';
        $this->load->view('admin/index', $view);
    }
    
    public function chart_obat() {
        //$view['adminmenu'] = true;
        $view['chart_obat'] = $this->live_model->obat()->result();
        $view['content'] = 'chart/chart_obat';
        $this->load->view('admin/index', $view);
    }

/* ===== SELISIH DIAGNOSA ===== */    
    // public function selisih_diagnosa_in() {
    //     //$view['adminmenu'] = true;
    //     $view['selisih_diagnosa_in'] = $this->dashboard_model->selisih_diagnosa_in()->result();
    //     $view['content'] = 'chart/selisih_diagnosa_in';
    //     $this->load->view('admin/index', $view);
    // }
    
    // public function selisih_diagnosa_out() {
    //     //$view['adminmenu'] = true;
    //     $view['selisih_diagnosa_out'] = $this->dashboard_model->selisih_diagnosa_out()->result();
    //     $view['content'] = 'chart/selisih_diagnosa_out';
    //     $this->load->view('admin/index', $view);
    // }
    
    // public function selisih_dokter_in() {
    //     //$view['adminmenu'] = true;
    //     $view['selisih_dokter_in'] = $this->dashboard_model->selisih_dokter_in()->result();
    //     $view['content'] = 'chart/selisih_dokter_in';
    //     $this->load->view('admin/index', $view);
    // }
    
    // public function selisih_dokter_out() {
    //     //$view['adminmenu'] = true;
    //     $view['selisih_dokter_out'] = $this->dashboard_model->selisih_dokter_out()->result();
    //     $view['content'] = 'chart/selisih_dokter_out';
    //     $this->load->view('admin/index', $view);
    // }
    
/* ===== INFORMASI BED ===== */
    public function bed() {
        //$view['adminmenu'] = true;
        $view['bed'] = $this->infobed();
        $view['content'] = 'chart/bed';
        $this->load->view('admin/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;
    }


}
