<?php

namespace App\Modules\Catalogo\Controllers;
use App\Modules\Catalogo\Models\Accountbank;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Yajra\Datatables\Datatables;


class AccountbanksController extends Controller
{
    protected $table;
    function __construct(){
        $this->middleware('auth');
        $this->table = new Accountbank();
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('Catalogo.views.accountbanks')->with('title','hola');
    }
    /**
     * Display a listing of the datatables list.
     *
     */
    public function getTable()
    {   

        $table = Datatables::of($this->table::all());
        $table->addColumn('action', function($row){
            $url = url('accountbanks/'.$row['id']);
            return view('layouts.buttons.buttons-dinamyc',['buttons'=> array(
                array('tipe'=>'edit',
                      'id_row'=>$row['id'],
                      'modal'=>'no',
                      'url'=>$url.'/edit',
                      'class'=>" btn btn-sm btn-primary",
                      'icon'=>"icon-compose"
                ),
                array('tipe'=>'delete',
                      'id_row'=>$row['id'],
                      'modal'=>'no',
                      'url'=>$url,
                      'class'=>"btn btn-sm btn-danger fn_delete",
                      'icon'=>"icon-trash"
                )
            )]);        
        })->rawColumns(['action']);
        return $table->make(true);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('Catalogo.views.add.accountbank');
    }

    /**
     * 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(),[
                        'nro' => 'required|int',
                        'nombreentidad' => 'required',
                        'propietario' => 'required',
                        'tipe' => 'required',
                        'moneda' => 'required',
                    ] );

                   if ($validator->fails()) {
                       return response()->json(['error'=>$validator->errors()->all()]);

                    }else{
                        if ($this->table::where('nrocuenta', '=', $request->input("nro"))->count() > 0) {
                           return response()->json(['error'=>'El numero de cuenta ya  existe']);
                        }else{
                            $this->table->nrocuenta = $request->input("nro");
                            $this->table->nombre = $request->input("nombreentidad");
                            $this->table->propietario = $request->input("propietario");
                            $this->table->tipo = $request->input("tipe");
                            $this->table->moneda = $request->input("moneda");
                            $this->table->tarjeta = $request->input("tarjeta");
                            $this->table->cobrar = $request->input("cobrar");
                            $this->table->pagar = $request->input("pagar");
                            $this->table->deposito = $request->input("deposito");
                            $this->table->nrcheque = $request->input("cheque");
                            $this->table->save();
                            return response()->json(['success'=>'Registro agregado con exito','url'=>url("accountbanks/")]);
                        }
                    }
                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)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->table::find($id);
        return view('Catalogo.views.edit.accountbank',['item'=>$data]);
    }

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