<?php

namespace App\Controllers\Contacts;

use App\Controllers\BaseController;

class ContactController extends BaseController
{
    public function enquiryForm(){
        $session = session();

        $data['page_title'] = "Enquiry Form || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Enquiry Form, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-enquiryform-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false 
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['form'] = $apiResponse['data'] ?? [];
            } else {
                $data['form'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch enquiry form';
            }

        } catch (\Exception $e) {
            log_message('error', 'Department API Call Failed: ' . $e->getMessage());
            $data['form'] = [];
            $data['error'] = 'Something went wrong while fetching enquiry form.';
        }

        return view('contacts/enquiry_form.php', $data);
    }
    public function feedbackForm(){
        $session = session();

        $data['page_title'] = "Feedback Form || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Feedback Form, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-feedbackform-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false 
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['form'] = $apiResponse['data'] ?? [];
            } else {
                $data['form'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch feedback form';
            }

        } catch (\Exception $e) {
            log_message('error', 'Department API Call Failed: ' . $e->getMessage());
            $data['form'] = [];
            $data['error'] = 'Something went wrong while fetching feedback form.';
        }

        return view('contacts/feedback_form.php', $data);
    }
    public function emergencyContactsList()
    {
        
        $session = session();

        $data['page_title'] = "Emergency Contacts || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Emergency Contacts, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-emergencycontacts-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false 
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['emergency_contacts'] = $apiResponse['data'] ?? [];
            } else {
                $data['emergency_contacts'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch emergency contacts list';
            }
           

        } catch (\Exception $e) {
            log_message('error', 'emergency contacts API Call Failed: ' . $e->getMessage());
            $data['emergency_contacts'] = [];
            $data['error'] = 'Something went wrong while fetching emergency contacts.';
        }

        return view('contacts/emergency_contacts/list', $data);
    }
    public function addEmergencyContacts(){
        
        $session = session();
        
        $data['page_title'] = "Add Emergency Contacts || " . 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">';
        return view('contacts/emergency_contacts/add', $data);
        
    }
    public function editEmergencyContacts($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

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

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-emergencycontacts-by-id', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'id' => $id
                ],
                'verify' => false
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                $data['contact'] = $apiResponse['data'];
                return view('contacts/emergency_contacts/edit', $data);
            } else {
                return redirect()->back()->with('error', 'courses not found.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    public function deleteEmergencyContacts($id)
    {

        $session = session();

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized action.');
        }
        try {
            $client = \Config\Services::curlrequest();
            $response = $client->post(base_url().'api/contact/delete-emergency-contacts', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'form_params' => [
                    'id' => $id
                ],
                'verify' => false // for local SSL
            ]);

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

          

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                return redirect()->to(base_url('backdesk/contacts/emergency-contacts/index'))->with('success', $apiResponse['message'] ?? 'Contacts deleted successfully.');
            } else {
                return redirect()->back()->with('error', $apiResponse['message'] ?? 'Failed to delete Contacts.');
            }

        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }

    public function KeyPersonnelList()
    {
        
        $session = session();

        $data['page_title'] = "Key Personnel || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Key Personnel, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-keypersonnel-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false 
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['keypersonnel'] = $apiResponse['data'] ?? [];
            } else {
                $data['keypersonnel'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch keypersonnel list';
            }
           

        } catch (\Exception $e) {
            log_message('error', 'keypersonnel API Call Failed: ' . $e->getMessage());
            $data['keypersonnel'] = [];
            $data['error'] = 'Something went wrong while fetching keypersonnel.';
        }

        return view('contacts/key_personnel/list', $data);
    }
    public function addkeyPersonnel(){
        
        $session = session();
        
        $data['page_title'] = "Add Key Personnel || " . 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">';
        return view('contacts/key_personnel/add', $data);
        
    }
    public function editkeyPersonnel($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

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

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-keypersonnel-by-id', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'id' => $id
                ],
                'verify' => false
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                $data['personnel'] = $apiResponse['data'];
                return view('contacts/key_personnel/edit', $data);
            } else {
                return redirect()->back()->with('error', 'courses not found.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    public function deletekeyPersonnel($id)
    {

        $session = session();

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized action.');
        }
        try {
            $client = \Config\Services::curlrequest();
            $response = $client->post(base_url().'api/contact/delete-key-personnel', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'form_params' => [
                    'id' => $id
                ],
                'verify' => false // for local SSL
            ]);

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

          

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                return redirect()->to(base_url('backdesk/contacts/emergency-contacts/index'))->with('success', $apiResponse['message'] ?? 'Contacts deleted successfully.');
            } else {
                return redirect()->back()->with('error', $apiResponse['message'] ?? 'Failed to delete Contacts.');
            }

        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }

    public function patinetSerivesList()
    {
        
        $session = session();

        $data['page_title'] = "Patient Services || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Key Personnel, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-patientservices-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false 
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['services'] = $apiResponse['data'] ?? [];
            } else {
                $data['services'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch services list';
            }
           

        } catch (\Exception $e) {
            log_message('error', 'services API Call Failed: ' . $e->getMessage());
            $data['services'] = [];
            $data['error'] = 'Something went wrong while fetching services.';
        }

        return view('contacts/patient_services/list', $data);
    }
    public function addpatinetSerives(){
        
        $session = session();
        
        $data['page_title'] = "Add Patient Services || " . 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">';
        return view('contacts/patient_services/add', $data);
        
    }
    public function editpatinetSerives($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

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

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-patientservices-by-id', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'id' => $id
                ],
                'verify' => false
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                $data['service'] = $apiResponse['data'];
                return view('contacts/patient_services/edit', $data);
            } else {
                return redirect()->back()->with('error', 'courses not found.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    public function deletepatinetSerives($id)
    {

        $session = session();

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized action.');
        }
        try {
            $client = \Config\Services::curlrequest();
            $response = $client->post(base_url().'api/contact/delete-patient-services', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'form_params' => [
                    'id' => $id
                ],
                'verify' => false // for local SSL
            ]);

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

          

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                return redirect()->to(base_url('backdesk/contacts/patient-services/index'))->with('success', $apiResponse['message'] ?? 'services deleted successfully.');
            } else {
                return redirect()->back()->with('error', $apiResponse['message'] ?? 'Failed to delete services.');
            }

        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }

    public function adminOfficesList()
    {
        
        $session = session();

        $data['page_title'] = "Administrative Offices || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Administrative Offices, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url().'api/contact/get-adminoffices-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false 
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['offices'] = $apiResponse['data'] ?? [];
            } else {
                $data['offices'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch offices list';
            }
           

        } catch (\Exception $e) {
            log_message('error', 'offices API Call Failed: ' . $e->getMessage());
            $data['offices'] = [];
            $data['error'] = 'Something went wrong while fetching offices.';
        }

        return view('contacts/admin_offices/list', $data);
    }
    public function addAdminOffices(){
        
        $session = session();
        
        $data['page_title'] = "Add Administrative Offices || " . 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">';
        return view('contacts/admin_offices/add', $data);
        
    }
    public function editAdminOffices($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

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

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/contact/admin-offices/get-by-id', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'id' => $id
                ],
                'verify' => false
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                $data['office'] = $apiResponse['data'];
                return view('contacts/admin_offices/edit', $data);
            } else {
                return redirect()->back()->with('error', 'Office not found.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    public function deleteAdminOffices($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized action.');
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->post(base_url() . 'api/contact/delete-administrative-office', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'form_params' => [
                    'id' => $id
                ],
                'verify' => false // Skip SSL verification for local/dev
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                return redirect()->to(base_url('backdesk/contacts/administrative-offices/index'))
                                ->with('success', $apiResponse['message'] ?? 'Office deleted successfully.');
            } else {
                return redirect()->back()->with('error', $apiResponse['message'] ?? 'Failed to delete office.');
            }

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'Error: ' . $e->getMessage());
        }
    }

    public function officeHoursList()
    {
        $session = session();

        $data['page_title'] = "Office Hours || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Office Hours, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

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

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/contact/get-office-hours-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false // optional for local SSL
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['office_hours'] = $apiResponse['data'] ?? [];
            } else {
                $data['office_hours'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch office hours list';
            }

        } catch (\Exception $e) {
            log_message('error', 'Office Hours API Call Failed: ' . $e->getMessage());
            $data['office_hours'] = [];
            $data['error'] = 'Something went wrong while fetching office hours.';
        }

        return view('contacts/office_hours/list', $data);
    }
    public function addOfficeHours(){
        
        $session = session();
        
        $data['page_title'] = "Add Office Hours || " . 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">';
        return view('contacts/office_hours/add', $data);
        
    }
    public function editOfficeHours($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

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

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/contact/get-officehours-by-id', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'id' => $id
                ],
                'verify' => false
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                $data['office'] = $apiResponse['data'];
                return view('contacts/office_hours/edit', $data);
            } else {
                return redirect()->back()->with('error', 'Office hour not found.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    public function deleteOfficeHours($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized action.');
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->post(base_url() . 'api/contact/delete-office-hours', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'form_params' => [
                    'id' => $id
                ],
                'verify' => false // Skip SSL verification for local/dev
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                return redirect()->to(base_url('backdesk/contacts/office-hours/index'))
                                ->with('success', $apiResponse['message'] ?? 'Office hour deleted successfully.');
            } else {
                return redirect()->back()->with('error', $apiResponse['message'] ?? 'Failed to delete office hour.');
            }

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'Error: ' . $e->getMessage());
        }
    }

    public function campusLocationsList()
    {
        $session = session();

        $data['page_title'] = "Campus Locations || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Campus Locations, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/contact/get-campus-locations-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['locations'] = $apiResponse['data'] ?? [];
            } else {
                $data['locations'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch campus locations list';
            }
        } catch (\Exception $e) {
            log_message('error', 'Campus locations API Call Failed: ' . $e->getMessage());
            $data['locations'] = [];
            $data['error'] = 'Something went wrong while fetching campus locations.';
        }

        return view('contacts/campus_locations/list', $data);
    }
    public function addCampusLocation()
    {
        $session = session();

        $data['page_title'] = "Add Campus Location || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Campus Location, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        return view('contacts/campus_locations/add', $data);
    }
    public function editCampusLocation($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId = $session->get('user_id');

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

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/contact/get-campus-location-by-id', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'id' => $id
                ],
                'verify' => false
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                $data['campusLocation'] = $apiResponse['data'];
                return view('contacts/campus_locations/edit', $data);
            } else {
                return redirect()->back()->with('error', 'Campus location not found.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    public function deleteCampusLocation($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized action.');
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->post(base_url() . 'api/contact/delete-campus-location', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'form_params' => [
                    'id' => $id
                ],
                'verify' => false // Skip SSL verification for local/dev
            ]);

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

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                return redirect()->to(base_url('backdesk/contacts/campus-locations/index'))
                                ->with('success', $apiResponse['message'] ?? 'Campus location deleted successfully.');
            } else {
                return redirect()->back()->with('error', $apiResponse['message'] ?? 'Failed to delete campus location.');
            }

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'Error: ' . $e->getMessage());
        }
    }

    public function address()
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId = $session->get('user_id');

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

        try {

            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/contact/get-address', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'verify' => false
            ]);

            

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

            $data['address'] = $apiResponse['data'] ?? [];
            return view('contacts/address/index', $data);

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    public function connect()
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId = $session->get('user_id');

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

        try {

            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/contact/get-connect-social', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'verify' => false
            ]);

            

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

            $data['connect'] = $apiResponse['data'] ?? [];
            return view('contacts/connect/index', $data);

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    










}