<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\User;
use App\Role;
use App\Dashboard;


class KakitanganController extends Controller
{
    public function __construct(){
        $this->middleware('auth');
    }

    public function daftarKakitangan(){
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $role = Role::get();
        $kakitangan = User::all();
        return view('operasi.daftar_kakitangan',compact('cawangan_info','api_url','role','kakitangan','auth_user'));
    }

    public function registerStaff(Request $request){
        $auth_user = Auth::user();
        $validated = $request->validate([
            'nama' => 'required',
            'email' => 'required|email',
            'nokp' => 'required',
            'telefon' => 'required',
            'role' => 'required'
        ]);

        if($request->role == 'operasi'){
            $new_dashboard = Dashboard::create([
                'name' => strtoupper($request->nama),
                'email' => $request->email,
                'password' => Hash::make($request->nokp),
                'no_kp' => $request->nokp,
                'no_telefon' => $request->telefon,
                'roles' => $request->role
            ]);
        }

        $new_staff = User::create([
            'name' => strtoupper($request->nama),
            'email' => $request->email,
            'password' => Hash::make($request->nokp),
            'no_kp' => $request->nokp,
            'no_telefon' => $request->telefon,
            'roles' => $request->role,
            'cawangan' => $request->cawangan
        ]);

        if($auth_user->roles == 'operasi'){
            return redirect()->route('daftar.kakitangan');
        }else if($auth_user->roles == 'admin'){
            return redirect()->route('admin.daftar.kakitangan');
        }
    }

    public function kakitanganHarian(Request $request){
        $auth_user = Auth::user();
        $user_cawangan = User::whereIn('roles',['pc','ppc','teller'])->where('cawangan','=',$auth_user['cawangan'])->get();

        foreach($user_cawangan as $index=>$user){
            if($user['roles'] == 'teller'){
                if($user['id'] == $request->penilai){
                    $role_harian = 'penilai';
                }elseif($user['id'] == $request->penilai2){
                    $role_harian = 'penilai2';
                }else{
                    $role_harian = 'teller';
                }
            }elseif($user['roles'] == 'ppc'){
                if($user['id'] == $request->khazanah){
                    $role_harian = 'khazanah';
                }else{
                    $role_harian = 'ppc';
                }
            }elseif($user['roles'] == 'pc'){
                if($user['id'] == $request->khazanah){
                    $role_harian = 'khazanah';
                }else{
                    $role_harian = 'pc';
                }
            }

            User::where('id','=',$user['id'])->update(array(
                'role_harian' => $role_harian
            ));
        }

        // Check teller tak serah baki tunai untuk generate balik

        $tunaisemalam = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/GetTunaiSemalam/'. $auth_user['cawangan'])->json();

        if(is_array($tunaisemalam))
        {
            if (sizeof($tunaisemalam) > 0)
            {
                foreach($tunaisemalam as $index=>$tunsem){

                    $latesttunai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/GetCurrentTunai/'. $auth_user['cawangan'], ['kodlaluan' => $tunsem['kodlaluan']])->json();

                    if(sizeof($latesttunai) == 0)
                    {
                        $alirantunai = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/tunai/daftarTunai/' . $auth_user['cawangan'], [
                            'kodcaw' => $tunsem['kodcaw'],
                            'teller' => $tunsem['kodlaluan'],
                            'bakiawal' => $tunsem['baki'],
                            'masuk' => 0.00,
                            'yuran' => 0.00,
                            'keluar' => 0.00,
                            'baki' => $tunsem['baki'],
                            'tambah' => 0.00,
                            'kurang' => 0.00
                        ])->json();
                    }
                }
            }

            //Denominasi
            $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/DenominasiController/'.$auth_user['cawangan'])->json();

            if(sizeof($denominasi) == 0)
            {
                $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/DenominasiCreate/'.$auth_user['cawangan'])->json();
            }
            else
            {
                $denominasi=$denominasi[0];
            }

            //Peti Besi Cawangan
            $getVault=Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/VaultController/'.$auth_user['cawangan'])->json();

            if(sizeof($getVault) == 0)
            {
                $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/daftarVault/'.$auth_user['cawangan'])->json();
                $getVault = $peti_besi_terkini['bakiakhir'];

            }else{
                $getVault = $getVault[0]['bakiakhir'];
            }

        }

        return redirect()->route('home');
    }

    public function tukartaraf(Request $request){
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $user = User::find($request->id);
        $role = Role::get();

        return view('operasi.tukar_taraf',compact('cawangan_info','api_url','role','user','auth_user'));
    }

    public function tukartarafUpdate(Request $request){
        $user = User::find($request->user_id);
        if($user->role_harian == null){
            if($user->role_asal == 'pc'){
                $user->roles = $request->role;
                $user->save();
            }
        }else{
            if($user->role_asal == 'pc'){
                $user->roles = $request->role;
                $user->role_harian = $request->role;
                $user->save();
            }
        }

        $auth_user = Auth::user();

        if($auth_user->roles == 'operasi'){
            return redirect()->route('daftar.kakitangan');
        }else if($auth_user->roles == 'admin'){
            return redirect()->route('admin.daftar.kakitangan');
        }
    }

    public function tukarcawangan(Request $request){
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $user = User::find($request->id);
        $role = Role::get();

        return view('operasi.tukar_cawangan',compact('cawangan_info','api_url','role','user','auth_user'));
    }

    public function tukarcawanganUpdate(Request $request){

        $auth_user = Auth::user();
        $user = User::find($request->user_id);

        $old_data = $user->toArray();
        $user->cawangan = $request->cawangan;
        $user->roles = $request->role;
        $user->role_harian = $request->role;
        $user->save();
        $auth_user = Auth::user();
        $new_data = $user->toArray();

        $id = auth()->user()->id;

        Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/tukarcawangan/'. $auth_user['cawangan'],['namaoperasi' => $auth_user['name'],'old_data' =>  $old_data,'new_data' => $new_data ])->json();

        if($auth_user->roles == 'operasi'){
            return redirect()->route('daftar.kakitangan');
        }else if($auth_user->roles == 'admin'){
            return redirect()->route('admin.daftar.kakitangan');
        }
    }

    public function deleteKakitangan(Request $request){

        $auth_user = Auth::user();
        $user = User::find($request->staff_id);
        $user->delete();

        // if($auth_user->roles == 'operasi'){
        //     // return redirect()->route('daftar.kakitangan');
        //     return "route('daftar.kakitangan')";
        // }else if($auth_user->roles == 'admin'){
        //     // return redirect()->route('admin.daftar.kakitangan');
        //     return "route('admin.daftar.kakitangan')";
        // }
        return response()->json('success');
    }

    public function editKakitangan(Request $request){
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $user = User::find($request->id);
        $role = Role::get();

        return view('operasi.edit_kakitangan',compact('cawangan_info','api_url','role','user','auth_user'));
    }

    public function editStoreKakitangan(Request $request){
        // dd($request->all());
        $auth_user = Auth::user();
        $user = User::find($request->user_id);
        $user->name = strtoupper($request->nama);
        $user->email = $request->email;
        $user->no_kp = $request->nokp;
        $user->no_telefon = $request->telefon;
        $user->perubatan = $request->perubatan;
        $user->password = Hash::make($request->nokp);
        $user->save();


        if($auth_user->roles == 'operasi'){
            return redirect()->route('daftar.kakitangan');
        }else if($auth_user->roles == 'admin'){
            return redirect()->route('admin.daftar.kakitangan');
        }
    }

    public function resetCredentials(Request $request){
        $user = User::find($request->id);
        $user->password = Hash::make(config('app.reset_password'));
        $user->save();
        return redirect()->route('admin.daftar.kakitangan');
    }
}
