<?php

namespace App\Http\Controllers;
use App\users;
use App\mensajes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

class adminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users    = DB::table('users')->count();
        $messages = DB::table('mensajes')->count();
        $requerimientos = DB::table('requerimientos')->count();


        return view('admin.admin',[
            'users'    => $users,
            'messages' => $messages,
            'requerimientos' => $requerimientos,
        ]);
    }
    public function profile()
    {
        return view('admin.profileAdmin');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|max:20',
            'email' => 'required|email',
            'surname' => 'required',
            'idCard' => 'required',
            'address' => 'required',
            'tel' => 'required',
        ]);
       
        if(Auth::user()->role=='admin'){
            $user = users::find($id);
            $user->name =$request->name;
            $user->email =$request->email;
            $user->surname =$request->surname;
            $user->idCard =$request->idCard;
            $user->address =$request->address;
            $user->tel =$request->tel;
            $user->update();
        }
        return redirect('profileAdmin');
    }
    
    public function updateAdminPassword(Request $request, $id) {
        $request->validate([
            'newPassword' => 'required|max:20|min:7',
        ]);
        $user = users::find($id);
        $user->password = Hash::make($request->newPassword);
        $user->update();
        return redirect('profileAdmin');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
