<?php

namespace App\Models;

use CodeIgniter\Model;

class ModelPayments extends Model

{
    protected $table = 'payments'; // Nama tabel di database
    protected $primaryKey = 'id_payments'; // Primary key tabel
    protected $allowedFields = ['id_bookings', 'amount', 'payment_method', 'payment_time', 'status']; // Kolom yang dapat diisi

    /**
     * Fungsi untuk mendapatkan semua data lansia
     *
     * @return array
     */
    public function getAllData()
    {
        return $this->select('
                payments.*, 
                bookings.booking_time, bookings.status as booking_status,
                customer.name as customer_name, 
                services.name as service_name, services.price, services.duration_minutes
            ')
            ->join('bookings', 'bookings.id_bookings = payments.id_bookings')
            ->join('customer', 'customer.id_customer = bookings.id_customer')
            ->join('services', 'services.id_service = bookings.id_service')
            ->orderBy('payments.id_payments', 'DESC')
            ->findAll();
    }

    /**
     * Fungsi untuk mendapatkan data lansia berdasarkan id_payments
     *
     * @param int $id_payments
     * @return array|null
     */
    public function getDataById($id_payments)
    {
        return $this->where('id_payments', $id_payments)->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_payments
     * @param array $data
     * @return bool
     */
    public function updateData($id_payments, $data)
    {
        return $this->update($id_payments, $data);
    }

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