<?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 App\Http\Model\CategoryModel;
	use Validator;
	
	class CategoryController extends Controller
	{
		public function __construct()
		{
			$this->Category= new CategoryModel;
		}
		public function LoadCategory()
		{
			
			$data['url']='category';
			$Mid=$this->Category->CategoryPermission($data);
			$data['delete']= $Mid->Delete;
			$data['edit']= $Mid->Edit;
			$data['create']= $Mid->create;
			$status= 'Deleted';
			$data['Category'] = DB::select( DB::raw("SELECT a.*,b.cat_name as maincat from cat_category as a left join cat_category as b on a.maincat_id=b.cat_id where a.cat_status != '$status'") );
			return view('category',compact('data'));
		}
		
		public function InsertCategory(Request $req)
		{
			$cat_id = $req->input('cat_id');
			$categoryname= $req->input('categoryname');
			$maincategory= $req->input('maincategory');
			$description= $req->input('description');
			$cat_type="";
			$status = $req->input('status');
			
			$data = array('maincat_id'=>$maincategory,'cat_type'=>$cat_type,'cat_name'=>$categoryname,'cat_description'=>$description,'cat_status'=>$status);
			if($cat_id == 0)
			{
				$insertdetails = DB::table('cat_category')->insert($data);
				if (!is_null($insertdetails))
				{
					return response()->json(['status' => '1', 'message' => trans('Record Inserted Successfully.')]);
				}
				else
				{
					return response()->json(['status' => '0', 'message' => trans('Error in Insertion.. Please Try again..')]);
				}
			}
			else
			{
				$updatedetails = DB::table('cat_category')->where('cat_id',$cat_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..')]);
				}
			}
			
			
		}
		public function LoadMainCategory()
		{
			$status= 'Deleted';
			$maincategory = DB::select( DB::raw("SELECT * FROM `cat_category` where cat_status != '$status' and maincat_id=0") );
			return response()->json($maincategory );
		}
		
		public function EditCategory(Request $req)
		{
			$id = $req->id;
			$category = DB::table('cat_category')->where(['cat_id'=>$id])->get();
			return response()->json($category);
		}
		
		public function DeleteCategory(Request $req)
		{
			$id = $req->id;
			$status= 'Deleted';
			$data = array('cat_status'=>$status);
			
			$UpdateDetails = DB::table('cat_category')->where('cat_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..')]);
			}
		}
		
	}
