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

class TambahProduk extends CI_Controller {

	public function __construct()
    {
		
        parent::__construct();
        $this->load->model("UserModel");
		$this->load->model("UmkmModel");
		$this->load->model("KategoriModel");
		$this->load->model("AbsensiModel");
		$this->load->model("AbsenModel");
		$this->load->model("ProdukModel");

		if($this->UserModel->isNotLogin()) {
			redirect(site_url('login'));
		} elseif(!($this->UserModel->isPemilik())) {
			redirect(site_url('admin/dashboard'));
		} 
		
		$get = $this->input->get();
		if($get['id_umkm']==NULL){
			redirect(site_url('user/dashboard'));
		}
    }

	public function index()
	{
		
		$get = $this->input->get();

		
		$umkm = $this->UmkmModel->getUmkmsById($get['id_umkm']);
		$pemilik = $this->UserModel->getUsersById($umkm->id_pemilik);
		$daftarkategori = $this->KategoriModel->getKategori();

		// var_dump($this->input->post());die();

		if($this->input->post()){
			$post=$this->input->post();
			if($post['produk']!=='default'){
					if($this->ProdukModel->save($umkm->id_umkm)){

						$current_produk = $this->ProdukModel->getLastProdukByIdUmkm($umkm->id_umkm);
		
						if(strlen($_FILES['foto_produk']['tmp_name'])>0){
							$config['upload_path']          = './assets/images/produk/';
							$config['allowed_types']        = 'jpg|jpeg|gif|png';
							$config['max_size']             = 100;
							$config['max_width']            = 1024;
							$config['max_height']           = 768;
							$config['file_name']			= $current_produk->id_produk;
							$config['overwrite'] 			= TRUE;
					
							$this->load->library('upload', $config);
		
							if ( ! $this->upload->do_upload('foto_produk')){
								$foto_produk = 'default.png';
								$this->ProdukModel->setFirstImage($current_produk->id_produk,$foto_produk);
								$data['error_upload'] = $this->upload->display_errors();
								$data['msg']="success";
							} else {
								$file = $this->upload->data();
		
								if($this->ProdukModel->setFirstImage($current_produk->id_produk,$file['file_name'])){
									$data['msg']="success";
								} else {
									$foto_produk = 'default.png';
									$this->ProdukModel->setFirstImage($current_produk->id_produk,$foto_produk);
									$data['error_upload'] = 'gagal upload foto';
									$data['msg']="success";
								}
							}
			
						} else {
									$foto_produk = 'default.png';
									$this->ProdukModel->setFirstImage($current_produk->id_produk,$foto_produk);
									$data['msg']="success";
						}
		
					} else {
						$data['msg']="error!";
					}
			} else {
				$data['msg']="error!";
			}
			
			
        }

		$cekabsensiakhir = $this->AbsensiModel->getLatestSignedAbsensiCountByUmkmId($umkm->id_umkm);

		if($cekabsensiakhir > 0 ){
			$absensiakhir = $this->AbsensiModel->getLatestSignedAbsensiByUmkmId($umkm->id_umkm);
			$aset = $absensiakhir->aset;
			$karyawan_l = $absensiakhir->karyawan_l;
			$karyawan_p = $absensiakhir->karyawan_p;
			$data['listabsensi'] = $this->AbsensiModel->getSignedAbsensiByUmkmId($umkm->id_umkm);
		} else {
			$aset = $umkm->aset_awal;
			$karyawan_l = $umkm->karyawan_l;
			$karyawan_p = $umkm->karyawan_p;
			$data['listabsensi'] = array();
		}
		
			
			$data['omset'] = $umkm->omset_awal;
			$data['karyawan_l'] = $karyawan_l;
			$data['karyawan_p'] = $karyawan_p;
			$data['aset'] = $aset;
			$data['daftarkategori'] = $daftarkategori;
			$data['umkm'] = $umkm;
			$data['pemilik'] = $pemilik;
			$data['user_logged'] = $this->session->userdata('user_logged');

			

			
			$this->load->view('user/tambahproduk',$data);
		
	}

	
}