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

class Logcab extends CI_Controller
{

	function __construct()
	{
		parent::__construct();
		check_not_login(); // kode akses login diambil dari helper
		check_admin();
		$this->load->model('Lokcab_m');
		$this->load->library('form_validation');
	}

	public function index()
	{
		$data['lokcabID'] = $this->Lokcab_m->get();
		$this->template->load('template', 'logcab', $data);
	}

	public function addcab()
	{
		$this->form_validation->set_rules('cabang', 'cabang', 'trim|required|is_unique[tb_cabang.cabang]');

		if ($this->form_validation->run() == False) {
			$data['lokcabID'] = $this->Lokcab_m->get();

			$this->template->load('template', 'logcab', $data);
		} else {
			$post = $this->input->post(null, TRUE);
			$config['upload_path']          = './upload/foto/cab/';
			$config['allowed_types']        = 'gif|jpg|png|jpeg';
			$config['max_size']             = 2048; // 2mb
			$config['file_name']            = 'KM-' . date('ymd') . '-' . substr(md5(rand()), 0, 2);
			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('foto')) {
				$post['foto'] = 'kimo.jpg';
				$this->Lokcab_m->addcab($post);
				if ($this->db->affected_rows() > 0) {
					$this->session->set_flashdata('success', 'Data Cabang Baru berhasil di Tambahkan');
				}
				redirect($_SERVER['HTTP_REFERER']);
			} else {
				$post['foto'] = $this->upload->data('file_name');
				$this->Lokcab_m->addcab($post);
				if ($this->db->affected_rows() > 0) {
					$this->session->set_flashdata('success', 'Data Cabang Baru Berhasil Disimpan');
				}
				redirect($_SERVER['HTTP_REFERER']);
			}
		}
	}
}
