<?php

namespace App\Controllers\Authentication;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\Common_M;
use App\Models\Authentication_M;

class AuthenticationController extends BaseController
{
    public function __construct(){

        $session = session();
        if($session->logged_in == true and !empty($session->get('id'))) {
            $this->session_exists = 1;
        }else{
            $this->session_exists = 0;
        }
        
        helper(['url', 'form']);
        $this->common = new Common_M();
        // $this->authentication = new Authentication_M();
        
    }
    
    public function login(){
        // REDIRECT IF ALRADY LOGGED IN
        if($this->session_exists){
            return redirect()->to(base_url('portal/dashboard'));
        }

        $data['page_title'] = "Login || ";
        $data['meta_tag'] = '<meta content="Baazar Kolkata, Sketch Me Global" name="keywords"><meta content="Baazar Kolkata, Sketch Me Global" name="description">';
        // return view('authentication/signin', $data);
        return view('authentication/login', $data);
    }
    
    public function login_form(){
      
        // if form is submitted
        $final_flag = array();
        if($this->request->getPost()){
            $agent = $this->request->getUserAgent();
            $data=array(
                "ip"=>$this->request->getIPAddress(),
                "browser"=>$agent->getBrowser(),
                "version"=>$agent->getVersion(),
                "platform"=>$agent->getPlatform(),
                "user_id"=>$this->request->getPost('user_id'),
            );

            $session = session();
            $login_rule = [
                'user_id' => 'required', //|is_unique[users.email]
                'password' => 'required|min_length[8]'
            ];

            if ($this->validate($login_rule)) {

                $post_array = array( 'user_id' => $this->request->getPost('user_id'), 'row_status' => 1 );
                $username_rows = $this->common->data_count('users',$post_array);
                echo $username_rows;
                die;

                if($username_rows == 0){ // no username/username found

                    $session->setFlashdata('msg', 'Invalid User, please try again.');
                    
                    $final_flag = array('status' => false, 'code' => '002', 'msg' => EM002);

                } elseif($username_rows > 1){ // multiple username / username found

                    $session->setFlashdata('msg', 'Multiple Presence, Contact Admin');

                } elseif($username_rows == 1){

                    $post_array = array( 'user_id' => $this->request->getPost('user_id'));
                    $user_rows = $this->common->data_count('users',$post_array);
                    $row_cond_array = array('user_id' => $this->request->getPost('user_id'));
                    $user_data = $this->common->data_current_row($table = 'users', $row_cond_array);
                    
                    if($user_rows == 0 ){  // username - password mismatches

                        if($user_data->blocked == 1){

                            $session->setFlashdata('msg', 'You are blocked, Contact Admin');
                           
                            $final_flag = array('status' => false, 'code' => '003', 'msg' => EM003);

                        } 

                        // echo $this->authentication->getLastQuery()->getQuery();

                    }else{  // // username - password matches

                        if($user_data->blocked == 1){

                            $session->setFlashdata('msg', 'You are blocked, Contact Admin');
                            $insertLogArray = array(
                                'target_table' =>'users',
                                'action_taken'=>'login',
                                'topic_details' => json_encode($data),
                                'user_id' => '',
                                'comment' => 'You are blocked, Contact Admin'
                            );

                            $this->common->LogInsert($insertLogArray);
                            $final_flag = array('status' => false, 'code' => '003', 'msg' => EM003);

                        } 
                        else{
                            
                           
                            $session->setFlashdata('msg', '');

                            $row_cond_array = array('id' => $user_data->id, 'row_status' => 1);
                            $usertype_id = $this->common->data_current_row('users',$row_cond_array)->user_type;
                            // $userrole = $this->common->data_current_row('users',$row_cond_array)->role_id;
                            // $user_detail_id = $this->common->data_current_row('users',$row_cond_array)->user_detail_id;
                          
                            if($usertype_id == 0){
                                $row_cond_array2 = array('id' => $user_data->master_id, 'row_status' => 1);
                                $user_details = $this->common->data_current_row('master_clinics',$row_cond_array2);
                            }
                            
                         
                            
                            $sess_user_data = [
                                'id'           => $user_data->id,
                                'user_id'     => $user_data->user_id,
                                'master_id'   => $user_data->master_id,
                                'name'     => $user_details->clinic_contact_person,
                                'email_id'     => $user_details->clinic_email_id,
                                'user_type'     => $user_data->user_type,
                                'logged_in'    => TRUE
                            ];
                            $session->set($sess_user_data);

                           
                                $final_flag = array('status' => true, 'code' => '011', 'msg' => EM011);
                                // return redirect()->to(base_url('dashboard'));
                            

                        }

                    }
                } else{
                    $session->setFlashdata('msg', 'Invalid User, please try again.');
                }

            } else{
                $final_flag = array('status' => false, 'code' => '001', 'msg' => EM001);
            } // validation
            
        } // form submit
        
        return json_encode($final_flag);
        
    }
    
    public function test_mail(){
        $to = 'sketchmeglobal@gmail.com';
        $message = "Hi";

        $to      = 'sketchmeglobal@gmail.com';
        $subject = 'Forget Password || Link verify';
        $headers = 'From: sayak@sketchmeglobal.com' . "\r\n" .
            'Reply-To: sayak@sketchmeglobal.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);

        // $email->setMessage($message);
        // $email->setMailType('html');
        // $email->send();
    }

    public function sendmail($mail_to,$msg,$mail_sub='',$mail_from='',$attachments=array(),$mailer_name='',$smtp_host='',$smtp_port='',$smtp_user='',$smtp_pass='') {

        $email = \Config\Services::email();


        if($mail_from == '') $mail_from=default_mail_from; else $mail_from=$mail_from;
        if($mailer_name == '') $mailer_name=default_mailer_name; else $mailer_name=$mailer_name;
        if($mail_sub == '') $mail_sub=default_mail_sub; else $mail_sub=$mail_sub;
        if($smtp_host == '') $smtp_host=default_smtp_host; else $smtp_host=$smtp_host;
        if($smtp_port == '') $smtp_port=default_smtp_port; else $smtp_port=$smtp_port;
        if($smtp_user == '') $smtp_user=default_smtp_user; else $smtp_user=$smtp_user;
        if($smtp_pass == '') $smtp_pass=default_smtp_pass; else $smtp_pass=$smtp_pass;

        $config = Array(
            'smtp_host' => $smtp_host,
            'smtp_port' => $smtp_port,
            'smtp_user' => $smtp_user,
            'smtp_pass' => $smtp_pass,
            'protocol' => 'smtp',
            'mailtype' => 'html',
            'charset' => 'utf-8',
            'wordwrap' => TRUE
        );
        $this->load->library('email', $config);

        $this->email->clear(TRUE);
        $this->email->set_mailtype("html");
        $this->email->from($mail_from, $mailer_name);
        $this->email->reply_to($mail_from, $mailer_name);
        $this->email->to($mail_to);
        $this->email->subject($mail_sub);
        $this->email->message($msg);
        //attaching files
        foreach($attachments as $att) {
            $this->email->attach($att);
        }
        //sending mail
        $this->email->send();
    }

    
    public function logout(){
        $session = session();
        $session->destroy();
        return redirect()->to(base_url('backdesk'));
    }
}
