<?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 PerubahansatuanController 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.PerubahansatuanList');
    }

    function get_data(){
    	DB::statement(DB::raw('set @rownum=0'));
        $d_data = DB::table('ms_perubahansatuan as mps')->join('ms_satuan as ms','mps.id_satuanawal','ms.id_satuan')->join('ms_satuan as ms2','mps.id_satuanperubahan','ms2.id_satuan')->join('ms_proses as mp','mps.id_proses','mp.id_proses')->leftjoin('ms_motif as mm','mps.id_motif','mm.id_motif')->leftjoin('ms_motif_barang as mmb','mps.id_motif_barang','mmb.id_motif_barang')->join('ms_lokasi_produksi as ml','mps.id_lokasi','ml.id_lokasi')->select(DB::raw('mps.*,ms.nama_satuan as satuan_awal,ms2.nama_satuan as satuan_perubahan,mp.nama_proses,mm.nama_motif as motif, mmb.motif_keterangan as motif_barang, ml.nama_lokasi as nama_lokasi,@rownum  := @rownum  + 1 AS rownum'))->orderBy('mps.id_satuanawal', 'DESC');
        if($d_data->count() > 0){
        foreach($d_data->get() as $d){
            $id_motif = '';
            $nama_motif = '';
            $id_motif_barang = '';
            $nama_motif_barang = '';

            if($d->id_motif != 0){
                $id_motif = $d->id_motif;
                $nama_motif = $d->motif;
            }

            if($d->id_motif_barang != 0){
                $id_motif_barang = $d->id_motif_barang;
                $nama_motif_barang = $d->motif_barang;
            }

            $arr[] = array('id_perubahansatuan' => $d->id_perubahansatuan,
                            'id_proses' => $d->id_proses,
                            'id_satuanawal' => $d->id_satuanawal,
                            'id_satuanperubahan' => $d->id_satuanperubahan,
                            'id_motif' => $d->id_motif,
                            'id_motif_barang' => $d->id_motif_barang,
                            'id_lokasi' => $d->id_lokasi,
                            'nama_proses' => $d->nama_proses,
                            'jumlah_satuanawal' => $d->jumlah_satuanawal,
                            'nama_satuanawal' => $d->satuan_awal,
                            'jumlah_satuanperubahan' => $d->jumlah_satuanperubahan,
                            'nama_satuanperubahan' => $d->satuan_perubahan,
                            'nama_motif' => $d->motif,
                            'nama_corak' => $d->motif_barang,
                            'nama_lokasi' => $d->nama_lokasi,
                            'aksi' => '<button class="btn bg-warning-400" onclick="edit('.$d->id_perubahansatuan.')"><i class="icon-pencil"></i> </button> <button class="btn bg-danger-800" type="button" onclick="hapus('.$d->id_perubahansatuan.')"><i class="icon-eraser"></i></button>'.
                        '<input type="hidden" id="table_idperubahansatuan'.$d->id_perubahansatuan.'" value="'.$d->id_perubahansatuan.'">'.
                       	'<input type="hidden" id="table_proses'.$d->id_perubahansatuan.'" value="'.$d->id_proses.'">'.
                       	'<input type="hidden" id="table_jumlahawal'.$d->id_perubahansatuan.'" value="'.$d->jumlah_satuanawal.'">'.
                       	'<input type="hidden" id="table_satuanawal'.$d->id_perubahansatuan.'" value="'.$d->id_satuanawal.'">'.
                       	'<input type="hidden" id="table_jumlahperubahan'.$d->id_perubahansatuan.'" value="'.$d->jumlah_satuanperubahan.'">'.
                       	'<input type="hidden" id="table_satuanperubahan'.$d->id_perubahansatuan.'" value="'.$d->id_satuanperubahan.'">'.
                       	'<input type="hidden" id="table_motif'.$d->id_perubahansatuan.'" value="'.$id_motif.'">'.
                       	'<input type="hidden" id="table_motifbarang'.$d->id_perubahansatuan.'" value="'.$id_motif_barang.'">'.
                       	'<input type="hidden" id="table_lokasi'.$d->id_perubahansatuan.'" value="'.$d->id_lokasi.'">');
        		}
        	}else{
        		$arr = array();
        	}




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

    function get_proses(){
        $d_data = DB::table('ms_proses')->where('parent', '!=', '0');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function get_motif(){
        $d_data = DB::table('ms_motif')->orderBy('nama_motif', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function get_corak(){
        $d_data = DB::table('ms_motif_barang')->orderBy('motif_keterangan', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function get_satuan(){
        $d_data = DB::table('ms_satuan')->orderBy('nama_satuan', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function get_lokasi(){
        $d_data = DB::table('ms_lokasi_produksi')->orderBy('nama_lokasi', 'asc');
        if($d_data->count() > 0){
            $arr['data'] = $d_data->get();
            $arr['status'] = array('1');
        }else{
            $arr['status'] = array('0');
        }

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

    function simpan(Request $request){
    	$id_perubahansatuan = $request->get('id_perubahansatuan');
    	$data['id_proses'] = $request->get('id_proses');
    	$data['jumlah_satuanawal'] = $request->get('jumlahawal');
    	$data['id_satuanawal'] = $request->get('id_satuanawal');
    	$data['jumlah_satuanperubahan'] = $request->get('jumlahperubahan');
    	$data['id_satuanperubahan'] = $request->get('id_satuanperubahan');
    	$data['id_motif'] = $request->get('id_motif');
    	$data['id_motif_barang'] = $request->get('id_motif_barang');
    	$data['id_lokasi'] = $request->get('id_lokasi');
        $data['jumlah_hasil'] = $request->get('jumlahhasil');

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

    }

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

}
