<?php

namespace App\Http\Controllers\inventori;

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;

class PersetujuangudangController extends Controller
{
    // --------------- Transfer Keluar
    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); 
        });

        $this->session = array('id_gudang'=>Session::get('id_gudang'),'id_proses'=>Session::get('id_proses'),'id_lokasi'=>Session::get('id_lokasi'), 'kode_group' => Session::get('kode_group'), 'kode_user' => Session::get('kode_user'));
    }
    
    public function index(){
    	return view('inventori.PersetujuangudangList');
    }

    function get_data(){
        $id_gudang = $this->session['id_gudang'];

        $arr = DB::table('inv_transfergudang as a')->join('ms_gudang as b','a.gudang_awal','b.id_gudang')->join('ms_gudang as c','a.gudang_tujuan','c.id_gudang')->select('a.*','b.nama_gudang as awal','c.nama_gudang as tujuan', DB::raw('@rownum  := @rownum  + 1 AS rownum'))->orderBy('a.tanggal', 'desc');
        if($id_gudang != 0){
            $arr->whereIn('a.gudang_awal', explode(",", $id_gudang));
        }
        $data = array();
        foreach($arr->get() as $a){
            $data[] = array('id_transfergudang' => $a->id_transfergudang,
                            'awal' => $a->awal,
                            'tujuan' => $a->tujuan,
                            'tanggal' => tgl_full($a->tanggal, ''),
                            'rownum' => '1',
                            'status_id' => $a->status,
                            'status' => $this->proses($a->status),
                            'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('persetujuangudang/verifikasigudang/').'/'.$a->id_transfergudang.'"><i class="icon-checkbox-checked2"></i></a>',
                            'checkbox' => status_checkbox($a->status, $a->id_transfergudang));
        }
        $val = new Collection($data);
        return Datatables::of($val)
                ->make(true);
    }

    public function verifikasigudang($id){
    	$d_data = DB::table('inv_transfergudang as it')->join('ms_gudang as mg','it.gudang_awal','mg.id_gudang')->join('ms_gudang as mg2','it.gudang_tujuan','mg2.id_gudang')->select(DB::raw('it.*, mg.nama_gudang as nama_gudangawal, mg2.nama_gudang as nama_gudangtujuan'))->where('id_transfergudang', '=', $id)->first();
        $data['gudang'] = DB::table('ms_gudang')->orderBy('id_gudang','asc')->get();
        $data['data'] = $this->data($d_data);        

    	return view('inventori.PersetujuangudangAdd')->with('data',$data);
    }

	function data($data = array()){
		if(count($data) > 0){
            $store['id_gudang'] = $data->id_transfergudang;
            $store['tanggal'] = date('d-m-Y', strtotime($data->tanggal));
            $store['gudang_awal'] = $data->gudang_awal;
            $store['nama_gudangawal'] = $data->nama_gudangawal;
            $store['gudang_tujuan'] = $data->gudang_tujuan;
            $store['nama_gudangtujuan'] = $data->nama_gudangtujuan;
            $store['status'] = $data->status;
            $store['is_cv'] = $data->is_cv;
            $store['nomor_minta'] = $data->nomor_minta;
            $store['tanggal_minta'] = tgl_full($data->tanggal_minta, '');
            $store['no_faktur'] = $data->no_faktur;
            $store['faktur'] = $data->faktur;
        }else{
            $store['id_gudang'] = '';
            $store['tanggal'] = '';
            $store['gudang_awal'] = '';
            $store['nama_gudangawal'] = '';
            $store['gudang_tujuan'] = '';
            $store['nama_gudangtujuan'] = '';
            $store['status'] = '';
             $store['is_cv'] = '';
            $store['nomor_minta'] = '';
            $store['tanggal_minta'] = '';
            $store['no_faktur'] = '';
            $store['faktur'] = '';
        }

        return $store;
	}

	function get_edit(Request $request){
		$id = request()->get('id');
        $d_data = DB::table('view_edit_transferbarang')->where('kode_transfergudang', '=', $id);
        if($d_data->count() > 0){
            $arr = $d_data->get();
        }else{
            $arr = array();
        }

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

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

        $tgl_verif = '';
        if($data['status'] == '1'){
            $data['tanggal_keluar'] = tgl_full(date("Y-m-d"), '5');
        }

		DB::table('inv_transfergudang')->where(array('id_transfergudang' => $id_gudang))->update($data);
		return redirect('persetujuangudang');
	}

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

        $data = array();

        for($i = 0; $i < count($id); $i++){
            $data[$jenis[$i]][] = array($id[$i]);
        }


        if(array_key_exists('1', $data)){
            $update['status'] = '1';
            $update['tanggal_keluar'] = tgl_full(date("Y-m-d"), '5');

            DB::table('inv_transfergudang')->whereIn('id_transfergudang', $data[1])->update($update);   
        }

        if(array_key_exists('2', $data)){
            $update['status'] = '2';
            $update['tanggal_masuk'] = tgl_full(date("Y-m-d"), '5');

            DB::table('inv_transfergudang')->whereIn('id_transfergudang', $data[2])->update($update);   
        }
        

        return response()->json(array('status' => '1'));
	}

    function proses($id){
        switch ($id) {
            case '0':
                $d = '<label class="label bg-warning-400">Belum Verifikasi</label>';
                break;
            case '1':
                $d = '<label class="label bg-primary-400">Terverifikasi</label>';
                break;
            case '2':
                $d = '<label class="label bg-success-400">Selesai</label>';
                break;
            default:
                $d = '<label class="label bg-warning-400">Belum Verifikasi</label>';
                break;
        }

        return $d;
    }


    // ----------- Transfer Masuk


    public function list_masuk(){
        return view('inventori.PersetujuangudangmasukList');
    }

    function get_datamasuk(){
        DB::statement(DB::raw('set @rownum=0'));
        $id_gudang = $this->session['id_gudang'];

        $arr = DB::table('inv_transfergudang as a')->join('ms_gudang as b','a.gudang_awal','b.id_gudang')->join('ms_gudang as c','a.gudang_tujuan','c.id_gudang')->select('a.*','b.nama_gudang as awal','c.nama_gudang as tujuan', DB::raw('@rownum  := @rownum  + 1 AS rownum'))->whereIn('a.status', ['1', '2'])->orderBy('a.tanggal', 'desc');
        if($id_gudang != 0){
            $arr->whereIn('a.gudang_tujuan', explode(",", $id_gudang));
        }
        $data = array();
        foreach($arr->get() as $a){
            $data[] = array('id_transfergudang' => $a->id_transfergudang,
                            'awal' => $a->awal,
                            'tujuan' => $a->tujuan,
                            'tanggal' => tgl_full($a->tanggal, ''),
                            'rownum' => '2',
                            'status_id' => $a->status,
                            'status' => $this->proses_masuk($a->status),
                            'aksi' => '<a class="btn bg-info-400" type="button" href="'.url('persetujuangudang/verifikasigudang_masuk/').'/'.$a->id_transfergudang.'"><i class="icon-checkbox-checked2"></i></a>',
                            'checkbox' => status_checkbox($a->status, $a->id_transfergudang));
        }
        $val = new Collection($data);
        return Datatables::of($val)
                ->make(true);
    }

    public function verifikasigudang_masuk($id){
        $d_data = DB::table('inv_transfergudang as it')->join('ms_gudang as mg','it.gudang_awal','mg.id_gudang')->join('ms_gudang as mg2','it.gudang_tujuan','mg2.id_gudang')->select(DB::raw('it.*, mg.nama_gudang as nama_gudangawal, mg2.nama_gudang as nama_gudangtujuan'))->where('id_transfergudang', '=', $id)->first();
        $data['gudang'] = DB::table('ms_gudang')->orderBy('id_gudang','asc')->get();
        $data['data'] = $this->data($d_data);        

        return view('inventori.PersetujuangudangmasukAdd')->with('data',$data);
    }

    function simpan_masuk(Request $request){
        $id_gudang = $request->get('id_gudang');
        $data['status'] = $request->get('status');

        $tgl_verif = '';
        if($data['status'] == '2'){
            $data['tanggal_masuk'] = tgl_full(date("Y-m-d"), '5');
        }

        DB::table('inv_transfergudang')->where(array('id_transfergudang' => $id_gudang))->update($data);
        return redirect('persetujuangudang_masuk');
    }



    function proses_masuk($id){
        switch ($id) {
            case '1':
                $d = '<label class="label bg-warning-400">Belum Verifikasi</label>';
                break;
            case '2':
                $d = '<label class="label bg-primary-400">Terverifikasi</label>';
                break;
            default:
                $d = '<label class="label bg-warning-400">Belum Verifikasi</label>';
                break;
        }

        return $d;
    }

}
