<?php

namespace App\Http\Controllers\master;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use DB;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Yajra\Datatables\Datatables;
use Illuminate\Support\Facades\Session;

class KategoriController 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);	
        });
    }

	function index(){
		$d_data['kelompok'] = DB::table('ms_kelompok')->orderby('nama_kelompok', 'asc')->get();
		return view('master.KategoriList')->with('data', $d_data);
	}

	function get_kb(){
		DB::statement(DB::raw('set @rownum=0'));
		$d_data = DB::table('ms_kategori_barang as mkb')->join('ms_kelompok as mk', 'mkb.id_kelompok', 'mk.id_kelompok')->select(DB::raw('mkb.*, mk.nama_kelompok, @rownum  := @rownum  + 1 AS rownum'))->where('induk_kb', '=', '0');

		return Datatables::of($d_data)
				->addColumn('aksi', function($d_data){
					$btnAction = '<button class="btn bg-warning-400" onclick="edit_kb('.$d_data->id_kb.')"><i class="icon-pencil"></i> </button>
	 					<button class="btn bg-danger-800" type="button" onclick="hapus_kb('.$d_data->id_kb.')"><i class="icon-eraser"></i></button>'.
						'<input type="hidden" id="table_idkb'.$d_data->id_kb.'" value="'.$d_data->id_kb.'">'.
						'<input type="hidden" id="table_nama'.$d_data->id_kb.'" value="'.$d_data->nama.'">'.
						'<input type="hidden" id="table_idkel'.$d_data->id_kb.'" value="'.$d_data->id_kelompok.'">'
						;
					return $btnAction;
				})->addColumn('kategori', function($d_data){
					$kategori = '<div data-id="'.$d_data->id_kb.'"><a href="'.url('kategori/sub_kategori').'/'.$d_data->id_kb.'">'.$d_data->nama.'</a></div>';
					return $kategori;
				})
				->make(true);
	}

	function simpan_kb(Request $request){

		$id_kb = $request->get('id');
		$data['nama'] = $request->get('nama');
		$data['id_kelompok'] = $request->get('id_kel');
		$data['inisial'] = strtoupper($request->get('inisial'));
		$data['induk_kb'] = $request->get('induk');

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

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

		DB::table('ms_kategori_barang')->where($data)->delete();
		DB::table('ms_kategori_barang')->where('induk_kb', '=', $data['id_kb'])->delete();
	}

	function sub_kategori($id){
		$data['id'] = $id;
		$d_data = DB::table('ms_kategori_barang')->where('id_kb', '=', $id);
		if($d_data->count() > 0){
			$data['d_data'] = $d_data->first();
			return view('master.KategoriSub')->with('data', $data);
		}else{
			return redirect('kategori');
		}


	}

	function get_sub(Request $request){
		if($_POST){
			$id = $request->get('id');
		}else{
			$id = $request->id;
		}
		DB::statement(DB::raw('set @rownum=0'));
		$d_data = DB::table('ms_kategori_barang')->select(DB::raw('*'))->where('id_kb', '=', $id);
		$sub = DB::table('ms_kategori_barang as mkb')->join('ms_kategori_barang as mkb2', 'mkb.id_kb', 'mkb2.induk_kb')->join('ms_kelompok as mk', 'mkb.id_kelompok', 'mk.id_kelompok')->select(DB::raw('mkb.*, mkb2.nama as nama_sub, mkb2.inisial as inisial_sub, mkb2.id_kb as id_sub, mk.id_kelompok, @rownum  := @rownum  + 1 AS rownum'))->where('mkb.id_kb', '=', $id);
		$arr;
		
		// if($d_data->count() > 0){
		// 	$sub = DB::table('ms_kategori_barang')->where('induk_kb', '=', $id);
		// 	if($sub->count() > 0){
		// 		foreach($sub->get() as $d){
		// 			$arr['data'][] = ['id_kb' => $d->id_kb,
		// 					'nama' => $d->nama,
		// 					'inisial' => $d->inisial,
		// 					'aksi' => '<button class="btn bg-warning-400" onclick="edit_kb('.$d->id_kb.')"><i class="icon-pencil"></i> </button>
		// 					<button class="btn bg-danger-800" type="button" onclick="hapus_kb('.$d->id_kb.')"><i class="icon-eraser"></i></button>'];
		// 		}
		// 		$arr['status'] = array('status' => '1', 'nama' => $d_data->get()[0]->nama);
		// 	}else{
		// 		$arr['status'] = array('status' => '2', 'nama' => $d_data->get()[0]->nama);
				
		// 	}
		// }else{
		// 	$arr['status'] = array('status' => '0');
			
		// }

		return Datatables::of($sub)
				->addColumn('aksi', function($sub){

					$btnAction = '<button class="btn bg-warning-400" onclick="edit_kb('.$sub->id_sub.')"><i class="icon-pencil"></i> </button>
	 					<button class="btn bg-danger-800" type="button" onclick="hapus_kb('.$sub->id_sub.')"><i class="icon-eraser"></i></button>'.
						'<input type="hidden" id="table_idkb'.$sub->id_sub.'" value="'.$sub->id_sub.'">'.
							'<input type="hidden" id="table_nama'.$sub->id_sub.'" value="'.$sub->nama_sub.'">'.
							'<input type="hidden" id="table_inisial'.$sub->id_sub.'" value="'.$sub->inisial_sub.'">'.
							'<input type="hidden" id="table_kategori'.$sub->id_sub.'" class="value_" value="'.$sub->nama.'">'.
							'<input type="hidden" id="table_idkel'.$sub->id_sub.'" value="'.$sub->id_kelompok.'">';
					return $btnAction;
				})
				->make(true);

	//	return response()->json($arr);
	}
	
}