<?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 UpahtenunBaruController 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.UpahtenunBaruList");
    }

    public function get_upahtenun()
    {
    	$data = DB::table("ms_upahtenun_baru AS ut")
    			->join("ms_motif AS m", "m.id_motif", "=", "ut.id_motif")
    			->join("ms_satuan AS s", "s.id_satuan", "=", "ut.id_satuan")
    			->selectRaw("ut.*, m.nama_motif, s.nama_satuan, s.satuan")
                ->where('status', '1')
    			->get();
    	return Datatables::of($data)
    		->addColumn("nominal_total", function($data){
    			return $data->nominal_rekanan+$data->nominal_rekanan_kelompok;
    		})
    		->addColumn("aksi", function($data){
    			$button = "<div class='btn-group'>";
    				$button .= "<a href='".url('upahtenun/edit/'.$data->id)."' class='btn btn-sm btn-warning'><i class='fa fa-pencil'></i></a>";
    				$button .= "<button type='button' class='btn btn-sm btn-danger' onclick='btnHapusUpahtenun($(this))'><i class='fa fa-trash'></i></button>";
    			$button .= "</div>";
    			return $button;
    		})
    		->make(true);
    }

    public function tambah()
    {
    	$lokasi = DB::table("ms_lokasi_produksi")->get();
    	$data['lokasi'] = array_reduce($lokasi->toArray(), function($carry, $item){
    		$carry[ $item->id_lokasi ] = $item->nama_lokasi;
    		return $carry;
    	});
    	$motif = DB::table("ms_motif")->get();
    	$data['motif'] = array_reduce($motif->toArray(), function($carry, $item){
    		$carry[ $item->id_motif ] = $item->nama_motif;
    		return $carry;
    	});
    	$satuan = DB::table("ms_satuan")->get();
    	$data['satuan'] = array_reduce($satuan->toArray(), function($carry, $item){
    		$carry[ $item->id_satuan ] = $item->nama_satuan;
    		return $carry;
    	});

        $data['status'] = array('0' => "Tidak Aktif", '1' => "Aktif");

    	$data['url_simpan'] = url("upahtenun/simpan");
    	$data['data'] = $this->data([]);

    	return view("master.UpahtenunBaruForm", $data);
    }

    public function edit($id)
    {
    	$lokasi = DB::table("ms_lokasi_produksi")->get();
    	$data['lokasi'] = array_reduce($lokasi->toArray(), function($carry, $item){
    		$carry[ $item->id_lokasi ] = $item->nama_lokasi;
    		return $carry;
    	});
    	$motif = DB::table("ms_motif")->get();
    	$data['motif'] = array_reduce($motif->toArray(), function($carry, $item){
    		$carry[ $item->id_motif ] = $item->nama_motif;
    		return $carry;
    	});
    	$satuan = DB::table("ms_satuan")->get();
    	$data['satuan'] = array_reduce($satuan->toArray(), function($carry, $item){
    		$carry[ $item->id_satuan ] = $item->nama_satuan;
    		return $carry;
    	});

        $data['status'] = array('0' => "Tidak Aktif", '1' => "Aktif");

    	$data['url_simpan'] = url("upahtenun/simpan/$id");
    	$upahtenun = DB::table("ms_upahtenun_baru")->find($id);
    	$data['data'] = $this->data($upahtenun);

    	return view("master.UpahtenunBaruForm", $data);
    }

    public function simpan(Request $request, $id='')
    {
    	$input = $request->all();

		$data = [
				'tanggal_berlaku'=>date_db($input['tanggal_berlaku'], false, 2),
				'id_lokasi'=>$input['id_lokasi'],
				'id_motif'=>$input['id_motif'],
				'id_satuan'=>$input['id_satuan'],
				'nominal_rekanan'=>$input['nominal_rekanan'],
				'nominal_rekanan_kelompok'=>$input['nominal_rekanan_kelompok'],
				'potongan_d'=>$input['potongan_d'],
				'potongan_n'=>$input['potongan_n'],
				'potongan_bs'=>$input['potongan_bs'],
                'nama_upah' => $input['nama_upah'],
                'status' => $input['status']
			];
		DB::beginTransaction();
		try {
	    	if($id==""){
	    		DB::table("ms_upahtenun_baru")->insert($data);
	    	}else{
	    		DB::table("ms_upahtenun_baru")->where("id", $id)->update($data);
	    	}

			DB::commit();
		} catch (Exception $e) {
			DB::rollBack();
		}

		return redirect(url('upahtenun'));
    }

    public function hapus(Request $request)
    {
    	$input = $request->all();
    	// dd($request);
    	DB::beginTransaction();
    	try {
    		if(DB::table("ms_upahtenun_baru")->where("id", $input['id'])->delete()){
    			$result = ["status"=>1, "pesan"=>"Data Berhasil Dihapus", "notif"=>"success"];
    		}else{
    			$result = ["status"=>0, "pesan"=>"Data Gagal Dihapus", "notif"=>"error"];

    		}

    		DB::commit();
    	} catch (Exception $e) {
    		$result = ["status"=>0, "pesan"=>"Data Gagal Dihapus", "notif"=>"error"];
    		DB::rollBack();
    	}

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

    private function data($value=[])
    {
    	$result = [];
    	if(sizeof($value)>0){
    		$result['id'] = $value->id;
    		$result['id_lokasi'] = $value->id_lokasi;
    		$result['tanggal_berlaku'] = date_db($value->tanggal_berlaku, true, 2);
    		$result['id_motif'] = $value->id_motif;
    		$result['id_satuan'] = $value->id_satuan;
    		$result['nominal_rekanan'] = $value->nominal_rekanan;
    		$result['nominal_rekanan_kelompok'] = $value->nominal_rekanan_kelompok;
    		$result['potongan_d'] = $value->potongan_d;
    		$result['potongan_n'] = $value->potongan_n;
    		$result['potongan_bs'] = $value->potongan_bs;
            $result['nama_upah'] = $value->nama_upah;
            $result['status'] = $value->status;
    	}else{
    		$result['id'] = "";
    		$result['id_lokasi'] = "1";
    		$result['tanggal_berlaku'] = date("d-m-Y");
    		$result['id_motif'] = "";
    		$result['id_satuan'] = "";
    		$result['nominal_rekanan'] = 0;
    		$result['nominal_rekanan_kelompok'] = 0;
    		$result['potongan_d'] = 0;
    		$result['potongan_n'] = 0;
    		$result['potongan_bs'] = 0;
            $result['nama_upah'] = "";
            $result['status'] = 0;
    	}
    	return $result;
    }
}
