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

class MerchandiseController extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('GuestModel');
		$this->load->model('CountryModel');
		$this->load->model('SiteModel');
		$this->load->model('CartModel');
		$this->load->model('LocationModel');
		$this->load->model('SouvenirModel');
		$this->load->model('GalleryModel');
		$this->load->model('AuthModel');
		$this->load->model('NotificationModel');

		// $this->load->model('SiteDataModel');
		
	}

	public function api_clixar()
	{
		if(!empty($_REQUEST['id']))
		{
			$id = base64_decode($_REQUEST['id']);
			$sql = "select * from ar_object where id = '".$id."'";
			$query = $this->db->query($sql);
			$value = $query->row_array();
	        
	        $url = array();
	        if(!empty($value))
	        {
        		if(!empty($value['ar_one']))
                {
                    $url[] = 'https://d38x82mkkml5o1.cloudfront.net/'.$value['ar_one'];
                }
                if(!empty($value['ar_two']))
                {
                    $url[] = 'https://d38x82mkkml5o1.cloudfront.net/'.$value['ar_two'];
                }
                if(!empty($value['ar_three']))
                {
                    $url[] = 'https://d38x82mkkml5o1.cloudfront.net/'.$value['ar_three'];
                }
                if(!empty($value['ar_four']))
                {
                    $url[] = 'https://d38x82mkkml5o1.cloudfront.net/'.$value['ar_four'];
                }
	        }

	        $json['status'] = true;
	        $json['result'] = $url;
		}
		else
		{
			$json['status'] = false;
	        $json['result'] = array();	
		}

		echo json_encode($json);
	}

	public function clixar()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');

		$site_row = $this->SiteModel->get_row($userdata['site_id']);

		$data['title'] = 'Clix AR | GL Clix';
		$data['userdata'] = $userdata;
		$data['guest_detail'] = $this->GuestModel->get_row($userdata['id']);


		$sql = "select * from ar_object where ar_object.site_id = '".$userdata['site_id']."' and ar_object.status = 1";
		$query = $this->db->query($sql);
		$getARObject = $query->result_array();


		// $getARObject = $this->db->select('ar_object.*');
        // $getARObject = $this->db->from('ar_object');
        // // $getARObject = $this->db->where('ar_object.site_id', $userdata['site_id']);
        // $getARObject = $this->db->where('ar_object.status', 1);
        // $getARObject = $this->db->get()->result_array();
       

		$data['getARObject'] = $getARObject;

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


	public function index()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
        
        $site_row = $this->SiteModel->get_row($userdata['site_id']);
        
		$data['title'] = 'Purchase Merchandise | MAKEABL';
		$data['userdata'] = $userdata;
		$data['guest_detail'] = $this->GuestModel->get_row($userdata['id']);
		//and souvenir_size_tb.price <> 0
		$data['merchandise_list'] = $this->SouvenirModel->get_rows(" and site_tb.id = '".$userdata['site_id']."' group by souvenir_tb.id order by souvenir_tb.id desc");
		$data['album_list'] = $this->GalleryModel->get_album_rows(" where album_tb.id > 0 and album_tb.isDeleted = 0 and album_tb.user_id = '".$userdata['id']."' and album_tb.site_id = '".$userdata['site_id']."' order by album_tb.id desc");
		$data['notification_list'] = $this->NotificationModel->get_rows(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' order by id desc");
		$data['new_notification_count'] = $this->NotificationModel->get_count(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' and isShow = 0");
		$data['site_logo'] = 'http://d38x82mkkml5o1.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];
		
		foreach ($data['merchandise_list'] as $item) {
			$query = $this->SouvenirModel->get_size_row2(" where souvenir_id = '".$item['id']."'");

			$data['merchandise_list2'][] = array(
				'id' => $item['id'],
				'photo_url' => $item['photo_url'],
				'comp_photo_url' => $item['comp_photo_url'],
				'discount' => $item['discount'],
				'title' => $item['title'],
				'photo_num' => $item['photo_num'],
				'site_id' => $item['site_id'], 
				'size' => $query->id,
				'price' => $item['price'],
				'symbol' => $item['symbol'],
				'currency_rate' => $item['currency_rate'],
				'visibility' => $item['visibility'],
				'type' => $item['type'],
				'pack_name' => $item['pack_name'],
				'pack_price' => $item['pack_price'],
			);
		}
		

		$aa = explode('/',$userdata['qr_token']);
		$token = rtrim($aa[4],'"'); 
		
		$data['paidusercount'] = $this->GalleryModel->get_paid_user_count(" where gt.id = '" . $userdata['id'] . "' ",$token);
		$data['cart_list'] = $this->CartModel->get_rows(" where cart_tb.site_id =  '".$userdata['site_id']."' and  cart_tb.user_id='".$userdata['id']."'");

		$data['header_title'] = 'Shop';

		// $this->SiteDataModel->insert_row();
		
		//var_dump($data['userdata']['id']);
		$this->load->view('template/header', $data);
		$this->load->view('template/sidebar', $data);
		$this->load->view('merchandise', $data);
		$this->load->view('template/footer', $data);
	}

	public function detail()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
		
		$id = $this->uri->segment(2);
		$site_row = $this->SiteModel->get_row($userdata['site_id']);
		$detail = $this->SouvenirModel->get_row($id);

		$getCombo = $this->SouvenirModel->getCombo($id);

		$combo_array = array();
        foreach($getCombo as $combo)
        {
    	    $combo_data = $this->SouvenirModel->get_rows(" and souvenir_tb.id = '" . $combo->join_id . "' group by souvenir_tb.id");

    	    $query_c_size = $this->SouvenirModel->get_size_rows(" where souvenir_size_tb.souvenir_id = '" . $combo->join_id . "'");

        	$data_c['join_id'] = $combo->join_id;
        	$data_c['query_c_size'] = $query_c_size[0];
        	$data_c['data'] = $combo_data[0];
        	$combo_array[] = $data_c;
        }
        $data['combo_array'] = $combo_array;

		
		$getsiteResult = $this->SiteModel->get_site_row($detail['site_id']);
		
		$data['title'] = 'Merchandise Detail | MAKEABL';
		$data['userdata'] = $userdata;
		$data['detail'] = $detail;
		$data['guest_detail'] = $this->GuestModel->get_row($userdata['id']);
		$data['cart_list'] = $this->CartModel->get_rows(" where cart_tb.site_id =  '".$userdata['site_id']."' and  cart_tb.user_id='".$userdata['id']."'");
		$data['detail_size'] = $this->SouvenirModel->get_size_rows(" where souvenir_size_tb.id > 0 and souvenir_size_tb.isDeleted = 0 and souvenir_size_tb.souvenir_id = '".$id."'");
		$data['notification_list'] = $this->NotificationModel->get_rows(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' order by id desc");
		$data['new_notification_count'] = $this->NotificationModel->get_count(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' and isShow = 0");
		$data['site_logo'] = 'http://d38x82mkkml5o1.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];

		$aa = explode('/',$userdata['qr_token']);
		$token = rtrim($aa[4],'"'); 
		
		$data['paidusercount'] = $this->GalleryModel->get_paid_user_count(" where gt.id = '" . $userdata['id'] . "' ",$token);
		
		$this->load->view('template/header', $data);		
		$this->load->view('template/sidebar', $data);	
		if(!empty($combo_array))
		{
			$this->load->view('merchandise-detail-combo', $data);
		}
		else
		{
			$this->load->view('merchandise-detail', $data);
		}	
		
		$this->load->view('template/footer', $data);		
	}

	public function add_cart(){
		//var_dump($this->input->post());
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');


		$site_id = $this->input->post('site_id',TRUE);
		$flag = true;
		$album_id = '';
		$album_chk_id = $this->input->post('album_chk_id',TRUE);
		$type = $this->input->post('type',TRUE);
		$quantity = $this->input->post('quantity',TRUE);
		$merchandise_id = $this->input->post('merchandise_id',TRUE);
		$user_id = $this->input->post('user_id',TRUE);
		$size = $this->input->post('size',TRUE);
		foreach ($album_chk_id as $key => $val) {
			if($flag == true){
				$album_id = $val; 
				$flag = false;
			}else{
				$album_id = $album_id .", ". $val; 
			// 	$amount = $amount+1;
			}
		}

		$insert_data = array(
			'merchandise_id' => $merchandise_id,
			'album_id' => $album_id,
			'size' => $size,
			'user_id' => $user_id,
			'site_id' => $site_id,
			'is_staff' => 0, //ganito ba talaga to?
			'created_at' => date('Y-m-d H:i:s'),
			'type' => $type,
			'amount' => $quantity
		);
		//var_dump($insert_data);

		$result = $this->CartModel->insert_row($insert_data);

		if($result){
		    //$this->session->set_flashdata('success', 'A merchandise added to cart!');
			redirect(base_url('merchandise'));
    		exit();
		}
		/*redirect(base_url('merchandise'));
    	exit();*/
	}
}