<?php

namespace App\Http\Controllers;

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

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

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

	$gadaidetail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadai/norujukan/'. $auth_user['cawangan'] . '/' . $norujukan)->json();
        $noic = $gadaidetail[0]['nokp'];
        $customerdetail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/'. $noic)->json();
        $nama = $customerdetail['nama'];

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

        $batuPermataExist = false;

        foreach($marhuns[0] as $marhun)
        {
            if($marhun['tag_permata'] == 1)
            {
                $batuPermataExist = true;
            }
        }

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

        if(sizeof($lelong) > 0)
        {
            $lelong = $lelong[0];
        }

        $gadai = $gadaidetail[0];

        return view('lelongan.index',compact('cawangan_info','api_url','gadai','nama','lelong', 'marhuns', 'batuPermataExist'));
    }

    public function lelong(){
        $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();
        $senaraibatchfasa1 = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/aktif/'. $auth_user['cawangan'] . '/' . 'fasa1')->json();
        $senaraibatchfasa2 = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/aktif/'. $auth_user['cawangan'] . '/' . 'fasa2')->json();

        return view('khazanah.lelong',compact('cawangan_info','api_url','senaraibatchfasa1','senaraibatchfasa2'));
    }

    public function resittuntutbaki($norujukan){

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

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

        // $lelong=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/TuntutBaki/resit/' . $auth_user['cawangan'] . '/' . $norujukan)->json();
        $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/TuntutBaki/resit/' . $auth_user['cawangan'] . '/' . $norujukan, [
        'name' => $auth_user['name']])->json();


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

        // dd($wakil);
        if(sizeof($wakil) != 0)
            $wakil = $wakil[0];

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

        $batuPermataExist = false;

        foreach($marhuns[0] as $marhun)
        {
            if($marhun['tag_permata'] == 1)
            {
                $batuPermataExist = true;
            }
        }

        return view('print.resit_tuntutbaki', compact('cawangan_detail','api_url','lelong','wakil', 'marhuns', 'batuPermataExist'));

    }

    public function transaksituntutbaki(Request $request){

        $auth_user = Auth::user();
        $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/TuntutBaki/resit/' . $auth_user['cawangan'] . '/' . $request->norujukan)->json();

        if($lelong)
        {
            return response('Berjaya');
        }

        $tuntutbaki = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/Tuntutbakisimpan/' . $auth_user['cawangan'], [
            'batchno' => $request->batchno,
            'jenis' => $request->jenis,
            'norujukan' => $request->norujukan,
            'teller' => $request->teller,
            'bakipjm' => $request->bakipjm,
            'bakiupah' => $request->bakiupah,
            'cajlain' => $request->cajlain,
            'cajlelong' => $request->cajlelong,
            'jumlah' => $request->jumlah,
            'nowakil' => $request->nowakil,
            'hargajual' => $request->hargajual
        ])->json();

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

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

        $keluar = $getTunai2['keluar'];
        $baki = $getTunai2['baki'];
        $masuk = $getTunai2['masuk'];

        $bakiakhir = $baki;

        if($request->jenis == 'T')
        {
            $bakiakhir -= $request->jumlah;
        }
        else
        {
            $bakiakhir += -($request->jumlah);
       }
        //masuk dalam tunai aliran
        $updateTunai=Http::withOptions(['verify' => config('app.api_verify')])->put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
            'kodlaluan' => $request->teller,
            'keluar' => $keluar,
            'masuk' => $masuk,
            'bakiakhir' => $bakiakhir
        ]);

        if($updatetuntutbaki)
        {
            return response('Berjaya');
        }else{
            return response('Gagal');
        }
    }
}
