<?php

namespace App\Models\teamstore;

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

class TeamStoreModel extends Model
{

	function selectAllTeamStore() // display all data from database
	{
		$i = DB::table('teamstores')
			->where("IsActive", "true")
			->orderBy('Id', 'DESC')
			->paginate(16);
		return $i;
	}

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

	function selectTeamStoreProductByIdHash($id)
	{
		$i = DB::table('teamstore_products')
			->where(DB::raw('md5(Id)'), $id)
			->get();
		return $i;
	}

	function selectTeamStore($field,  $teamstoreURL)
	{
		$i = DB::table('teamstores')
			->where($field, $teamstoreURL)
			->get();
		return $i;
	}

	function checkStorePassword($storeid, $password)
	{
		$i = DB::table('teamstores')
			->where('Id', $storeid)
			->where('Password', $password)
			->get();
		return $i;
	}

	function selectTeamStoreGroupByCartKey($cartKey)
	{
		$i = DB::table('cart_tmp')
			->where('CartKey', $cartKey)
			->groupby('CartKey')
			->get();
		return $i;
	}

	function getProductThumbnails($productId)
	{

		$i = DB::table('teamstore_product_thumbnails')
			->where('ProductId', $productId)
			->orderby('Ordering', 'ASC')
			->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)
			->orderby('Ordering', 'ASC')
			->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 getSizesByBracket($bracket)
	{

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


	function insertToCart($data)
	{

		$i = DB::table('cart_tmp')->insert($data);
		$id = DB::getPdo()->lastInsertId();

		return array(
			'i' => $i,
			'lastId' => $id
		);
		// 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')
				->leftjoin('teamstore_voucher', 'cart_tmp.VoucherId', '=', 'teamstore_voucher.Id')
				->select('cart_tmp.*', 'teamstore_voucher.VoucherCode',  'teamstore_voucher.VoucherType',  'teamstore_voucher.VoucherValue', 'teamstore_voucher.VoucherExpiryDate', 'teamstore_voucher.Status')
				->where('cart_tmp.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, Price * SUM(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)
			->where('VoucherId', '=', null)
			->get();
		return $i;
	}

	function getSubtotalNew($cartKey)
	{

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

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

	function getSoldQty($productId)
	{
		$i = DB::table('orders')->select(DB::raw('SUM(Quantity) AS SoldQty'))
			->where('ProductId', $productId)
			->get();
		return $i;
	}

	function selectVoucher($data)
	{
		$i = DB::table('teamstore_voucher')
			->where(DB::raw('BINARY `VoucherCode`'), $data['voucher'])
			->where('TeamStoreId', $data['store_id'])
			->get();
		return $i;
	}

	function selectVoucherWhereIn($data)
	{
		$i = DB::table('teamstore_voucher')
			->whereIn('Id', $data)
			->get();
		return $i;
	}

	function updateVoucherValueInCart($data, $id)
	{
		$i = DB::table('cart_tmp')
			->where('Id', $id)
			->update($data);
		return $i;
	}

	function selectTeamstoreSearch($field, $value, $keyword)
	{

		$i = DB::table('teamstores')
			->where("StoreName", "LIKE", "%$keyword%")
			->where("IsActive", "true")
			->orderby($field, $value)
			->paginate(16);
		return $i;
	}

	function selectTeamstoreFilter($field, $value)
	{

		$i = DB::table('teamstores')
			->where("IsActive", "true")
			->orderby($field, $value)
			->paginate(12);
		return $i;
	}


	function selectFeaturedProducts($store_id = 0)
	{
		if ($store_id == 0) {
			$i = DB::select("SELECT t.StoreUrl, tp.ProductName, tp.ProductPrice, tp.ProductURL, tpt.Image FROM teamstores AS t
			INNER JOIN teamstore_products AS tp ON tp.TeamStoreId = t.Id
			INNER JOIN teamstore_product_thumbnails as tpt ON tp.Id = tpt.ProductId 
			WHERE t.Password IS NULL AND t.IsHibernated IS NULL AND tpt.ImageClass = 'active' AND tp.PrivacyStatus = 'public' 
			ORDER BY RAND()
			LIMIT 6");
		} else {
			$i = DB::select("SELECT t.StoreUrl, tp.ProductName, tp.ProductPrice, tp.ProductURL, tpt.Image FROM teamstores AS t
			INNER JOIN teamstore_products AS tp ON tp.TeamStoreId = t.Id
			INNER JOIN teamstore_product_thumbnails as tpt ON tp.Id = tpt.ProductId 
			WHERE t.Password IS NULL AND t.IsHibernated IS NULL AND tpt.ImageClass = 'active' AND tp.PrivacyStatus = 'public' AND t.Id = $store_id
			ORDER BY RAND()
			LIMIT 6");
		}

		return $i;
	}

	function checkVoucher($userid, $voucherid)
	{
		$i = DB::select("SELECT *
			FROM payment_details AS pd
			INNER JOIN orders AS o ON pd.CartKey = o.CartKey
			WHERE pd.UserId = $userid AND o.VoucherId = $voucherid");
		return $i;
	}

	function getCarousel()
	{
		$i = DB::select("SELECT * FROM teamstores WHERE IsActive = 'true' ORDER BY RAND() LIMIT 10");
		return $i;
	}

	function selectRoster($productId)
	{

		$i = DB::table('roster')
			->where("ProductId", $productId)
			->orderBy('Name', 'ASC')
			->get();
		return $i;
	}


	function filterTeamstoreProducts($field1, $value1, $field2, $value2)
	{

		$i = DB::table('teamstore_products')
			->where($field1, $value1)
			->where('PrivacyStatus', 'public')
			->orderBy($field2, $value2)->get();
		// ->orderBy('Ordering', 'ASC')->get();
		return $i;
	}

	function searchTeamstoreProducts($field1, $value1, $field2, $value2, $keyword)
	{
		$i = DB::table('teamstore_products')
			->where($field1, $value1)
			->where('PrivacyStatus', 'public')
			->where("ProductName", "LIKE", "%$keyword%")
			->orderBy($field2, $value2)->get();
		// ->orderBy('Ordering', 'ASC')->get();
		return $i;
	}
}
