<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Http\Request;
use DB;
use Validator;
use App\Http\Model\UserTypeModel;

class UserTypeController extends Controller
{
	public function __construct()
	{
		$this->User= new UserTypeModel;
	}
	public function LoadUserType()
    {
		$data['url']='usertype';
		$Mid=$this->User->UserTypePermission($data);
		$data['delete']= $Mid->Delete;
		$data['edit']= $Mid->Edit;
		$data['create']= $Mid->create;
		$status= 'Deleted';
		$data['UserType']=DB::table('ut_usertype')
		->LEFTJOIN("br_branch","ut_usertype.br_id","=","br_branch.br_id")
		->where([["ut_status","<>",$status]])
		->get();
		return view('usertype',compact('data'));
    }
    public function InsertUserTypePost(Request $req)
    {
		$ut_id = $req->input('ut_id');
		$usertype = $req->input('usertype');
		$description = $req->input('description');
		$status = $req->input('status');
		$branch = $req->input('branch');
		
		if($ut_id == 0)
		{
			$qinsert = DB::table('ut_usertype')->insertGetId(['br_id' => $branch,'ut_type' => $usertype,'ut_description' => $description,'ut_status' => $status]);	
			$modules=DB::table('modules')->get();
			foreach($modules as $modu)
			{
				$ModuleId=$modu->Mid;
				DB::table('permission')->insert(['UsrId' => $qinsert,'Mid' => $ModuleId]);
			}			
			return response()->json(['status' => '1', 'message' => trans('User type record inserted...')]);
		}
		else
		{
			$data = array('br_id'=>$branch,'ut_type'=>$usertype,'ut_description'=>$description,'ut_status'=>$status);
			$updatedetails = DB::table('ut_usertype')->where('ut_id',$ut_id)->update($data);
			if (!is_null($updatedetails))
			{
				return response()->json(['status' => '1', 'message' => trans('Record Updated Successfully.')]);
			}
			else
			{
				return response()->json(['status' => '0', 'message' => trans('Error in Updation.. Please Try again..')]);
			}
		}
    }
	
	//Edit User type records
	public function EditUserType(Request $req)
	{
		$id = $req->id;
		$sqlstatement = DB::table('ut_usertype')->where(['ut_id'=>$id])->get();
		return response()->json($sqlstatement);
	}
	//Delete User type records
	public function DeleteUserType(Request $req)
	{
		$id = $req->id;
		$status= 'Deleted';
		$data = array('ut_status'=>$status);
		
		$UpdateDetails = DB::table('ut_usertype')->where('ut_id',$id)->update($data);
		if (!is_null($UpdateDetails))
		{
			return response()->json(['status' => '1', 'message' => trans('Record Deleted Successfully.')]);
		}
		else
		{
			return response()->json(['status' => '0', 'message' => trans('Error in Deletion.. Please Try again..')]);
		}
	}
	//This will load User type details to the combo box
	public function LoadUserTypeList()
	{
		$status= 'Active';
		$reclist = DB::select( DB::raw("SELECT * FROM `ut_usertype` where ut_status = '$status'") );
		return response()->json($reclist);
	}
}