<?php
class Peminjam extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('m_siplabs');
		if (null==$this->session->level) {
			redirect('auth');
		}else{
			if ($this->session->level!="Peminjam") {
				redirect('auth','refresh');
			}
		}
	}

	public function index()
	{
		$data['title'] = 'Peminjam';
		$data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
		
		$data['ruangan'] = $this->m_siplabs->get_data('ruangan')->result();
		// $data['ruangan'] = $this->db->get('ruangan')->result();
		$this->load->view('templates/header', $data);
		$this->load->view('peminjam/dashboard', $data);
		$this->load->view('templates/footer');
	}

	// PINJAM
	public function pinjam()
	{
		$id_user = $this->input->post('id_user');
		$username = $this->input->post('username');
		$id_ruangan = $this->input->post('id_ruangan');
		$jam_mulai = $this->input->post('jam_mulai');
		$jam_berakhir = $this->input->post('jam_berakhir');
		$tanggal = $this->input->post('tanggal');
		$keterangan = $this->input->post('keterangan');

		$array = [
			'id_peminjaman' => null,
			'id_user' => $id_user,
			'id_ruangan' => $id_ruangan,
			'jam_mulai' => $jam_mulai,
			'jam_berakhir' => $jam_berakhir,
			'tanggal' => $tanggal,
			'keterangan' => $keterangan,
			'status_peminjaman' => 0
		];

		$this->m_siplabs->add_data('peminjaman', $array);
		$this->session->set_flashdata('message', '<div class="alert alert-success alert-dismissible">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
			<h5><i class="icon fas fa-check"></i> Pemesanan berhasil!</h5>
			<span>Harap tunggu konfirmasi admin untuk menerima jadwal</span></div>');
		$tanggalformatted=date_create($tanggal);

		$admin = $this->db->get_where('user', ['level'=>'Admin'])->row_array();

		$url = 'https://api.whatsapp.com/send?phone=+62'.$admin['no_telp'].'&text=Assalumalaikum%20Admin.%0A%0ARequest%20Peminjaman%20dengan%20Info%0Ausername%20%3A%20'.$username.'%0Ajam%20mulai%20%3A%20'.$jam_mulai.'%0Ajam%20selesai%20%3A%20'.$jam_berakhir.'%0Atanggal%20%3A%20'.date_format($tanggalformatted, 'd / m / Y').'%0Aketerangan%20%3A%20'.$keterangan;
		redirect($url);
	}

	public function batalpinjam($id_user, $id_ruangan)
	{
		$batalpinjam = $this->m_siplabs->deletedata('peminjaman', array('id_user'=>$id_user, 'id_ruangan'=>$id_ruangan));
		if ($batalpinjam) {
			$this->session->set_flashdata('message', '<div class="alert alert-success alert-dismissible">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<h5><i class="icon fas fa-check"></i> Dibatalkan!</h5></div>');
			redirect('peminjam');
		}else{
			$this->session->set_flashdata('message', '<div class="alert alert-danger alert-dismissible">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<h5><i class="icon fas fa-ban"></i> Gagal membatalkan peminjaman!</h5>
				<span>Hubungi admin untuk detailnya!</span></div>');
			redirect('peminjam');
		}
	}

	public function jadwal()
	{
		$data['title'] = 'Jadwal';
		$data['user'] = $this->session->userdata('id_user');
		$data['id_user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
		$data['jadwal'] = $this->db->query("SELECT * FROM jadwal INNER JOIN peminjaman, ruangan, user WHERE jadwal.id_peminjaman=peminjaman.id_peminjaman 
			AND peminjaman.id_ruangan=ruangan.id_ruangan
			AND peminjaman.id_user=user.id_user
			AND peminjaman.status_peminjaman!=0
			AND jadwal.status_jadwal !=3
			")->result();
		$this->load->view('templates/header', $data);
		$this->load->view('peminjam/jadwal', $data);
		$this->load->view('templates/footer');
	}

	public function inventaris()
	{
		$data['title'] = 'Inventaris';
		$data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
		$data['inventaris'] = $this->db->get('inventaris')->result();
		$this->load->view('templates/header', $data);
		$this->load->view('peminjam/inventaris', $data);
		$this->load->view('templates/footer');
	}

	public function pinjam_inv()
	{
		$id_user = $this->input->post('id_user');
		$username = $this->input->post('username');
		$id_inv = $this->input->post('id_inv');
		$nama_inv = $this->input->post('nama_inv');
		$tgl_pinjam = $this->input->post('tgl_pinjam');
		$yg_melayani = $this->input->post('melayani');
		$array = [
			'id_user' =>$id_user,
			'id_inv' =>$id_inv,
			'tgl_pinjam' =>$tgl_pinjam,
			'status'  => 0,
			'yg_melayani' => $yg_melayani
		];

		$this->m_siplabs->add_data('pinjam_inv', $array);
		$this->session->set_flashdata('message', '<div class="alert alert-success alert-dismissible">
			<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
			<h5><i class="icon fas fa-check"></i> Pemesanan berhasil!</h5>
			<span>Harap tunggu konfirmasi admin untuk Menerima Peminjaman</span></div>');
		$tanggalformatted=date_create($tanggal);

		$admin = $this->db->get_where('user', ['level'=>'Admin'])->row_array();

		$url = 'https://api.whatsapp.com/send?phone=+62'.$admin['no_telp'].'&text=Assalumalaikum%20Admin.%0A%0ARequest%20Peminjaman%20dengan%20Info%0Ausername%20%3A%20'.$username.'%0ANama%20Inventaris%20%3A%20'.$nama_inv.'%0ATanggal%20Permintaan%20Peminjaman%20%3A%20'.$tgl_pinjam.'%0Atanggal%20%3A%20'.date_format($tanggalformatted, 'd / m / Y');
		redirect($url);
	}

	public function batalpinjaminv($id_user,$id_inv)
	{	$batalpinjam = $this->m_siplabs->deletedata('pinjam_inv', array('id_user'=>$id_user, 'id_inv'=>$id_inv));
		if ($batalpinjam) {
			$this->session->set_flashdata('message', '<div class="alert alert-danger alert-dismissible">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<h5><i class="icon fas fa-check"></i> Dibatalkan!</h5></div>');
			redirect('peminjam/inventaris');
		}else{
			$this->session->set_flashdata('message', '<div class="alert alert-danger alert-dismissible">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<h5><i class="icon fas fa-ban"></i> Gagal membatalkan peminjaman!</h5>
				<span>Hubungi admin untuk detailnya!</span></div>');
			redirect('peminjam/inventaris');
		}
	}

	public function profil()
	{
		$data['title'] = "Profil Saya";
		$data['user'] = $this->session->userdata('id_user');
		$data['username'] = $this->session->userdata('username');
		$data['profil'] = $this->db->get_where('user', ['id_user'])->row_array();
		$this->load->view('templates/header', $data);
		$this->load->view('peminjam/profil', $data);
		$this->load->view('templates/footer');
	}

	public function editprofil()
	{
		$id_user = $this->input->post('id_user');
		$username = $this->input->post('username');
		$nama_lengkap = $this->input->post('nama_lengkap');
		$bio = $this->input->post('bio');
		$no_telp = $this->input->post('no_telp');
		$nip = $this->input->post('nip');
		$uploaded = $_FILES['image']['name'];
		$imagetype = end(explode('.', $uploaded));
		$image = uniqid() . '.' . $imagetype;

		$cekusername = $this->db->get_where('user', ['id_user' != $id_user])->row_array();
		if ($cekusername['username'] != $username) {
			if (!empty($uploaded)) {
				$config['upload_path'] = './files/userprofil/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']  = '500';
				$config['file_name'] = $image;

				$this->load->library('upload', $config);

				if (!$this->upload->do_upload('image')) {
					$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">' . $this->upload->display_errors() . '</div>');
					redirect('admin/myprofile/' . $id_user);
				} else {
					$userdata = $this->db->get_where('user', ['id_user' => $id_user])->row_array();
					if (!empty($userdata['image'])) {
						$path = 'files/userprofil/' . $userdata['image'];
						unlink($path);
					}

					$array = [
						'username' => $username,
						'nama_lengkap' => $nama_lengkap,
						'bio' => $bio,
						'nip' => $nip,
						'no_telp' => $no_telp,
						'image' => $image
					];
					$this->upload->do_upload($image);
					$this->db->update('user', $array, ['id_user' => $id_user]);
					$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Sukses</div>');
					redirect('peminjam' . $id_user);
				}
			} else {
				$array = [
					'username' => $username,
					'nama_lengkap' => $nama_lengkap,
					'bio' => $bio,
					'nip' => $nip,
					'no_telp' => $no_telp
				];
				$this->db->update('user', $array, ['id_user' => $id_user]);
				$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Sukses</div>');
				redirect('peminjam' . $id_user);
			}
		} else {
			$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Username sudah ada!</div>');
			redirect('peminjam' . $id_user);
		}
	}
}
