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

class Motor extends CI_Controller
{

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

	public function index()
	{
		$data['merekID'] = $this->Motor_m->getmerek();
		$data['tipeID'] = $this->Motor_m->gettipe();
		$data['seriM'] = $this->Motor_m->getseri();
		$this->template->load('template', 'motor', $data);
	}

	public function addmerekken()
	{
		$this->form_validation->set_rules('merek_kendaraan', 'merek_kendaraan', 'trim|required|is_unique[tb_merekkendaraan.merek_kendaraan]');
		$this->form_validation->set_message('required', '" masih kossong "');
		$this->form_validation->set_message('is_unique', '" sudah terdaftar "');
		if ($this->form_validation->run() == false) {
			$this->template->load('template', 'motor/addmerekken');
		} else {
			$post = $this->input->post(null, TRUE);
			$this->Motor_m->addmerekken($post);
			if ($this->db->affected_rows() > 0) {
				$this->session->set_flashdata('success', 'Data Merek Motor Baru berhasil Disimpan');
			}
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function addtipeken()
	{
		$this->form_validation->set_rules('id_merek', 'id_merek', 'trim|required',);
		$this->form_validation->set_rules('tipeken', 'tipeken', 'trim|required|is_unique[tb_tipekendaraan.tipeken]');
		$this->form_validation->set_message('required', '" masih kossong "');
		$this->form_validation->set_message('is_unique', '" sudah terdaftar "');

		if ($this->form_validation->run() == false) {
			$this->template->load('template', 'motor/addtipeken');
		} else {
			$post = $this->input->post(null, TRUE);
			$this->Motor_m->addtipeken($post);
			if ($this->db->affected_rows() > 0) {
				$this->session->set_flashdata('success', 'Data Tipe Motor Baru berhasil Disimpan');
			}
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function addseriken()
	{
		$this->form_validation->set_rules('seriken', 'seriken', 'trim|required',);
		$this->form_validation->set_message('required', '" masih kossong "');
		$this->form_validation->set_message('is_unique', '" sudah terdaftar "');

		if ($this->form_validation->run() == false) {
			$this->template->load('template', 'motor/addseriken');
		} else {
			$post = $this->input->post(null, TRUE);
			$this->Motor_m->addseriken($post);
			if ($this->db->affected_rows() > 0) {
				$this->session->set_flashdata('success', 'Data Seri Motor Baru berhasil Disimpan');
			}
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function delinvkm($id)
	{
		$item = $this->Motor_m->get($id)->row();
		if ($item->invkm_foto != null) {
			$target_file = './upload/foto/invkm/' . $item->invkm_foto;
			unlink($target_file);
		}

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