<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use App\Models\pr_resolution;
use App\Models\area_master;
use DB;
use Validator;


class PrResolutionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
          if($request->url!='dashboard'){
            Session::forget('dashboardurl');

          }
           
        DB::enableQueryLog();
       $pr_district_wise=pr_resolution::with(['area_district' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(DB::raw("count(district_id) as count"),DB::raw("SUM(light_18w) as 18w"),DB::raw("SUM(light_35w) as 35w"),DB::raw("SUM(light_70w) as 70w"),DB::raw("SUM(light_110w) as 110w"),DB::raw("SUM(light_190w) as 190w"),'district_id')->groupby('district_id')->get()->toArray();
        return view('backend.pr_resolution.pr_district_wise',compact('pr_district_wise'));
        
    }
    public function mandal_pr_list(Request $request)
    {

        
        $mandal_pr_list=pr_resolution::with(['area_mandal' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(DB::raw("count(mandal_id) as count"),DB::raw("SUM(light_18w) as 18w"),DB::raw("SUM(light_35w) as 35w"),DB::raw("SUM(light_70w) as 70w"),DB::raw("SUM(light_110w) as 110w"),DB::raw("SUM(light_190w) as 190w"),'mandal_id')->groupby('mandal_id')->where('district_id',$request->id)->get()->toArray();
        $district  = area_master::where('id',$request->id)->first();
        return view('backend.pr_resolution.pr_mandal_list',compact('mandal_pr_list','district'));
    }
     public function gram_pr_list(Request $request)
    {
        $gram_pr_list=pr_resolution::with(['area_gram' => function ($query) {
        $query->select('id', 'area_name');
        }])->select(DB::raw("count(gram_id) as count"),DB::raw("SUM(light_18w) as 18w"),DB::raw("SUM(light_35w) as 35w"),DB::raw("SUM(light_70w) as 70w"),DB::raw("SUM(light_110w) as 110w"),DB::raw("SUM(light_190w) as 190w"),'gram_id')->groupby('gram_id')->where('mandal_id',$request->id)->get()->toArray();
        //dd(gram_pr_list);
         $district  = area_master::with('parent_details')->where('id',$request->id)->first();
        // dd($district);
        return view('backend.pr_resolution.pr_gram_list',compact('gram_pr_list','district'));
    }
    public function gram_approve_list(Request $request)
    {
        $gram_pr_list=pr_resolution::with(['area_gram' => function ($query) {
        $query->select('id', 'area_name');
        }])->where('gram_id',$request->id)->get()->toArray();
        $district  = area_master::with(['parent_details','super_details'])->where('id',$request->id)->first();
         return view('backend.pr_resolution.gram_approve_list',compact('gram_pr_list','district'));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function add_resolution(Request $request)
    {

         $jilla=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    
                    ->get();
       return view('backend.pr_resolution.add_resolution',compact('jilla'));
        
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function resolutionstore(Request $request)
    {
        $validation = Validator::make(
            $request->all(),
            [
                'district_name' => 'required',
                'taluka_name' => 'required',
                'gram_name' => 'required',
                '18_w' => 'required',
                '35_w' => 'required',
                '70_w' => 'required',
                '110_w' => 'required',
                '190_w' => 'required',
                
            ]
        );
        if ($validation->fails()) {        
            return redirect(url('add_prresolution'))->with('error',$validation->getMessageBag()->first());
    }
     $pr = new pr_resolution();
     
         $pr->district_id = $request->input('district_name');
         $pr->mandal_id = $request->input('taluka_name');
         $pr->gram_id = $request->input('gram_name');
         $pr->light_18w = $request->input('18_w');
         $pr->light_35w = $request->input('35_w');
         $pr->light_70w = $request->input('70_w');
         $pr->light_110w = $request->input('110_w');
         $pr->light_190w = $request->input('190_w');
         $pr->status = 0;
         $insert=[];
         if($request->hasFile('document') )
         {
            foreach($request->file('document') as $key =>$file){

                $path=$file->store('storage/files');
            
            $insert[]=$path;
            
            }
         }
         $pr->document=implode('|',$insert);
         $pr->save();
        
         $area = area_master::where('id',$request->input('gram_name'))->first();
         $prs = pr_resolution::find($pr->id);
         $prs->resolution_id=$area->code.$pr->id;
         $prs->save();
         return redirect(url('add_prresolution'))->withSuccess('Added Successfully.');

}

   public function pr_delete(Request $request)
   {
       pr_resolution::where('id',$request->id)->delete();
       return redirect(url('gram_approve_list')."?id=".$request->gram_id)->withSuccess('Deleted Successfully');
   }
   public function pr_status()
   {
		/*pr_resolution::where('id',$_POST['id'])->update('status',$_POST['status']);
       return 1;*/
	   if($_POST['status'] == 1){
			$stat = "active";
		}else{
			$stat = "inactive";
		}
		$area=pr_resolution::find($_POST['id']);
		$area->status=$stat;
		$area->update();
		return 1;
   }
    
}
