<?php

namespace App\Http\Controllers\master;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use CodeGenerator;
use Yajra\Datatables\Datatables;
use App\DepartemenPegawai;
use App\LokasiProduksi;
use App\Perkiraan;
use Illuminate\Support\Facades\Session;

class PegawaiController 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(){
        $data['lokasi'] = LokasiProduksi::all();
    	return view('master.PegawaiList', $data);
    }

    function get_departement(){
        // DB::statement(DB::raw('set @rownum=0'));
    	$d_data = DepartemenPegawai::with(['lokasiProduksi'])->get();

        return Datatables::of($d_data)
                ->addColumn("lokasi", function($d_data){
                    return $d_data->lokasiProduksi->nama_lokasi;
                })
                ->addColumn('aksi', function($d_data){
                    $btnAction = '<button class="btn bg-warning-400" onclick="edit_departement('.$d_data->id_departement.')"><i class="icon-pencil"></i> </button> <button class="btn bg-danger-800" type="button" onclick="hapus_departement('.$d_data->id_departement.')"><i class="icon-eraser"></i></button>'.
                        '<input type="hidden" id="table_id'.$d_data->id_departement.'" value="'.$d_data->id_departement.'">'.
                        '<input type="hidden" id="table_nama'.$d_data->id_departement.'" value="'.$d_data->nama.'">'.
                        '<input type="hidden" id="table_lokasi'.$d_data->id_departement.'" value="'.$d_data->lokasiProduksi->id_lokasi.'">'
                        ;
                    return $btnAction;
                })
                ->make(true);
    }

    function simpan_departement(Request $request){
    	$id_departement = $request->get('id');
        $data['nama'] = $request->get('nama');
    	$data['id_lokasi'] = $request->get('id_lokasi');

    	if($id_departement == ""){
    		DB::table('ms_departement')->insert($data);

    	}else{
    		DB::table('ms_departement')->where('id_departement',$id_departement)->update($data);
    	}

    }

    function hapus_departement(Request $request){
    	$data['id_departement'] = $request->get('id');

    	DB::table('ms_departement')->where($data)->delete();
    }

    public function pegawai(){
        // $akun = Perkiraan::where([['used', '=', 1], ['kd_rek_1', '=', 1], ['parent', '=', 20]])->get();
        // $akun = Perkiraan::get();
        // $data['akunPiutang'] = array_reduce($akun->toArray(), function($carry, $item){
        //     $carry[$item['id']] = format_akun_perkiraan($item);
        //     return $carry;
        // });
        $akun = dataAkun();
        $data['akunPiutang'] = array_reduce($akun->toArray(), function($carry, $item){
            $carry[$item['id']] = format_akun_perkiraan($item);
            return $carry;
        });
		return view('master.PegawaiSub', $data);
    }

    public function get_pegawai(){
        DB::statement(DB::raw('set @rownum=0'));

    	$sub = DB::table('ms_pegawai as mp')->join('ms_departement as md','mp.id_departement','md.id_departement')->leftjoin("ms_lokasi_produksi AS mlp", "md.id_lokasi", "mlp.id_lokasi")->select('mp.*','md.nama as nama_departement', DB::raw('@rownum  := @rownum  + 1 AS rownum'), "md.id_lokasi", "mlp.nama_lokasi")->orderBy('mp.nomor_pegawai', 'asc');

        return Datatables::of($sub)
                ->addColumn('aksi', function($sub){
                    $btnAction = '<button class="btn bg-warning-400" onclick="edit_pegawai('.$sub->id_pegawai.')"><i class="icon-pencil"></i> </button>
                            <button class="btn bg-danger-800" type="button" onclick="hapus_pegawai('.$sub->id_pegawai.')"><i class="icon-eraser"></i></button>'.
                        '<input type="hidden" id="table_id'.$sub->id_pegawai.'" value="'.$sub->id_pegawai.'">'.
                            '<input type="hidden" id="table_departement'.$sub->id_pegawai.'" value="'.$sub->id_departement.'">'.
                            '<input type="hidden" id="table_nomor_pegawai'.$sub->id_pegawai.'" value="'.$sub->nomor_pegawai.'">'.
                            '<input type="hidden" id="table_nama_pegawai'.$sub->id_pegawai.'" value="'.$sub->nama_pegawai.'">'.
                            '<input type="hidden" id="table_tahun_awal'.$sub->id_pegawai.'" value="'.$sub->tahun_awal.'">'.
                            '<input type="hidden" id="table_tahun_akhir'.$sub->id_pegawai.'" value="'.$sub->tahun_akhir.'">'.
                            '<input type="hidden" id="table_akun_piutang'.$sub->id_pegawai.'" value="'.$sub->id_akun_piutang.'">'.
                            '<input type="hidden" id="table_status'.$sub->id_pegawai.'" value="'.$sub->status.'">'
                        ;
                    return $btnAction;
                })->addColumn('tgl_awal', function($d_data){
                    $tgl_awal = tgl_full($d_data->tahun_awal, '');
                    return $tgl_awal;
                })->addColumn('tgl_akhir', function($d_data){
                    $tgl_akhir = tgl_full($d_data->tahun_akhir, '');
                    return $tgl_akhir;
                })->addColumn('status', function($d_data){
                    return get_status_master($d_data->status);
                })
                ->make(true);
    }

    function get_pegawai_departement(){
		$d_data = DB::table('ms_departement');
		if($d_data->count() > 0){
			$arr['data'] = $d_data->get();
			$arr['status'] = array('1');
		}else{
			$arr['status'] = array('0');
		}

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

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

        DB::beginTransaction();
        try{
            if($id_pegawai == ""){
                $data['id_departement']     = $request->get('id_departement');
                $data['id_akun_piutang']    = $request->get('id_akun_piutang');
                // $data['nomor_pegawai'] = $request->get('nomor_pegawai');
                $data['nomor_pegawai']      = CodeGenerator::code('ms_pegawai', 'nomor_pegawai', 5, 'ID');
                $data['nama_pegawai']       = strtoupper($request->get('nama'));
                $data['tahun_awal']         = date("Y-m-d");
                $data['tahun_akhir']        = date("Y-m-d");
                $data['status']       = $request->get('status');
                $id                         = DB::table('ms_pegawai')->insertGetId($data);
                $rw['id_departement']       = $request->get('id_departement');
                $rw['id_pegawai']           = $id;
                DB::table('ms_pegawai_riwayat')->insert($rw);

            }else{
                $data['id_departement']     = $request->get('id_departement');
                $data['id_akun_piutang']    = $request->get('id_akun_piutang');
                $data['nama_pegawai']       = strtoupper($request->get('nama'));
                $data['status']       = $request->get('status');
                DB::table('ms_pegawai')->where('id_pegawai',$id_pegawai)->update($data);
            }
            DB::commit();
            return response()->json(['status'=>'Success']);
        }catch(Exception $e){
            DB::rollback();
            return response()->json(['status'=>'Failed']);
        }
    } 

    public function hapus_pegawai(Request $request){
		$data['id_pegawai'] = $request->get('id');

		DB::table('ms_pegawai')->where($data)->delete();
		DB::table('ms_pegawai_riwayat')->where($data)->delete();

	}
}
