<?php

namespace App\Controllers;

class AdminController extends BaseController
{
        public $config, $sapi_url, $sapi_key;
    protected $api_url,$session;
    protected $baseURL;
    public $uploadPath;

    protected $image_url;
    protected $api_key;

    public function __construct()
    {
        // parent::__construct();
        $this->config = config('AccessProperties');
        // $this->api_url = $this->config->api_url;
        // $this->uploads_url = $this->config->uploads;
        // $this->api_key = $this->config->key;
        $this->session = session();
        $this->uploadPath = realpath(FCPATH . '../') . '/uploads/images/';

        // $this->sapi_url = $this->config->sapi['url'];
        // $this->sapi_key = $this->config->sapi['key'];



    }

    public function dashboard()
    {
        // return view('admin/layout');
        return view('admin/dashboard');
    }

    public function login()
    {
        $isLoggedIn = $this->session->get('loggedIn');
        if ($isLoggedIn) {
            return redirect()->to('dashboard');
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      
        // print_r($this->request->getPost());die;

        return redirect()->to('dashboard');


         


         

        }
        return view('login');
    }

        public function apiPostFetch($postData, $url)
    {
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($postData),
            CURLOPT_HTTPHEADER => [
                'X-Api: Bearer ' . $this->api_key
            ]
        ]);

        $response = curl_exec($curl);

        if ($response === false) {
            throw new \Exception('cURL error: ' . curl_error($curl));
        }

        $apiResponse = json_decode($response, true);
        return $apiResponse;
    }

    public function apiGetfetch($url)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => array(
                'X-Api: Bearer ' . $this->api_key
            )
        ));
        $response = curl_exec($curl);
        curl_close($curl);

        $data = json_decode($response, true);
        return $data;
    }



}