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

class Mekanikid extends CI_Controller
{

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

	public function index()
	{
		$data['lokcabID'] = $this->Lokcab_m->get();
		$data['mekid'] = $this->Mekanikid_m->get();

		$this->template->load('template', 'mekanikid', $data);
	}

	public function addmekanik()
	{
		$this->form_validation->set_rules('no_ktp', 'no_ktp', 'trim|required|is_unique[tb_user.no_ktp]');

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

			$this->template->load('template', 'mekanikid', $data);
		} else {
			$post = $this->input->post(null, TRUE);
			$config['upload_path']          = './upload/foto/karyawan/';
			$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->Mekanikid_m->addmekanik($post);
				if ($this->db->affected_rows() > 0) {
					$this->session->set_flashdata('success', 'Data Karyawan Baru berhasil di Tambahkan');
				}
				redirect($_SERVER['HTTP_REFERER']);
			} else {
				$post['foto'] = $this->upload->data('file_name');

				$this->Mekanikid_m->addmekanik($post);
				if ($this->db->affected_rows() > 0) {
					$this->session->set_flashdata('success', 'Data Karyawan Baru Berhasil Disimpan');
				}
				redirect($_SERVER['HTTP_REFERER']);
			}
		}
	}

	public function editmekanik()
	{

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

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

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

			$this->template->load('template', 'editmekanik', $data);
		} else {
			$post = $this->input->post(null, TRUE);
			$config['upload_path']          = './upload/foto/karyawan/';
			$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'] = $this->upload->data('file_name');
				$item = $this->Mekanikid_m->get($post['id_user'])->row();
				if ($item->foto != null) {
					$target_file = './upload/foto/karyawan/' . $item->foto;
					unlink($target_file);
				}
				$this->Mekanikid_m->editmekanik($post);
				if ($this->db->affected_rows() > 0) {
					$this->session->set_flashdata('success', 'Data Team berhasil di Perbaharui');
				}
				redirect($_SERVER['HTTP_REFERER']);
			} else {
				$post['foto'] = null;
				$this->Mekanikid_m->editmekanik($post);
				if ($this->db->affected_rows() > 0) {
					$this->session->set_flashdata('success', 'Data Team berhasil di Perbaharui');
				}
				redirect($_SERVER['HTTP_REFERER']);
			}
		}
	}

	public function editkeahlian()
	{
		$this->form_validation->set_rules('mmetik', 'mmetik', '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', 'editmekanik');
		} else {
			$post = $this->input->post(null, TRUE);
			$this->Mekanikid_m->editkeahlian($post);
			if ($this->db->affected_rows() > 0) {
				$this->session->set_flashdata('success', 'Data Keahlian Karyawan berhasil DiPerbaharui');
			}
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function editpribadi()
	{
		$this->form_validation->set_rules('konkerja', 'konkerja', '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', 'editmekanik');
		} else {
			$post = $this->input->post(null, TRUE);
			$this->Mekanikid_m->editpribadi($post);
			if ($this->db->affected_rows() > 0) {
				$this->session->set_flashdata('success', 'Data pribadian Karyawan berhasil DiPerbaharui');
			}
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function delmekanikid($id)
	{
		$item = $this->Mekanikid_m->get($id)->row();
		if ($item->foto != null) {
			$target_file = './upload/foto/karyawan/' . $item->foto;
			unlink($target_file);
		}

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