<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

//function list

//end function list

class Customerinfo extends CI_Model {
	public function __construct() {
		parent::__construct();
		$this->load->library('User_auth');
		$this->user_auth->check_customer_auth();
	}

	public function get_customer_details_nolimit_custom_id($customer_id) {
		$this->db->select('*');
		$this->db->from('customer_information');
		$this->db->where('customer_id', $customer_id);
		// $this->db->order_by('a.order', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}

	//get_customer_details
	public function get_customer_details() {
		$customer_id = $this->session->userdata('customer_id');
		$this->db->select('*');
		$this->db->from('customer_information');
		$this->db->where('customer_id', $customer_id);
		// $this->db->order_by('a.order', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}

	//update user info
	public function user_update($data) {
		$customer_id = $this->session->userdata('customer_id');

		$this->db->trans_begin();

		$this->db->where('customer_id', $customer_id);
		$this->db->update('customer_information', $data);

		// $item_id = $this->db->insert_id();

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return FALSE;
		} else {
			$this->db->trans_commit();
			// return ($item_id);
			return TRUE;
		}
	}
	//end update user info

	//added customer details
	public function insert_billing_address($data) {
		$customer_id = $this->session->userdata('customer_id');

		$data['customer_id'] = $customer_id;
		$this->db->trans_begin();
		$this->db->insert('customer_delivery_address', $data);

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

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return FALSE;
		} else {
			$this->db->trans_commit();
			// return ($item_id);
			return TRUE;
		}
	} //public function insert_billing_address($data) {
	public function get_delivery_address($delivry_id = "") {
		//for customer logged in in to system
		//then get customer delivery address
		$customer_id = $this->session->userdata('customer_id');
		$this->db->select('*');
		$this->db->from('customer_delivery_address');
		$this->db->where('customer_id', $customer_id);
		if (!empty($delivery_id)) {
			$this->db->where('id', $customer_id);
		} //if(!empty($delivery_id)){
		// $this->db->order_by('a.order', 'desc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	} //public function get_delivery_address($delivry_id) {

	public function insert_delivery_address($data) {
		$customer_id = $this->session->userdata('customer_id');

		$data['customer_id'] = $customer_id;
		$this->db->trans_begin();
		$this->db->insert('customer_delivery_address', $data);

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

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return FALSE;
		} else {
			$this->db->trans_commit();
			// return ($item_id);
			return TRUE;
		}
	} //public function insert_delivery_address($data) {

	public function update_delivery_address($data) {
		$customer_id = $this->session->userdata('customer_id');

		$data['customer_id'] = $customer_id;
		$data2 = array(

			'customer_name' => $data['first_name'] . " " . $data['last_name'],
			'first_name' => $data['first_name'],
			'last_name' => $data['last_name'],
			'customer_email' => $data['customer_email'],
			'customer_mobile' => $data['customer_mobile'],
			'customer_address_1' => $data['customer_address_1'],
			'customer_address_2' => $data['customer_address_2'],
			'city' => $data['city'],
			'state' => $data['state'],
			'country' => $data['country'],
			'zip' => $data['zip'],
		);
		$this->db->trans_begin();
		$this->db->where('id', $data['delivery_id']);
		$this->db->where('customer_id', $customer_id);
		$this->db->update('customer_delivery_address', $data2);

		// $item_id = $this->db->insert_id();

		if ($this->db->trans_status() === FALSE) {
			$this->db->trans_rollback();
			return FALSE;
		} else {
			$this->db->trans_commit();
			// return ($item_id);
			return TRUE;
		}
	} //public function insert_delivery_address($data) {

	//end added customer details
	public function get_customer_billing_address() {
		$customer_id = $this->session->userdata('customer_id');
		$this->db->select('*');
		$this->db->from('customer_delivery_address');
		$this->db->where('customer_id', $customer_id);
		$this->db->order_by('created_at', 'asc');
		$query = $this->db->get();
		if ($query->num_rows() > 0) {
			return $query->result_array();
		}
		return false;
	}
}