<?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 Illuminate\Support\Collection;
use Illuminate\Support\Facades\Session;
use App\Perkiraan;

class RekananController 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 = DB::table('ms_kabupaten');
		return view('master.RekananList')->with('kabupaten',$data);
	}

	function tampil(){
		DB::statement(DB::raw('set @rownum=0'));
		$d_data = DB::table('ms_rekanan_kelompok as mrk')->leftjoin('ms_rekanan as mr', 'mrk.id_kelompok', 'mr.id_kelompok')->select(DB::raw('mrk.*, count(id_rekanan) jum_rekanan, @rownum  := @rownum  + 1 AS rownum'))->groupby('mrk.id_kelompok');

		if($d_data->count() > 0){
			foreach($d_data->get() as $d){
				$arr[] = array('id_kelompok' => $d->id_kelompok,
								'nama' => $d->nama,
								'npwp' => $d->npwp,
								'email' => $d->email,
								'telepon' => $d->telepon,
								'no_rek' => $d->no_rek,
								'kota' => $d->kota,
								'alamat' => $d->alamat,
								'jumlah_petenun' => $d->jum_rekanan,
								'rownum' => $d->rownum,
								'aksi' => '<a class="btn  btn-xs bg-warning-400" type="button" href="'.url('rekanan/edit/'.$d->id_kelompok).'"><i class="icon-pencil"></i> </a> <button class="btn btn-xs bg-danger-800" type="button" onclick="hapus('.$d->id_kelompok.')"><i class="icon-eraser"></i></button>');
			}
		}else{
			$arr = array();	
		}

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

	function tambah(){
		$data['kabupaten'] = DB::table('ms_kabupaten')->orderby('nama', 'asc')->get();
		$data['akun_piutang'] = Perkiraan::where([ ["used", "=", 1], ["parent", "=", 20] ])->get();
		$data['akun_hutang'] = Perkiraan::where([ ["used", "=", 1], ["parent", "=", 205] ])->orWhere([ ["used", "=", 1], ["parent", "=", 60] ])->get();
		// dd($data);
		$data['tabel'] = $this->data(array());
		return view('master.RekananAdd')->with('data', $data);
	}

	function edit($id){
		$d_table = DB::table('ms_rekanan_kelompok')->where('id_kelompok', $id);

		if($d_table->count() > 0){
			$data['kabupaten'] = DB::table('ms_kabupaten')->orderby('nama', 'asc')->get();
			$data['akun_piutang'] = Perkiraan::where([ ["used", "=", 1], ["parent", "=", 20] ])->get();
			$data['akun_hutang'] = Perkiraan::where([ ["used", "=", 1], ["parent", "=", 57] ])->orWhere([ ["used", "=", 1], ["parent", "=", 60] ])->get();
			$data['tabel'] = $this->data($d_table->first());
			return view('master.RekananAdd')->with('data', $data);
		}else{
			return redirect('rekanan');
		}
	}

	function simpan(Request $request){

		$id = $request->get('id_kelompok');
		$data['nama'] = $request->get('nama');
		$data['npwp'] = $request->get('npwp');
		$data['email'] = $request->get('email');
		$data['telepon'] = $request->get('telepon');
		$data['no_rek'] = $request->get('norek');
		$data['alamat'] = $request->get('alamat');
		$data['kota'] = $request->get('kota');
		$data['id_akun_hutang'] = $request->get('id_akun_hutang');
		$data['id_akun_piutang'] = $request->get('id_akun_piutang');

		$tabel_id = $request->get('tabel_idtable');
		$tabel_urut = $request->get('tabel_urut');
		$tabel_nama = $request->get('tabel_nama');
		$tabel_npwp = $request->get('tabel_npwp');
		$tabel_email = $request->get('tabel_email');
		$tabel_telepon = $request->get('tabel_telepon');
		$tabel_norek = $request->get('tabel_norek');
		$tabel_alamat = $request->get('tabel_alamat');
		$tabel_kota = $request->get('tabel_kota');

		print_r($data);

		if($id == ''){
			$id_kelompok = DB::table('ms_rekanan_kelompok')->insertGetId($data);

			for($i = 0; $i < count($tabel_id); $i++){
				$rekanan['id_kelompok'] = $id_kelompok;
				$rekanan['no_urut'] = $tabel_urut[$i];
				$rekanan['nama'] = $tabel_nama[$i];
				$rekanan['npwp'] = $tabel_npwp[$i];
				$rekanan['email'] = $tabel_email[$i];
				$rekanan['telepon'] = $tabel_telepon[$i];
				$rekanan['no_rek'] = $tabel_norek[$i];
				$rekanan['kota'] = $tabel_kota[$i];
				$rekanan['alamat'] = $tabel_alamat[$i];

				DB::table('ms_rekanan')->insert($rekanan);
			}
		}else{
			DB::table('ms_rekanan_kelompok')->where('id_kelompok', $id)->update($data);

			$id_del = array();
            $id_store = array();
            $d_data = DB::table('ms_rekanan')->where('id_kelompok', $id);
            foreach($d_data->get() as $d){
                $id_store[] = $d->id_rekanan;
            }

            foreach($id_store as $d){
                if(!in_array($d, $tabel_id)){
                    $id_del[] = $d;
                }
            }

            for($i = 0; $i < count($tabel_id); $i++){
				$rekanan['id_kelompok'] = $id;
				$rekanan['no_urut'] = $tabel_urut[$i];
				$rekanan['nama'] = $tabel_nama[$i];
				$rekanan['npwp'] = $tabel_npwp[$i];
				$rekanan['email'] = $tabel_email[$i];
				$rekanan['telepon'] = $tabel_telepon[$i];
				$rekanan['no_rek'] = $tabel_norek[$i];
				$rekanan['kota'] = $tabel_kota[$i];
				$rekanan['alamat'] = $tabel_alamat[$i];

				if($tabel_id[$i] == ''){
					DB::table('ms_rekanan')->insert($rekanan);
				}else{
					DB::table('ms_rekanan')->where('id_rekanan', $tabel_id[$i])->update($rekanan);
				}
			}

            if(count($id_del) > 0){
                DB::table('ms_rekanan')->whereIn('id_rekanan', $id_del)->delete();
            }
		}

		return redirect('rekanan');
	}

	function data($data = array()){
        if(count($data) > 0){
            $store['id_kelompok'] = $data->id_kelompok;
            $store['nama'] = $data->nama;
            $store['npwp'] = $data->npwp;
            $store['email'] = $data->email;
            $store['telepon'] = $data->telepon;
            $store['no_rek'] = $data->no_rek;
            $store['kota'] = $data->kota;
            $store['alamat'] = $data->alamat;
            $store['id_akun_hutang'] = $data->id_akun_hutang;
            $store['id_akun_piutang'] = $data->id_akun_piutang;

        }else{
            $store['id_kelompok'] = '';
            $store['nama'] = '';
            $store['npwp'] = '';
            $store['email'] = '';
            $store['telepon'] = '';
            $store['no_rek'] = '';
            $store['kota'] = '';
            $store['alamat'] = '';
            $store['id_akun_hutang'] = 0;
            $store['id_akun_piutang'] = 0;
        }

        return $store;
    }

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

	}


	function get_rekanan(Request $request){
		$id_kelompok = $request->get('id_kelompok');

		$d_data = DB::table('ms_rekanan')->where('id_kelompok', $id_kelompok)->orderby('no_urut', 'desc');

		if($d_data->count() > 0){
			foreach($d_data->get() as $d){
				$arr[] = array('id_rekanan' => $d->id_rekanan,
								'no_urut' => $d->no_urut,
								'id_kelompok' => $d->id_kelompok,
								'nama' => $d->nama,
								'npwp' => $d->npwp,
								'email' => $d->email,
								'telepon' => $d->telepon,
								'no_rek' => $d->no_rek,
								'kota' => $d->kota,
								'alamat' => $d->alamat);
			}
		}else{
			$arr = array();
		}

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