<?php

defined('BASEPATH') OR exit('No direct script access allowed');



require FCPATH  . 'vendor/autoload.php';

class AdminLoginAccess extends MY_Controller

{

    public function __construct()

    {

        parent::__construct();



        // phpinfo();

        $this->load->model('User_model');

    }



    // Registration API





    public function access()

    {
        // echo "string"; die();


        if(isset($_POST['email'])){



        // Load the validation library

        $this->load->library("form_validation");



        $this->form_validation->set_rules(

            "email",

            "Email",

            "required|valid_email"

        );

        $this->form_validation->set_rules("password", "Password", "required");



        if ($this->form_validation->run() == false) {

            // Collect validation errors

            $errors = [

                "email" => form_error("email"),

                "password" => form_error("password"),

            ];



            echo json_encode(["status" => "error", "errors" => $errors]);

        } else {

            // Get input data

            $email = $this->input->post("email");

            $password = $this->input->post("password");



            // Validate credentials

            $this->db->where("role", 'admin');

            $this->db->where("email", $email);

            $query = $this->db->get("tbl_users");



            if ($query->num_rows() == 1) {



                $user = $query->row();



                // Verify password (assuming password is hashed)

                if (password_verify($password, $user->password)) {

                    if ($user) {

                        $this->db->select('agency_logo,');

                        $this->db->from('tbl_agents');

                        $this->db->where('user_id', $_POST['agent_id']);

                        $logo = $this->db->get()->row();



                        // Set session data for the logged-in user

                        $this->session->set_userdata([

                            "user_id" => $_POST['agent_id'],

                            "user_email" => $_POST['agent_email'],

                            "role"=>'agent',

                            "logo"=>$logo->agency_logo,

                            "is_logged_in" => true,

                        ]);

//                        print_r($_SESSION); die();

                        redirect('dashboard');

                        echo json_encode([

                            "status" => "success",

                            "redirect_url" => base_url("dashboard"), // Redirect to dashboard

                        ]);

                    } else {

                        // Invalid credentials

                        echo json_encode([

                            "status" => "error",

                            "errors" => [

                                "credentials" => "Invalid email or password.",

                            ],

                        ]);

                    }

                }else{

                    echo json_encode([

                        "status" => "error",

                        "errors" => [

                            "credentials" => "Invalid email or password.",

                        ],

                    ]);





                }

            }else{

                echo json_encode([

                    "status" => "error",

                    "errors" => [

                        "credentials" => "Invalid email or password.",

                    ],

                ]);



            }

        }



        }else{



            $this->load->view('agent/auth/login');

        }


    }



}

