<?php

namespace DMZ\Http\Controllers\Web;

use Illuminate\Http\Request;
use DMZ\Models\Authorities;
use DMZ\Http\Requests;
use DMZ\Http\Requests\Web\AuthoritiesStoreRequest;
use DMZ\Http\Requests\Web\AuthoritiesPutRequest;
use DMZ\Http\Controllers\Controller;
use Auth; 

class AuthoritiesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //return 'list auth';

        if (Auth::user()) {
            $data['authorities'] = Authorities::getAuthorities()->paginate(5);
        }else{
            $data['authorities'] = Authorities::getAuthorities()->where('status',true)->paginate(5); 
        }
        
             
        return view('web.authorities.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.authorities.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response AuthoritiesStoreRequest Request
     */ 
    public function store(AuthoritiesStoreRequest $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 Authorities;
        $data->name  = $request->name;  
        $data->lastname1  = $request->lastname1;  
        $data->lastname2  = $request->lastname2;  
        $data->position  = $request->position;  
        $data->community  = $request->community;  
        $data->movil  = $request->movil;  
        $data->email  = $request->email;  
        $data->fb  = $request->fb;  
        $data->publish_up  = $request->publish_up;    
        $data->save();

        alert()->info('Registrado correctamente', $request->name.' '.$request->lastname1.' '.$request->lastname2);

        return redirect('autoridades'); 
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return "show".$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['authority'] = Authorities::find($id);

        if (!Authorities::find($id)) {
           alert()->error('No se ha encontrado el registro', 'ERROR');
           return redirect('autoridades');
        }
        
        return view('web.authorities.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AuthoritiesPutRequest $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('/'); 
        }
        //return "para editar";
        //return $request->all();
        $authority = Authorities::find($id);
        $authority->name = $request->name;
        $authority->lastname1 = $request->lastname1;
        $authority->lastname2 = $request->lastname2;
        $authority->position = $request->position;
        $authority->community = $request->community;
        $authority->movil = $request->movil;
        $authority->email = $request->email;
        $authority->fb = $request->fb;
        $authority->publish_up = $request->publish_up;
        $authority->save();

        alert()->success('Registro actualizado correctamente', $request->name.' '.$request->lastname1.' '.$request->lastname2);
              
        return redirect('autoridades');

    }

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

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

}
