<?php

namespace Mehrab\Slider\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Mehrab\City\Http\Requests\city_store_request;
use Mehrab\City\Http\Requests\city_update_request;
use Mehrab\City\Models\City;
use Mehrab\Media\Services\MediaFileService;
use Mehrab\Service\Repositories\ServiceRepo;
use Mehrab\Slider\Http\Requests\slide_store_request;
use Mehrab\Slider\Http\Requests\slider_update_request;
use Mehrab\Slider\Models\Slider;
use Mehrab\Slider\Repositories\SliderRepo;


class SliderController extends Controller
{
    private $SliderRepo;

    public function __construct(SliderRepo $SliderRepo)
    {
        $this->SliderRepo = $SliderRepo;
    }

    public function index(){

        $this->authorize('manage',Slider::class);

        $slides = $this->SliderRepo->all();

        return view('Slider::Admin.list_slide',compact('slides'));

    }

    public function create()
    {
        $this->authorize('manage',Slider::class);
        return view('Slider::Admin.create_slide');
    }

    public function store(slide_store_request $request){
        $this->authorize('manage',Slider::class);
        $media_id = MediaFileService::publicUpload($request->slide)->id;
        $this->SliderRepo->store($request,$media_id);
         return redirect()->route('slider.index')->with('status','created');
    }


    public function edit($slideId){
        $this->authorize('manage',Slider::class);
      $slide = $this->SliderRepo->findById($slideId);
      return view('Slider::Admin.edit_slide',compact('slide'));
    }

    public function update(slider_update_request $request,$id){
        $this->authorize('manage',Slider::class);
        $this->SliderRepo->update($request,$id);
        return redirect()->route('slider.index')->with('status','updated');
    }


    public function destroy($id){
//        $this->authorize('manage',City::class);
       return  $this->SliderRepo->delete($id);
    }

    public function service_index(ServiceRepo $serviceRepo){
        $services = $serviceRepo->all_Parent_slider();
        return view('Slider::Admin.list_service_slide',compact('services'));
    }

    public function service_store(Request $request,ServiceRepo $serviceRepo){
        $request->validate(['slider_id' => ['required','exists:services,id']]);
        $serviceRepo->service_is_slider($request->slider_id);
        return redirect()->back()->with('status','created');
    }

    public function service_delete(ServiceRepo $serviceRepo,$id){
        return  $serviceRepo->service_delete_slider($id);
    }

    public function service_discount_index(ServiceRepo $serviceRepo){
        $services = $serviceRepo->all_children_slider();
        return view('Slider::Admin.list_service_discount_slide',compact('services'));
    }


    public function service_discount_store(Request $request,ServiceRepo $serviceRepo){
        $request->validate(['slider_id' => ['required','exists:services,id']]);
        $serviceRepo->service_is_discount($request->slider_id);
        return redirect()->back()->with('status','created');
    }

    public function service_discount_delete(ServiceRepo $serviceRepo,$id){
        return  $serviceRepo->service_delete_discount($id);
    }



}
