<?php

namespace App\Http\Controllers\Owner\Package;

use App\Modules\HtlCampSchedule\Application\Order\MapSchedule;
use Illuminate\Support\Facades\DB;

class CalendarRepository
{
    public function findTourCalendar($idTour, $dateStart, $dateEnd)
    {
        $result = DB::select(
            'SELECT   COUNT(`btp`.`Id_BookingTourPassenger`) AS `pax`,
                    `bt`.`BookingTour_DateStart`
            FROM    `t_booking_tour`                AS `bt` INNER JOIN
                    `t_booking`                     AS `b` ON `b`.`Id_Booking` = `bt`.`Id_Booking` INNER JOIN
                    `t_booking_tour_passenger`      AS `btp` ON `btp`.`Id_BookingTour` = `bt`.`Id_BookingTour`
            WHERE   `bt`.`BookingTour_DateStart` BETWEEN ? AND ? AND
                    `bt`.`Id_Tour` = ? AND
                    `btp`.`BookingTourPassenger_Status` = 2 AND
                    `bt`.`BookingTour_Status` = 2 AND
                    `fn_booking_tour_passenger_check`(
                        `b`.`Booking_Status`,
                        `b`.`Booking_ManuallyConfirmed`,
                        1,
                        1,
                        `btp`.`BookingTourPassenger_PaymentStatus`
                    )
            GROUP BY `bt`.`BookingTour_DateStart`',
            [$dateStart, $dateEnd, $idTour]
        );
        /** append date element to every row */
        foreach ($result as $key => $value) {
            $result[$key]->date = $value->BookingTour_DateStart;
        }
        return new MapSchedule($result);
    }

    public function findTourLock($idTour, $dateStart, $dateEnd)
    {
        return DB::select(
            'SELECT `TourLock_DateStart`, `TourLock_DateEnd`, `TourLock_Remark`
            FROM `t_tour_lock`
            WHERE `Id_Tour` = ?
            AND `TourLock_DateStart` <= ?
            AND `TourLock_DateEnd` >= ?',
            [$idTour, $dateEnd, $dateStart]
        );
    }
}
