<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Models\ApiModel;
use ArrayObject;
use Carbon\Carbon;
use Illuminate\Support\Facades\Input;

class ApiController extends Controller
{

	public function login(Request $request)
	{
		$ApiModel 		= new ApiModel;
		$post 			=	$request->all();
		$response 	 = $ApiModel->loginProductionUser($post['username'], $post['password']);
		
		
		if (!$response) {
			return response()->json(['status' => false, 'message' => "Invalid user"], 401);
		}

		$selectTrackingStepLabel 	 = $ApiModel->selectTrackingStepLabel($response[0]->StepId);
		$response[0]->AssignedStep = $selectTrackingStepLabel[0]->StepLabel;
		return response()->json(['status' => true, 'data' => $response[0]], 200);
	}

	public function insert(Request $request)
	{
		$ApiModel 		= new ApiModel;
		$post 			=	$request->json()->all();

		$data = array(
			"StepId" => $post['StepId'],
			"ScannedBy" => $post['ScannedBy'],
			"InvoiceNumber" => $post['invoice'],
			"OrdersId" => $post['ordersId'],
			"ProductId" => $post['productId'],
			"QuantityCounter" => $post['quantityCounter'],
			"Timezone" => $post['timezone'],
			"TimezoneOffset" => date('H:i:s', strtotime($post['timezoneOffset'])),
			"DeviceId" => $post['deviceId'],
			"created_at" => date('Y-m-d H:i:s', strtotime($post['datetime']))
		);

	
		$checkIfTrackExist = $ApiModel->checkIfTrackExist($post['StepId'], $post['productId'], $post['ordersId'], $post['invoice'], $post['quantityCounter']);
		if ($checkIfTrackExist) {
			return response()->json(['status' => false, 'message' => "Already scanned."], 500);
		}

		// $selectNextStep = $ApiModel->selectNextStep($post['invoice']);
	
		// if(($selectNextStep->StepId + 1) != $post['StepId']){
		// 	return response()->json(['status' => false, 'message' => "Your account is not allowed to update this item."], 401);
		// }

		$response 	 = $ApiModel->insertTracking($data);

		if (!$response) {
			return response()->json(['status' => false, 'message' => "Something went wrong."], 500);
		}

		return response()->json(['status' => true, 'message' => 'Successfully updated.'], 201);
	}

	// public function getTrackingStatus()
	// {
	// 	$ApiModel 		= new ApiModel;
	// 	$invoice = Input::get('invoice');
	// 	$response 	 = $ApiModel->getTrackingStatus($invoice);
	// 	return response()->json(['status' => true, 'data' => $response], 200);
	// }

	public function getTrackingStatus()
	{
		$ApiModel 		= new ApiModel;
		$invoice = Input::get('invoice');
		$getStep = Input::get('step');
		

		// $response 	 = $ApiModel->getTrackingStatus($invoice);
		$selectPaymentDetails 	 = $ApiModel->selectPaymentDetails($invoice);
		if (!$selectPaymentDetails) {
			return response()->json(['status' => false, 'message' => "Not found."], 404);
		}

		$selectOrderList 	 = $ApiModel->selectOrderList($selectPaymentDetails[0]->CartKey);
		$getCurrentTrackingSteps 	 = $ApiModel->getCurrentTrackingSteps($invoice);
		$selectPaymentDetails[0]->tracking_steps = $getCurrentTrackingSteps;
		$currentStep 	 = $ApiModel->selectCurrentStep($invoice);
		$selectPaymentDetails[0]->current_step = $currentStep;

		$stp = ($getStep != "") ? $getStep : $currentStep->Order; // check if step
		foreach ($selectOrderList as $k => $order) {
			$table_fields[] = $ApiModel->selectOrderListTableFields($order->CartKey, $order->ProductId, $stp);
			$product_images[] = $ApiModel->selectProductImages($order->ProductId);

			$selectOrderList[$k]->table_fields = $table_fields[$k];
			$selectOrderList[$k]->product_images = $product_images[$k];
		}
		
		return response()->json([
			'status' => true,
			'payment_details' => $selectPaymentDetails[0],
			'order_list' => $selectOrderList
		], 200);
	}

	public function getOrderStatus()
	{
		$ApiModel 		= new ApiModel;
		$invoice = Input::get('invoice');
		$productid = Input::get('productid');
		$orderid = Input::get('orderid');
		$qcounter = Input::get('qcounter');
		
		$getStatus 	 = $ApiModel->getStatus($invoice, $productid, $orderid, $qcounter);
		
		if (!$getStatus) {
			return response()->json(['status' => false, 'data' => ""], 404);
		}

		return response()->json([
			'status' => true,
			'data' => $getStatus[0]
		], 200);
	}

	public function getSteps(){
		$ApiModel 		= new ApiModel;
		$selectSteps 	 = $ApiModel->selectSteps();

		if (!$selectSteps) {
			return response()->json(['status' => false, 'data' => ""], 404);
		}

		return response()->json([
			'status' => true,
			'data' => $selectSteps
		], 200);
		
	}
}
