<?php

namespace DMZ\Http\Controllers\Web;

use Illuminate\Http\Request;
use DMZ\Models\Multimedia;
use DMZ\Http\Requests;
use DMZ\Http\Requests\Web\MultimediaStoreRequest;
use DMZ\Http\Requests\Web\MultimediaPutRequest;
use DMZ\Http\Controllers\Controller;
use Auth; 

class MultimediaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {         
        if (Auth::user()) {
            $data['multimedia'] = Multimedia::getMultimedia()->paginate(6);
        }else{
            $data['multimedia'] = Multimedia::getMultimedia()->where('status',true)->paginate(6); 
        }       
        $data['category'] = 'Multimedia';
        return view('web.multimedia.index', $data);
    }

    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (!Auth::user()) {
            alert()->error($_SERVER ['HTTP_USER_AGENT'], 'Usted no tiene permisos para realizar ésta actividad, se notificará al administrador')->persistent('Cerrar');
            return redirect('/'); 
        }
        //return "formulario de creacion de autoridad";
        return view('web.multimedia.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response AuthoritiesStoreRequest Request
     */ 
    public function store(MultimediaStoreRequest $request)
    {        
        if (!Auth::user()) {
            alert()->error($_SERVER ['HTTP_USER_AGENT'], 'Usted no tiene permisos para realizar ésta actividad, se notificará al administrador')->persistent('Cerrar');
            return redirect('/'); 
        }
        $data = new Multimedia;
        $data->title  = $request->title;  

        //explode(' ', $request->embed);
        $data->embed  = $request->embed;  
        $data->description  = $request->description;  
        $data->publish_up  = $request->publish_up;     
        $data->save();

        alert()->info('Registrado correctamente', $request->title. "<br>".$request->embed )
               ->html()->persistent("OK");;
             
        return redirect('multimedia');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return "multimedia-".$id;
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    { 
        if (!Auth::user()) {
            alert()->error($_SERVER ['HTTP_USER_AGENT'], 'Usted no tiene permisos para realizar ésta actividad, se notificará al administrador')->persistent('Cerrar');
            return redirect('/'); 
        }
        $data['multimedia'] = Multimedia::find($id);
        
        if (!Multimedia::find($id)) {
           alert()->error('No se ha encontrado el registro', 'ERROR');
           return redirect('multimedia');
        }
        
        return view('web.multimedia.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(MultimediaPutRequest $request, $id)
    { 
        if (!Auth::user()) {
            alert()->error($_SERVER ['HTTP_USER_AGENT'], 'Usted no tiene permisos para realizar ésta actividad, se notificará al administrador')->persistent('Cerrar');
            return redirect('/'); 
        }
        $data = Multimedia::find($id);
        $data->title  = $request->title;  
        $data->embed  = $request->embed;  
        $data->description  = $request->description;  
        $data->publish_up  = $request->publish_up;     
        $data->save();

        alert()->success('Registro actualizado correctamente', $request->title);

        if (Auth::user()) {
            $data['multimedia'] = Multimedia::getMultimedia()->paginate(5);
        }else{
            $data['multimedia'] = Multimedia::getMultimedia()->where('status',true)->paginate(5); 
        }
             
        return redirect('multimedia');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if (!Auth::user()) {
            alert()->error($_SERVER ['HTTP_USER_AGENT'], 'Usted no tiene permisos para realizar ésta actividad, se notificará al administrador')->persistent('Cerrar');
            return redirect('/'); 
        }
        if(\Request::ajax()) {
            Multimedia::destroy($id);
            return response()->json(['element' => $id, 'message' => 'delete'], 202);
        }
        //Authorities::destroy($id);

        Multimedia::find($id)->delete();
    }

    public function statusChange($id,$status)
    {
        if (!Auth::user()) {
            alert()->error($_SERVER ['HTTP_USER_AGENT'], 'Usted no tiene permisos para realizar ésta actividad, se notificará al administrador')->persistent('Cerrar');
            return redirect('/'); 
        }
        if(\Request::ajax()) {
            if ($status) {
                Multimedia::where('id', $id)->update(['status' => false]);
                return response()->json(['element' => $id, 'message' => 'to False', 'now' => false, 'previous' => $status], 202);
            }else{
                Multimedia::where('id', $id)->update(['status' => true]);
                return response()->json(['element' => $id, 'message' => 'to False', 'now' => true, 'previous' => $status], 202);
            }
        }
        return response()->json(['element' => $id, 'message' => 'error'], 404);
    }

}
