<?php

namespace App\Http\Controllers\master;

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

class MotifController 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.MotifList');
    }

    function get_data(){
        DB::statement(DB::raw('set @rownum=0'));
        $arr = DB::table('ms_motif as m')->select('m.*', DB::raw('@rownum  := @rownum  + 1 AS rownum'));
        $data = array();
        foreach($arr->get() as $a){
            $data[] = array('id_motif' => $a->id_motif,
                            'kode_motif' => $a->kode_motif,
                            'nama_motif' => $a->nama_motif,
                            'rownum' => $a->rownum,
                            'aksi' => '<a class="btn bg-warning-400" type="button" href="'.url('motif/edit').'/'.$a->id_motif.'"><i class="icon-pencil"></i> </a> <button class="btn bg-danger-800" type="button" onclick="hapus('.$a->id_motif.')"><i class="icon-eraser"></i></button>');
        }
        $val = new Collection($data);
        return Datatables::of($val)
                ->make(true);
    }

    public function tambah(){
        $data['data'] = $this->data(array());
    	return view('master.MotifAdd')->with('data',$data);
    }

     public function edit($id){
        $d_data = DB::table('ms_motif')->where('id_motif', '=', $id)->first();
        $data['data'] = $this->data($d_data);
        return view('master.MotifAdd')->with('data',$data);
    }

     function get_edit(Request $request){
        $id = request()->get('id');
        $d_data = DB::table('ms_motif_barang')->where('id_motif', '=', $id);
        if($d_data->count() > 0){
            foreach($d_data->get() as $d){
                $arr[] = array('id_motif_barang' => $d->id_motif_barang,
                                'id_motif' => $d->id_motif,
                                'motif_keterangan' => $d->motif_keterangan);
            }
        }else{
            $arr = array();
        }

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

    
    public function simpan(Request $request){
        $id_motif = $request->get('id_motif');
        $data['nama_motif'] = $request->get('nama_motif');
        $data['kode_motif'] = $request->get('inisial_motif');
        
        $tabel_id = $request->get('tabel_id');
        $tabel_nama = $request->get('tabel_nama');


        if($id_motif == ''){
            $id = DB::table('ms_motif')->insertGetId($data);
            
            for($i = 0; $i < count($tabel_id); $i++){
                $barang['id_motif'] = $id;
                $barang['motif_keterangan'] = $tabel_nama[$i];

                DB::table('ms_motif_barang')->insert($barang);
            }
        }else{
            DB::table('ms_motif')->where(array('id_motif' => $id_motif))->update($data);
            $id_del = array();
            $id_store = array();
            $d_motif = DB::table('ms_motif_barang')->where('id_motif', '=', $id_motif);
            foreach($d_motif->get() as $d){
                $id_store[] = $d->id_motif_barang;
            }

            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_motif'] = $id_motif;
                $barang['motif_keterangan'] = $tabel_nama[$i];

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

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

       return redirect('motif');

    }

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

        return $store;
    }

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

        DB::table('ms_motif')->where(array('id_motif' => $id))->delete();
        DB::table('ms_motif_barang')->where(array('id_motif' => $id))->delete();
    }

    public function cetak($id){
        require('fpdf181/fpdf.php');  
        $data['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('it.*','mg.nama_gudang as nama_gudang_awal','mg2.nama_gudang as nama_gudang_tujuan')->where('id_transfergudang', '=', $id)->first();
        $data['detail'] =DB::table('inv_transfergudang_barang')->where('kode_transfergudang', '=', $id)->get();
        $html = view('inventori.TransfergudangCetak')->with('data',$data);
        return response($html)->header('Content-Type', 'application/pdf');
        
    }

    
}
