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

class NotificationController extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('GuestModel');
		$this->load->model('SiteModel');
		$this->load->model('NotificationModel');
	}

	public function index()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
		
		$del_btn = $this->input->post('del_btn', TRUE);
		if($del_btn == "ok"){
			$del_ids = $this->input->post('del_ids', TRUE);
			foreach ($del_ids as $key => $val) {
				$update_data = array(
					'isDeleted' => 1
				);

				$this->NotificationModel->update_row($update_data, array('id' => $val));
			}
			
			$this->session->set_flashdata('success', 'The notification deleted successfully!');
			redirect(base_url('notification'));
			exit();
		}

		$where = " where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."'";
		
		////// Get New Notifications ///////
		
		$unread_list = $this->NotificationModel->get_rows($where." and isShow = 0");
		
		/*
		    if there are new notifications, the value of isShow field update 1. 
		*/
		
		if(count($unread_list) > 0){
		    foreach($unread_list as $key => $val){
		        $update_data = array(
					'isShow' => 1
				);

				$this->NotificationModel->update_row($update_data, array('id' => $val['id']));
		    }
		}
		
		$site_row = $this->SiteModel->get_row($userdata['site_id']);
		
		$data['title'] = 'Notification | MAKEABL';
		$data['userdata'] = $userdata;
		$data['guest_detail'] = $this->GuestModel->get_row($userdata['id']);
		$data['notification_list'] = $this->NotificationModel->get_rows($where." order by id desc");
		$data['new_notification_count'] = $this->NotificationModel->get_count($where." and isShow = 0");
		$data['total_count'] = $this->NotificationModel->get_count($where);
		$data['site_logo'] = 'http://d1z6a3vjnfowyr.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];

		$this->load->view('template/header', $data);
		$this->load->view('template/sidebar', $data);
		$this->load->view('notifications', $data);
		$this->load->view('template/footer', $data);
	}
}
