<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Validator;
use App\UnorJenis;
use Yajra\DataTables\Facades\DataTables;

class unorJenisController extends Controller
{
    //
    public function index()
    {
        return view('backend.unorjenis.index');
    }

    public function data(Request $request)
    {
        if ($request->ajax()) {
            $menu = UnorJenis::latest();
            return Datatables::of($menu)
            ->addIndexColumn()
            ->addColumn(
                'action',
                          '<center>
                              <a class="edit ubah" data-toggle="tooltip" data-placement="top" title="Edit" menu-id="{{ $id }}" href="#edit-{{ $id }}">
                                  <i class="fa fa-pencil text-warning"></i>
                              </a>&nbsp; &nbsp;
                              <a class="delete hidden-xs hidden-sm hapus" data-toggle="tooltip" data-placement="top" title="Delete" menu-id="{{ $id }}" href="#hapus-{{ $id }}">
                                  <i class="fa fa-trash text-danger"></i>
                              </a>
                          </center>'
                        )
             ->addColumn('submenu', '<center><a href="{{ url(config("master.url.admin")."/submenu/".$id)  }}" class="paket" data-toggle="tooltip" data-placement="top" title="Submenu"><i class="fa fa-external-link text-info"></i></a>')
             ->addColumn('tampilkan', function ($menu) {
                 $status  = $menu->status == 1 ? '<span class="badge badge-pill badge-info">Tampil</span>':'<span class="badge badge-pill badge-danger">Tidak Tampil</span>';
                 $private = $menu->tampil == 1 ? '<span class="badge badge-pill badge-primary">Private</span>':'<span class="badge badge-pill badge-success">Public</span>';
                 return '<center>'.$status .'&nbsp;&nbsp;'. $private.'</center>';
             })
               ->rawColumns(['tampilkan', 'action', 'submenu'])->make(true);
        } else {
            exit("Not an AJAX request -_-");
        }
    }

    public function create()
    {
        return view('backend.unorjenis.tambah');
    }

   /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id'		=> 'required',
            'nama'		=> 'required',
            
            
        ]);
        if ($validator->fails()) {
            $respon = array('status'=>false, 'pesan' => $validator->messages());
        } else {
            if (Menu::create($request->all())) {
                $respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil disimpan']);
            } else {
                $respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal disimpan']);
            }
        }
        return response()->json($respon);
    }

    public function edit($id)
    {
        $menu = UnorJenis::find($id);
        return view('backend.unorjenis.ubah', ['menu' => $menu]);
    }

    public function update(Request $request)
    {
        
    }
}
