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

class CartModel extends CI_Model { 

    public function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	function get_rows($where)
    {
		$sql = "select cart_tb.*, 
		               souvenir_tb.comp_photo_url as comp_photo_url, souvenir_tb.sku as sku,
		               souvenir_tb.description as description, souvenir_tb.discount as discount, 
		               souvenir_size_tb.title as title, souvenir_size_tb.price as price, 
		               country_tb.symbol as symbol, country_tb.rate as currency_rate,
		               site_tb.logo as venue_logo, souvenir_tb.title as merchandise_title
		        from cart_tb 
		        left join souvenir_tb on cart_tb.merchandise_id = souvenir_tb.id
		        left join souvenir_size_tb on cart_tb.size = souvenir_size_tb.id
		        left join country_tb on souvenir_tb.country_id = country_tb.id
		        left join site_tb on souvenir_tb.site_id = site_tb.id
		        ".$where;
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function insert_row($insert_data)
	{
		$insert_data['created_date'] = time();
		/*$this->db->insert('cart_tb', $insert_data);*/
		$result = $this->db->insert('cart_tb', $insert_data);

		$insert_id = $this->db->insert_id();
        return  $insert_id;

		// return $result ? true : false;
	}

	function delete_row($edit_id)
    {		
		$this->db->delete('cart_tb', array('id' => $edit_id));

	}

	public function update_row($update_data, $id)
	{
		$this->db->update('cart_tb', $update_data, $id);
	}
	
	function get_country_rows()
    {
		$sql = "select * from country_data_tb";
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	
	function get_country_row($site_id)
    {
		$sql = "select country_tb.* 
		        from country_tb
		        left join site_tb on site_tb.country_id = country_tb.id
		        where site_tb.id = '".$site_id."'";
		$query = $this->db->query($sql);
		return $query->row_array();
	}
	
	public function insert_delivery_row($insert_data)
	{
		$this->db->insert('delivery_tb', $insert_data);
	}
	
	public function insert_detail_row($insert_data)
	{
		$insert_data['created_date'] = time();
		$this->db->insert('order_history_tb', $insert_data);
		$insert_id = $this->db->insert_id();
        return  $insert_id;		
	}
	
	function get_amount_row($edit_id)
    {
		$sql = "select amount from cart_tb where id = '".$edit_id."'";
		$query = $this->db->query($sql);
		return $query->row_array();
	}
	
	public function get_currency($site_id)
	{
	    
	    $sql = "select country_id from site_tb where id = '".$site_id."'";
		$query = $this->db->query($sql);
		$id = $query->row_array()['country_id'];
		$sql = "select iso_code from country_tb where id = '".$id."'";
		$query = $this->db->query($sql);
		return $query->row_array()["iso_code"];
	}
	function get_discount($discountcode, $site_id)
    {
		$sql = "select * from discount_codes_tb where discountCode = '".$discountcode."' and site_id = '".$site_id."'";
		$query = $this->db->query($sql);
		return $query->row_array();
	}

	function get_discount_qr($discountcode, $site_id)
    {
		$sql = "select discount_codes_tb.*,discount_codes_qr_tb.id as discount_code_qr_id from discount_codes_qr_tb INNER JOIN discount_codes_tb ON discount_codes_tb.id = discount_codes_qr_tb.discount_code_id WHERE discount_codes_qr_tb.qr_code = '".$discountcode."' AND discount_codes_qr_tb.is_delete = 0 AND discount_codes_qr_tb.is_used = 0 AND discount_codes_tb.site_id = '".$site_id."'";
		$query = $this->db->query($sql);
		return $query->row_array();
	}

	
    public function insertTransaction($data)
    {
        $insert = $this->db->insert('order_payments_tb', $data);

        return $insert ? true : false;
    }
}