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

class Kmgpusat extends CI_Controller
{

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

	public function index()
	{
		$data['katbrg'] = $this->Kmpart_m->get();
		$data['Part'] = $this->Kmpart_m->getpart();
		$this->template->load('template', 'kmgpusat', $data);
	}

	public function addkatalog()
	{
		$this->form_validation->set_rules('nama_katbrg', 'nama_katbrg', 'trim|required');
		$this->form_validation->set_message('required', '" masih kossong "');

		if ($this->form_validation->run() == false) {
			$data['katbrg'] = $this->Kmpart_m->get();
			$data['Part'] = $this->Kmpart_m->getpart();
			$this->template->load('template', 'kmgpusat', $data);
		} else {
			$post = $this->input->post(null, TRUE);
			$this->Kmgpusat_m->addkatalog($post);
			if ($this->db->affected_rows() > 0) {
				$this->session->set_flashdata('success', 'Data Katalog baru berhasil di tambahkan');
			}
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function addpart()
	{
		$this->form_validation->set_rules('persen_brg', 'persen_brg', 'trim|required');

		if ($this->form_validation->run() == False) {
			$data['katbrg'] = $this->Kmpart_m->get();
			$data['Part'] = $this->Kmpart_m->getpart();

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

	public function editpart()
	{

		$this->form_validation->set_rules('persen_brg', 'persen_brg', 'trim|required');

		$this->form_validation->set_message('required', '" masih kossong "');

		if ($this->form_validation->run() == false) {
			$data['katbrg'] = $this->Kmpart_m->get();
			$data['Part'] = $this->Kmpart_m->getpart();

			$this->template->load('template', 'kmgpusat', $data);
		} else {
			$post = $this->input->post(null, TRUE);
			$config['upload_path']          = './upload/foto/part/';
			$config['allowed_types']        = 'gif|jpg|png|jpeg';
			$config['max_size']             = 2048; // 2mb
			$config['file_name']            = 'PA-' . date('ymd') . '-' . substr(md5(rand()), 0, 2);
			$this->load->library('upload', $config);
			if ($this->upload->do_upload('img_brg')) {
				$post['img_brg'] = $this->upload->data('file_name');
				$item = $this->Kmgpusat_m->get($post['id_katbrgstok'])->row();
				if ($item->img_brg != null) {
					$target_file = './upload/foto/part/' . $item->img_brg;
					unlink($target_file);
				}
				$this->Kmgpusat_m->editpart($post);
				if ($this->db->affected_rows() > 0) {
					$this->session->set_flashdata('success', 'Data Sparepart berhasil di Perbaharui');
				}
				redirect($_SERVER['HTTP_REFERER']);
			} else {
				$post['img_brg'] = null;
				$this->Kmgpusat_m->editpart($post);
				if ($this->db->affected_rows() > 0) {
					$this->session->set_flashdata('success', 'Data Sparepart berhasil di Perbaharui');
				}
				redirect($_SERVER['HTTP_REFERER']);
			}
		}
	}

	public function delpart($id)
	{
		$item = $this->Kmgpusat_m->get($id)->row();
		if ($item->img_brg != null) {
			$target_file = './upload/foto/part/' . $item->img_brg;
			unlink($target_file);
		}

		$this->Kmgpusat_m->delpart($id);
		if ($this->db->affected_rows() > 0) {
			$this->session->set_flashdata('success', 'Data Sperpart berhasil Dihapus');
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
}
