<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Yajra\Datatables\Html\Builder;
use Yajra\Datatables\Datatables;
use App\Menu;
use Laratrust\LaratrustFacade as Laratrust;

class GuestController extends Controller
{
  public function index(Request $request, Builder $htmlBuilder)
{
    if ($request->ajax()) {
      $menus = Menu::with('tokos');
      return Datatables::of($menus)
        ->addColumn('stock',function($menu){
          return $menu->stock;
        })
        ->addColumn('action', function($menu){
          if (Laratrust::hasRole('admin')) return '';
          return '<a class="btn btn-xs btn-primary" href="'.route('guest.menu.beli',$menu->id).'">Pesan</a>';
        })->make(true);
      }
    $html = $htmlBuilder
    ->addColumn(['data' => 'toko_id', 'name'=>'toko_id', 'title'=>'ID Toko'])
    ->addColumn(['data' => 'title', 'name'=>'title', 'title'=>'Makanan'])
    ->addColumn(['data' =>  'price','name' =>'price','title'=>'Harga'])
    ->addColumn(['data' => 'stock', 'name'=>'stock', 'title'=>'Stok', 'orderable'=>false, 'searchable'=>false])
    ->addColumn(['data' => 'action', 'name'=>'action', 'title'=>'', 'orderable'=>false, 'searchable'=>false]);
    return view('guest.index')->with(compact('html'));
  }
}
