<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use App\Models\gramuser;
use App\Models\area_master;
use App\Models\Roleslist;
use App\Models\subvendor_lights;
use DB;
class GramUserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    
    public function index(Request $request)
    {
       
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
          
        $roleslist= Roleslist::get();
        $roles=Roleslist::where('id','=','7')->orWhere('id','=','8')->orWhere('id','=','3')->get();
        $jilla=DB::table('area_masters')
					->where('parent_id','=','0')
					->where('super_id','=','0')
					->where('status','=','active')
                    ->whereIn('id',$request->session()->get('districts'))
					->get();
        $login=$request->session()->get('uname');
        if($login==null)
        {
            return redirect('/');
        }
        else
        {
        $regi=gramuser::get();
        $regis=gramuser::where('role',7)->orWhere('role',8)->orWhere('role',3)->get();
        return view('backend.gramuser',compact('regi','jilla','roleslist','regis','roles'));
        }
    }
	public function gramuserchngestat(){
		if($_POST['status'] == 1){
			$stat = "active";
		}else{
			$stat = "inactive";
		}
		$user=gramuser::find($_POST['id']);
		$user->status=$stat;
		$user->update();
		return 1;
	}


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
             $regis=gramuser::where('role',7)->get();
         $roleslist= Roleslist::get(); 
        
        $request->validate([
		'user_name'=>'required',
		'mobile_number'=>'required',
		'password'=>'required',
		]);
		$reg=new gramuser([
		'name'=>$request->get('user_name'),
		'mobile'=>$request->get('mobile_number'),
		'password'=>md5($request->get('password')),
        'role'=>$request->get('role'),
        'status'=>'active'
		]);
		$reg->save();
		return redirect('/gramuser/create');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

          $roleslist= Roleslist::get();
        $regi=gramuser::find($id);
         
		return view('backend.register_show',compact('regi','roleslist','regis'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $regi=gramuser::find($id);
        $roleslist= Roleslist::get();
		return view('backend.gramuser_edit',compact('regi','roleslist'));
    }

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

         $roleslist= Roleslist::get();
         $request->validate([
		'user_name'=>'required',
		'mobile_number'=>'required',
		'password'=>'required',
		]);
		$reg=gramuser::find($id);
		$reg->name=$request->get('user_name');
		$reg->mobile=$request->get('mobile_number');
        $reg->role=$request->get('role');
		$reg->password=md5($request->get('password'));
		$reg->status='active';
		$reg->update();
		return redirect('/gramuser/create');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $reg=gramuser::find($id);
		$reg->delete();
		return redirect('/gramuser/create');
    }
	 public function usergramstatusupdate($id)
    {
        $user=gramuser::find($id);
        $user=$user->status;
        if($user=='active')
        {
            $user=gramuser::find($id);
            $user->status='inactive';
            $user->update();
            return redirect('/gramuser/create');
        }
        else if($user=='inactive')
        {
            $user=gramuser::find($id);
            $user->status='active';
            $user->update();
            return redirect('/gramuser/create');
        }
    }
    public function userlogin(Request $request)
    {
        $arr = array();
		$mbl=$request->get('mbl');
		$pws=md5($request->get('pws'));
		$usr = gramuser::where('mobile',''.$mbl)->where('password',''.$pws)->where('status','active')->get();
        if(sizeof($usr)>0)
		{
            
            $uid=$usr[0]->id;
            $gramuserallot=DB::table('gramuserallots')
                    
                    ->join('area_masters','area_masters.id','=','gramuserallots.jilla')
                    ->where('gramuserallots.user_id','=',$uid)
                    ->where('gramuserallots.status',1)
                    ->select('gramuserallots.*','area_masters.*','area_masters.id as aaid')
                    ->get();
            $gramuserallotmandal=DB::table('gramuserallots')
                    ->join('area_masters','area_masters.id','=','gramuserallots.taluka')
                    ->where('gramuserallots.user_id','=',$uid)
                    ->where('gramuserallots.status',1)
                    ->select('gramuserallots.*','area_masters.*','area_masters.id as aaid')
                    ->get();
            $panchayat=DB::table('gramuserallots')
                    ->join('area_masters','area_masters.id','=','gramuserallots.gram')
                    ->where('gramuserallots.user_id','=',$uid)
                    ->where('gramuserallots.status',1)
                    ->select('gramuserallots.*','area_masters.*','area_masters.id as aaid')
                    ->get();
        
           if(sizeof($gramuserallot)> 0  && sizeof($gramuserallotmandal)>0 && sizeof($panchayat)>0){
            $district=$gramuserallot[0]->area_name;
            $district_id=$gramuserallot[0]->aaid;
            $mandal=$gramuserallotmandal[0]->area_name;
            $mandal_id=$gramuserallotmandal[0]->aaid;
            $data['panchayat']=$panchayat;
            $data['district_name']=$district;
            $data['district_id']=$district_id;
            $data['user_id'] = $uid;			
            $data['mandal_name']=$mandal;
            $data['mandal_id']=$mandal_id;
            $data['status'] = "success";
			$data['msg'] = "Login Success";
			array_push($arr,$data);
            }else{
            $data['status'] = "Failed";
            $data['msg'] = "User Gram Allot status is inactive. Please contact admin";
            array_push($arr,$data);
            }
		}
		else
		{
			$data['status'] = "Failed";
			$data['msg'] = "Login Fail";
			//echo "insert";
			array_push($arr,$data);
		}
		echo json_encode($arr);

    }


    public function gramuserlogin(Request $request)
    {
        $arr = array();
        $mbl=$request->get('mbl');
        $pws=md5($request->get('pws'));
        $rolesget=$request->get('role_id');
        //dd($pws);
        $usr = gramuser::where('mobile',''.$mbl)->where('password',''.$pws)->where(function($q) use($rolesget){
            if(!empty($rolesget)){
            $q->where('role',$rolesget);
            }
        } )->where('status','active')->get();
        //dd($request->get('pws'));
        if(sizeof($usr)>0)
        {
            
            $uid=$usr[0]->id;
            $role=$usr[0]->role;

            $gramuserallot=DB::table('gramuserallots')
                    
                    ->join('area_masters','area_masters.id','=','gramuserallots.jilla')
                    ->where('gramuserallots.user_id','=',$uid)
                    ->where('gramuserallots.status',1)
                    ->select('gramuserallots.*','area_masters.*','area_masters.id as aaid')
                    ->get();
            $gramuserallotmandal=DB::table('gramuserallots')
                    ->join('area_masters','area_masters.id','=','gramuserallots.taluka')
                    ->where('gramuserallots.user_id','=',$uid)
                    ->where('gramuserallots.status',1)
                    ->select('gramuserallots.*','area_masters.*','area_masters.id as aaid')
                    ->get();
            $panchayat=DB::table('gramuserallots')
                    ->join('area_masters','area_masters.id','=','gramuserallots.gram')
                    ->where('gramuserallots.user_id','=',$uid)
                    ->where('gramuserallots.status',1)
                    ->select('gramuserallots.*','area_masters.*','area_masters.id as aaid')
                    ->get();
        
           if(sizeof($gramuserallot)> 0  && sizeof($gramuserallotmandal)>0 ){
            $district=$gramuserallot[0]->area_name;
            $district_id=$gramuserallot[0]->aaid;
            $mandal=$gramuserallotmandal[0]->area_name;
            $mandal_id=$gramuserallotmandal[0]->aaid;
            $data['panchayat']=$panchayat;
            $data['district_name']=$district;
            $data['district_id']=$district_id;
            $data['user_id'] = $uid;            
            $data['mandal_name']=$mandal;
            $data['mandal_id']=$mandal_id;

            $roles=Roleslist::where('id',$role)->first();
            //dd($roles);

            $data['role'] = $roles->name;
            $data['role_id'] = $roles->id;
             $data['status'] = "success";
            $data['msg'] = "Login Success";
            array_push($arr,$data);
            }else{
            $data['status'] = "Failed";
            $data['msg'] = "User Gram Allot status is inactive. Please contact admin";
            array_push($arr,$data);
            }
        }
        else
        {
            $data['status'] = "Failed";
            $data['msg'] = "Login Fail";
            //echo "insert";
            array_push($arr,$data);
        }
        echo json_encode($arr);

    }
    public function get_makeslist_and_wattage(Request $request)
    {

         if(!empty($request->gram_id)){



            $makers = subvendor_lights::where('mandal_id',$request->gram_id)->groupby('maker_name')->select('maker_name',DB::raw('SUM(rcv_qty_18w) as 18w'),DB::raw('SUM(rcv_qty_35w) as 35w'),DB::raw('SUM(rcv_qty_70w) as 70w'),DB::raw('SUM(rcv_qty_110w) as 110w'),DB::raw('SUM(rcv_qty_190w) as 190w'))->get()->toArray();

            if(!empty($makers)){

                foreach ($makers as $key => $value) {
                    $makers[$key]['watts']=[
                        ['watts_name'=>'18w',"size"=>$value['18w']??0],
                        ['watts_name'=>'35w',"size"=>$value['35w']??0],
                        ['watts_name'=>'70w',"size"=>$value['70w']??0],
                        ['watts_name'=>'110w',"size"=>$value['110w']??0],
                        ['watts_name'=>'190w',"size"=>$value['190w']??0],
                    ];
                    unset($makers[$key]['18w']);
                    unset($makers[$key]['35w']);
                    unset($makers[$key]['70w']);
                    unset($makers[$key]['110w']);
                    unset($makers[$key]['190w']);
                }

            
            $data['status'] = true;


            $data['msg'] = "Makers List with Wattage";

            $data['list'] = $makers;
            
            }else{
            $data['status'] = false;
            $data['msg'] = "Gram Id not have any maker Name";
           
            }
            }else{
            $data['status'] = false;
            $data['msg'] = "Gram Id is required";
        }
            echo json_encode($data);
            

            
    }
    // public function panchayat(Request $request)
    // {
    //     $uid=$request->session()->get('panchayatuser');
    //     $arr = array();
		
    // }
}
