<?php

namespace App\Http\Controllers;


//Events
use App\Events\OperasiNotice;
use App\Events\StatusLiked;
use App\Events\NotifikasiKelulusan;
use App\Events\Dashboard\KomoditiNotify;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Hash;


use Carbon\Carbon;

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

    public function kelulusan(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();

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

        $arraykelulusan = [];


        foreach($kelulusan as $index=>$lulus)
        {
            foreach($lulus['kelulusanall'] as $index=>$kel)
            {
                array_push($arraykelulusan,["cawangan" => $lulus['cawangan'], "cawangankod" => $lulus['cawangankod'],"namateller" => $kel['teller'], 'siri' => $kel['siri'] , "norujukan" => $kel['norujukan'], 'pinjaman' => $kel['jumpinj'],'komen' => $kel['komen'], 'nilaimarhun' => ($kel['nilaimarhun'] ?? '-'), 'berat' => ($kel['berat'] ?? '-'),'margin' => ($kel['margin'] ?? '-'), 'status' => ($kel['status'] ?? '-'), 'pelulus' => $kel['pelulus']]);
            }
        }


        return view('operasi.kelulusan',compact('cawangan_info','api_url','arraykelulusan'));
    }

    public function getkelulusan(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $norujukan = $request->norujukan;

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

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

        if(sizeof($kelulusan) > 0)
        {
            $kelulusan = $kelulusan[0]['masalulus'];
        }
        else
        {
            $kelulusan = '';
        }

        return response($kelulusan);
    }

    public function updatekelulusan(Request $request)
    {
        $auth_user = Auth::user();

        $api_url = config('api.url');

        $norujukan = $request->norujukan;


        if($request->pembatalan == null)
        {
            $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusanKomen/' . $request->kodcaw . '/' . $norujukan,[
                "komen" => $request->komenkelulusan,
                'siri' => $request->siri
            ])->json();

            $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusan/' . $request->kodcaw . '/' . $norujukan,[
                'sahkan' => 1,
                'namaoperasi' => $request->namaoperasi,
                'siri' => $request->siri
            ])->json();

            if($updatelulus)
            {
                event(new NotifikasiKelulusan($norujukan,"lulus",$request->siri,$request->komenkelulusan,$request->teller));
            }
        }
        else
        {
            $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusanKomen/' . $request->kodcaw . '/' . $norujukan,[
                "komen" => $request->komenkelulusan,
                'siri' => $request->siri
            ])->json();

            $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusan/' . $request->kodcaw . '/' . $norujukan,[
                'sahkan' => 2,
                'namaoperasi' => $request->namaoperasi,
                'siri' => $request->siri
            ])->json();

            if($updatelulus)
            {
                event(new NotifikasiKelulusan($norujukan,"batal",$request->siri,$request->komenkelulusan,$request->teller));
            }
        }




        return response()->json($updatelulus,200);
    }

    public function createkelulusan(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $current = Carbon::now();
        $current = new Carbon();

        $norujukan = $request->norujukan;

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

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

        if(isset($getsiri['siri']))
        {
            $siri = $getsiri['siri'] + 1;
        }else{
            $siri = 1;
        }

        $createlulus = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/CreateKelulusan/' . $auth_user['cawangan'] . '/' . $norujukan . '/roles/operasi', [
            'komen' => $request->komen,
            'dategadai' => $current->toDateString(),
            'bilgadai' => 1,
            'siri' => $siri,
            'margin' => $request->margin,
            'berat' => $request->berat,
            'nilaimarhun' => $request->nilaimarhun,
            'status' => $request->status,
            'jumpinjam' => $request->jumpinjam,
            'jenistebus' => "G",
            'teller' => $request->teller,
            'peloperasi' => "operasi"

        ])->json();

        event(new StatusLiked($auth_user['cawangan'],$auth_user['name']));

        return response()->json($siri,201);
    }

    public function mintakatalaluansemula(Request $request)
    {

        if(Hash::check($request->passsemula, Auth::user()->password))
        {
            return "True";
        }
        else
        {
            return "False";
        }

    }

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

        return view('operasi.parameter',compact('cawangan_info','api_url'));
    }

    public function hargaemas()
    {
        $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();
        $harga_emas = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/harga_emas/')->json();

        return view('operasi.hargaemas',compact('cawangan_info','api_url','harga_emas'));
    }

    public function margin(){
        $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();
        $margin_pembiayaan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/maxpinjaman')->json();
        return view('operasi.margin_pembiayaan',compact('cawangan_info','api_url','margin_pembiayaan'));
    }

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

        //declare array baru
        $arraykelulusan = [];

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


        foreach($kelulusan as $index=>$k)
        {
            foreach($k['kelulusanall'] as $index1=>$kel)
            {
                $komen = explode("|",$kel['komen']);

                $results=array_search("",$komen,true);
                if($results !== false) {
                    unset($komen[$results]);
                }

                array_push($arraykelulusan,['norujukan' => $kel['norujukan'], 'pinjaman' => $kel['jumpinj'], 'nilaimarhun' => ($komen[1] ?? $kel['nilaimarhun'] ?? '-'), 'berat' => ($komen[2] ?? $kel['berat'] ?? '-'),'margin' => ($komen[3] ?? $kel['margin'] ?? '-'), 'komen' => ($komen[0] ?? $kel['komen'] ?? '-'), 'status' => ($kel['status'] ?? '-'), 'pelulus' => $kel['operasi'],]);

            }
        }

        return view('operasi.rekodtelahlulus',compact('cawangan_info','api_url','arraykelulusan'));

    }

    public function notifyKomoditi()
    {
        $auth_user = Auth::user();
        event(new KomoditiNotify('lulus',$auth_user['cawangan']));
    }
}
