<?php

namespace App\Models;

use CodeIgniter\Model;

class ModelBookings extends Model

{
    protected $table = 'bookings'; // Nama tabel di database
    protected $primaryKey = 'id_bookings'; // Primary key tabel
    protected $allowedFields = ['id_customer', 'id_employee', 'id_service', 'booking_time', 'status']; // Kolom yang dapat diisi

    /**
     * Fungsi untuk mendapatkan semua data lansia
     *
     * @return array
     */
    public function getAllData()
    {
        return $this->select('bookings.*, customer.name AS customer_name, employees.name AS employee_name, services.name AS service_name, services.price, services.duration_minutes')
            ->join('customer', 'customer.id_customer = bookings.id_customer')
            ->join('employees', 'employees.id_employees = bookings.id_employee')
            ->join('services', 'services.id_service = bookings.id_service')
            ->orderBy('bookings.id_bookings', 'DESC')
            ->findAll();
    }

    /**
     * Fungsi untuk mendapatkan data lansia berdasarkan id_bookings
     *
     * @param int $id_bookings
     * @return array|null
     */
    public function getDataById($id_bookings)
    {
        return $this->where('id_bookings', $id_bookings)->first();
    }

    /**
     * Fungsi untuk menambahkan data baru
     *
     * @param array $data
     * @return bool
     */
    public function tambahData($data)
    {
        return $this->insert($data);
    }

    /**
     * Fungsi untuk memperbarui data
     *
     * @param int $id_bookings
     * @param array $data
     * @return bool
     */
    public function updateData($id_bookings, $data)
    {
        return $this->update($id_bookings, $data);
    }

    /**
     * Fungsi untuk menghapus data
     *
     * @param int $id_bookings
     * @return bool
     */
    public function deleteData($id_bookings)
    {
        return $this->delete($id_bookings);
    }
}
