<?php
/**
 * Created by PhpStorm.
 * User: glam
 * Date: 4/20/16
 * Time: 11:02 PM
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Prodi extends MY_Admin
{
    function __construct()
    {
        parent::__construct();
        $this->load->library(['Template','form_validation']);
        $this->load->model(['Prodi_model','Fakultas_model']);
        $this->load->library(['Template','form_validation','Cipasswordhash','Datatables']);
    }

    public function index()
    {
        $data['title'] = 'Data Program Studi';
        $data['css']    = [ base_url().'assets/css/jquery.dataTables.css'];
        $data['js']     = [ base_url().'assets/js/jquery.dataTables.min.js', base_url().'assets/js/jvalidator.js'];
        $data['fakultas'] = $this->Fakultas_model->get_all();
        $this->template->tampil('admin/Prodi_view',$data);
    }

    public function detail($id)
    {
        echo json_encode($this->Prodi_model->get_one($id));
    }
    public function index_json(){
        $this -> datatables -> select('kd_prodi,fakultas,prodi,nilai_prodi,tb_prodi.alias as alias')
            -> add_column('Action',
                '<a href="'.site_url('admin/prodi/detail').'/$1" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-eye-open"></span><a/>
							<a href="#nama" class="btn btn-xs btn-default" onclick="edit($1)"><span class="glyphicon glyphicon-pencil"></span></a>
							<a href="'.site_url('admin/prodi/delete').'/$1" class="btn btn-xs btn-default" onclick="return confirm(\'Yakin ingin menghapus?\')"><span class="glyphicon glyphicon-trash"></span></a>',
                'kd_prodi')
            -> from('tb_prodi')
            -> join('tb_fakultas','tb_prodi.kd_fakultas=tb_fakultas.kd_fakultas');
        echo $this -> datatables -> generate();
    }

    public function add()
    {
        $msg = '';
        $this->_set_rules();
        if ($this->form_validation->run()===FALSE) {
            $msg .= '<div class="alert alert-warning">Harap lengkapi data.</div>';
        }else{
            $arr = [
                'prodi' => $this->input->post('nama'),
                'nilai_prodi' => $this->input->post('grade'),
                'alias' => $this->input->post('alias'),
                'kd_fakultas' => $this->input->post('fakultas')
            ];
            $this->Prodi_model->insert($arr);
            $msg .= '<div class="alert alert-success">Data berhasil disimpan.</div>';
        }
        $this->session->set_flashdata('response',$msg);
        redirect(site_url('admin/prodi'));
    }

    public function edit()
    {
        $msg = '';
        $this->form_validation->set_rules('id','ID','required');
        $this->_set_rules();
        if ($this->form_validation->run()===FALSE) {
            $msg .= '<div class="alert alert-warning">Harap lengkapi data.</div>';
        }else{
            $arr = [
                'prodi' => $this->input->post('nama'),
                'nilai_prodi' => $this->input->post('grade'),
                'alias' => $this->input->post('alias'),
                'kd_fakultas' => $this->input->post('fakultas')
            ];
            $this->Prodi_model->update($this->input->post('id'),$arr);
            $msg .= '<div class="alert alert-success">Data berhasil diperbaharui.</div>';
        }
        $this->session->set_flashdata('response',$msg);
        redirect(site_url('admin/prodi'));
    }

    public function delete($id=0)
    {
        $msg = '';
        if ($id!=0) {
            $this->Prodi_model->delete($id);
            $msg .= '<div class="alert alert-success">Data berhasil dihapus.</div>';
            $this->session->set_flashdata('response',$msg);
            redirect(site_url('admin/prodi'));
        }
    }

    public function _set_rules()
    {
        $this->form_validation->set_rules('nama','Nama Prodi','trim|required');
        $this->form_validation->set_rules('grade','Grade Prodi','trim|required');
        $this->form_validation->set_rules('fakultas','Fakultas','trim|required');
        $this->form_validation->set_rules('alias','Alias','trim|required');
    }
}