<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use App\Komoditi;
use PDF;

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

    public function index(){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $user_name = $auth_user['name'];
        $komoditi = Komoditi::first();
        $min_komoditi = $komoditi['min_komoditi'];
        return view('operasi.komuditi.index',compact('user_name','api_url','min_komoditi'));
    }

    public function cetakRekodKomoditi(){
        $rekod = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/komuditi/rekodkomuditi')->json();

        return view('print.laporan_komoditi',compact('rekod'));
    }

    public function rekodKomoditi(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $user_name = $auth_user['name'];
        return view('operasi.komuditi.rekod_transaksi',compact('user_name','api_url'));
    }

    public function printRekodTransaksi(Request $request){
        $tarikh_rekod = $request->tarikh_print;
        $rekod_print = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/komoditi/rekod/transaksi',[
            'tarikh' => $request->tarikh_print
        ])->json();

        return view('print.laporan_transaksi_komoditi',compact('rekod_print','tarikh_rekod'));
    }

    public function komoditiUpdate(Request $request){

        $komoditi = Komoditi::first();

        $komoditi->min_komoditi = $request->komoditi_limit;
        $komoditi->save();

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