<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Validator;
use Hidehalo\Nanoid\Client as Nanoid;
use Hidehalo\Nanoid\GeneratorInterface as NanoidInterface;
use App\Responses;
use App\Helper;
use App\Models\ConfRates;
use App\Models\Region;
use App\Models\ConfTruckTypes;
use App\Models\Users;

class ConfRateController extends Controller
{
	/**
	 * View
	 */

	public function view_rates(Request $req)
	{
		$data = [
			'lanes' => ConfRates::getLanesActive(),
			'provs' => Region::listProv(),
			'vendors' => Users::getUsersActiveByRole(Users::ROLE_VENDOR),
			'truck_types' => ConfTruckTypes::listTruckTypes(ConfTruckTypes::IS_ACTIVE),
		];

		return view('menu_v1.configs.pricing', $data);
	}


	/**
	 * API
	 */

	public function api_list_rates(Request $req)
	{
		try {
			$now = time();
			$input = [];
			$rulesInput = [];

			// validasi input
			// $isValidInput = Validator::make($input, $rulesInput);
			// if (!$isValidInput->passes()) {
			// 	$apiResp = Responses::bad_input($isValidInput->messages()->first());
			// 	return new Response($apiResp, $apiResp['meta']['code']);
			// }

			$list = ConfRates::listRates();
			foreach ($list as $key => $row) {
				$list[$key]->DT_RowIndex = $key + 1;
				$list[$key]->action = '-';
			}

			$apiResp = Responses::success('success list rates');
			$apiResp['count'] = count($list);
			$apiResp['data'] = $list;
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_show_rate(Request $req, $rid)
	{
		try {
			$now = time();

			$input = [
				'rid' => $rid,
			];
			$rulesInput = [
				'rid' => 'required|integer|not_in:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$rate = ConfRates::showRateById($rid);
			if (count($rate) < 1) {
				$apiResp = Responses::not_found('rate not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}
			
			$apiResp = Responses::success('success get detail rate');
			$apiResp['data'] = $rate[0];
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_add_rate(Request $req)
	{
		try {
			$now = time();

			$input = [
				'origin_prov' => $req->origin_prov,
				// 'origin_city' => $req->origin_city,
				'via' => $req->via,
				'dest_prov' => $req->dest_prov,
				'dest_city' => $req->dest_city,
				'dest_district' => $req->dest_district,
				'vendor' => $req->vendor_id,
				'vehicle_type' => $req->vhc_type_id,
				'fast_time' => $req->fast_time,
				'long_time' => $req->long_time,
				'sell_kg' => $req->sell_kg,
				'buy_kg' => $req->buy_kg,
				'margin_kg' => $req->margin_kg,
				'percent_kg' => $req->percent_kg,
				'sell_cbm' => $req->sell_cbm,
				'buy_cbm' => $req->buy_cbm,
				'margin_cbm' => $req->margin_cbm,
				'percent_cbm' => $req->percent_cbm,
				'sell_ftl' => $req->sell_ftl,
				'buy_ftl' => $req->buy_ftl,
				'margin_ftl' => $req->margin_ftl,
				'percent_ftl' => $req->percent_ftl,
				'status' => $req->status,
			];
			$rulesInput = [
				'origin_prov' => 'required|numeric|min:0',
				// 'origin_city' => 'required|numeric|min:0',
				'via' => 'required|numeric|min:0',
				'dest_prov' => 'required|numeric|min:0',
				'dest_city' => 'required|numeric|min:0',
				'dest_district' => 'nullable|numeric|min:0',
				'vendor' => 'required|integer|not_in:0',
				'vehicle_type' => 'required|integer|not_in:0',
				'fast_time' => 'required|numeric|min:0',
				'long_time' => 'required|numeric|min:0',
				'sell_kg' => 'required|numeric|min:0',
				'buy_kg' => 'required|numeric|min:0',
				'margin_kg' => 'required|numeric|min:0',
				'percent_kg' => 'required|numeric|min:0',
				'sell_cbm' => 'required|numeric|min:0',
				'buy_cbm' => 'required|numeric|min:0',
				'margin_cbm' => 'required|numeric|min:0',
				'percent_cbm' => 'required|numeric|min:0',
				'sell_ftl' => 'required|numeric|min:0',
				'buy_ftl' => 'required|numeric|min:0',
				'margin_ftl' => 'required|numeric|min:0',
				'percent_ftl' => 'required|numeric|min:0',
				'status' => 'required|numeric|min:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$nanoid = new Nanoid();
			$code = $nanoid->formattedId('123456789', 6);

			$uniqCode = ConfRates::getRateByCode($code);
			if (count($uniqCode) > 0) {
				$apiResp = Responses::bad_request('code has been used, try again');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			$insRate = [
				'code' => $code,
				'origin_prov' => $req->origin_prov,
				// 'origin_city' => $req->origin_city,
				'lane' => $req->via,
				'dest_prov' => $req->dest_prov,
				'dest_city' => $req->dest_city,
				'vdr_id' => $req->vendor_id,
				'vhc_type' => $req->vhc_type_id,
				'fast_time' => $req->fast_time,
				'long_time' => $req->long_time,
				'unit_time' => ConfRates::UNIT_DAY,
				'sell_kg' => $req->sell_kg,
				'buy_kg' => $req->buy_kg,
				'margin_kg' => $req->margin_kg,
				'percent_kg' => $req->percent_kg,
				'sell_cbm' => $req->sell_cbm,
				'buy_cbm' => $req->buy_cbm,
				'margin_cbm' => $req->margin_cbm,
				'percent_cbm' => $req->percent_cbm,
				'sell_ftl' => $req->sell_ftl,
				'buy_ftl' => $req->buy_ftl,
				'margin_ftl' => $req->margin_ftl,
				'percent_ftl' => $req->percent_ftl,
				'is_active' => $req->status,
				'crt' => $now,
				'crt_by' => $req->auth->uid,
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			if ($req->dest_district) {
				$insRate['dest_district'] = $req->dest_district;
			}
			$rid = ConfRates::addRate($insRate);

			$apiResp = Responses::created('success add new rate');

			DB::commit();
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_edit_rate(Request $req, $rid)
	{
		try {
			$now = time();

			$input = [
				'rid' => $rid,
				'origin_prov' => $req->origin_prov,
				// 'origin_city' => $req->origin_city,
				'via' => $req->via,
				'dest_prov' => $req->dest_prov,
				'dest_city' => $req->dest_city,
				'dest_district' => $req->dest_district,
				'vendor' => $req->vendor_id,
				'vehicle_type' => $req->vhc_type_id,
				'fast_time' => $req->fast_time,
				'long_time' => $req->long_time,
				'sell_kg' => $req->sell_kg,
				'buy_kg' => $req->buy_kg,
				'margin_kg' => $req->margin_kg,
				'percent_kg' => $req->percent_kg,
				'sell_cbm' => $req->sell_cbm,
				'buy_cbm' => $req->buy_cbm,
				'margin_cbm' => $req->margin_cbm,
				'percent_cbm' => $req->percent_cbm,
				'sell_ftl' => $req->sell_ftl,
				'buy_ftl' => $req->buy_ftl,
				'margin_ftl' => $req->margin_ftl,
				'percent_ftl' => $req->percent_ftl,
				'status' => $req->status,
			];
			$rulesInput = [
				'rid' => 'required|integer|not_in:0',
				'origin_prov' => 'required|numeric|min:0',
				// 'origin_city' => 'required|numeric|min:0',
				'via' => 'required|numeric|min:0',
				'dest_prov' => 'required|numeric|min:0',
				'dest_city' => 'required|numeric|min:0',
				'dest_district' => 'nullable|numeric|min:0',
				'vendor' => 'required|integer|not_in:0',
				'vehicle_type' => 'required|integer|not_in:0',
				'fast_time' => 'required|numeric|min:0',
				'long_time' => 'required|numeric|min:0',
				'sell_kg' => 'required|numeric|min:0',
				'buy_kg' => 'required|numeric|min:0',
				'margin_kg' => 'required|numeric|min:0',
				'percent_kg' => 'required|numeric|min:0',
				'sell_cbm' => 'required|numeric|min:0',
				'buy_cbm' => 'required|numeric|min:0',
				'margin_cbm' => 'required|numeric|min:0',
				'percent_cbm' => 'required|numeric|min:0',
				'sell_ftl' => 'required|numeric|min:0',
				'buy_ftl' => 'required|numeric|min:0',
				'margin_ftl' => 'required|numeric|min:0',
				'percent_ftl' => 'required|numeric|min:0',
				'status' => 'required|numeric|min:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$rate = ConfRates::showRateById($rid);
			if (count($rate) < 1) {
				$apiResp = Responses::not_found('rate not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			$insRate = [
				'origin_prov' => $req->origin_prov,
				// 'origin_city' => $req->origin_city,
				'lane' => $req->via,
				'dest_prov' => $req->dest_prov,
				'dest_city' => $req->dest_city,
				'vdr_id' => $req->vendor_id,
				'vhc_type' => $req->vhc_type_id,
				'fast_time' => $req->fast_time,
				'long_time' => $req->long_time,
				'unit_time' => ConfRates::UNIT_DAY,
				'sell_kg' => $req->sell_kg,
				'buy_kg' => $req->buy_kg,
				'margin_kg' => $req->margin_kg,
				'percent_kg' => $req->percent_kg,
				'sell_cbm' => $req->sell_cbm,
				'buy_cbm' => $req->buy_cbm,
				'margin_cbm' => $req->margin_cbm,
				'percent_cbm' => $req->percent_cbm,
				'sell_ftl' => $req->sell_ftl,
				'buy_ftl' => $req->buy_ftl,
				'margin_ftl' => $req->margin_ftl,
				'percent_ftl' => $req->percent_ftl,
				'is_active' => $req->status,
				'crt' => $now,
				'crt_by' => $req->auth->uid,
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			if ($req->dest_district) {
				$insRate['dest_district'] = $req->dest_district;
			}
			$rid = ConfRates::updateRate($rid, $insRate);

			$apiResp = Responses::created('success add new rate');

			DB::commit();
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_del_rate(Request $req, $rid)
	{
		try {
			$now = time();

			$input = [
				'rid' => $rid,
			];
			$rulesInput = [
				'rid' => 'required|integer|not_in:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$rate = ConfRates::showRateById($rid);
			if (count($rate) < 1) {
				$apiResp = Responses::not_found('rate not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			ConfRates::updateRate($rid, [
				'dlt' => $now,
				'dlt_by' => $req->auth->uid,
			]);

			DB::commit();
			
			$apiResp = Responses::success('success delete rate');
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}
}
