<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\VisaService;
use App\Models\VisaServiceSection;
use App\Models\VisaServiceSectionPoint;
use App\Models\Setting;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class VisaServiceSectionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    protected $view = 'admin.visa_service_section.';
    protected $redirect = 'admin/visa_services/{id}/sections';

    public function index($id)
    {
        $service_name = VisaService::findorfail($id)->name;
        $service_section = VisaServiceSection::where('visa_service_id',$id);


        // if(\request('name')){
        //     $key = \request('name');
        //     $services = $services->where('name','like','%'.$key.'%');
        // }
        // if(\request('status')){
        //     $key = \request('status');
        //     $services = $services->where('status',$key);
        // }
        $service_sections = $service_section->paginate(config('custom.per_page'));
        return view($this->view.'index',compact('service_sections','service_name','id'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        $service_name = VisaService::findorfail($id)->name;
        return view($this->view.'create',compact('service_name','id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,$id)
    {
        
       
        $this->validate(\request(),[
            
            
            'description' => 'required',
            'status' => 'required',
            'order_by' => 'required'
           
        ]);
        $service_section = new VisaServiceSection();

        $service_section->title = \request('title');
        $service_section->sub_title = \request('sub_title');
        $service_section->description = (\request('description'));
        $service_section->sub_description = (\request('sub_description'));
        $service_section->visa_service_id = $id;
        $service_section->status = \request('status');
        $service_section->order_by = \request('order_by');
        // $service->image_alt = \request('image_alt');
        // $service->slug = Setting::create_slug(\request('seo_title'));


        if($request->hasFile('image')){
            $extension = \request()->file('image')->getClientOriginalExtension();
            $image_folder_type = array_search('visa_service',config('custom.image_folders')); //for image saved in folder
            // dd($image_folder_type);
            $count = rand(100,999);
            $out_put_path = User::save_image(\request('image'),$extension,$count,$image_folder_type);
                $image_path = $out_put_path[0];
                $service_section->image = $image_path;
        }


        if($service_section->save()){
            // $points = $request->points;
            // $point_descriptions = $request->point_descriptions ?? [];
            // $icons = $request->icons ?? [];
            
            // if($points[0] != null){
               
            // foreach($points as $key => $point){

            //     $service_section_point = new VisaServiceSectionPoint();
                
            //     $service_section_point->visa_service_section_id = $service_section->id;
                
            //     $service_section_point->point = $point;
            //     $service_section_point->save();
            // }
            // }
            Session::flash('success','Visa Service Section has been created!');
            return redirect('admin/visa_services/'.$id.'/sections');

        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id,$secId)
    {
        
        
        // $service = new Service();
        // $service_section = new ServiceSection();
        $service = VisaService::findorfail($id);

        $service_section = VisaServiceSection::findorfail($secId);



        return view($this->view . 'show', compact('service','service_section'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id,$secId)
    {
        $service = new VisaService();
        $service_section = new VisaServiceSection();
        $service = $service->findorfail($id);
        $service_section = $service_section->findorfail($secId);




        return view($this->view . 'edit', compact('service','service_section'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id, $secId)
    {
        $this->validate(\request(),[
            'description' => 'required',
            'status' => 'required',
            'order_by' => 'required'
        ]);
        $service_section = VisaServiceSection::findOrFail($secId);
        $service_section->title = \request('title');
        $service_section->sub_title = \request('sub_title');
        $service_section->description = (\request('description'));
        $service_section->sub_description = (\request('sub_description'));
        $service_section->visa_service_id = $id;
        $service_section->status = \request('status');
        $service_section->order_by = \request('order_by');

        if($request->hasFile('image')){
            $extension = \request()->file('image')->getClientOriginalExtension();
            $image_folder_type = array_search('service',config('custom.image_folders')); //for image saved in folder
            $count = rand(100,999);
            $out_put_path = User::save_image(\request('image'),$extension,$count,$image_folder_type);
            $image_path = $out_put_path[0];
            if (is_file(public_path().'/'.$service_section->image) && file_exists(public_path().'/'.$service_section->image)){
                unlink(public_path().'/'.$service_section->image);
            }
            $service_section->image = $image_path;
        }
        if($service_section->update()){
            
            
        
            Session::flash('success','Visa Service Section has been successfully updated!');
            return redirect('admin/visa_services/'.$id.'/sections');

        }


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function service_point($service_point_id){
        
        if(Auth::user()){
            $setting = ServiceSectionPoint::findorfail($service_point_id);
            $setting->delete();
            return response()->json(['service_point_id' => $service_point_id]);
        }

    }
}