<?php

namespace App\Http\Controllers\master;

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

class MapingUpahTenunController extends Controller
{
    //
    public function index(){
    	$data['makelar'] = DB::table('ms_rekanan_kelompok')->get();
    	$data['pick']	 = DB::table('ms_pick')->get();
    	$data['upah']	 = DB::table('ms_upahtenun_baru AS mub')->join('ms_motif AS mm','mub.id_motif','mm.id_motif')
                            ->select(DB::Raw('mub.*,mm.nama_motif'))->get();
    	return view('master.MapingUpahTenunList')->with('data',$data);
    }

    function get_data(){
    	DB::statement(DB::raw('set @rownum=0'));
		$d_data = DB::table('ms_mapingtenun as mm')
            ->join('ms_rekanan_kelompok as mk','mm.id_kelompok','mk.id_kelompok')
            ->join('ms_upahtenun_baru as mub','mm.id_upahtenun','mub.id')
            ->join('ms_pick as mp','mm.id_pick','mp.id_pick')
            ->select([DB::raw('mm.*,mk.nama as nama_makelar, mub.nama_upah, mp.nama_pick as nama_pick')]);

		return Datatables::of($d_data)
				->addColumn('aksi', function($d_data){
                    /*<button class="btn btn-primary" onclick="detail('.$d_data->kode_menu.','.$d_data->parent_menu.')"><i class="icon-notification2"></i></button>*/
					$btnAction = '<div class="btn-group">
                        <button class="btn bg-warning-400" onclick="edit('.$d_data->id_mapingupahtenun.')"><i class="icon-pencil"></i> </button>
                        <button class="btn bg-danger-800" type="button" onclick="hapus('.$d_data->id_mapingupahtenun.')"><i class="icon-eraser"></i></button></div>'.
						'<input type="hidden" id="table_id'.$d_data->id_mapingupahtenun.'" value="'.$d_data->id_mapingupahtenun.'">'.
						'<input type="hidden" id="table_makelar'.$d_data->id_mapingupahtenun.'" value="'.$d_data->id_kelompok.'">'.
						'<input type="hidden" id="table_upah'.$d_data->id_mapingupahtenun.'" value="'.$d_data->id_upahtenun.'">'.
						'<input type="hidden" id="table_pick'.$d_data->id_mapingupahtenun.'" value="'.$d_data->id_pick.'">'.
						'<input type="hidden" id="table_tanggal'.$d_data->id_mapingupahtenun.'" value="'.tgl_full($d_data->tanggal,'').'">'.
						'<input type="hidden" id="table_status'.$d_data->id_mapingupahtenun.'" value="'.$d_data->status.'">';
					return $btnAction;
				})
				->addColumn('status', function($d_data){						
					$status = '<label class="label bg-success-400">Aktif</label>';
					if($d_data->status == 2){
					$status = '<label class="label bg-danger-400">Tidak Aktif</label>';
					}
					return $status;
				})
				->addColumn('tgl_maping', function($d_data){
					$tgl_full = tgl_full($d_data->tanggal,'');
					return $tgl_full;
				})->make(true);
    }

    function get_pick(Request $request){
        $id = $request->get('id_motif');
        $arr = array();

        $d_data = DB::table('ms_pick as mp')->join('ms_motif as mm','mp.id_motif','mm.id_motif')->where('mp.id_motif',$id)->where('mp.status','1')->select(DB::Raw('mp.*,mm.nama_motif'));
        if($d_data->count() > 0){
            foreach($d_data->orderBy('nama_pick')->get() AS $d){
            $arr[] = array('id_pick'    => $d->id_pick,
                            'nama_pick' => $d->nama_pick,
                            'nama_motif'=> $d->nama_motif);
            }
        }else{
            $arr = array();
        }
        return response()->json($arr);

    } 

    function simpan(Request $request){
    	$id_maping = $request->get('id_mapingupahtenun');
    	$data['id_kelompok'] 	= $request->get('popup_makelar');
    	$data['id_upahtenun'] 	= $request->get('popup_upah');
    	$data['id_pick']		= $request->get('popup_pick');
    	$data['tanggal']		= tgl_full($request->get('popup_tanggal'),'2'); 
    	$data['status']			= $request->get('popup_status');

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

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

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

    }

}
