<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use App\Http\Requests;

class SigninController extends Controller
{   
   

    //
    public function __construct(){
        $this->middleware(function ($request, $next) {
        if(Session::get('kode_group') != '' && Session::get('kode_user') != ''  && Session::get('decode') == 1)
         {
             return redirect('/');
         }
        return $next($request); 
        });
    }

    public function index(){
    	return view('admin/login_admin');
    }

    public function doLogin(Request $request){
    	$username = $request->username;
    	$password = $request->password;
    	$data = DB::table('tb_user')->where([['username',$username],['password',$password],['is_aktif','1']]);
        $cek = DB::table('tb_user')->where('username',$username);
    	if($data->count() > 0 && $cek->count() > 0){
            $d = $data->first();
            Session::put('kode_user',$d->kode_user);
    		Session::put('username',$username);
    		Session::put('password',$password);
    		Session::put('nama',$d->nama);
            Session::put('id_gudang',$d->id_gudang);
            Session::put('id_lokasi',$d->id_lokasi);
            Session::put('id_proses',$d->id_proses);
            Session::put('is_aktif',$d->is_aktif);
            Session::put('kode_group',$d->kode_group);
            Session::put('foto',$d->foto);
            Session::put('decode',1);
            Session::flash('message','Berhasil Login!!');
            
            return redirect('/');
        }elseif($cek->count() == 0){
            Session::flash('message','Username Belum terdaftar!!');
            return redirect('login');
    	}else{
            Session::flash('message','Username dan Passowrd Salah, Coba lagi!!');
			return redirect('login');
    	}

    	
    }

    public function logout(){
        Session::flush();
    	return redirect('login');
    }

    function get_menu(){
        $d_data = DB::table('tb_menu as tm')->join('tb_role as tr','tm.kode_menu','tr.kode_menu')->where('tr.status_role','1')->where('tm.active','1')->where('tm.parent_menu','0')->where('tr.kode_group','3')->select(DB::Raw('tm.*'));

        if($d_data->count() > 0){
            foreach($d_data->orderBy('tm.sort_menu','asc')->get() as $d){
                $arr[] = ['kode_menu' => $d->kode_menu,
                        'nama_menu' => $d->nama_menu,
                        'link_menu' => $d->link_menu,
                        'sort_menu' => $d->sort_menu,
                        'icon_menu' => $d->icon_menu,
                        'parent' => $d->parent_menu,
                        'cek' => $this->cek_menu($d->kode_menu,$d->parent_menu)];
            }
        }else{
            $arr = array();
        }
        return response()->json($arr);
    }

    function cek_menu($kode_group,$parent){
        $cek = DB::table('tb_menu')->where('parent_menu',$kode_group);
        $jumlah = $cek->count();
        return $jumlah;
    }

}
