<?php

namespace App\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Livewire\Attributes\Layout;  // Required for layout declaration

#[Layout('layouts.dashboard')]  // Attribute syntax for Laravel 11
class TermsAndPrivacy extends Component
{
    public $termsAndPrivacy = [];

    public function mount()
    {
        $this->loadTermsAndPrivacy();  // Load users initially
    }

    public function loadTermsAndPrivacy()
    {
        try {
            $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/TermsAndPrivacy');

            // dd($response->json());
            if ($response->successful()) {

                // dd($response->json()['data']);
                // Properly use collect to handle the response data
                $this->termsAndPrivacy = collect($response->json()['data'])
                ->map(function ($termsAndPrivacy) {
                              
                    return [
                        'tp_uuid' => $termsAndPrivacy['tp_uuid'],
                        'title' => $termsAndPrivacy['title'],
                        'details' => $termsAndPrivacy['details'],
                        'type' => $termsAndPrivacy['type'],

                    ];
                });

                    // dd($this->loadLockedAccounts());
                    
            } else {
                $this->addError('users', 'Failed to load terms and privacy.');
            }
        } catch (\Exception $e) {
            $this->addError('users', 'Error: ' . $e->getMessage());
        }
    }

    public function render()
    {
        return view('livewire.about-us.terms-and-privacy', [
            'terms_and_privacy' => $this->termsAndPrivacy,  // Pass all users to the table
        ]);
    }
}
