<?php
defined('BASEPATH') or exit('No direct script access allowed');

class User extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("ModelUser");
    }

    function index()
    {
        $user = $this->ModelUser->get_alldata();
        $data = array(
            'body' => 'User/list',
            'user' => $user
        );
        $this->load->view('index', $data);
    }
    function input()
  {

    $data = array(
      'form'    => 'User/form',
      'body'    => 'User/input',
      'unit' => $this->ModelUser->get_alldata(),
    );
    $this->load->view('index', $data);
  }
  function edit(){
    $id = $this->uri->segment(3);
    $user = $this->ModelUser->get_data_edit($id);
    // die(var_dump($id));
    $data = array(
      'form'    => 'User/form_edit',
      'body'    => 'User/edit',
      'user' => $user,
    );
    $this->load->view('index', $data);
  }
  function update()
  {
    $id = $this->uri->segment(3);
    $username = $this->input->post("username");
    $jabatan = $this->input->post("jabatan");
    $password = $this->input->post("password");
    $nama = $this->input->post('nama');
    $tanggallahir = $this->input->post('tgl_lahir');
    $tinggibadan = $this->input->post('tinggi_badan');
    $beratbadan = $this->input->post('berat_badan');
    $alamat = $this->input->post('alamat');
    $kecamatan = $this->input->post('kecamatan');
    $kabupaten = $this->input->post('kabupaten');
    $provinsi = $this->input->post('provinsi');
    $jeniskelamin = $this->input->post('jekel');
    $notelepon = $this->input->post('no_telp');
    $tanggaldaftar = date("Y-m-d");
    $email = $this->input->post('email');
    $umur = $this->input->post('umur');
    $data = array(
      'username'    => $username,
      'jabatan' => $jabatan,
      'nama'=> $nama,
      'tgl_lahir'=> $tanggallahir,
      'tinggi_badan'=> $tinggibadan,
      'berat_badan'=> $beratbadan,
      'alamat'=> $alamat,
      'kecamatan'=> $kecamatan,
      'kabupaten' => $kabupaten,
      'provinsi' => $provinsi,
      'jekel'=> $jeniskelamin,
      'no_telp'=> $notelepon,
      'tgl_daftar'=> $tanggaldaftar,
      'email'=> $email,
      'umur'=> $umur,
    );
    if (!empty($pass)) {
      $data['password']=password_hash($password,PASSWORD_DEFAULT,array("cost"=>10));
    }
    $this->db->where('id_user',$id);
    if ($this->db->update('user',$data )){
      // $this->session->set_flashdata('notif', $this->Notif->Berhasil('Berhasil Tersimpan'));
      redirect('User');
    }else{
      $this->session->set_flashdata('notif', $this->Notif->gagal('Gagal Tersimpan'));
      redirect('User/input');
    }
  }
  function insert()
  {
    $username = $this->input->post("username");
    $jabatan = $this->input->post("jabatan");
    $password = $this->input->post("password");
    $nama = $this->input->post('nama');
    $tanggallahir = $this->input->post('tgl_lahir');
    $tinggibadan = $this->input->post('tinggi_badan');
    $beratbadan = $this->input->post('berat_badan');
    $alamat = $this->input->post('alamat');
    $kecamatan = $this->input->post('kecamatan');
    $kabupaten = $this->input->post('kabupaten');
    $provinsi = $this->input->post('provinsi');
    $jeniskelamin = $this->input->post('jekel');
    $notelepon = $this->input->post('no_telp');
    $tanggaldaftar = date("Y-m-d");
    $email = $this->input->post('email');
    $umur = $this->input->post('umur');
    $data = array(
      'username'    => $username,
      'jabatan' => $jabatan,
      'password' => password_hash($pass,PASSWORD_DEFAULT,array("cost"=>10)),
      'nama'=> $nama,
      'tgl_lahir'=>$tanggallahir,
      'tinggi_badan'=> $tinggibadan,
      'berat_badan'=> $beratbadan, 
      'alamat'=> $alamat,
      'kecamatan'=> $kecamatan,
      'kabupaten' => $kabupaten,
      'provinsi' => $provinsi,
      'jekel'=> $jeniskelamin,
      'no_telp'=> $notelepon,
      'tgl_daftar'=> $tanggaldaftar,
      'email'=> $email,
      'umur'=> $umur,
    );
    if ($this->db->insert('user',$data )){
      // $this->session->set_flashdata('notif', $this->Notif->Berhasil('Berhasil Tersimpan'));
      redirect('User');
    }else{
      $this->session->set_flashdata('notif', $this->Notif->gagal('Gagal Tersimpan'));
      redirect('User/input');
    }
  }
  
  function delete()
  {
    $id = $this->input->post("id_user");
      $this->db->where_in("id_user",$id);
      if ($delete = $this->db->delete('user')) {
        // $this->session->set_flashdata('notif', $this->Notif->berhasil('Berhasil Hapus User'));
      }else{
        $this->session->set_flashdata('notif', $this->Notif->gagal('Gagal Hapus Data!!!'));
      };
      redirect('User');
  }

}
