<?php

namespace App\models;

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

class ApiModel extends Model
{

	function loginProductionUser($username, $password)
	{
		$i = DB::table('production_user')
			->where('Username', $username)
			->where('Password', $password)
			->get();
		return $i;
	}

	function selectTrackingStepLabel($id)
	{
		$i = DB::table('tracking_steps')->select('StepLabel')
			->where('Id', $id)
			->get();
		return $i;
	}

	function getTrackingStatus($invoice)
	{
		$i = DB::table('tracking')->select('tracking.Id', 'tracking.InvoiceNumber', 'tracking_steps.StepLabel', 'production_user.Name', DB::raw('DATE_FORMAT(tracking.created_at, "%b %d, %Y") AS date'), DB::raw('DATE_FORMAT(tracking.created_at, "%H:%i") AS time'))
			->leftjoin('tracking_steps', 'tracking_steps.Id', '=', 'tracking.StepId')
			->leftjoin('production_user', 'production_user.Id', '=', 'tracking.ScannedBy')
			->where('tracking.InvoiceNumber', '=', $invoice)
			->orderBy('tracking.created_at', 'DESC')
			->get();
		return $i;
	}

	function selectPaymentDetails($invoice)
	{
		$i = DB::table('payment_details')
			->where('InvoiceNumber', $invoice)
			->get();
		return $i;
	}

	function selectOrderList($cartKey)
	{
		$i = DB::table('orders')->select('ProductId', 'ProductName', 'CartKey')
			->where('CartKey', $cartKey)
			->groupBy('ProductId')
			->get();
		return $i;
	}

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

	function selectOrderListTableFields($cartKey, $productId, $stepid)
	{
		// $i = DB::table('orders')->select('Id', 'Name', 'Name2', 'Number', 'Size', 'JerseySize', 'ShortsSize', 'Quantity')
		// 	->where('CartKey', $cartKey)
		// 	->where('ProductId', $productId)
		// 	->get();
		// return $i;

		$i = DB::table('orders')->select('orders.Id', 'orders.Name', 'orders.Name2', 'orders.Number', 'orders.Size', 'orders.JerseySize', 
		'orders.ShortsSize', 'orders.Quantity', DB::raw('(SELECT COUNT(*) FROM tracking WHERE StepId = '.$stepid.' AND  OrdersId = orders.Id) AS Status'))
			// ->leftjoin('tracking', 'orders.Id', '=', 'tracking.OrdersId')
			// ->where('tracking.StepId', $stepid)
			->where('orders.CartKey', $cartKey)
			->where('orders.ProductId', $productId)
			->groupBy('orders.Id')
			->get();
		return $i;
	}

	function insertTracking($data)
	{
		$i = DB::table('tracking')->insert($data);
		return $i;
	}

	// function selectNextStep($invoice)
	// {
	// 	$i = DB::table('tracking')->select('StepId')
	// 				->where('InvoiceNumber', $invoice)
	// 				->orderBy('StepId', 'DESC')->first();
	// 	return $i;
	// }

	function checkIfTrackExist($stepid, $productid, $orderid, $invoice, $qcounter)
	{
		$i = DB::table('tracking')
					->where('StepId', $stepid)
					->where('ProductId', $productid)
					->where('OrdersId', $orderid)
					->where('InvoiceNumber', $invoice)
					->where('QuantityCounter', $qcounter)
					->get();
		return $i;
	}

	function getCurrentTrackingSteps($invoice){
		$i = DB::table('tracking')->select('StepId')
				->where('InvoiceNumber', $invoice)
				->groupBy('StepId')
				->orderBy('StepId', 'ASC')
				->get();
		return $i;
	}

	function getStatus($invoice, $productid, $orderid, $qcounter){
		$i = DB::table('tracking')->select('production_user.Name', DB::raw('DATE_FORMAT(tracking.created_at, "%b %d, %Y - %H:%i") AS datetime'))
				->leftjoin('production_user', 'production_user.Id', '=', 'tracking.ScannedBy')
				->where('tracking.InvoiceNumber', $invoice)
				->where('tracking.ProductId', $productid)
				->where('tracking.OrdersId', $orderid)
				->where('tracking.QuantityCounter', $qcounter)
				->get();
		return $i;
	}

	function selectSteps(){
		$i = DB::table('tracking_steps')
				->orderBy('Order', 'ASC')
				->get();
		return $i;
	}

	function selectCurrentStep($invoice){
		$i = DB::table('tracking')->select('tracking_steps.*')
				->leftjoin('tracking_steps', 'tracking_steps.Id', '=', 'tracking.StepId')
				->where('tracking.InvoiceNumber', $invoice)
				->orderBy('tracking.StepId', 'DESC')
				->first();
		return $i;
	}


	function selectCurrentStepOrder($stepOrder){
		$i = DB::table('tracking_steps')
				->where('Order', $stepOrder)
				->get();
		return $i;
	}
}
