<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use App\User;

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

    public function index(){
        $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();
        if($auth_user->roles == 'teller' || $auth_user->roles == 'ppc' || $auth_user->roles == 'pc'){
            return view('user.index',compact('cawangan_info','api_url','auth_user'));
        }else{
            return view('user.index_operasi',compact('cawangan_info','api_url','auth_user'));
        }
    }

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

        if($auth_user->roles == 'teller' || $auth_user->roles == 'ppc' || $auth_user->roles == 'pc'){
            return view('user.edit',compact('cawangan_info','api_url','auth_user'));
        }else{
            return view('user.edit_operasi',compact('cawangan_info','api_url','auth_user'));
        }
    }

    public function editStore(Request $request){


        $validated = $request->validate([
            'nama' => 'required|max:255',
            'notel' => 'required|numeric|min:10',
        ]);

        $auth_user = Auth::user();
        $user = User::find($auth_user->id);
        
        $user->name = $request->nama;
        $user->no_telefon = $request->notel;
        
        $user->save();

        return redirect()->route('profile')->with('success', 'Perubahan Maklumat Pengguna Telah Dikemaskini.');
    }
}
