<?php

namespace App\Http\Controllers\user;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\Datatables\Datatables;
use DB;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
    //
    public function __construct(){      
        $this->middleware(function ($request, $next) {
        $url = request()->segment(1);
        $kode_group = Session::get('kode_group');
        $d_menu = DB::table("tb_menu as a")->join("tb_role as b","a.kode_menu","b.kode_menu")->where([['a.link_menu',$url],['b.kode_group',$kode_group],['b.status_role','1'],['a.active','1']])->orderBy('a.sort_menu','asc')->count();
        if(Session::get('decode') <> 1)
         {
            Session::flash('message', 'Silahkan login dengan username dan password anda.!!');
             return redirect()->to('login');
         }
         if($d_menu == 0){
            Session::flush();
            Session::flash('message', 'Anda Tidak Punya Akses ke Halaman ini.!!');
             return redirect()->to('login');
         }
        return $next($request); 
        });
    }

    public function index(){
        $data['proses'] = DB::table('ms_proses as mp')->leftJoin('ms_proses as mp2','mp.parent','mp2.id_proses')->select('mp.nama_proses as nama_proses','mp2.nama_proses as nama_proses2','mp.id_proses')->orderBy('mp.id_proses','ASC')->get();
    	return view('user.UserList')->with('data',$data);
    }

    public function tampil(){
    	DB::statement(DB::raw('set @rownum=0'));
		$d_data = DB::table('tb_user as tu')->leftjoin('ms_gudang as mg','tu.id_gudang','mg.id_gudang')->leftjoin('ms_lokasi_produksi as ml','tu.id_lokasi','ml.id_lokasi')->join('tb_group as tg','tu.kode_group','tg.kode_group')->leftjoin('ms_proses as mp','tu.id_proses','mp.id_proses')->select([DB::raw('tu.*, mg.nama_gudang, ml.nama_lokasi, tg.nama_group, mp.nama_proses')]);
        if($d_data->count() > 0 ){
            foreach ($d_data->get() as $row) {
                # code...
                $status = '<label class="label bg-success-400">Aktif</label>';
                if($row->is_aktif == 2){
                    $status = '<label class="label bg-danger-400">Tidak Aktif</label>';
                }

                $id_gudang = explode(',',$row->id_gudang);
                $d_gudang = DB::table('ms_gudang')->whereIn('id_gudang',$id_gudang);
                $nama_gudang = array();
                if($d_gudang->count() > 1){
                    foreach($d_gudang->get() as $d){
                        $nama_gudang[] = " ".$d->nama_gudang;      
                               
                    }
                }else{
                    $nama_gudang[] = $row->nama_gudang;
                }

                $id_proses = explode(',', $row->id_proses);
                $d_proses = DB::table('ms_proses')->whereIn('id_proses',$id_proses);
                $nama_proses= array();
                if($d_proses->count() > 1){
                    foreach ($d_proses->get() as $d) {
                        $nama_proses[] = " ".$d->nama_proses;
                    }
                }else{
                    $nama_proses[] = $row->nama_proses;
                }
                $password = "";
                for($i=0; $i < strlen($row->password); $i++){
                    $password .= "*";
                }
                
                $arr[] = array("kode_user" => $row->kode_user,
                                "nama" => $row->nama,
                                "username" => $row->username,
                                "password" => $password,
                                "nama_group" => $row->nama_group,
                                "nama_lokasi" => $row->nama_lokasi,
                                "nama_gudang"=> $nama_gudang,
                                "nama_proses" => $nama_proses,
                                "status" => $status,
                                "aksi" => '<div class="btn-group"><button class="btn bg-warning-400" onclick="edit_table('.$row->kode_user.')"><i class="icon-pencil"></i> </button>
                                               <button class="btn bg-danger-800" type="button" onclick="hapus('.$row->kode_user.')"><i class="icon-eraser"></i></button></div>'.
                        '<input type="hidden" id="table_id'.$row->kode_user.'" value="'.$row->kode_user.'">'.
                        '<input type="hidden" id="table_nama'.$row->kode_user.'" value="'.$row->nama.'">'.
                        '<input type="hidden" id="table_username'.$row->kode_user.'" value="'.$row->username.'">'.
                        '<input type="hidden" id="table_password'.$row->kode_user.'" value="'.$row->password.'">'.
                        '<input type="hidden" id="table_idgudang'.$row->kode_user.'" value="'.$row->id_gudang.'">'.
                        '<input type="hidden" id="table_idlokasi'.$row->kode_user.'" value="'.$row->id_lokasi.'">'.
                        '<input type="hidden" id="table_idproses'.$row->kode_user.'" value="'.$row->id_proses.'">'.
                        '<input type="hidden" id="table_group'.$row->kode_user.'" value="'.$row->kode_group.'">'.
                        '<input type="hidden" id="table_status'.$row->kode_user.'" value="'.$row->is_aktif.'">'.
                        '<input type="hidden" id="table_gudang'.$row->kode_user.'" value="'.$row->is_aktif.'">');

            }
        }else{
            $arr = array();
        }

        $data = new Collection($arr);
        return Datatables::of($data)->make(true);
        
    }

    function get_gudang(){
		$arr = array();

        $d_data = DB::table('ms_gudang');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_gudang', 'asc')->get() as $d){
                $arr['gudang'][] = array('id_gudang' => $d->id_gudang,
                                'nama_gudang' => $d->nama_gudang);
            }
        }else{
            $arr['gudang'] = array();
        }

		$d_data = DB::table('ms_lokasi_produksi');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_lokasi', 'asc')->get() as $d){
                $arr['lokasi'][] = array('id_lokasi' => $d->id_lokasi,
                                'nama_lokasi' => $d->nama_lokasi);
            }
        }else{
            $arr['lokasi'] = array();
        }

        $d_data = DB::table('tb_group');
        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_group', 'asc')->get() as $d){
                $arr['group'][] = array('kode_group' => $d->kode_group,
                                'nama_group' => $d->nama_group);
            }
        }else{
            $arr['group'] = array();
        }

		return response()->json($arr);
		
    }

    function simpan(Request $request){
    	$id = $request->get('popup_id');
    	$data['nama'] = $request->get('popup_nama');
    	$data['username'] = $request->get('popup_username');
    	$data['password'] = $request->get('popup_password');
    	$data['id_gudang'] = is_null($request->get('popup_idgudang')) ? "":implode(',',$request->get('popup_idgudang'));
    	$data['id_lokasi'] = $request->get('popup_idlokasi');
        $data['id_proses'] = is_null($request->get('popup_idproses')) ? "":implode(',',$request->get('popup_idproses')); //implode(',',$request->get('popup_idproses'))
    	$data['is_aktif'] = $request->get('popup_status');
    	$data['kode_group'] = $request->get('popup_group');

    	if($id == ''){
    		DB::table('tb_user')->insert($data);
    	}else{
    		DB::table('tb_user')->where('kode_user',$id)->update($data);
    	}

    	return response()->json(array('status'=> '1'));
    }

    function hapus(Request $request){
    	$data['kode_user'] = $request->get('id');
    	DB::table('tb_user')->where($data)->delete();
    }

    function myprofil($id){
        $c_data = DB::table('tb_user as tu')->leftjoin('ms_lokasi_produksi as ml','tu.id_lokasi','ml.id_lokasi')->leftjoin('ms_gudang as mg','tu.id_gudang','mg.id_gudang')->leftjoin('ms_proses as mp','tu.id_proses','mp.id_proses')->join('tb_group as tg','tu.kode_group','tg.kode_group')->where('tu.kode_user','=',$id)->select(DB::raw('tu.*, ml.nama_lokasi, tg.nama_group, mg.nama_gudang, mp.nama_proses'));
        $d_data = $c_data->first();
        if($c_data->count() > 0){
            $id_gudang = explode(',',$d_data->id_gudang);
            $d_gudang = DB::table('ms_gudang')->whereIn('id_gudang',$id_gudang);
            $data['gudang'] = array();
                if($d_gudang->count() > 1){
                    foreach($d_gudang->get() as $d){
                        $data['gudang'][] = " ".$d->nama_gudang;       
                    }
                }else{
                        $data['gudang'][] = $d_data->nama_gudang;
                }
            $id_proses = explode(',',$d_data->id_proses);
            $d_proses = DB::table('ms_proses')->whereIn('id_proses',$id_proses);
            $data['proses'] = array();
                if($d_proses->count() > 1){
                    foreach ($d_proses->get() as $d){
                        $data['proses'][] = " ".$d->nama_proses;
                    }
                }else{
                        $data['proses'][] = $d_data->nama_proses;
                }
        }
        $data['d_proses'] = DB::table('ms_proses as mp')->leftJoin('ms_proses as mp2','mp.parent','mp2.id_proses')->select('mp.nama_proses as nama_proses','mp2.nama_proses as nama_proses2','mp.id_proses')->where('mp.jenis', '0')->orderBy('mp.id_proses','ASC')->get();
        $data['d_gudang'] = DB::table('ms_gudang')->get();
        $data['d_lokasi'] = DB::table('ms_lokasi_produksi')->get();
        $data['d_group'] = DB::table('tb_group')->get();
        $data['data'] = $this->data($d_data);
        return view('user.MyProfilpage')->with('data',$data);
        //print_r($data['gudang']);
    }

    function data($data = array()){
        if(count($data) >0){
        $store['kode_user']     = $data->kode_user;
        $store['nama']          = $data->nama;
        $store['username']      = $data->username;
        $store['password']      = $data->password;
        $store['group']         = $data->kode_group;
        $store['nama_group']    = $data->nama_group;
        $store['id_gudang']     = $data->id_gudang;
        $store['id_lokasi']     = $data->id_lokasi;
        $store['id_proses']     = $data->id_proses;
        $store['nama_gudang']   = $data->nama_gudang;
        $store['nama_lokasi']   = $data->nama_lokasi;
        $store['nama_proses']   = $data->nama_proses;
        $store['status']        = $data->is_aktif;
        $store['foto']          = $data->foto;
        }else{
        $store['kode_user']     = "";
        $store['nama']          = "";
        $store['username']      = "";
        $store['password']      = "";
        $store['group']         = "";
        $store['nama_group']    = "";
        $store['id_gudang']     = "";
        $store['id_lokasi']     = "";
        $store['id_proses']     = "";
        $store['nama_gudang']   = "";
        $store['nama_lokasi']   = "";
        $store['nama_proses']   = "";
        $store['status']        = "";
        $store['foto']          = "";
        }
        return $store;
    }

    function simpan_myprofil(Request $request){
        if($request->get('popup_nama')!="" || $request->get('popup_kelamin')!=""){
        $id = $request->get('popup_id_edit');
        if(!empty($_FILES['popup_foto']['tmp_name'])){
                $tmp =$_FILES['popup_foto']['tmp_name'];
                $nama_file = $id.'_'.date('dmY').'_'.$_FILES['popup_foto']['name'];
                $nf  ='images/profil/'.$nama_file;
                move_uploaded_file($tmp,$nf);
                @unlink($request->get('popup_namefoto_hidden'));
            }else{
                $nf='images/placeholders/placeholder.jpg';
            }
        $data['nama'] = $request->get('popup_nama');
        $data['foto'] = $nf;
        if($id==''){
            DB::table('tb_user')->insert($data);
        }else{
            DB::table('tb_user')->where('kode_user',$id)->update($data);
        }
            Session::put('foto',$nf);
            return redirect('myprofil/'.$id);
        }else{
            return redirect()->back()->withInput($request->all)->withErrors($request->all);
        }

    }


}
