<?php

namespace App\Http\Controllers\Admin;

use App\Slide;
use App\SlideTranslation;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SlideController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Slide $slide)
    {
        $this->authorize('view', $slide);

        $data = [];
        $data['slide']  = $slide;
        $data['slides'] = Slide::with('ctranslation')->get();

        //print_r($data['slides']); exit;

        return view('admin.slides.index',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Slide $slide)
    {
        $this->authorize('create', $slide);

        $data = [];
        $data['slide']   = $slide;
        $data['create'] = 1;
        return view('admin.slides.form', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Slide $slide)
    {
        $this->authorize('create', $slide);

        $rules = [
            'sl_act'    => 'required|integer|in:1,0',
        ];

        foreach(config('app.languages') as $lang) {

            $rules[$lang.'_sl_src']   = 'required|string|min:15';
            $rules[$lang.'_sl_title'] = 'required|string|min:3';
            $rules[$lang.'_sl_link']  = 'nullable|string|min:15';
        }

        $this->validate( $request, $rules);

        $slide = new Slide();

        $slide->sl_act      = $request->sl_act;

        $slide->save();

        foreach(config('app.languages') as $lang){

            $st = new SlideTranslation();

            $st->lang      = $lang;
            $st->sl_src    = $request->{$lang.'_sl_src'};
            $st->sl_title  = $request->{$lang.'_sl_title'};
            $st->sl_link   = $request->{$lang.'_sl_link'};

            $slide->translations()->save($st);

        }

        return redirect()->route('admin.slide.index')->with('result', __('adminforms.slides.insert_controller_msg', ['name' => $slide->ctranslation->sl_title]));



    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Slide  $slide
     * @return \Illuminate\Http\Response
     */
    public function edit(Slide $slide)
    {
        //
        $this->authorize('update', $slide);

        $data = [];
        $data['slide']   = $slide;
        $data['create'] = 0;
        return view('admin.slides.form', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Slide  $slide
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Slide $slide)
    {

        $this->authorize('update', $slide);


        $rules = [
            'sl_act'    => 'required|integer|in:1,0',
        ];

        foreach(config('app.languages') as $lang) {

            $rules[$lang.'_sl_src']   = 'required|string|min:15';
            $rules[$lang.'_sl_title'] = 'required|string|min:3';
            $rules[$lang.'_sl_link']  = 'nullable|string|min:15';
        }

        $this->validate( $request, $rules);


        $slide->sl_act      = $request->sl_act;

        $slide->save();

        foreach(config('app.languages') as $lang){

            $st = $slide->translations()->where('lang', $lang)->first();

            if (!is_null($st)){

                $st->sl_src    = $request->{$lang.'_sl_src'};
                $st->sl_title  = $request->{$lang.'_sl_title'};
                $st->sl_link   = $request->{$lang.'_sl_link'};

                $st->save();

            } else {

                $st = new SlideTranslation();

                $st->lang      = $lang;
                $st->sl_src    = $request->{$lang.'_sl_src'};
                $st->sl_title  = $request->{$lang.'_sl_title'};
                $st->sl_link   = $request->{$lang.'_sl_link'};

                $slide->translations()->save($st);

            }


        }

        return redirect()->route('admin.slide.index')->with('result', __('adminforms.slides.insert_controller_msg', ['name' => $slide->ctranslation->sl_title]));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Slide  $slide
     * @return \Illuminate\Http\Response
     */
    public function destroy(Slide $slide)
    {
        $name = $slide->sl_title;


        // Delete picture
        if(!empty($slide->translations)){
            foreach($slide->translations as $t){
                $path  = public_path($t->sl_src);
                if(file_exists($path) && is_readable($path)){ unlink($path);}
            }

        }

        $slide->destroy($slide->sl_id);

        return redirect()->route('admin.slide.index')->with('result', __('adminforms.slides.delete_controller_msg', ['name' => $name]));
    }
}
