<?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\Users;
use App\Models\Clients;
use App\Models\StaticInsurances;

class StaticInsuranceController extends Controller
{
    public function view_static_insurances(Request $req)
    {
        return view('menu_v1.configs.static_insurances');
    }

    /**
	 * API
	 */

	public function api_list_static_insurances(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 = StaticInsurances::listStaticInsurances();
			foreach ($list as $key => $row) {
				$list[$key]->DT_RowIndex = $key + 1;
				$list[$key]->action = '-';
			}

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

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

			$input = [
				'id' => $id,
				'desc' => $req->desc,
				'amt_percent' => $req->amt_percent,
				// 'status' => $req->status,
			];
			$rulesInput = [
				'id' => 'required|integer|not_in:0',
				'desc' => 'required|string',
				'amt_percent' => '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']);
			}

			$insurance = StaticInsurances::getInsuranceById($id);
			if (count($insurance) < 1) {
				$apiResp = Responses::not_found('insurance not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			$updtInsurance = [
				'desc' => $req->desc,
				'amt_percent' => $req->amt_percent,
				// 'is_active' => $req->status,
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			if ($req->dest_district) {
				$updtInsurance['dest_district'] = $req->dest_district;
			}
			$id = StaticInsurances::updateInsurance($id, $updtInsurance);

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

			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']));
		}
	}
}
