<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\allotuser;
use App\Models\survey_info;
use App\Models\Register;
use App\Models\gramuser;
use App\Models\gramuserallot;
use App\Models\clusters;
use App\Models\cluster_district;
use DB;
class AlloruserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $count=$request->get('rowcount');
        $user=$request->get('userid');
        $r=$count-1;
       
        for($i=1; $i<=$r; $i++)
        {
            $jilla=$request->get('jilla'.$i);
            $taluka=$request->get('taluka'.$i);
            $gram=$request->get('gram'.$i);
            $countuser=DB::table('allotusers')
                        ->where('user_id','=',$user)
                        ->where('jilla','=',$jilla)
                        ->where('taluka','=',$taluka)
                        ->where('gram','=',$gram)
                        ->count();
            if($countuser==1)
            {
                $countuser=DB::table('allotusers')
                        ->where('user_id','=',$user)
                        ->where('jilla','=',$jilla)
                        ->where('taluka','=',$taluka)
                        ->where('gram','=',$gram)
                        ->get();
                $id=$countuser[0]->id;
                $updateuser=allotuser::find($id);
                $updateuser->user_id=$user;
                $updateuser->jilla=$jilla;
                $updateuser->taluka=$taluka;
                $updateuser->gram=$gram;
                $updateuser->update();
                
            }
            else
            {
                $data=new allotuser([
                  'user_id'=>$user,
                  'jilla'=>$jilla,
                  'taluka'=>$taluka,
                  'gram'=>$gram
                ]);
                $data->save();
            }
            
            
            
        }
       
        return redirect('/user/create');
    }
	public function gramallotuser(Request $request)
	{
		$count=$request->get('rowcount');
        $user=$request->get('userid');
        $r=$count-1;
       
        for($i=1; $i<=$r; $i++)
        {
            $jilla=$request->get('jilla'.$i);
            $taluka=$request->get('taluka'.$i);
            $gram=$request->get('gram'.$i);
            $countuser=DB::table('gramuserallots')
                        ->where('user_id','=',$user)
                        ->where('jilla','=',$jilla)
                        ->where('taluka','=',$taluka)
                        ->where('gram','=',$gram)
                        ->count();
            if($countuser==1)
            {
                $countuser=DB::table('gramuserallots')
                        ->where('user_id','=',$user)
                        ->where('jilla','=',$jilla)
                        ->where('taluka','=',$taluka)
                        ->where('gram','=',$gram)
                        ->get();
                $id=$countuser[0]->id;
                $updateuser=gramuserallot::find($id);
                $updateuser->user_id=$user;
                $updateuser->jilla=$jilla;
                $updateuser->taluka=$taluka;
                $updateuser->gram=$gram;
                $updateuser->update();
                
            }
            else
            {
                $data=new gramuserallot([
                  'user_id'=>$user,
                  'jilla'=>$jilla,
                  'taluka'=>$taluka,
                  'gram'=>$gram
                ]);
                $data->save();
            }
            
            
            
        }
       
        return redirect('/gramuser/create');
	}
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        
    }
    public function getallot($id)
    {
        $allotuser=DB::table('registers')
                ->join('allotusers','allotusers.user_id','registers.id')
                ->where('allotusers.gram','=',$id)
                ->count();
        echo $allotuser;
    }
    public function getareanames(Request $request)
    {
        $jilla=$request->get('jilla');
        $taluka=$request->get('taluka');
        $mandal_id = $request->get('taluka');
        $gram=$request->get('gram');
        $userid=$request->get('userid');
        
        $jilla = DB::table('area_masters as A')
					->where('A.id','=',$jilla)
					->select('*','A.area_name as jilla')
					->get();
		$taluka = DB::table('area_masters as A')
					->where('A.id','=',$taluka)
					->select('*','A.area_name as taluka')
					->get();
		$gram = DB::table('area_masters as A')
					->where('A.id','=',$gram)
					->select('*','A.area_name as gram')
					->get();

         $query=gramuserallot::where('user_id',$userid)->where('status',1)->first();
         //dd($query);
        if(empty($query)){
            echo $jilla[0]->jilla."::".$taluka[0]->taluka."::".$gram[0]->gram;

        }else{
            //dd($query->taluka,$taluka);
            if($query->taluka==$mandal_id){
                echo $jilla[0]->jilla."::".$taluka[0]->taluka."::".$gram[0]->gram;
            }else{
              echo "Failed";
            }
          }
		

    }
    public function userallotform(Request $request)
    {
        $id=$request->get('userid');
        $user=DB::table('registers')
                    ->where('id','=',$id)
                    ->get();
        $regi=Register::get();
        $allotuser=DB::table('allotusers')
                    ->where('user_id','=',$id)
                    ->select('*')
                    ->get();
        $talukaarr=array();
        $gramarr=array();
        $jillaarr=array();
            $jillaselect=DB::table('area_masters')
						->join('allotusers','allotusers.jilla','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('allotusers.user_id','=',$id)
						->get();
		    $talukaselect=DB::table('area_masters')
						->join('allotusers','allotusers.taluka','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('allotusers.user_id','=',$id)
						->get();
			$gramselect=DB::table('area_masters')
						->join('allotusers','allotusers.gram','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('allotusers.user_id','=',$id)
						->get();
			foreach($jillaselect as $jilla)
			{
			    $tll=$jilla->area_name;
			    array_push($jillaarr,$tll);
			}
			foreach($talukaselect as $taluka)
			{
			    $tl=$taluka->area_name;
			    array_push($talukaarr,$tl);
			}
			foreach($gramselect as $gram)
			{
			    $gl=$gram->area_name;
			    array_push($gramarr,$gl);
			}
			$jilladata=DB::table('area_masters')
					->where('parent_id','=','0')
					->where('super_id','=','0')
					->where('status','=','active')
					->get();
			return view('backend.allotuserform',compact('user','allotuser','jilladata','regi','talukaarr','gramarr','jillaarr'));
	
        
                    
    }
	public function gramuserallotform(Request $request)
	{
		$id=$request->get('userid');
        $user=DB::table('gramusers')
                    ->where('id','=',$id)
                    ->get();
        $regi=gramuser::get();
        $allotuser=DB::table('gramuserallots')
                    ->where('user_id','=',$id)
                    ->select('*')
                    ->get();
        $talukaarr=array();
        $gramarr=array();
        $jillaarr=array();
            $jillaselect=DB::table('area_masters')
						->join('gramuserallots','gramuserallots.jilla','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('gramuserallots.user_id','=',$id)
						->get();
		    $talukaselect=DB::table('area_masters')
						->join('gramuserallots','gramuserallots.taluka','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('gramuserallots.user_id','=',$id)
						->get();
			$gramselect=DB::table('area_masters')
						->join('gramuserallots','gramuserallots.gram','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('gramuserallots.user_id','=',$id)
						->get();
			foreach($jillaselect as $jilla)
			{
			    $tll=$jilla->area_name;
			    array_push($jillaarr,$tll);
			}
			foreach($talukaselect as $taluka)
			{
			    $tl=$taluka->area_name;
			    array_push($talukaarr,$tl);
			}
			foreach($gramselect as $gram)
			{
			    $gl=$gram->area_name;
			    array_push($gramarr,$gl);
			}
			$jilladata=DB::table('area_masters')
					->where('parent_id','=','0')
					->where('super_id','=','0')
					->where('status','=','active')
                      ->whereIn('id',$request->session()->get('districts'))
					->get();
			return view('backend.gramallotuserform',compact('user','allotuser','jilladata','regi','talukaarr','gramarr','jillaarr'));
	}
	public function gramuserlist(Request $request)
	{
		$id=$request->id;
        $user=DB::table('gramusers')
                    ->where('id','=',$id)
                    ->get();
        $regi=gramuser::get();
        $allotuser=DB::table('gramuserallots')
                    ->where('user_id','=',$id)
                    ->select('*')
                    ->get();
        $talukaarr=array();
        $gramarr=array();
        $jillaarr=array();
            $jillaselect=DB::table('area_masters')
						->join('gramuserallots','gramuserallots.jilla','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('gramuserallots.user_id','=',$id)
						->get();
		    $talukaselect=DB::table('area_masters')
						->join('gramuserallots','gramuserallots.taluka','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('gramuserallots.user_id','=',$id)
						->get();
			$gramselect=DB::table('area_masters')
						->join('gramuserallots','gramuserallots.gram','=','area_masters.id')
						->select('*','area_masters.id as aid')
						->where('gramuserallots.user_id','=',$id)
						->get();
			foreach($jillaselect as $jilla)
			{
			    $tll=$jilla->area_name;
			    array_push($jillaarr,$tll);
			}
			foreach($talukaselect as $taluka)
			{
			    $tl=$taluka->area_name;
			    array_push($talukaarr,$tl);
			}
			foreach($gramselect as $gram)
			{
			    $gl=$gram->area_name;
			    array_push($gramarr,$gl);
			}
			$jilladata=DB::table('area_masters')
					->where('parent_id','=','0')
					->where('super_id','=','0')
					->where('status','=','active')
					->get();
			$html = '<table><tr><td>S.No</td><td>District Name</td><td>Mandal Name</td><td>Panchayat Name</td><td>Status</td></tr>';
			$s = array();$i=1;
			foreach($allotuser as $l=>$r){
				if($r->status == '1'){$ss = "Active";}else{$ss ="Inactive";}
				$html.='<tr><td>'.$i.'</td><td>'.$jillaarr[$l].'</td><td>'.$talukaarr[$l].'</td><td>'.$gramarr[$l].'</td><td>'.$ss.'</td><tr>';
				$i++;
			}			
			return response()->json($html);
	}
    public function allotuserdel($id)
    {
        //echo $id;
        $all=allotuser::find($id);
        $all->delete();
        echo "delete";
    }
	public function gramallotuserdel($id)
	{
		$all=gramuserallot::find($id);
        $all->delete();
        echo "delete";
	}
    public function allotChangeStatus(Request $request,$id)
    {
   

    $area=gramuserallot::find($id);
        $s=$area->status;
        // dd($request->url);
        if($s=='0')
        {
            $exist= gramuserallot::where('user_id',$area->user_id)->where('status',1)->first();

            if(!empty($exist)){
                if($exist->taluka != $request->mandal_id){
                    $data = ['status'=>'failed','class_name'=>$exist];
                return json_encode($data);
                }
            }
            $area=gramuserallot::find($id);
            $area->status='1';
            $area->update();
            if($request->ajax()){

                $data = ['status'=>'active','class_name'=>"btn btn-success statuschk"];
                return json_encode($data);

            }else{
            
                return redirect('/gramuser/create');
            }
        }
        else if($s=='1')
        {
            $area=gramuserallot::find($id);
            $area->status='0';
            $area->update();
            if($request->ajax()){

                $data = ['status'=>'inactive','class_name'=>"btn btn-warning statuschk"];
                return json_encode($data);

            }else
                return redirect('/gramuser/create');
            }
        
    }
    
    public function clusterform(Request $request ){
        
     $id=$request->id;
     $user = gramuser::where('id',$id)->first();
    $allotuser=gramuserallot::where('user_id',$id)->where('cluster_id','>',0)->get();
       $clusternames= clusters::get();
    return view('backend.cluster_form',compact('clusternames','user','allotuser'));

    }

    public function allotedcluster($id)
    {
        //echo $id;
        $all=allotuser::find($id);
        $all->delete();
        echo "delete";
    } 
    public function getclusternames(Request $request)
    {
        $cluster_name=$request->get('cluster_name');
        $userid=$request->get('userid');

         $query=gramuserallot::where('user_id',$userid)->where('cluster_id',$cluster_name)->where('status',1)->first();
         
        if(empty($query)){

           gramuserallot::create(['user_id'=>$userid,'cluster_id'=>$cluster_name]);

           return "success";

        }else{
              echo "Failed";
          }
        

    }

     
public function selecteddistrict(Request $request,$id) {

    if($id==0){
         $clusters = gramuserallot::where('user_id',session()->get('id'))->where('status',1)->pluck('cluster_id')->toArray();
                $districts = cluster_district::whereIn('cluster_id',$clusters)->pluck('district_id')->toArray();
     $request->session()->put('districts',$districts);
    }
    else{
      $request->session()->put('districts',[$id]);
     
      }
       return "success";
  }

  public function subvendorallotform(Request $request)
    {
        $id=$request->get('userid');
        $user=DB::table('gramusers')
                    ->where('id','=',$id)
                    ->get();
        $regi=gramuser::get();
        $allotuser=DB::table('gramuserallots')
                    ->where('user_id','=',$id)
                    ->select('*')
                    ->get();
        $jillaarr=array();
        $talukaarr=array();
            $jillaselect=DB::table('area_masters')
                        ->join('gramuserallots','gramuserallots.jilla','=','area_masters.id')
                        ->select('*','area_masters.id as aid')
                        ->where('gramuserallots.user_id','=',$id)

                        ->get();
            $talukaselect=DB::table('area_masters')
                        ->join('gramuserallots','gramuserallots.taluka','=','area_masters.id')
                        ->select('*','area_masters.id as aid')
                        ->where('gramuserallots.user_id','=',$id)
                        ->get();
            foreach($jillaselect as $jilla)
            {
                $tll=$jilla->area_name;
                array_push($jillaarr,$tll);
            }
            foreach($talukaselect as $taluka)
            {
                $tl=$taluka->area_name;
                array_push($talukaarr,$tl);
            }
            $jilladata=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')
                    ->whereIn('id',$request->session()->get('districts'))
                    ->get();
            return view('backend.subvendorallot',compact('user','allotuser','jilladata','regi','talukaarr','jillaarr'));
    }
     public function getvendorareanames(Request $request)
    {
        $jilla=$request->get('jilla');
        $taluka=$request->get('taluka');
        $mandal_id = $request->get('taluka');
        $userid=$request->get('userid');
        
        $jilla = DB::table('area_masters as A')
                    ->where('A.id','=',$jilla)
                    ->select('*','A.area_name as jilla')
                    ->get();
        $taluka = DB::table('area_masters as A')
                    ->where('A.id','=',$taluka)
                    ->select('*','A.area_name as taluka')
                    ->get();

         $query=gramuserallot::where('user_id',$userid)->where('status',1)->first();
         //dd($query);
        if(empty($query)){
            echo $jilla[0]->jilla."::".$taluka[0]->taluka;

        }else{
            //dd($query->taluka,$taluka);
            if($query->taluka==$mandal_id){
                echo $jilla[0]->jilla."::".$taluka[0]->taluka;
            }else{
              echo "Failed";
            }
          }
        

    }
     public function subvendorChangeStatus(Request $request,$id)
    {
   

    $area=gramuserallot::find($id);
        $s=$area->status;
        // dd($request->url);
        if($s=='0')
        {
            
            $area=gramuserallot::find($id);
            $area->status='1';
            $area->update();
            if($request->ajax()){

                $data = ['status'=>'active','class_name'=>"btn btn-success statuschk"];
                return json_encode($data);

            }else{
            
                return redirect('/gramuser/create');
            }
        }
        else if($s=='1')
        {
            $area=gramuserallot::find($id);
            $area->status='0';
            $area->update();
            if($request->ajax()){

                $data = ['status'=>'inactive','class_name'=>"btn btn-warning statuschk"];
                return json_encode($data);

            }else
                return redirect('/gramuser/create');
            }
        
    }

    public function subvendorsurveyallotform(Request $request)
    {
        $id=$request->get('userid');
        $user=DB::table('gramusers')
                    ->where('id','=',$id)
                    ->get();
        $regi=gramuser::get();
        $allotuser=DB::table('gramuserallots')
                    ->where('user_id','=',$id)
                    ->select('*')
                    ->get();
        $jillaarr=array();
        $talukaarr=array();
            $jillaselect=DB::table('area_masters')
                        ->join('gramuserallots','gramuserallots.jilla','=','area_masters.id')
                        ->select('*','area_masters.id as aid')
                        ->where('gramuserallots.user_id','=',$id)

                        ->get();
            $talukaselect=DB::table('area_masters')
                        ->join('gramuserallots','gramuserallots.taluka','=','area_masters.id')
                        ->select('*','area_masters.id as aid')
                        ->where('gramuserallots.user_id','=',$id)
                        ->get();
            foreach($jillaselect as $jilla)
            {
                $tll=$jilla->area_name;
                array_push($jillaarr,$tll);
            }
            foreach($talukaselect as $taluka)
            {
                $tl=$taluka->area_name;
                array_push($talukaarr,$tl);
            }
            $jilladata=DB::table('area_masters')
                    ->where('parent_id','=','0')
                    ->where('super_id','=','0')
                    ->where('status','=','active')
                    ->whereIn('id',$request->session()->get('districts'))
                    ->get();
            return view('backend.subvendorallot',compact('user','allotuser','jilladata','regi','talukaarr','jillaarr'));
    }

}
