<?php namespace App\Models\teamstore;

use Illuminate\Database\Eloquent\Model;
use DB;

class TeamStoreModel extends Model {
	
	function selectAllTeamStore() // display all data from database
	{
		$i = DB::table('teamstores')
						->where("IsActive", "true")
						->get();
        return $i;
	}

	function selectTeamStoreProducts($field, $teamstoreId) // display all data from database
	{
		$i = DB::table('teamstore_products')
						->where($field, $teamstoreId)
						->orderBy('Ordering', 'ASC')->get();
        return $i;
	}

	function selectTeamStore($field,  $teamstoreURL) // display all data from database
	{
		$i = DB::table('teamstores')
						->where($field, $teamstoreURL)
						->get();
        return $i;
	}	
	
	function getProductThumbnails($productId){

		$i = DB::table('teamstore_product_thumbnails')->where('ProductId', $productId)->get();
		return $i;
	}

	function insertTeamStoreProduct($item_details){

		$i = DB::table('teamstore_products')->insert($item_details);
		$id = DB::getPdo()->lastInsertId();
		
		return array(
				'i' => $i,
				'lastId' => $id
		);
	}

	function insertTeamStoreProductThumbnails($item_details){

		$i = DB::table('teamstore_product_thumbnails')->insert($item_details);
		return $i;
	}

	function getThumbnails($productId){

		$i = DB::table('teamstore_product_thumbnails')->where('ProductId', $productId)->get();
		return $i;
	}

	function getTeams($productId){

		$i = DB::table('teams')->where('ProductId', $productId)->get();
		return $i;
	}

	function getSizes(){

		$i = DB::table('sizes')
							->where('IsActive', 'TRUE')
							->orderby('Ordering', 'ASC')
							->get();
		return $i;
	}

	function insertToCart($data){

		$i = DB::table('cart_tmp')->insert($data);
		return $i;
	}

	function selectDisplayCartThumb(){

		$i = DB::table('teamstore_product_thumbnails')
										->where('ImageClass', 'active')
										->get();
		return $i;
	}

	function myCart($cartKey){
		// echo $cartKey;
		if($cartKey != ""){

			// $i = DB::table('cart_tmp')->select('cart_tmp.Id', 'cart_tmp.Order', 'cart_tmp.ProductId', 'cart_tmp.CartKey', 'cart_tmp.ProductURL', 'cart_tmp.TeamName', 'teams.Team', 'cart_tmp.Name', 'cart_tmp.Number', 'cart_tmp.Size', 'cart_tmp.Price', 'cart_tmp.Quantity', 'teamstore_products.ProductName', 'teamstore_products.ProductURL', 'teamstore_product_thumbnails.Image')
			// 						  ->leftjoin('teamstore_products', 'cart_tmp.ProductURL','=','teamstore_products.ProductURL')
			// 						  ->leftjoin('teams', 'cart_tmp.TeamName','=','teams.Team')
			// 						  ->leftjoin('teamstore_product_thumbnails', 'cart_tmp.ProductId','=','teamstore_product_thumbnails.ProductId')
			// 						  ->where('cart_tmp.CartKey','=',$cartKey)
			// 						  ->where('teamstore_product_thumbnails.ImageClass','=','active')
			// 						  ->orderby('cart_tmp.Id', 'DESC')
			// 				          ->get();
			$i = DB::table('cart_tmp')
								->where('CartKey', $cartKey)	
								->get();

			// var_dump($i);
   			return $i;
		}
	}

	function myCartGroup($cartKey){
		if($cartKey != ""){
			$pdo 	= 	DB::connection()->getPdo();
			$query	=	$pdo->prepare("SELECT *,  COUNT(Id) AS qty, SUM(Price) * Quantity AS total_price FROM cart_tmp WHERE CartKey = :ck GROUP BY ProductId");
			$query->execute([':ck'=>$cartKey]);
			$row = $query->fetchAll(\PDO::FETCH_OBJ);
   			return $row;
		}
	}


	function getSubtotal($cartKey){

		$i = DB::table('cart_tmp')->select(DB::raw('SUM(Quantity * Price) AS Subtotal'))
								->where('CartKey','=',$cartKey)
								->get();
		return $i;
		
	}

	function updateStoreItem($data, $url){
		$i = DB::table('teamstore_products')->where('ProductURL', $url)
										->update($data);			
        return $i;
	}
	
}
