<?php

namespace App\Http\Controllers\master;

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

class PelangganController 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(){
		$data['kabupaten'] = DB::table('ms_kabupaten');
		$akun = Perkiraan::where([['used', '=', 1], ['kd_rek_1', '=', 1]])->get();
		$data['akun'] = array_reduce($akun->toArray(), function($carry, $item){
			$carry[$item['id']] = format_akun_perkiraan($item);
			return $carry;
		});

		return view('master.PelangganList', $data);
	}

	function tampil(){
		DB::statement(DB::raw('set @rownum=0'));
		$d_data = DB::table('ms_pelanggan')->select(DB::raw('*, @rownum  := @rownum  + 1 AS rownum'));



		return Datatables::of($d_data)
				->addColumn('aksi', function($d_data){
					$btnAction = '<button class="btn bg-warning-400" onclick="edit_pelanggan('.$d_data->id_pelanggan.')"><i class="icon-pencil"></i> </button> <button class="btn bg-danger-800" type="button" onclick="hapus_pelanggan('.$d_data->id_pelanggan.')"><i class="icon-eraser"></i></button>'.
						'<input type="hidden" id="table_id'.$d_data->id_pelanggan.'" value="'.$d_data->id_pelanggan.'">'.
						
						'<input type="hidden" id="table_nama'.$d_data->id_pelanggan.'" value="'.$d_data->nama.'">'.
						'<input type="hidden" id="table_pic'.$d_data->id_pelanggan.'" value="'.$d_data->pic.'">'.
						'<input type="hidden" id="table_npwp'.$d_data->id_pelanggan.'" value="'.$d_data->npwp.'">'.
						'<input type="hidden" id="table_email'.$d_data->id_pelanggan.'" value="'.$d_data->email.'">'.
						'<input type="hidden" id="table_telepon'.$d_data->id_pelanggan.'" value="'.$d_data->telepon.'">'.
						'<input type="hidden" id="table_alamat'.$d_data->id_pelanggan.'" value="'.$d_data->alamat.'">'.
						'<input type="hidden" id="table_kota'.$d_data->id_pelanggan.'" value="'.$d_data->id_kabupaten.'">'.
						'<input type="hidden" id="table_no_rek'.$d_data->id_pelanggan.'" value="'.$d_data->no_rek.'">'.
						'<input type="hidden" id="table_status'.$d_data->id_pelanggan.'" value="'.$d_data->status_pelanggan.'">'.
						'<input type="hidden" id="table_id_akun_piutang'.$d_data->id_pelanggan.'" value="'.$d_data->id_akun_piutang.'">'
						;
					return $btnAction;
				})
				->make(true);
	}

	function simpan(Request $request){

		$id = $request->get('id');
		$data['nama'] = $request->get('nama');
		$data['pic'] = $request->get('pic');
		$data['npwp'] = $request->get('npwp');
		$data['email'] = $request->get('email');
		$data['telepon'] = $request->get('telp');
		$data['no_rek'] = $request->get('norek');
		$data['alamat'] = $request->get('alamat');
		$data['status_pelanggan'] = $request->get('status');
		$data['id_kabupaten'] = $request->get('kota');
		$data['id_akun_piutang'] = $request->get('id_akun_piutang');

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

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

	}
}