<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use App\ServiceType;
use Illuminate\Http\Request;

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

    public function __construct()
    {
        $this->middleware('auth'); //Ordinady
        $this->middleware('role:developer');
    }


    public function index()
    {
        $ServiceTypes = ServiceType::all();
        return view('ServiceType.index',compact('ServiceTypes'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    { 
       
        $request-> validate([
            'title'=>'required|max:255',
            'narration'=>'max:255', 
        ]);

        $ServiceTypeItem = ServiceType::create($request->all());

        $ServiceType = new ServiceType();
        if($request->hasFile('image')) 
        {
            $ServiceType->imageUpload($request->image,$ServiceTypeItem);            
        } 


        return redirect(route('ServiceType.index'))->with('Message','Saved Successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\ServiceType  $serviceType
     * @return \Illuminate\Http\Response
     */
    public function show(ServiceType $serviceType)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\ServiceType  $serviceType
     * @return \Illuminate\Http\Response
     */
    public function edit(ServiceType $ServiceType)
    {
        return view('ServiceType.edit',compact('ServiceType')); 
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\ServiceType  $serviceType
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ServiceType $ServiceType)
    {
        $request-> validate([
            'title'=>'required|max:255',
            'narration'=>'max:255',         
            
        ]);

        $ServiceType->update([
            'title'=>$request->title,
            'narration'=>$request->narration,
        ]);

        if($request->hasFile('image')) 
        {
            $ServiceType->imageUpload($request->image,$ServiceType);            
        } 

        return redirect(route('ServiceType.index'))->with('Message','Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\ServiceType  $serviceType
     * @return \Illuminate\Http\Response
     */
    public function destroy(ServiceType $ServiceType)
    {
        // dd($ServiceType);
        $this->ServiceFileDeleteIfExist('/public/images/services/'.$ServiceType->image);
        $ServiceType->delete();
        return redirect(route('ServiceType.index'))->with('Message','Deleted Successfully');
    }

    public static function ServiceTypeListForCombo($ServiceType_id)
    {
                    $ServiceTypes=ServiceType::all();
                    //$Categories=['Items'=>$Items,'Selected'=>$category_id];
                    return view('ServiceType.combo',compact('ServiceTypes','ServiceType_id'));
    }

    private function ServiceFileDeleteIfExist($File)
      {
        //dd($Product);
              if($File)
              {
                    Storage::delete($File);
              }
      }
}
