<?php

namespace App\Http\Controllers\master;

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

class KonversiController 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.KonversiList');
    }

    public function tampil(){
        DB::statement(DB::raw('set @rownum=0'));
    	$d_data = DB::table('ms_konversi as mkv')->join('ms_barang as mb', 'mkv.kode', 'mb.id')->join('ms_satuan as ms', 'mkv.satuan_awal', 'ms.id_satuan')->join('ms_satuan as ms2', 'mkv.satuan_akhir', 'ms2.id_satuan')->select(DB::raw('mkv.*, mb.kode_barang, mb.nama_barang, ms.id_satuan id_satuanawal, ms.nama_satuan satuan_awal, ms2.id_satuan id_satuanakhir, ms2.nama_satuan satuan_akhir, @rownum  := @rownum  + 1 AS rownum'));
    	// if($d_data->count() > 0){
    	// 	foreach($d_data->get() as $d){
     //            $d_satuan = DB::table('ms_satuan')->where('id_satuan', '=', $d->satuan_awal)->get();
    	// 	$arr[] = ['id_konversi' => $d->id_konversi,
    	// 			  'kode'	=>	$d->kode,
    	// 			  'barang'	=>	$d->kode_barang.' - '.$d->nama_barang,
    	// 			  'nama'	=>	$d->nama,
    	// 			  'satuan_awal'	=>	$d->satuan_awal,
    	// 			  'satuan_akhir'	=>	$d->satuan_akhir,
    	// 			  'nilai_awal'	=>	$d->nilai_awal,
    	// 			  'nilai_akhir'	=>	$d->nilai_akhir,
     //                  'nm_satuan_awal' => DB::table('ms_satuan')->where('id_satuan', '=', $d->satuan_awal)->get()[0]->nama_satuan,
     //                  'nm_satuan_akhir' => DB::table('ms_satuan')->where('id_satuan', '=', $d->satuan_akhir)->get()[0]->nama_satuan,
    	// 			  'aksi' => 
    	// 			  	'<button class="btn bg-warning-400" onclick="edit('.$d->id_konversi.')"><i class="icon-pencil"></i></button>
					// 	<button class="btn bg-danger-800" type="button" onclick="hapus('.$d->id_konversi.')">
					// 	<i class="icon-eraser"></i></button>'];
    	// 	}
    	// }else{
    	// 	$arr = array();
    	// }
    	// return response()->json($arr);

        return Datatables::of($d_data)
                ->addColumn('aksi', function($d_data){
                    $btnAction = '<button class="btn bg-warning-400" onclick="edit('.$d_data->id_konversi.')"><i class="icon-pencil"></i></button>
                        <button class="btn bg-danger-800" type="button" onclick="hapus('.$d_data->id_konversi.')">
                        <i class="icon-eraser"></i></button>'.
                        '<input type="hidden" id="table_id'.$d_data->id_konversi.'" value="'.$d_data->id_konversi.'">'.
                        '<input type="hidden" id="table_kode'.$d_data->id_konversi.'" value="'.$d_data->kode.'">'.
                        '<input type="hidden" id="table_nama'.$d_data->id_konversi.'" value="'.$d_data->nama_barang.'">'.
                        '<input type="hidden" id="table_satuan_awal'.$d_data->id_konversi.'" value="'.$d_data->id_satuanawal.'">'.
                        '<input type="hidden" id="table_satuan_akhir'.$d_data->id_konversi.'" value="'.$d_data->id_satuanakhir.'">'.
                        '<input type="hidden" id="table_nilai_awal'.$d_data->id_konversi.'" value="'.$d_data->nilai_awal.'">'.
                        '<input type="hidden" id="table_nilai_akhir'.$d_data->id_konversi.'" value="'.$d_data->nilai_akhir.'">'
                        ;
                    return $btnAction;
                })->addColumn('nm_barang', function($d_data){
                    $nm_barang = $d_data->kode_barang.' - '.$d_data->nama_barang;
                    return $nm_barang;
                })
                ->make(true);
    }

    function get_barang(){
        $d_data = DB::table('ms_barang AS mb')->join('ms_kategori_barang AS mkb', 'mb.id_kb', 'mkb.id_kb')->join('ms_kategori_barang AS mkb2', 'mkb.induk_kb', 'mkb2.id_kb')->join('ms_satuan AS ms', 'mb.id_satuan', 'ms.id_satuan')->select(DB::raw('mb.*, mkb2.id_kb AS id_kategori, mkb2.nama nama_kategori, mkb.id_kb id_sub, mkb.nama nama_sub, ms.id_satuan, ms.nama_satuan'));

        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_kategori', 'asc')->orderBy('nama_sub', 'asc')->orderBy('kode_barang', 'asc')->orderBy('nama_barang', 'asc')->get() as $d){
                $arr[] = array('id_barang' => $d->id,
                                'kode_barang' => $d->kode_barang,
                                'nama_barang' => $d->nama_barang,
                                'id_kategori' => $d->id_kategori,
                                'kategori' => $d->nama_kategori,
                                'id_sub' => $d->id_sub,
                                'nama_sub' => $d->nama_sub,
                                'id_satuan' => $d->id_satuan,
                                'satuan' => $d->nama_satuan);
            }
        }else{
            $arr = array();
        }

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



    public function simpan(Request $request){
    	$id = $request->get('id');
    	$data['kode'] = $request->get('kode');
    	$data['satuan_awal'] = $request->get('satuan_awal');
    	$data['satuan_akhir'] = $request->get('satuan_akhir');
    	$data['nilai_awal'] = $request->get('nilai_awal');
    	$data['nilai_akhir'] = $request->get('nilai_akhir');

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

    }

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

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

    function get_satuan(){
        $d_data = DB::table('ms_satuan');
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id_satuan' => $d->id_satuan,
                                'nama_satuan' => $d->nama_satuan);
            }
        }else{
            $arr = array();
        }

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