<?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\absensi;
use App\Pegawai;
use DB;
use Auth;


class absensiController 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('absensi.list');
		
		// echo 'grup dept'.Auth::user()->idgroupdept; echo '<br />';
		// echo 'grup loc'.Auth::user()->idgrouploc;
		
		$absensi 	= DB::table('absensi as a')
					->join('Pegawai as b','b.id','=','a.pegawai')
					->join('mastergroupdept as c','b.dept', '=', 'c.iddept')
					->join('mastergrouploc as d','b.loct', '=', 'd.idloc')	
					->where('c.idgroupdept',Auth::user()	->idgroupdept)
					->where('d.idgrouploc',Auth::user()->idgrouploc)
					->where('c.flagactive','1')
					->where('d.flagactive','1')
					// ->where('c.idgroupdept','2')
					// ->where('d.idgrouploc','5')
					->select('a.*','b.*')
					->paginate(10);
		
		// $absensi = absensi::all();
         $params = array('absensi' => $absensi,'pbulan'=> input::get('bulan'));
        return $theme->scope('absensi.list', $params)->render();
    }
	
	
		public function FilterAbsensi()
    {
        $theme = Theme::uses('notebook')->layout('default');
        $theme->asset()->container('post-scripts')->usePath()->add('laravel', 'js/laravel.js');
        $theme->setMenu('absensi.list');
		
		
		$absensi 	= DB::table('absensi as a')
					->join('Pegawai as b','b.id','=','a.pegawai')
					->join('mastergroupdept as c','b.dept', '=', 'c.iddept')
					->join('mastergrouploc as d','b.loct', '=', 'd.idloc')	
					->where('c.idgroupdept',Auth::user()	->idgroupdept)
					->where('d.idgrouploc',Auth::user()->idgrouploc)
					->where('c.flagactive','1')
					->where('d.flagactive','1')
					->where('bulan',input::get('bulan'))
					->select('a.*','b.*')
					->paginate(10);
		
	    $params = array('absensi' => $absensi,'pbulan'=> input::get('bulan'));
        return $theme->scope('absensi.list', $params)->render();
		

    }
	
	     // public function FilterAbsensi(Request $request)
    // {
      // $theme = Theme::uses('notebook')->layout('default');
      // $theme->asset()->container('post-scripts')->usePath()->add('laravel', 'js/laravel.js');
      // $theme->setMenu('absensi.list');

      // $absensi = DB::table('absensi')
              // ->select('Id','tahun','bulan','pegawai','finger','flapangan','absen','suratdok','terlambat','makan','pulcep')
              // ->where('bulan','like','%'.$request->input('bulan').'%')
              // ->paginate(100);
        // $params = array('absensi' => $absensi);
        // return $theme->scope('absensi.list', $params)->render();
	// }
	
    public function GET_createUserForm()
    {
        $theme = Theme::uses('notebook')->layout('default');
        $theme->setMenu('absensi.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');
		$listpegawai = Pegawai::select('id','nama')->get()->lists('nama','id')->all();
        $absensi = absensi::all();
		$params = array('absensi' => $absensi,'listpegawai'=> $listpegawai);
        return $theme->scope('absensi.create', $params)->render();
        $masterdept = masterdept::all();
		$params = array('masterdept' => $absensi);
       return $theme->scope('absensi.create', $params)->render();
	}

	
	   public function POST_createUser(Request $request)
    {
		$absensi = new absensi();
        $absensi->tahun = $request->tahun;
        $absensi->bulan = $request->bulan;
		$absensi->pegawai = $request->pegawai;
        $absensi->finger = $request->finger;
		$absensi->flapangan = $request->flapangan;
        $absensi->absen = $request->absen;
		$absensi->suratdok = $request->suratdok;
        $absensi->terlambat = $request->terlambat;
		$absensi->makan = $request->makan;
        $absensi->pulcep = $request->pulcep;
        $absensi->save();
        return redirect(route('absensi.list'))->with('STATUS_OK', 'bulan`'.$request->input('bulan').'` successfully created.');
        
     
    }
	public function GET_updateUserForm($id)
    {
        $theme = Theme::uses('notebook')->layout('default');
        $theme->setMenu('absensi.update');
		
		$listpegawai = Pegawai::select('id','nama')->get()->lists('nama','id')->all();
		
        $absensi = absensi::find($id);
		$params = array('params'=>$absensi,'listpegawai'=>$listpegawai);
        return $theme->scope('absensi.update', $params)->render();
    }

    public function PUT_updateUser(Request $request, $id)
    {
        $theme = Theme::uses('notebook')->layout('default');
        $theme->setMenu('absensi.update');
		
		$absensi = absensi::findOrFail($id);
        $absensi->tahun = $request->tahun;
        $absensi->bulan = $request->bulan;
		$absensi->pegawai = $request->pegawai;
        $absensi->finger = $request->finger;
		$absensi->flapangan = $request->flapangan;
        $absensi->absen = $request->absen;
		$absensi->suratdok = $request->suratdok;
        $absensi->terlambat = $request->terlambat;
		$absensi->makan = $request->makan;
        $absensi->pulcep = $request->pulcep;
        $absensi->save();
		return redirect(route('absensi.list'));
	}
	 public function DELETE_deleteUser($id)
    {
       
	   // $absensi = absensi::find('3');
	   // $absensi->delete();
       // return redirect(route('absensi.list'));     
	    $user = absensi::find($id);
        if(!$user) {
            $msg = 'absensi not found.';
            return redirect(route('absensi.list'))->with('STATUS_FAIL', $msg);
        }
        else {
            $name = $user->pegawai;
            $user->delete();
            return redirect(route('absensi.list'))->with('STATUS_OK', 'absensi `'.$name.'` has been deleted.');
        }
    }
	
}
