<?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
		        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)
	{
		$this->db->insert('cart_tb', $insert_data);
	}

	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)
	{
		$this->db->insert('order_history_tb', $insert_data);
	}
	
	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"];
	}
}