<?php

namespace App\Exports;

use App\Models\Matrimony_model;
use App\Models\Registration;
use App\Models\Batches_Model;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\Exportable;
use DB;

class SurveyExport implements FromView,WithColumnFormatting
{
    use Exportable;

     public function __construct($request)
    {
        //dd($name->all());
        $this->request=$request;

    }

    public function view(): View
    {
      
            $survey=DB::table('area_masters')
                    ->join('survey_infos','survey_infos.taluka_id','=','area_masters.id')
                    ->join('pole_details','pole_details.pole_id','=','survey_infos.id')
                    ->where('survey_infos.jilla_id','=',$this->request->jilla)
                    ->where('survey_infos.taluka_id','=',$this->request->taluka)
                    ->where('survey_infos.gram_id','=',$this->request->gram);
                    if(!empty($this->request->selecteddate)){
                        $survey->whereDate('survey_infos.created_at',date('Y-m-d',strtotime($this->request->selecteddate)));                      
                    }
                    if(!empty($this->request->phase_no)){
                         $survey->where('survey_infos.phase_no',$this->request->phase_no);
                    }
                $survey_i=$survey->select('*','survey_infos.created_at as screated_at','pole_details.id as pdid','area_masters.id as aid',DB::Raw('substring(`pole_num`,7,4) as pole'),DB::Raw('SUBSTRING_INDEX(`survey_infos`.`switch_point`,"/",1) as port'))
                    ->orderByRaw('CAST(port AS UNSIGNED),SUBSTR(pole FROM 1 FOR 1), CAST(SUBSTR(pole FROM 2) AS UNSIGNED)')
                    ->get();
                    //dd($survey_i->isEmpty());
                    
            $jillanamesss=array();
            $jillaaname=DB::table('area_masters')
                            ->where('id','=',$this->request->jilla)
                            ->get();
                $tname=$jillaaname[0]->area_name;
                array_push($jillanamesss,$tname);
        $areanamesss=array();
        $talukaname=DB::table('area_masters')
                    ->where('id','=',$this->request->taluka)
                    ->get();
            $tname=$talukaname[0]->area_name;
            array_push($areanamesss,$tname);
        
        $gramnamesss=array();
        $gramname=DB::table('area_masters')
                            ->where('id','=',$this->request->gram)
                            ->get();
            $tname=$gramname[0]->area_name;
            array_push($gramnamesss,$tname);

        return view('exports.surveyReport', [
            'survey_i' => $survey_i,"jillanamesss"=>$jillanamesss,
            "areanamesss"=>$areanamesss,"gramnamesss"=>$gramnamesss
        ]);
    }
    public function columnFormats(): array
    {
        return [
            'A' => NumberFormat::FORMAT_TEXT,
            'B' => NumberFormat::FORMAT_TEXT,
            'C' => NumberFormat::FORMAT_TEXT,
            'D' => NumberFormat::FORMAT_TEXT,
            'E' => NumberFormat::FORMAT_TEXT,
            'F' => NumberFormat::FORMAT_TEXT,
            'G' => NumberFormat::FORMAT_TEXT,
            'H' => NumberFormat::FORMAT_TEXT,
            'I' => NumberFormat::FORMAT_TEXT,
            'J' => NumberFormat::FORMAT_TEXT,
            'K' => NumberFormat::FORMAT_TEXT,
            'L' => NumberFormat::FORMAT_TEXT,
            'M' => NumberFormat::FORMAT_TEXT,
            'N' => NumberFormat::FORMAT_TEXT,
            'O' => NumberFormat::FORMAT_TEXT,
            'P' => NumberFormat::FORMAT_TEXT,
            'Q' => NumberFormat::FORMAT_TEXT,
            'R' => NumberFormat::FORMAT_TEXT,
            'S' => NumberFormat::FORMAT_TEXT,
            'T' => NumberFormat::FORMAT_TEXT,
            'U' => NumberFormat::FORMAT_TEXT,
            'V' => NumberFormat::FORMAT_TEXT,
            'W' => NumberFormat::FORMAT_TEXT,
            'X' => NumberFormat::FORMAT_TEXT,
            'Y' => NumberFormat::FORMAT_TEXT,
            'Z' => NumberFormat::FORMAT_TEXT
        ];
    }
}
