<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

// use components
use	Config,
	DB,
	Session,
	Storage,
	Carbon\Carbon,
	App\Helper;

// use services
use App\Services\SharedInterface,
	App\Services\MasterInterface,
	App\Services\BookingInterface;

// use emuns
use App\Enums\SortStatusType;

class StatusController extends Controller
{
	protected
		$shared,
		$master,
		$booking;

	public function __construct(
		SharedInterface $shared,
		MasterInterface $master,
		BookingInterface $booking
	) {
		parent::__construct();

		$this->shared = $shared;
		$this->master = $master;
		$this->booking = $booking;
	}

	/**
	 * Display a listing of the resource.
	 */
	public function index(int $yy = null, int $mm = null, int $dd = null, int $pm = null, int $sort = null)
	{
		// initialized
		$data = [];

		// get parameeters
		$data = [
			'yy' => (is_null($yy)) ? date('Y') : $yy,
			'mm' => (is_null($mm)) ? date('n') : $mm,
			'dd' => (is_null($dd)) ? date('j') : $dd,
			'pm' => (is_null($pm)) ? 0 : $pm,
			'sort' => (is_null($sort)) ? 2 : $sort,
		];
		// dd($data);

		// get date
		$data['date'] = [
			'present' => Helper::getDate(Carbon::create($data['yy'], $data['mm'], $data['dd'])->toDateString()),
			'prev' => Helper::getDate(Carbon::create($data['yy'], $data['mm'], $data['dd'])->subDay(1)->toDateString()),
			'next' => Helper::getDate(Carbon::create($data['yy'], $data['mm'], $data['dd'])->addDay(1)->toDateString()),
		];
		// dd($data['date']);

		$data['nav'] = '';

		$data['title'] = '';

		// dd($data);
		return view('admin.status.index', [
			'data' => $data,
		]);
	}

	/**
	 * Show the form for creating a new resource.
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 */
	public function store(Request $request)
	{
		// initialized
		$data = [];

		// get request
		$data = $request->all();
		// dd($data);

		/**
		 * --------------------------------------------------
		 * transaction
		 * --------------------------------------------------
		 */
		DB::transaction(function() use($data) {
			// dd($data);

			$this->booking->storeBookingStatus($data);
		});
		// dd($data);

		// redirect
		return to_route('admin.status.index', [
			$data['booking']['yy'],
			$data['booking']['mm'],
			$data['booking']['dd'],
			$data['booking']['pm'],
		]);
	}

	/**
	 * Display the specified resource.
	 */
	public function show(int $id)
	{
		// 
	}

	/**
	 * Show the form for editing the specified resource.
	 */
	public function edit(int $yy = null, int $mm = null, int $dd = null, int $pm = null, string $dtb_booking_code = null)
	{
		// initialized
		$data = [];

		// get parameeters
		$data = [
			'yy' => $yy,
			'mm' => $mm,
			'dd' => $dd,
			'pm' => $pm,
			'dtb_booking_code' => $dtb_booking_code,
		];
		// dd($data);

		// get booking
		$data['booking'] = $this->booking->getBooking(
			$keys = [
				['dtb_booking.dtb_booking_code', '=', $data['dtb_booking_code']],
			]
		);
		// dd($data['booking']);

		// set form
		$data['form'] = route('admin.status.store');

		$data['nav'] = 'home';

		$data['title'] = '';

		// dd($data);
		return view('admin.status.edit', [
			'data' => $data,
		]);
	}

	/**
	 * Update the specified resource in storage.
	 */
	public function update(Request $request, string $id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 */
	public function destroy(string $id)
	{
		//
	}
}
