<?php

namespace App\Modules\Visitas\Controllers;
use App\Modules\Visitas\Models\Empleado;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Yajra\Datatables\Datatables;
use DB;
class EmpleadosController extends Controller
{
    protected $table;
    function __construct(){
        $this->middleware('auth');
        $this->table = new Empleado();
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        return view('Visitas.views.empleados');
    }
    /**
     * Display a listing of the datatables list.
     *
     */
    public function getTable()
    {   

        $table = Datatables::of($this->table::all());
        $table->addColumn('action', function($row){
            return view('layouts.buttons.buttons-tables')->with('url','empleados')->with('id',$row['id'])->with('modal','1');
        })->rawColumns(['action']);
        return $table->make(true);
    }
    /**
     * 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)
    {
        if ($request->has('action')) {
            switch ($request->input('action')) {
                case 'showtable':
                    return $this->getTable();
                break;
                case 'add':
                    $validator = Validator::make($request->all(),[
                        'nombre' => 'required',
                    ]);
                     if ($validator->fails()) {
                       return response()->json(['error'=>$validator->errors()->all()]);
                    }else{
                        $this->table->fill($request->except(['action']))->save();
                        return response()->json(['success'=>'Registro agregado con exito','tablereload'=>'tablereload']);
                    }
                break;
                default:
                    # code...
                break;
            }
        }else{
            return response()->json(['error'=>'No existe ningun token por favor no borre nuesto codigo html']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = $this->table::find($id);
        return view('Visitas.views.edit.empleados')->with('data',$data);
    }

    /**
     * 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)
    {
      $data = $this->table::find($id);
      if (!empty($data)) {
        $data->fill($request->all())->save();      
        return response()->json(['success'=>'Registro actualizado con exito','tablereload'=>'tablereload']);
      }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->table::find($id);
        if (!empty($data)) {
            $data->delete();
            return response()->json(['success'=>'Registro eliminado con exito']);
        }
    }
}
