<?php

namespace DMZ\Http\Controllers\Web;

use Illuminate\Http\Request;
use DMZ\Models\Fotos;
use DMZ\Models\Albums;
use DMZ\Http\Requests;
use DMZ\Http\Requests\Web\FotoStoreRequest;
use DMZ\Http\Requests\Web\FotoPutRequest;
use DMZ\Http\Requests\Web\AlbumStoreRequest;
use DMZ\Http\Requests\Web\AlbumPutRequest;
use DMZ\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
use Auth; 

class AlbumsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {           
        if (Auth::user()) {
            $data['albums'] = Albums::getAlbums()->paginate(9);
        }else{
            $data['albums'] = Albums::getAlbums()->where('status',true)->paginate(9); 
        }       
        return view('web.albums.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.albums.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response AuthoritiesStoreRequest Request
     */  
     // FotoStoreRequest
    public function store(AlbumStoreRequest $request)
    {        
        //return $request->all();
        
        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 Albums;
            $data->album  = $request->album;    
            $data->description  = $request->description;  
            $data->publish_up  = $request->publish_up;
     
            $data->save();

        alert()->info('Registrado correctamente', $request->album );
        return redirect('albums');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return "fotos-".$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['album'] = Albums::find($id);
        if (!Albums::find($id)) {
           alert()->error('No se ha encontrado el registro', 'ERROR');
           return redirect('albums');
        }
        
        return view('web.albums.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AlbumPutRequest $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 = Albums::find($id);
        $data->album  = $request->album;    
        $data->description  = $request->description;  
        $data->publish_up  = $request->publish_up; 


        $data->save();

        alert()->success('Registro actualizado correctamente', $request->album);
 
        return redirect('albums');
    }

    /**
     * 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()) {
            Albums::destroy($id);
            return response()->json(['element' => $id, 'message' => 'delete'], 202);
        }
        //Authorities::destroy($id);

        Albums::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) {
                Albums::where('id', $id)->update(['status' => false]);
                return response()->json(['element' => $id, 'message' => 'to False', 'now' => false, 'previous' => $status], 202);
            }else{
                Albums::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);
    }
    
    public function scopeSetAlbum(){
        
    }

}
