<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Validator;
use App\Responses;
use App\Helper;
use App\Models\AItems;
use App\Models\Users;
use App\Models\OrdersAItems;
use App\Models\UnitTypes;

class AItemsController extends Controller
{
    public function view_a_items(Request $req)
    {
		$unitTypes = UnitTypes::listUnitTypes(['is_active' => UnitTypes::IS_ACTIVE, 'is_publish' => UnitTypes::IS_PUBLISH]);
		$data = [
			'unitTypes' => $unitTypes,
			'user' => $req->auth,
		];
        return view('menu_v2.Finance.adtItems', $data);
    }

    /**
	 * API
	 */

	public function api_list_a_items(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']);
			// }
			$filter = [];

			if ($req->auth->role === Users::ROLE_ADMIN) {
				$filter['crt_type'] = AItems::CRT_TYPE_ADMIN;
			} else if ($req->auth->role === Users::ROLE_FINANCE) {
				$filter['crt_type'] = AItems::CRT_TYPE_FINANCE;
			} else {
				$filter['crt_type'] = 99;
			}

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

			$apiResp = Responses::success('success list a_items');
			$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_a_items(Request $req, $id)
	{
		try {
			$now = time();

			$input = [
				'id' => $id,
			];
			$rulesInput = [
				'id' => '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']);
			}

			$a_item = AItems::showAItemsById($id);
			if (count($a_item) < 1) {
				$apiResp = Responses::not_found('a_item not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}
			
			$apiResp = Responses::success('success get detail a_item');
			$apiResp['data'] = $a_item[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_a_items(Request $req)
	{
		try {
			$now = time();

			$input = [
				'name' => $req->name,
				'type' => $req->type,
				'price' => $req->price,
				'status' => $req->status,
				'is_adm_price' => $req->is_adm_price,
			];
			$rulesInput = [
				'name' => 'required|string',
				'type' => 'required|integer|not_in:0',
				'price' => 'required|numeric',
				'status' => 'required|integer|min:0',
				'is_adm_price' => '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']);
			}

			$uniqName = AItems::getByName($req->name);
			if (count($uniqName) > 0) {
				$apiResp = Responses::bad_request('nama additional item sudah terdata');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			if ($req->auth->role === Users::ROLE_ADMIN) {
				$crt_type = AItems::CRT_TYPE_ADMIN;
			} else if ($req->auth->role === Users::ROLE_FINANCE) {
				$crt_type = AItems::CRT_TYPE_FINANCE;
			} else {
				$crt_type = 99;
			}

			DB::beginTransaction();

			$insAItems = [
				'name' => $req->name,
				'type' => $req->type,
				'price' => $req->price,
				'is_active' => $req->status,
				'is_adm_price' => $req->is_adm_price,
				'crt' => $now,
				'crt_by' => $req->auth->uid,
				'crt_type' => $crt_type,
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			$id = AItems::add($insAItems);

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

			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_updt_a_items(Request $req, $id)
	{
		try {
			$now = time();

			$input = [
				'id' => $id,
				'name' => $req->name,
				'type' => $req->type,
				'price' => $req->price,
				'status' => $req->status,
				'is_adm_price' => $req->is_adm_price,
			];
			$rulesInput = [
				'id' => 'required|integer|not_in:0',
				'name' => 'required|string',
				'type' => 'required|integer|not_in:0',
				'price' => 'required|numeric',
				'status' => 'required|integer|min:0',
				'is_adm_price' => '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']);
			}

			$a_item = AItems::showAItemsById($id);
			if (count($a_item) < 1) {
				$apiResp = Responses::not_found('additional item not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			$uniqName = AItems::getByName($req->name);
			if (count($uniqName) > 0) {
				$notSameUser = 1;
				foreach ($uniqName as $key => $row) {
					if ($row->id == $id) {
						$notSameUser = 0;
					}
				}
				if ($notSameUser) {
					$apiResp = Responses::bad_request('nama additional item sudah terdata');
					return (new Response($apiResp, $apiResp['meta']['code']));
				}
			}

			DB::beginTransaction();

			$updtAItems = [
				'name' => $req->name,
				'type' => $req->type,
				'price' => $req->price,
				'is_active' => $req->status,
				'is_adm_price' => $req->is_adm_price,
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			AItems::updt($id, $updtAItems);

			$apiResp = Responses::created('success update additional item');

			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_a_items(Request $req, $id)
	{
		try {
			$now = time();

			$input = [
				'id' => $id,
			];
			$rulesInput = [
				'id' => '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']);
			}

			$a_item = AItems::showAItemsById($id);
			if (count($a_item) < 1) {
				$apiResp = Responses::not_found('additional item not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			AItems::updt($id, [
				'dlt' => $now,
				'dlt_by' => $req->auth->uid,
			]);

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