<?php

namespace App\Http\Controllers;
use App\Toko;
use Session;
use Illuminate\Http\Request;
use Yajra\Datatables\Html\Builder;
use Yajra\Datatables\Datatables;

class TokoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,Builder $htmlBuilder)
    {
      if($request->ajax()){
         $tokos = Toko::select(['id','name']);
        return Datatables::of($tokos)
        ->addColumn('action',function($toko){
          return view('datatable._action',[
            'model' =>$toko,
            'form_url'=>route('toko.destroy',$toko->id),
            'edit_url'=>route('toko.edit',$toko->id),
            'confirm_message'=>'Yakin mau menghapus ' .$toko->name. '?'
          ]);
        })->make(true);
      }

        $html = $htmlBuilder
        ->addColumn(['data' =>  'name', 'name'  =>  'name',   'title'  => 'Nama'])
        ->addColumn(['data' =>  'action','name' =>  'action', 'title'  =>'' , 'orderable' =>  false , 'searchable'  =>  false]);
        return  view('toko.index')->with(compact('html'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('toko.create');//
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,['name'=>'required|unique:tokos']);
        $toko = Toko::create($request->only('name'));
        Session::flash("flash_notification",[
          "level"=>"success",
          "message"=>"Berhasil Menyimpan $toko->name"
        ]);
        return redirect()->route('toko.index');//
    }

    /**
     * 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)
    {
        $toko = Toko::find($id);
        return view('toko.edit')->with(compact('toko'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,['name'=>'required|unique:tokos,name,'.$id]);
        $toko = Toko::find($id);
        $toko->update($request->only('name'));
        Session::flash("flash_notification",[
          "level"=>"success",
          "message"=>"Berhasil menyimpan $toko->name"
        ]);

        return redirect()->route('toko.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(!Toko::destroy($id))
        return redirect()->back();

        Session::flash("flash_notification",[
          "level"=>"success",
          "message"=>"Toko berhasil dihapus"
        ]);
        return redirect()->route('toko.index');
    }
}
