<?php

namespace App\Controllers\Interpages;

use App\Controllers\BaseController;

class PageContentsController extends BaseController
{
    public function index()
    {
        $session = session();

        $data['page_title'] = "Page Content || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
            . '<meta content="School Management System, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = $session->get('jwt_token');
        $userId = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->to('/login')->with('error', 'Unauthorized access.');
        }

        $client = \Config\Services::curlrequest();

        try {
            // ✅ Only fetch menus and submenus
            $response = $client->get(base_url() . 'api/menu/get-menu-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false
            ]);

            $apiResponse = json_decode($response->getBody(), true);

          

           $data['menus'] = $apiResponse['data'];   

            // No existing content, always show insert form
            $data['pageContent'] = null;

        } catch (\Exception $e) {
            log_message('error', 'Menu API Call Failed: ' . $e->getMessage());
            $data['menus'] = [];
            $data['error'] = 'Unable to fetch menu data.';
            $data['pageContent'] = null;
        }

        return view('interpages/index', $data);
    }

    
}
?>