<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Services\CustomerService;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $current = Carbon::now();
        $current = new Carbon();
        $current = $current->toDateString();

        $auth_user = Auth::user();
        $api_url = config('api.url');
  
        $check_nokp = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/checknokp/' . $auth_user['cawangan'] . '/' . $auth_user['no_kp'] . '/' . $current) ->json();
        if($check_nokp == $auth_user['no_kp']){   
           
     
    

            if(session()->has('iklan') && session()->get('iklan') == 0)
            {
                session()->put('iklan', 1);
            }else{
                if (Hash::check(config('app.reset_password'), $auth_user['password']) || Hash::check($auth_user['no_kp'], $auth_user['password'])) {
                    session()->put('iklan', 0);
                }
            }
    
    
            if($auth_user['roles'] == 'teller'){
                if($auth_user['role_harian'] == 'teller'){
                    $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
                    return view('homepage.'.$auth_user['role_harian'].'.home',compact('cawangan_info','api_url'));
                }elseif($auth_user['role_harian'] == 'penilai' || $auth_user['role_harian'] == 'penilai2'){
                    $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
                    $gadaian_semasa = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadaiansemasa/'. $auth_user['cawangan'])->json();
                    $gadaian_belumserah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadaiansemasa/belum_serah/'. $auth_user['cawangan'])->json();
                    return view('homepage.penilai.home',compact('cawangan_info','gadaian_semasa','gadaian_belumserah','api_url'));
                }
                else{
                    return view('not_assign');
                }
            }
            elseif($auth_user['roles'] == 'pc' || $auth_user['roles'] == 'ppc'){
                $countkhazanah = 0;
                $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
    
                $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'])->json();
                $listserah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/listSerahMarhunController/'.$auth_user['cawangan'])->json();
    
                $countkhazanah = sizeof($gadai) + sizeof($listserah);
    
                $session = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/logsession/' . $auth_user['cawangan'], [
                    'id' => $auth_user['id'],
                    'nama' => $auth_user['name'],
                    'nokp' => $auth_user['no_kp'],
                    'email' => $auth_user['email'],
                    'taraf' => $auth_user['role_harian'],
                    'tarikh' => $current,
                    
        
                ]);
                return view('homepage.khazanah.home',compact('cawangan_info','api_url','countkhazanah'));
            }
            else{
                $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
                $session = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/logsession/' . $auth_user['cawangan'], [
                    'id' => $auth_user['id'],
                    'nama' => $auth_user['name'],
                    'nokp' => $auth_user['no_kp'],
                    'email' => $auth_user['email'],
                    'taraf' => $auth_user['role_harian'],
                    'tarikh' => $current,
                    
        
                ]);
                return view('homepage.'.$auth_user['roles'].'.home',compact('cawangan_info','api_url'));
            }

        }

        else{  
            
            $session = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/logsession/' . $auth_user['cawangan'], [
                'id' => $auth_user['id'],
                'nama' => $auth_user['name'],
                'nokp' => $auth_user['no_kp'],
                'email' => $auth_user['email'],
                'taraf' => $auth_user['role_harian'],
                'tarikh' => $current,
                
    
            ]);
    
            if(session()->has('iklan') && session()->get('iklan') == 0)
            {
                session()->put('iklan', 1);
            }else{
                if (Hash::check(config('app.reset_password'), $auth_user['password']) || Hash::check($auth_user['no_kp'], $auth_user['password'])) {
                    session()->put('iklan', 0);
                }
            }
    
    
            if($auth_user['roles'] == 'teller'){
                if($auth_user['role_harian'] == 'teller'){
                    $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
                    return view('homepage.'.$auth_user['role_harian'].'.home',compact('cawangan_info','api_url'));
                }elseif($auth_user['role_harian'] == 'penilai' || $auth_user['role_harian'] == 'penilai2'){
                    $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
                    $gadaian_semasa = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadaiansemasa/'. $auth_user['cawangan'])->json();
                    $gadaian_belumserah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadaiansemasa/belum_serah/'. $auth_user['cawangan'])->json();
                    return view('homepage.penilai.home',compact('cawangan_info','gadaian_semasa','gadaian_belumserah','api_url'));
                }
                else{
                    return view('not_assign');
                }
            }
            elseif($auth_user['roles'] == 'pc' || $auth_user['roles'] == 'ppc'){
                $countkhazanah = 0;
                $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
    
                $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'])->json();
                $listserah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/listSerahMarhunController/'.$auth_user['cawangan'])->json();
    
                $countkhazanah = sizeof($gadai) + sizeof($listserah);
    
                return view('homepage.khazanah.home',compact('cawangan_info','api_url','countkhazanah'));
            }
            else{
                $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
                return view('homepage.'.$auth_user['roles'].'.home',compact('cawangan_info','api_url'));
            }


            
        }
        
        
    }

    public function poskod(){
        $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();

        return view('admin.poskod',compact('cawangan_info','api_url','auth_user'));
    }
}
