<?php

namespace App\Http\Controllers\master;

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

class MapingController 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(){    	
    	$d_data = DB::table('ms_maping as mm')->join('ms_proses as mp','mp.id_proses','mm.id_proses')->select('mp.*','mm.id_maping')->groupBy('mp.id_proses')->get();
    	
    	return view('master.MapingList')->with('data',$d_data);
    }

    /*function detail(){
    	$PDO = DB::connection('mysql')->getPdo();
    	$billingStmt = $PDO->prepare("SELECT * from (
    		select a.id_barang, c.*,'0' as proses from pro_barangkeluar_barang a join pro_barangkeluar b on a.id_barangkeluar=b.id_barangkeluar LEFT join ms_proses c on b.id_proses=c.id_proses
    		union
    		select a.id_barang, e.*,'1' as proses from pro_barangmasuk_barang a join pro_barangmasuk b on a.id_barangmasuk=b.id_barangmasuk join pro_barangkeluar c on b.id_barangkeluar=c.id_barangkeluar join pro_barangkeluar_barang d on d.id_barangkeluar=c.id_barangkeluar LEFT JOIN ms_proses e on c.id_proses=e.id_proses ) A");
    	$billingStmt->execute();
    	$d_data = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));
    	if(count($d_data) >0){
    		foreach ($d_data as $row) {
    			 
    		}

    	}
    }*/

    public function tambah(){
        $id_store = array();
        $id_barang = array();
        $where = DB::table('ms_maping as mm')->select('mm.id_proses');
        foreach($where->get() as $d){
                $id_store[] = $d->id_proses;
            }
        foreach($id_store as $d){
               $id_del[] = $d;
            }

    	$data['proses'] = DB::table('ms_proses')->whereNotIn('id_proses',$id_del)->get(); 
    	$data['barang'] = DB::table('ms_barang')->get();
    	$data['data'] = $this->data(array());
    	return view('master.MapingAdd')->with('data',$data);
    }

    function get_barang(){
    	
    	/*if($_POST){
            $id_barang = $request->get('id_barang');
            $where = '';
            if($id_barang != ''){
                $where = "where B.id_barang = '$id_barang'";
            }
        }else{
            $id_barang = $request->id_barang;
            $where = '';
            if($id_barang != ''){
                $where = "where B.id_barang = '$id_barang'";
            }
        }

        $PDO = DB::connection('mysql')->getPdo();

        $billingStmt = $PDO->prepare("SELECT B.*, ms.nama_satuan, mkb.nama kategori, mkb2.nama sub_kategori
                                    FROM (
                                        SELECT A.*, sum(masuk - keluar) as stok
                                        FROM (
                                          SELECT * FROM view_penerimaan
                                         UNION
                                         SELECT * FROM view_transfer 
                                         UNION
                                         SELECT * FROM view_keluar
                                         UNION
                                         SELECT * FROM view_masuk
                                         UNION
                                         SELECT * FROM view_penjualan
                                        ) A 
                                        GROUP BY A.id_barang, A.id_gudang
                                    ) B
                                    JOIN ms_barang mb ON B.id_barang = mb.id
                                    JOIN ms_satuan ms ON mb.id_satuan = ms.id_satuan
                                    JOIN ms_kategori_barang mkb ON mb.id_kb = mkb.id_kb
                                    JOIN ms_kategori_barang mkb2 ON mkb.induk_kb = mkb2.id_kb
                                    $where");
        $billingStmt->execute();
        $usersBills = $billingStmt->fetchAll((\PDO::FETCH_ASSOC));

        return response()->json($usersBills);*/
        $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,
                                'satuan' => $d->nama_satuan);
            }
        }else{
            $arr = array();
        }

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

     public function edit($id){
    	$d_data = DB::table('ms_maping')->where('id_maping',$id)->first();
    	$data['proses'] = DB::table('ms_proses')->get(); 
    	$data['barang'] = DB::table('ms_barang')->get();
    	$data['data'] = $this->data($d_data);
    	return view('master.MapingAdd')->with('data',$data);
    }

    function data($data = array()){
    	if(count($data) > 0){
    		$store['id_maping'] = $data->id_maping;
    		$store['id_proses'] = $data->id_proses;
    	}else{
    		$store['id_proses'] = '';
    		$store['id_maping'] = '';
    	}

    	return $store;

    }

    function get_edit_barangkeluar(Request $request){
    	$id = request()->get('id');
    	$d_data = DB::table('ms_maping_barang as mmb')->join('ms_maping as mm','mmb.id_maping','mm.id_maping')->join('ms_barang as mb','mmb.id_barang','mb.id')->select('mmb.*','mb.kode_barang','mb.nama_barang')->where('mmb.id_maping',$id)->where('mmb.status','0');
    	if($d_data->count() > 0){
    		foreach($d_data->get() as $d){
    			$arr[] = ['id' => $d->id,
    					  'id_maping' => $d->id_maping,
    					  'id_barang' => $d->id_barang,
    					  'kode_barang' => $d->kode_barang,
    					  'nama_barang' => $d->nama_barang,
    					  'id_status' => $d->status,
    					  'status' => 'Barang Keluar'];
    		}
    	}else{
    		$arr = array();
    	}
    	return response()->json($arr);
    }

    function get_edit_barangmasuk(Request $request){
    	$id = request()->get('id');
    	$d_data = DB::table('ms_maping_barang as mmb')->join('ms_maping as mm','mmb.id_maping','mm.id_maping')->join('ms_barang as mb','mmb.id_barang','mb.id')->select('mmb.*','mb.kode_barang','mb.nama_barang')->where('mmb.id_maping',$id)->where('mmb.status','1');
    	if($d_data->count() > 0){
    		foreach($d_data->get() as $d){
    			$arr[] = ['id' => $d->id,
    					  'id_maping' => $d->id_maping,
    					  'id_barang' => $d->id_barang,
    					  'kode_barang' => $d->kode_barang,
    					  'nama_barang' => $d->nama_barang,
    					  'id_status' => $d->status,
    					  'status' => 'Barang Masuk'];
    		}
    	}else{
    		$arr = array();
    	}
    	return response()->json($arr);
    }

    function simpan(Request $request){
    	$id_maping = $request->get('id_maping');
    	$data['id_proses'] = $request->get('id_proses');

    	$tabel_id = $request->get('tabel_id');
    	$tabel_idbarang = $request->get('tabel_idbarang');
    	$tabel_status = $request->get('tabel_status');

    	$tabel_id2 = $request->get('tabel_id2');
    	$tabel_idbarang2 = $request->get('tabel_idbarang2');
    	$tabel_status2 = $request->get('tabel_status2');

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

    		for($i=0; $i < count($tabel_id); $i++){
    			$barang['id_maping'] = $id;
    			$barang['id_barang'] = $tabel_idbarang[$i];
    			$barang['status'] = $tabel_status[$i];
    			DB::table('ms_maping_barang')->insert($barang);
    		}

    		for($i=0; $i < count($tabel_id); $i++){
    			$barang2['id_maping'] = $id;
    			$barang2['id_barang'] = $tabel_idbarang2[$i];
    			$barang2['status'] = $tabel_status2[$i];
    			DB::table('ms_maping_barang')->insert($barang2);
    		}
    	}else{
    		DB::table('ms_maping')->where(array('id_maping'=>$id_maping))->update($data);
    		$id_del = array();
            $id_store = array();
            $id_del2 = array();
            $id_store2 = array();
    		$d_barang = DB::table('ms_maping_barang')->where('id_maping',$id_maping)->where('status','0');
    		$d_barang2 = DB::table('ms_maping_barang')->where('id_maping',$id_maping)->where('status','1');
    		
    		foreach($d_barang->get() as $d){
    			$id_store[] = $d->id;
    		}

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

            for($i = 0; $i < count($tabel_id); $i++){
            	$barang['id_maping'] = $id_maping;
            	$barang['id_barang'] = $tabel_idbarang[$i];
            	$barang['status'] = $tabel_status[$i];

            	if($tabel_id[$i] == ''){
                    DB::table('ms_maping_barang')->insert($barang);
                }else if($tabel_id != ''){
                    $where = 
                    DB::table('ms_maping_barang')->where(array('id' => $tabel_id[$i]))->update($barang);
                }
            }

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


            foreach($d_barang2->get() as $d){
    			$id_store2[] = $d->id;
    		}

    		foreach($id_store2 as $d){
                if(count($tabel_id2) > 0){
                	if(!in_array($d, $tabel_id2)){
	                    $id_del2[] = $d;
	                }
                }else{
                	$id_del2[] = $d;
                }
            }


            for($i = 0; $i < count($tabel_id2); $i++){
            	$barang2['id_maping'] = $id_maping;
            	$barang2['id_barang'] = $tabel_idbarang2[$i];
            	$barang2['status'] = $tabel_status2[$i];

            	if($tabel_id2[$i] == ''){
                    DB::table('ms_maping_barang')->insert($barang2);
                }else if($tabel_id2 != ''){
                    $where = 
                    DB::table('ms_maping_barang')->where(array('id' => $tabel_id2[$i]))->update($barang2);
                }
            }

            if(count($id_del2) > 0){
                DB::table('ms_maping_barang')->whereIn('id', $id_del2)->delete();
            }
    	}

    	return redirect('maping');
    }

    function hapus(Request $request){
        $id = $request->get('id');

        DB::table('ms_maping')->where(array('id_maping' => $id))->delete();
        DB::table('ms_maping_barang')->where(array('id_maping' => $id))->delete();
    }
   
}
