<?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\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Database\Eloquent\Model;
use App\Models\Category_model;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\DB;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Session;
class Category_controller extends BaseController
{
   
     public $category ; 
	 public function __construct( $category=NULL)
    {
      //  $this->category = new Category_model();
     $this->db=  DB::table("tblchcategory");
    }
	 
	 
	public function  index(Request $request, $page = 1)
	 {
	  
	
	   $data=array(
						'pagetitle' => 'View Channel Category ',
						'action' =>'view',
						'result' =>  $this->db->get()->where("status",'1'),
						
	                );
	   
	   
	   return view("category",$data);
	 }
	 
	public function addaction()
	 {
		 
		$data=array(
	     'pagetitle' => 'Add Channel Category' ,
		 'action' =>'Add'
	    );
		 
	   return view("category",$data);
     }
	
	 public function create(Request $request){ 
		$rules = [
			'chcategory_title' => 'required|string|min:3|max:255', 
			'chchcategory_id' => 'required|string|min:3|max:255', 
		         ];
		 $validator = Validator::make( $request->all(),$rules);  
		 
		if ($validator->fails()) {
			return redirect('addaction')
			->withInput()->with('failed',"operation failed")
			->withErrors($validator);
		}
		else{
		    $postdata = $request->input();
			try{
				  
			   $data=array(
			     "chcategory_title" => $postdata['chcategory_title'],
			     "chchcategory_id" => $postdata['chchcategory_id'],
                 "PublishStatus" => $postdata['PublishStatus'],
				 "create_date" => date("Y-m-d")
			   
			    ); 
				
			  $affectedrow= $this->db->insert( $data);
			   return redirect('addaction')->with('status',"Insert successfully");
			}
			catch(Exception $e){
				return redirect('addaction')->with('failed',"operation failed");
			}
		}
		 
    }
	 
	 
	 
     public function editsuccess(Request $request){
        
        $postdata = $request->input();
		 $id= $postdata["heid"];
		    $rules = [
				'chcategory_title' => 'required|string|min:3|max:255', 
				'chchcategory_id' => 'required|string|min:3|max:255', 
		         ];
		$validator = Validator::make($request->all(),$rules);
		if ($validator->fails()) {
			return redirect('Editaction/'.$id)
			->withInput()->with('failed',"operation failed")
			->withErrors($validator);
		}
		else{
		   
			try{
			 
			   $data=array(
				"chcategory_title" => $postdata['chcategory_title'],
				"chchcategory_id" => $postdata['chchcategory_id'],
				"PublishStatus" => $postdata['PublishStatus'],
			  // 'userid'=>$request->session()->get('userid');
			    ); 
				
			  $affectedrow= $this->db->where('id', $id)
                 ->update($data); 
			   return redirect('/Cat')->with('status',"Update successfully");
			}
			catch(Exception $e){
				return redirect('Editaction/'.$id)->with('failed',"operation failed");
			}
		}
		 
    }
	 
	  
	
	 public function edit($id=NULL)
	 {
	    $eid=$id;
		$chcategory_title="";
		$chchcategory_id="";
		$PublishStatus="";
		$result= $this->db->find($id); 
		if($result!=""){ 
		 	 $chcategory_title=$result->chcategory_title; 
	    	 $chchcategory_id=$result->chchcategory_id ; 
			 $PublishStatus=$result->PublishStatus ; 
		 }
		 
		$data=array(
	     'pagetitle' => 'Edit Categoy' ,
		 'action' =>'Edit',
		 'result' =>$result,
		 'chcategory_title'=>$chcategory_title,
		 'Eid'=>$eid,
		 'chchcategory_id' =>$chchcategory_id ,
		 'PublishStatus' =>$PublishStatus 
	    );
		 
	   return view("category",$data);
     }
	
	   public function destroy(Request $request){
        $affectedrow=0;
         $postdata = $request->input();
		  try{
			 
			 for($i=1;$i<$postdata["totalcount"];$i++)
			   { $id= isset($postdata["chk_".$i])==true?$postdata["chk_".$i]:"";
				 if($id!="")
				     {
						  $data=array(
			                      "status" => 0, 
			                       ); 
				
			       $affectedrow=  $this->db->where('id', $id)
                 ->update($data); 
						 
					  }
				   
			   } 
			  if($affectedrow>0)
			  {
			   return redirect('/Cat')->with('status',"Successfully Deleted");
			  }
			  else
			  {
				 return redirect('/Cat'); 
			  }
			}
			catch(Exception $e){
				return redirect('/Cat/')->with('failed',"operation failed");
			}
		 
		 
    }
	function categorydetails($id=NULL)
	  {
		  $result= $this->db->find($id); 
		  $chcategory_title="";
		  $chchcategory_id="";
		  $PublishStatus="";
		  $result= $this->db->find($id); 
		  if($result!=""){ 
				$chcategory_title=$result->chcategory_title; 
			   $chchcategory_id=$result->chchcategory_id ; 
			   $PublishStatus=$result->PublishStatus ; 
		   }
		   
		  $str="<table class='table' width='100%'>";
		   $str.="<tr>
		      <td><strong>Title</strong></td>
			  <td>".$chcategory_title."</td>
			</tr>";
		   $str.="<tr>
		      <td><strong>ID</strong></td>
			  <td>".$chchcategory_id."</td>
			</tr>";

			$str.="<tr>
			<td><strong>Publish</strong></td>
			<td>".$PublishStatus."</td>
		  </tr>";


			$str.="</table>";
		echo $str;	
           			
	  }
	  
	
}
