<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\abstract_of_survey;
use App\Models\survey_info;
use DB;
use App\Models\survey_info;
class AbstractController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $abstract=DB::table('abstract_of_surveys')
        ->join('area_masters','area_masters.id','=','abstract_of_surveys.district')
        ->select('*','area_masters.id as area_id')
        ->get();
        return response()->json($abstract);
    }

    /**
     * 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)
    {
        $district=$request->get('district');
        $uibname=$request->get('uibname');
        $surveydone=$request->get('surveydone');
        $arr=array();
        $arrapi=array();
        $demo=array(
            'ward_no'=>$request->get('ward_no'),
            'ftl_40w'=>$request->get('ftl_40w'),
            'cfl'=>$request->get('cfl'),
            'sv_70w'=>$request->get('sv_70w'),
            'sv_150w'=>$request->get('sv_150w'),
            'sv_250w'=>$request->get('sv_250w'),
            'led'=>$request->get('led'),
            'high_mast'=>$request->get('high_mast'),
            'empty_poles'=>$request->get('empty_poles'),
            'total'=>$request->get('total'),
            'no_of_poles'=>$request->get('no_of_poles'),
            'no_of_clamps'=>$request->get('no_of_clamps'),
            'remark'=>$request->get('remark')
        );
        array_push($arr,$demo);
        $d=json_encode($arr);
        $abstract=new abstract_of_survey([
            'district'=>$district,
            'uib_name'=>$uibname,
            'survey_done_by'=>$surveydone,
            'description'=>$d,
        ]);
        $abstract->save();
        $data['status']="success";
        $data['msg']="data inserted";
        array_push($arrapi,$data);
        echo json_encode($arrapi);
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id,Request $request)
    {
        if(!$request->token){
		    return response()->json(['msg'=>"unautherized access"]);
		}
        
        $abstract=DB::table('abstract_of_surveys')          
                    ->where('survey_done_by','=',$id)
                    ->get();
        return response()->json($abstract);
    }

    /**
     * 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)
    {
        $district=$request->get('district');
        $uibname=$request->get('uibname');
        $surveydone=$request->get('surveydone');
        $arr=array();
        $arrapi=array();
        $demo=array(
            'ward_no'=>$request->get('ward_no'),
            'ftl_40w'=>$request->get('ftl_40w'),
            'cfl'=>$request->get('cfl'),
            'sv_70w'=>$request->get('sv_70w'),
            'sv_150w'=>$request->get('sv_150w'),
            'sv_250w'=>$request->get('sv_250w'),
            'led'=>$request->get('led'),
            'high_mast'=>$request->get('high_mast'),
            'empty_poles'=>$request->get('empty_poles'),
            'total'=>$request->get('total'),
            'no_of_poles'=>$request->get('no_of_poles'),
            'no_of_clamps'=>$request->get('no_of_clamps'),
            'remark'=>$request->get('remark')
        );
        array_push($arr,$demo);
        $d=json_encode($arr);
        $abstract=abstract_of_survey::find($id);
        $abstract->district=$district;
        $abstract->uib_name=$uibname;
        $abstract->survey_done_by=$surveydone;
        $abstract->description=$d;
        $abstract->update();
        $data['status']="success";
        $data['msg']="data updated";
        array_push($arrapi,$data);
        echo json_encode($arrapi);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $arr=array();
        $abstract=abstract_of_survey::find($id);
        $abstract->delete();
        $data['status']="success";
        $data['msg']="Data Deleted";
        array_push($arr,$data);
        echo json_encode($arr);
    }
    public function abstract_survey(Request $request)
    {
        $login=$request->session()->get('uname');
		$gramlogin=$request->session()->get('gramuser');
		if($login!=null || $gramlogin!=null)
        {
        $login=$request->session()->get('uname');
        $login=$request->session()->get('gramuser');
        $survey_i=DB::table('area_masters')
		            ->join('survey_infos','survey_infos.taluka_id','=','area_masters.id')
					->select('*','area_masters.id as aid')
					
					->get();
		$jilla_i=DB::table('area_masters')
					->join('survey_infos','survey_infos.jilla_id','=','area_masters.id')
					->select('*','area_masters.id as aid')
					->get();
		$gram_i=DB::table('area_masters')
					->join('survey_infos','survey_infos.gram_id','=','area_masters.id')
					->select('*','area_masters.id as aid')
					->get();
		$jillanamesss=array();
		foreach($survey_i as $d)
			{
				$jillaaname=DB::table('area_masters')
							->where('id','=',$d->jilla_id)
							->get();
				$tname=$jillaaname[0]->area_name;
				array_push($jillanamesss,$tname);
				//echo $areanamesss;
			}
		
		$areanamesss=array();
		foreach($survey_i as $d)
		{
			$talukaname=DB::table('area_masters')
					->where('id','=',$d->taluka_id)
					->get();
			$tname=$talukaname[0]->area_name;
			array_push($areanamesss,$tname);
			//echo $areanamesss;
		}
		
		$gramnamesss=array();
		foreach($survey_i as $d)
		{
			$gramname=DB::table('area_masters')
							->where('id','=',$d->gram_id)
							->get();
			$tname=$gramname[0]->area_name;
			array_push($gramnamesss,$tname);
			//echo $areanamesss;
		}
		
		//echo $gramid;
		//exit();
		$demo="";
		
		
		
		//var_dump($demo);
		//exit();
		$jilla=DB::table('area_masters')
					->where('parent_id','=','0')
					->where('super_id','=','0')
					->where('status','=','active')
					->get();
		$talukadata=DB::table('area_masters')
		        ->where('parent_id','!=','0')
		        ->where('super_id','=','0')
		        ->where('status','=','active')
		        ->select('*')
		        ->get();
		
		$taluka=DB::table('area_masters')
					->where('parent_id','!=','0')
					->where('super_id','=','0')
					->where('status','=','active')
					->get();
		$gram=DB::table('area_masters')
					->where('parent_id','!=','0')
					->where('super_id','!=','0')
					->where('status','=','active')
					->get();
		$jillaselect=DB::table('area_masters')
					->join('survey_infos','survey_infos.jilla_id','=','area_masters.id')
					->select('*','area_masters.id as aid')
					->get();
		$talukaselect=DB::table('area_masters')
					->join('survey_infos','survey_infos.taluka_id','=','area_masters.id')
					->select('*','area_masters.id as aid')
					->get();
		$gramselect=DB::table('area_masters')
					->join('survey_infos','survey_infos.gram_id','=','area_masters.id')
					->select('*','area_masters.id as aid')
					->get();
		
		$userlist=DB::table('gramusers')
					->where('status','=','active')
					->select('*')
					->get();

		return view('backend.survey_abstract',compact('userlist','survey_i','demo','jilla','talukadata','taluka','gram','jillaselect','talukaselect','gramselect','areanamesss','jillanamesss','gramnamesss'));
         
        
        }
        else
        {
            return redirect('/');
        }
    }
    public function displayabstractsurvey(Request $request)
    {
        $district=$request->get('jillase');
        $user=$request->get('user_id');
        if($district=="" && $user=="")
        {
            $abstract=DB::table('abstract_of_surveys')
                    ->join('area_masters','area_masters.id','=','abstract_of_surveys.district')
                    // ->where('area_masters.super_id','=','0')
                    // ->where('area_masters.parent_id','=','0')
                    ->select('*','abstract_of_surveys.id as aaid')
                    ->get();
            return view('backend.display_abstractsurvey',compact('abstract'));
        }else if($district!="" && $user=="")
        {
            $abstract=DB::table('abstract_of_surveys')
                    ->join('area_masters','area_masters.id','=','abstract_of_surveys.district')
                    ->where('area_masters.super_id','=','0')
                    ->where('area_masters.parent_id','=','0')
                    ->where('area_masters.id','=',$district)
                    ->select('*','abstract_of_surveys.id as aaid')
                    ->get();
                    $count=sizeof($abstract);
                    if($count>0)
                    {
                        return view('backend.display_abstractsurvey',compact('abstract'));
                    }
                    else
                    {
                        echo "<script>alert('No Data Found')</script>";
                        echo "<script>window.location.href='/abstract_survey'</script>";
                    }

        }
        else if($user!="" && $district=="")
        {   
            $abstract=DB::table('abstract_of_surveys')
                    ->join('area_masters','area_masters.id','=','abstract_of_surveys.district')
                    ->join('gramusers','gramusers.id','=','abstract_of_surveys.survey_done_by')
                    ->where('area_masters.super_id','=','0')
                    ->where('area_masters.parent_id','=','0')
                    ->where('gramusers.id','=',$user)
                    ->select('*','abstract_of_surveys.id as aaid')
                    ->get();
            $count=sizeof($abstract);
            if($count>0)
            {
                return view('backend.display_abstractsurvey',compact('abstract'));
            }
            else
            {
                echo "<script>alert('No Data Found')</script>";
                echo "<script>window.location.href='/abstract_survey'</script>";
            }
        }else if($district!="" && $user!="")
        {
            $abstract=DB::table('abstract_of_surveys')
                    ->join('area_masters','area_masters.id','=','abstract_of_surveys.district')
                    ->join('gramusers','gramusers.id','=','abstract_of_surveys.survey_done_by')
                    ->where('area_masters.super_id','=','0')
                    ->where('area_masters.parent_id','=','0')
                    ->where('gramusers.id','=',$user)
                    ->where('area_masters.id','=',$district)
                    ->select('*','abstract_of_surveys.id as aaid')
                    ->get();
            $count=sizeof($abstract);
            if($count>0)
            {
                return view('backend.display_abstractsurvey',compact('abstract'));
            }
            else
            {
                echo "<script>alert('No Data Found')</script>";
                echo "<script>window.location.href='/abstract_survey'</script>";
            }
        }
    }
    public function view_abstract_survey($id)
    {
        $abstract=DB::table('abstract_of_surveys')
        ->join('area_masters','area_masters.id','=','abstract_of_surveys.district')
        ->where('area_masters.super_id','=','0')
        ->where('area_masters.parent_id','=','0')
        ->where('abstract_of_surveys.id','=',$id)
        ->select('*','abstract_of_surveys.id as aaid')
        ->get();
        return view('backend.view_abstract_survey',compact('abstract'));
    }
    public function update_arms_and_clamps(Request $request)
    {
        $lists = survey_info::where('gram_id',$request->id)->get();
        foreach ($lists as  $value) {
            $arms = explode('||',$value->types_of_arms);
            $wattage = explode('||',$value->LED_wattage);
           // dd($wattage);
            $type=[];
            // if($value->id==10973){
            //     dd($wattage);
            // }
            foreach ($wattage as $key =>$wat) {
                if($wat == "18 W"){
                    $type[]="25mm Dia 1.2mt";
                }elseif($wat == "35 W"){
                    $type[]="32mm Dia 1.2mt";
                }elseif($wat == "70 W"){
                    $type[]="32mm Dia 1.2mt";
                }elseif($wat == "110 W"){
                    $type[]=$arms[$key];
                }
                elseif($wat == "190 W"){
                    $type[]=$arms[$key];
                }else{
                    $type[]="N/A";
                }
            }
            
            survey_info::where('id',$value->id)->update(['types_of_arms'=>implode('||', $type)]);
        }
        echo "updated Successfully";
    }
}
