<?php

namespace App\Http\Controllers;

use App\Services\ImbangdugaServices as ImbanganDuga;

//Events
use App\Events\StatusLiked;
use App\Events\NotifikasiPembayaran;
use App\Events\OperasiNotice;
use App\Events\PendahuluanSerahan;
use App\Events\NotifikasiKelulusan;
use App\Events\NotifikasiUbahHapus;
use App\Events\NotifikasiAGH;
use App\Events\NotifikasiPetiBesi;

use App\User;
use App\Cawangan;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;
use PDF;
use DateTime;

class WakilController extends Controller
{


    public function __construct()
    {
        $this->middleware('auth');
    }

    public function indexwakil()
    {
        $search = false;

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $idtype = 'nokp';
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/' . $auth_user['cawangan'])->json();
        $bank = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bank')->json();

        return view('khazanah.wakil', compact('search', 'cawangan_info', 'bank', 'api_url', 'idtype', 'auth_user'));
    }

    public function suratwakil(Request $request)
    {
        $noic = $request->noic;
        // $idtype = $request->customerIDType;
        Session::put('noic', $noic);
        $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();

        $bank = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bank')->json();
        $customer_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/' . $noic)->json();


        $request->session()->forget('page_reload');
        $request->session()->forget('norujukan');

        $search = true;

        if ($customer_info) {
            Session::put('customer_info_khazanah', $customer_info);

            return redirect()->route('khazanah.sag_info')
                ->with([
                    'customer_info' => $customer_info,
                    'cawangan_info' => $cawangan_info,
                    'api_url' => $api_url
                ]);
        } else {

            return view('khazanah.wakil', compact('search', 'noic', 'cawangan_info', 'bank', 'api_url', 'idtype'));
        }
    }

    public function sagInfo(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/' . $auth_user['cawangan'])->json();
        $customer_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/' . Session::get('noic'))->json();


        $bakilelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bakilelong/cawangan/' . $auth_user['cawangan'] . '/' . Session::get('noic') . '/baki')->json();
        $rugilelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bakilelong/cawangan/' . $auth_user['cawangan'] . '/' . Session::get('noic') . '/rugi')->json();

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



        $request->session()->forget('page_reload');
        $request->session()->forget('norujukan');


        return view('khazanah.saginfo', compact('cawangan_info', 'api_url', 'customer_info', 'bakilelong', 'rugilelong', 'negeri'));
    }

    public function printSuratWakil($norujukan, Request $request)
    {

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $t_cetak = $current->toDateString();

        $tarikh_cetak = date('d/m/Y');

        $check_norujukan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/checksurat/' . $auth_user['cawangan'] . '/' . $norujukan)->json();
        if ($check_norujukan == $norujukan) {
            //get details gadai && detail customer
            $update_wakil = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/updatewakil/' . $auth_user['cawangan'] . '/' . $norujukan)->json();

            $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadai/norujukan/' . $norujukan)->json();
            $gadai_details = $gadai[0][0];

            $noic = $gadai_details['nokp'];
            $customer_detail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/' . $noic)->json();

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

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

            // $nosiri = $getNoSiri[0];
            // $pdf=PDF::loadView('print.surat_wakil', compact('customer_detail','current','auth_user','api_url','cawangan_detail'))
            // ->setPaper('A4','potrait');
            // return $pdf->download('notislelong_'.'.pdf');
            // dd($gadai_details);


            return view('print.surat_wakil', compact('customer_detail', 'current', 'auth_user', 'api_url', 'cawangan_detail', 'tarikh_cetak', 'gadai_details', 'noic', 'nosiri'));
        } else {

            // insert into wakil_surat

            $current_year = substr($current->year, 2, 2);
            $current_month = sprintf("%02d", $current->month);
            $current_day = sprintf("%02d", $current->day);


            $surat_wakil = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/wakil/' . $auth_user['cawangan'] . '/' . $t_cetak)->json();
            if ($surat_wakil) {
                $new_suratid = $surat_wakil['nosiri'] + 1;
            } else {
                $new_suratid = 1;
            }


            $new_surat = sprintf("%04d", $new_suratid);

            $no_surat = 'WK' . $current_year . $current_month . $current_day . '-' . $new_surat;
            Session::put('norujukan', $norujukan);
            Session::put('no_surat', $no_surat);
            Session::put('page_reload', true);
            $gadai = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/wakil/' . $auth_user['cawangan'], [
                'nosiri' => $new_suratid,
                'no_surat' => $no_surat,
                'norujukan' => $norujukan,
                't_cetak' => $t_cetak,
                'nama' => Auth::user()->name,

            ]);

            //get details gadai && detail customer

            $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadai/norujukan/' . $norujukan)->json();
            $gadai_details = $gadai[0][0];

            $noic = $gadai_details['nokp'];
            $customer_detail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/' . $noic)->json();

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

            $cawangan_detail = $cawangan[0];

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

            // $pdf=PDF::loadView('print.surat_wakil', compact('customer_detail','current','auth_user','api_url','cawangan_detail'))
            // ->setPaper('A4','potrait');
            // return $pdf->download('notislelong_'.'.pdf');

            return view('print.surat_wakil', compact('customer_detail', 'current', 'auth_user', 'api_url', 'cawangan_detail', 'tarikh_cetak', 'gadai_details', 'noic', 'nosiri'));
        }
    }

    public function printSuratWakilPenilai($norujukan, Request $request)
    {

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $t_cetak = $current->toDateString();

        $tarikh_cetak = date('d-m-Y');

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

        if ($check_norujukan == $norujukan) {

            //get details gadai && detail customer

            $update_wakil = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/updatewakil/' . $auth_user['cawangan'] . '/' . $norujukan)->json();

            $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadai/norujukan/' . $norujukan)->json();
            $gadai_details = $gadai[0][0];

            $noic = $gadai_details['nokp'];
            $customer_detail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/' . $noic)->json();

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

            $cawangan_detail = $cawangan[0];

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

            // $pdf=PDF::loadView('print.surat_wakil', compact('customer_detail','current','auth_user','api_url','cawangan_detail'))
            // ->setPaper('A4','potrait');
            // return $pdf->download('notislelong_'.'.pdf');
            // dd($gadai_details);


            return view('print.surat_wakil', compact('customer_detail', 'current', 'auth_user', 'api_url', 'cawangan_detail', 'tarikh_cetak', 'gadai_details', 'noic', 'nosiri'));
        } else {

            // insert into wakil_surat

            $current_year = substr($current->year, 2, 2);
            $current_month = sprintf("%02d", $current->month);
            $current_day = sprintf("%02d", $current->day);


            $surat_wakil = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/wakil/' . $auth_user['cawangan'] . '/' . $t_cetak)->json();
            if ($surat_wakil) {
                $new_suratid = $surat_wakil['nosiri'] + 1;
            } else {
                $new_suratid = 1;
            }


            $new_surat = sprintf("%04d", $new_suratid);

            $no_surat = 'WK' . $current_year . $current_month . $current_day . '-' . $new_surat;
            Session::put('norujukan', $norujukan);
            Session::put('no_surat', $no_surat);
            Session::put('page_reload', true);
            $gadai = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/wakil/' . $auth_user['cawangan'], [
                'nosiri' => $new_suratid,
                'no_surat' => $no_surat,
                'norujukan' => $norujukan,
                't_cetak' => $t_cetak,
                'nama' => Auth::user()->name,

            ]);

            //get details gadai && detail customer

            $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadai/norujukan/' . $norujukan)->json();
            $gadai_details = $gadai[0][0];

            $noic = $gadai_details['nokp'];
            $customer_detail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/' . $noic)->json();

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

            $cawangan_detail = $cawangan[0];

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


            // $pdf=PDF::loadView('print.surat_wakil', compact('customer_detail','current','auth_user','api_url','cawangan_detail'))
            // ->setPaper('A4','potrait');
            // return $pdf->download('notislelong_'.'.pdf');

            return view('print.surat_wakil', compact('customer_detail', 'current', 'auth_user', 'api_url', 'cawangan_detail', 'tarikh_cetak', 'gadai_details', 'noic', 'nosiri'));
        }
    }
}
