<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
    function __construct() {
        error_reporting(0);
        parent::__construct();
        $this->load->helper('Security');
        $this->load->model('CommonModel');

        
    }
    

    public function index() 
    {
        $this->load->view('login');
    }
    
    
    public function save_login_details() {
        $data = $this->input->post();
        $data = $this->security->xss_clean($data);
        //echo "<pre>".json_encode($data, 128);exit;
        $md5_convert_data = md5($data['password']);
        $salt_key = get_encript_id($data['password']);
        $password = $md5_convert_data . $salt_key;
        $user_id = $this->db->escape_like_str($data['username']);
        $user_exsist = $this->db->select('*')->where('email', $user_id)->where('password', $password)->where('role_id', 1)->get('admin')->result_array();

        // echo json_encode($user_exsist, 128);exit;


        if (!empty($user_exsist) && count($user_exsist) > 0) 
        {
            $this->session->set_userdata('admin_id', $user_exsist[0]['admin_id']);
            $this->session->set_userdata('hotel_name', $user_exsist[0]['hotel']);
            $this->session->set_userdata('admin_lvl', $user_exsist[0]['role_id']);
            $this->session->set_userdata('password_change', $user_exsist[0]['password_change']);
            $this->session->set_userdata('admin_user_id', $user_exsist[0]['email']);
            $this->session->set_userdata('admin_name', $user_exsist[0]['fullname']);
            $this->session->set_userdata('web_email', $user_exsist[0]['web_email']);
            $this->session->set_userdata('web_mobile', $user_exsist[0]['web_mobile']);
            $this->session->set_userdata('admin_login_session', true);
            $audit_trail_table='audit_trail';
            $audit_trail_array=array(
                                        'user_name'=>$user_exsist[0]['fullname'],
                                        'action'=>'Action -> "Login"',
                                        'action_time'=>date('Y-m-d H:i:s')
                                    );
            $this->CommonModel->insert($audit_trail_table,$audit_trail_array);
                echo "success";
        } 
        else 
        {
            echo "failed";
        }
    }
    public function account_login($user) {
        $user_exsist = $this->db->select('*')->where('admin_id', $user)->where('role_id', 1)->get('admin')->result_array();

            $this->session->set_userdata('admin_id', $user_exsist[0]['admin_id']);
            $this->session->set_userdata('hotel_name', $user_exsist[0]['hotel']);
            $this->session->set_userdata('admin_lvl', $user_exsist[0]['role_id']);
            $this->session->set_userdata('password_change', $user_exsist[0]['password_change']);
            $this->session->set_userdata('admin_user_id', $user_exsist[0]['email']);
            $this->session->set_userdata('admin_name', $user_exsist[0]['fullname']);
            $this->session->set_userdata('web_email', $user_exsist[0]['web_email']);
            $this->session->set_userdata('web_mobile', $user_exsist[0]['web_mobile']);
            $this->session->set_userdata('admin_login_session', true);
            $audit_trail_table='audit_trail';
            $audit_trail_array=array(
                                        'user_name'=>$user_exsist[0]['fullname'],
                                        'action'=>'Action -> "Login"',
                                        'action_time'=>date('Y-m-d H:i:s')
                                    );
            $this->CommonModel->insert($audit_trail_table,$audit_trail_array);
           redirect(base_url('AdminDashboard'));     
       
    }
    public function daily_upd() 
    {
        $res_details = $this->db->where('room_available', 0)->order_by('wr_id','desc')->get('walk_in_reservation')->result_array();

        foreach ($res_details as $key => $value) {

                 
             $tguest_details = $this->db->where('wr_id', $value['wr_id'])->where('gd_date',date('Y-m-d'))->order_by('gd_id','desc')->get('guest_details')->result_array();
             if (!$tguest_details) {
             

             $upd_reservation="UPDATE `walk_in_reservation` SET `check_out` = '".date('Y-m-d', strtotime(' +1 day'))."' WHERE `wr_id` = '".$value['wr_id']."'";
                $query_reservation = $this->db->query($upd_reservation);   

             $guest_details = $this->db->where('wr_id', $value['wr_id'])->order_by('gd_id','desc')->get('guest_details')->result_array();
             $data = $this->db->where('r_id', $guest_details['0']['gd_room_type'])->get('rooms')->result_array();
                 


        //print_r($rooms_details);

          $num_gust=$guest_details['0']['gd_check_in'];
        $extra_bed_price=0;
        if($num_gust>2)
        {
        $extra_guest=$num_gust-2;

            $extra_guest=$extra_guest/2;

         $total_extra_bed=ceil($extra_guest);
        }

          $extra_bed_price=$total_extra_bed*$data[0]['extra_bed'];

                 $total_amount=$data[0]['r_price']+$extra_bed_price;
                  
                       $insert_data['wr_id'] = $value['wr_id'];
                       $insert_data['gd_check_in'] = $num_gust;
                       $insert_data['gd_check_out'] = 0;
                       $insert_data['gd_amount'] = $total_amount;
                       $insert_data['gd_room_type'] = $guest_details['0']['gd_room_type'];
                       $insert_data['gd_date'] = date('Y-m-d');
                    
                       $this->db->insert('guest_details',$insert_data);

        }


        }
    }
}
?>
