<?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\LogbookTypes;

class LogbookTypesController extends Controller
{
	/**
	 * View
	 */

	public function view_lgb_types(Request $req)
	{
		$data = [];

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


	/**
	 * API
	 */

	public function api_list_lgb_types(Request $req)
	{
		try {
			$now = time();
			$input = [
				'cptid' => $req->cptid,
			];
			$rulesInput = [
				'cptid' => 'nullable|numeric'
			];

			// 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 ($input['cptid'] != null && $input['cptid'] != 0) {
				$filter['cptid'] = $input['cptid'];
			}

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

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

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

			$lgb_type = LogbookTypes::showLgbType(['lgb_type_id' => $lgb_type_id]);
			if (count($lgb_type) < 1) {
				$apiResp = Responses::not_found('logbook type not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}
			
			$apiResp = Responses::success('success get detail logbook type');
			$apiResp['data'] = $lgb_type[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_lgb_type(Request $req)
	{
		try {
			$now = time();

			$input = [
				'name' => $req->add_name,
				'status' => $req->status,
			];
			$rulesInput = [
				'name' => 'required|string|max:255',
				'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']);
			}

			DB::beginTransaction();

			$insLgbType = [
				'name' => $input['name'],
				'is_active' => $input['status'],
				'crt' => $now,
				'crt_by' => $req->auth->uid,
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			$lgb_type_id = LogbookTypes::addLgbType($insLgbType);

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

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

			$input = [
        'lgb_type_id' => $lgb_type_id,
				'name' => $req->updt_name,
				'status' => $req->status,
			];
			$rulesInput = [
        'lgb_type_id' => 'required|integer|not_in:0',
				'name' => 'required|string|max:255',
				'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']);
			}

			$lgb_type = LogbookTypes::showLgbType(['lgb_type_id' => $lgb_type_id]);
			if (count($lgb_type) < 1) {
				$apiResp = Responses::not_found('logbook type not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			$updtPocket = [
				'name' => $input['name'],
				'is_active' => $input['status'],
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			LogbookTypes::updateLgbType($input['lgb_type_id'], $updtPocket);

			$apiResp = Responses::created('success update logbook type');

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

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

			$lgb_type = LogbookTypes::showLgbType(['lgb_type_id' => $lgb_type_id]);
			if (count($lgb_type) < 1) {
				$apiResp = Responses::not_found('logbook type not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			LogbookTypes::updateLgbType($lgb_type_id, [
				'dlt' => $now,
				'dlt_by' => $req->auth->uid,
			]);

			DB::commit();
			
			$apiResp = Responses::success('success delete logbook type');
			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_selection_lgb_types(Request $req)
	{
		try {
			$now = time();
			$input = [
				'is_active' => $req->is_active,
			];
			$rulesInput = [
				'is_active' => 'nullable|numeric',
			];

			// 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 ($input['is_active'] != null && $input['is_active'] != 0) {
				$filter['is_active'] = $input['is_active'];
			}

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

			$apiResp = Responses::success('success list logbook type');
			$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']));
		}
	}
}
