<?php

namespace App\Http\Controllers;

use App\User;
use App\Group;
use App\UserGroup;
use App\Classes\Helper;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Theme;
use Input;
use Validator;
use App\Pegawai;
use App\masterdept;
use App\masterloc;
use DB;
use Auth;


class PegawaiController extends Controller
{
	
	public function __construct()
    {
        $this->middleware('auth');
    }
	public function GET_listUser()
    {
        $theme = Theme::uses('notebook')->layout('default');
        $theme->asset()->container('post-scripts')->usePath()->add('laravel', 'js/laravel.js');
        $theme->setMenu('pegawai.list');
		
		$pegawai = DB::table('pegawai as a')
					->join('masterdept as b','b.iddept','=','a.dept')
					->join('masterloc as c','c.idloc','=','a.loct')
					
					->join('mastergroupdept as d','b.iddept', '=', 'd.iddept')
					->join('mastergrouploc as e','c.idloc', '=', 'e.idloc')	
					->where('d.idgroupdept',Auth::user()	->idgroupdept)
					->where('e.idgrouploc',Auth::user()->idgrouploc)
					->where('d.flagactive','1')
					->where('e.flagactive','1')
		
					->select('a.id as id','a.nama as nama','c.ket as loct','b.ket as dept')
					->paginate(10);
		
        $params = array('Pegawai' => $pegawai);
        return $theme->scope('pegawai.list', $params)->render();
    }
	
    public function GET_createUserForm()
    {
        $theme = Theme::uses('notebook')->layout('default');
        $theme->setMenu('pegawai.create');

        $theme->asset()->container('post-scripts')->usePath()->add('typeahead', 'js/typeahead/bootstrap3-typeahead.js');
        $theme->asset()->container('post-scripts')->usePath()->add('custom', 'js/custom.js');
		$idcompcombo = masterdept::select('iddept','ket')->get()->lists('ket','iddept')->all();
        $idloccombo = masterloc::select('idloc','ket')->get()->lists('ket','idloc')->all();
    
		$pegawai = Pegawai::all();
		// $pegawai = DB::table('pegawai')
		$params = array('Pegawai' => $pegawai,'idcompcombo'=>$idcompcombo, 'idloccombo'=>$idloccombo);
       return $theme->scope('pegawai.create', $params)->render();
    }

	
	   public function POST_createUser(Request $request)
    {
		$pegawai = new Pegawai();
        $pegawai->nama = $request->nama;
        $pegawai->dept = $request->dept;
        $pegawai->loct = $request->loct;
        $pegawai->save();
        return redirect(route('pegawai.list'))->with('STATUS_OK', 'Nama`'.$request->input('nama').'` successfully created.');
        
     
    }
	public function GET_updateUserForm($id)
    {
        $theme = Theme::uses('notebook')->layout('default');
        $theme->setMenu('pegawai.update');
		
        $pegawai = pegawai::find($id);
		$idcompcombo = masterdept::select('iddept','ket')
						// ->where('iddept',$pegawai->dept)
						->get()->lists('ket','iddept')->all();
		$idloccombo = masterloc::select('idloc','ket')
						// ->where('iddept',$pegawai->dept)
						->get()->lists('ket','idloc')->all();
		$params = array('Pegawai' => $pegawai,'idcompcombo'=>$idcompcombo, 'idloccombo'=>$idloccombo);
        return $theme->scope('pegawai.update', $params)->render();
    }

    public function PUT_updateUser(Request $request, $id)
    {
        $theme = Theme::uses('notebook')->layout('default');
        $theme->setMenu('pegawai.create');
		$pegawai = Pegawai::findOrFail($id);
        $pegawai->nama = $request->nama;
        $pegawai->dept = $request->dept;
        $pegawai->loct = $request->loct;
        $pegawai->save();
		return redirect(route('pegawai.list'));
	}
	 public function DELETE_deleteUser($id)
    {
       
	   // $Pegawai = Pegawai::find('3');
	   // $Pegawai->delete();
       // return redirect(route('pegawai.list'));     
	    $user = Pegawai::find($id);
        if(!$user) {
            $msg = 'User not found.';
            return redirect(route('pegawai.list'))->with('STATUS_FAIL', $msg);
        }
        else {
            $name = $user->nama;
            $user->delete();
            return redirect(route('pegawai.list'))->with('STATUS_OK', 'Pegawai `'.$name.'` has been deleted.');
        }
    }
	
}
