<?php

namespace App\Modules\Utilitarios\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use App\Modules\Utilitarios\Models\Eventos_operation;
use App\Services\CurlService;
use Yajra\Datatables\Datatables;

class Eventos_operationController extends Controller
{
    protected $table;
    function __construct(){
        $this->table = new Eventos_operation();
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('utilitarios.views.eventos_operation');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
    /**
     * Get DataTables Data
     *
     * @return Views Data
    */
    public function getTable($search=''){
        $table = Datatables::of($this->table::all());
        $table->addColumn('action', function($row){
            return view('layouts.buttons.buttons-tables')->with('url','eventos_operation')->with('id',$row['id'])->with('tipe','compress');
        })->rawColumns(['action']);
        return $table->make(true);
    }

    /**
     * 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':
                    $table = $this->getTable();
                    return $table;    
                break;
                case 'add':
                     $validator = Validator::make($request->all(),[
                        'nombre' => 'required',
                        'descripcion' => '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:

                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);
        if (!empty($data)) {
            return view('Utilitarios.views.edit.editeventos_operation')->with('data',['evento'=>$data]);
        }else{

        }
    }

    /**
     * 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->except(['action']))->save();
            return response()->json(['success'=>'Registro actualizado con exito','tablereload'=>'1']);
        }
    }

    /**
     * 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']);
        }
    }
}
