<?php

namespace App\Modules\Catalogo\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Facades\Validator;
use App\Modules\Catalogo\Models\Plan_cuenta;
class CuentasController extends Controller
{
    protected $table;
    function __construct(){
        $this->table = new Plan_cuenta();
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $cuentas = $this->table::all();
       return view('Catalogo.views.cuentas')->with('cuentas',$cuentas);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

     /**
     * Display a listing of the datatables list.
     *
     */
    public function getTable()
    {   
        $table = Datatables::of($this->table::select('*')->orderBy('codigo')->get());
        $table->addColumn('auxiliar',function($row){
            return view('Catalogo.views.utilities.icon_cuentas')->with('campo',$row['auxiliar']);
        })->addColumn('articulo',function($row){
            return view('Catalogo.views.utilities.icon_cuentas')->with('campo',$row['articulos']);
        })->addColumn('action', function($row){
            $url = url('cuentas/'.$row['id']);
            return view('layouts.buttons.buttons-dinamyc',['buttons'=> array(
                array('tipe'=>'edit',
                      'id_row'=>$row['id'],
                      'modal'=>'si',
                      'url'=>$url,
                      'class'=>" btn btn-primary fn_edit",
                      'icon'=>"icon-compose"
                ),
                array('tipe'=>'delete',
                      'id_row'=>$row['id'],
                      'modal'=>'no',
                      'url'=>$url,
                      'class'=>"btn btn-danger fn_delete",
                      'icon'=>"icon-trash"
                )
            )]);        
        })->rawColumns(['auxiliar','articulo','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':
                    return $this->getTable();
                break;
                case 'add':
                    $validator = Validator::make($request->all(),[
                        'codigo' => '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'=>'reload']);
                    }
                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)
    {
        $plan_cuentas =  $this->table::find($id);
        $cuentas =  $this->table::all();
        if (!empty($plan_cuentas)) {
            return view('Catalogo.views.edit.cuentas')->with('data',['cuenta'=>$plan_cuentas,'cuentas'=>$cuentas]);
        }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->all())->save();
            return response()->json(['success'=>'Registro actualizado con exito','tablereload'=>'reload']);
        }else{
            return response()->json(['error'=>'El registro no existe']);
        }
    }

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

    }
}

