<?php

namespace App\Http\Controllers\master;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;

class MatauangController extends Controller
{
    //
    public function __construct(){      
        $this->middleware(function ($request, $next) {
        $url = request()->segment(1);
        $kode_group = Session::get('kode_group');
        $d_menu = DB::table("tb_menu as a")->join("tb_role as b","a.kode_menu","b.kode_menu")->where([['a.link_menu',$url],['b.kode_group',$kode_group],['b.status_role','1'],['a.active','1']])->orderBy('a.sort_menu','asc')->count();
        if(Session::get('decode') <> 1)
         {
            Session::flash('message', 'Silahkan login dengan username dan password anda.!!');
             return redirect()->to('login');
         }
         if($d_menu == 0){
            Session::flush();
            Session::flash('message', 'Anda Tidak Punya Akses ke Halaman ini.!!');
             return redirect()->to('login');
         }
        return $next($request); 
        });
    }

    public function index(){
        return view('master.MatauangList');
    }

    public function tampil(){
        DB::statement(DB::raw('set @rownum=0'));
        $d_data = DB::table('ms_matauang')->select(DB::raw('*, @rownum  := @rownum  + 1 AS rownum'));
        if($d_data->count() > 0){
            foreach ($d_data->get() as $d) {
                # code...
                $arr[] = array('id_matauang' => $d->id_matauang,
                          'nama_matauang' => $d->nama_matauang,
                          'inisial' => $d->inisial,
                          'simbol' => $d->simbol,
                          'rownum' => $d->rownum,
                          'aksi'=> '<button class="btn bg-warning-400" onclick="edit('.$d->id_matauang.')"><i class="icon-pencil"></i> </button>
                        <button class="btn bg-danger-800" type="button" onclick="hapus('.$d->id_matauang.')"><i class="icon-eraser"></i></button>'.
                        '<input type="hidden" id="table_id'.$d->id_matauang.'" value="'.$d->id_matauang.'">'.
                        '<input type="hidden" id="table_nama'.$d->id_matauang.'" value="'.$d->nama_matauang.'">'.
                        '<input type="hidden" id="table_inisial'.$d->id_matauang.'" value="'.$d->inisial.'">'.
                        '<input type="hidden" id="table_simbol'.$d->id_matauang.'" value="'.$d->simbol.'">');
            }
        }else{
            $arr = array();
        }

        $data = new Collection($arr);
        return Datatables::of($data)
                ->make(true);
    }

    function simpan(Request $request){
        $id_matauang = $request->get('id_matauang');
        $data['nama_matauang'] = $request->get('nama_matauang');
        $data['inisial'] = $request->get('inisial');
        $data['simbol'] = $request->get('simbol');

        if($id_matauang == ''){
            DB::table('ms_matauang')->insert($data);
        }else{
            DB::table('ms_matauang')->where(array('id_matauang'=>$id_matauang))->update($data);
        }
    }

    function hapus(Request $request){
        $data['id_matauang'] = $request->get('id');

        DB::table('ms_matauang')->where($data)->delete();
    }
}
