<?php

namespace App\Livewire\Buttons;

use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;

class ViewLockedAccount extends Component
{
    public $uuid;
    public $card_number, $firstname, $lastname, $birthdate, $status, $card_type, $unlocked_by;

    public function mount($uuid)
    {
        $this->uuid = $uuid;

        $token = Session::get('user')['access_token'] ?? null;

        if (!$token) {
            $this->addError('users', 'No access token found.');
            return;
        }

        $response = Http::withToken($token)->get(config('services.backend_api.url') . "/api/cms/member/{$uuid}");

        if ($response->successful()) {
            $cardMember = $response->json('data');

            $this->card_number = $cardMember['card_number'] ?? '';
            $this->firstname = $cardMember['firstname'] ?? '';
            $this->lastname = $cardMember['lastname'] ?? '';
            $this->birthdate = $cardMember['birthdate'] ?? '';
            $this->status = $cardMember['status'] ?? '';
            $this->card_type = $cardMember['card_type'] ?? '';
            $this->unlocked_by = $cardMember['unlocked_by'] ?? '';
        } else {
            $this->addError('locked accoutns', 'Failed to fetch locked account data.');
        }
    }
    public function render()
    {
        return view('livewire.buttons.view-locked-account');
    }
}
