<?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 CarabayarController 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.CarabayarList');
    }

    function tampil(){
    	DB::statement(DB::raw('set @rownum=0'));
		$d_data = DB::table('ms_carabayar')->leftJoin('ms_perkiraan','ms_carabayar.kode_perkiraan','ms_perkiraan.id')->orderByRaw("ms_carabayar.jenis_bayar", "ms_carabayar.id_carabayar")->select('ms_carabayar.id_carabayar','ms_carabayar.cara','ms_perkiraan.kode_perkiraan','ms_perkiraan.id','ms_perkiraan.nama_perkiraan','ms_carabayar.tipe', 'ms_carabayar.jenis_bayar', DB::raw('@rownum  := @rownum  + 1 AS rownum'));

		// $d_data = DB::table('ms_carabayar')->join('ms_perkiraan','ms_carabayar.kode_perkiraan','ms_perkiraan.id')->select('ms_carabayar.id_carabayar','ms_carabayar.cara','ms_perkiraan.kode_perkiraan','ms_perkiraan.id','ms_perkiraan.nama_perkiraan','ms_carabayar.tipe', DB::raw('@rownum  := @rownum  + 1 AS rownum'));

		// if($d_data->count() > 0){
		// 	foreach($d_data->get() as $d){
		// 		$arr[] = ['id_carabayar' => $d->id_carabayar,
		// 					'cara' => $d->cara,
		// 					'kode' => $d->kode_perkiraan.' - '.$d->nama_perkiraan,
		// 					'tipe' => $d->tipe,
		// 					'perkiraan' => $d->id,
		// 				'aksi' => '<button class="btn bg-warning-400" onclick="edit_carabayar('.$d->id_carabayar.')"><i class="icon-pencil"></i> </button>
		// 				<button class="btn bg-danger-800" type="button" onclick="hapus_carabayar('.$d->id_carabayar.')"><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_carabayar('.$d_data->id_carabayar.')"><i class="icon-pencil"></i> </button><button class="btn bg-danger-800" type="button" onclick="hapus_carabayar('.$d_data->id_carabayar.')"><i class="icon-eraser"></i></button>'.
						'<input type="hidden" id="table_jenis_bayar'.$d_data->id_carabayar.'" value="'.$d_data->jenis_bayar.'">'.
						'<input type="hidden" id="table_id'.$d_data->id_carabayar.'" value="'.$d_data->id_carabayar.'">'.
						'<input type="hidden" id="table_cara'.$d_data->id_carabayar.'" value="'.$d_data->cara.'">'.
						'<input type="hidden" id="table_kode'.$d_data->id_carabayar.'" value="'.$d_data->id.'">'.
						'<input type="hidden" id="table_tipe'.$d_data->id_carabayar.'" value="'.$d_data->tipe.'">'
						;
					return $btnAction;
				})->addColumn('perkiraan', function($d_data){
					$value = $d_data->kode_perkiraan.' - '.$d_data->nama_perkiraan;
					return $value;
				})
				->make(true);
	}

	function get_perkiraan(){
		$d_data = DB::table('ms_perkiraan');
		if($d_data->count() > 0){
			$arr['data'] = $d_data->get();
			$arr['status'] = array('1');
		}else{
			$arr['status'] = array('0');
		}

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

	function simpan(Request $request){

		$id = $request->get('id');
		$data['cara'] = $request->get('carabayar');
		$data['kode_perkiraan'] = $request->get('kode');
		$data['tipe'] = $request->get('tipe');
		$data['jenis_bayar'] = $request->get('jenis_bayar');

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

	function hapus(Request $request){
		$data['id_carabayar'] = $request->get('id');
		DB::table('ms_carabayar')->where($data)->delete();

	}
}
