<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */
abstract class BaseController extends Controller
{
    /**
     * Instance of the main Request object.
     *
     * @var CLIRequest|IncomingRequest
     */
    protected $request;

    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var list<string>
     */
    protected $helpers = [];

    /**
     * Be sure to declare properties for any property fetch you initialized.
     * The creation of dynamic property is deprecated in PHP 8.2.
     */
    // protected $session;

    /**
     * @return void
     */
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        // Preload any models, libraries, etc, here.
        $this->request = \Config\Services::request();
        // E.g.: $this->session = \Config\Services::session();
    }

    protected function renderExternalPage($page_name, $data):string {
        return view('template/header', $data).
               view('' . $page_name, $data).
               view('template/footer', $data);
    }

    protected function loginUser($data, $out) {

        $data['action'] = MERMS_PROVIDERS_LOGIN;
        //  $data['mlog'] = $mlog;
        // $data['member_id'] = $_SESSION['member_id'];
        $this->load->model('backend_model');
        $out = array();
        $res = $this->backend_model->mermsemr_api($data, $out);

        $loginReturn = false;

        if ($res == PHP_LOGIN_OK && isset($out["practice_id"]) && $out["practice_id"] > 0) {
            $_SESSION['session_id'] = $out['sessionid']; // "";
            $_SESSION['username'] = $out['username']; // "";
            $_SESSION['practice_name'] = $out['practice_name'];
            $_SESSION['practice_code'] = "IFE0001A";
            $_SESSION['user_firstname'] = $out['firstname'];
            $_SESSION['user_lastname'] = $out['lastname'];
            $_SESSION['user_email'] = $out['email'];
            $_SESSION['user_id'] = $out['user_id'];
            $_SESSION['practice_id'] = $out['practice_id'];
            $_SESSION['user_provider'] = "1";
            $_SESSION['user_admin'] = "1";
            $loginReturn = true;
        } else {
            $data['error_message'] = "Invalid Username or Password";
        }


        return $loginReturn;
    }
}
