<?php
class Profil extends CI_Controller {

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

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

    }

    public function index() {
        $view['content'] = 'profil/profil';
        $view['bed'] = $this->infobed();
        $view['sdm'] = $this->dashboard_model->profil_sdm()->result();
        $view['dewas'] = $this->dashboard_model->profil_dewas()->result();
       
        // input
        // $data = $this->dashboard_model->get_profil_sdm()->result();
        // foreach ($data as $row) {
        //     $this->db->set('profesi', $row->profesi);
        //     $this->db->set('jml', $row->jml);
        //     $this->db->insert('profil_sdm');
        // }
        
        $this->load->view('admin/index', $view);
    }

    public function infobed() {
        date_default_timezone_set('Asia/Jakarta');
        $nip = $this->dashboard_model->get_ip_local2()->result();
            foreach( $nip as $content ) 
            {
                $id = $content->id;
                $noip = $content->nomorip;
                $port = $content->port;
            }
        $nomorip = $noip;
        $nomorport = $port;
        $html= '<table class="table table-bordered table-striped">
                  <thead>
                  <tr>
                    <td align="center"><b>Kelas</b></td>
                    <td align="center"><b>Jumlah</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>
                            </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');
                }
                $html.='</tbody></table><br><p align="center"><b>Update terakhir: '.date('d-m-Y H:i',strtotime($content->tgl_update)).'</b></p>';
        } else {
            //echo 'false';
            $data = $this->db->get('bednew');
            foreach ($data->result() as $content) {
                $html.='<tr>
                            <td align="center">'.$content->kelas.'</td>
                            <td align="center">'.$content->total_TT.'</td>
                            </tr>';
            }
            $html.='<tr><td align="center" valign="middle"><b>Total</b></td><td align="center" valign="middle"><b>1.011</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;
    }    
}