<?php

namespace App\Models;

use CodeIgniter\Model;

class ModelTindakan extends Model
{
    protected $table            = 'tindakan';
    protected $primaryKey       = 'id';
    protected $allowedFields    = [
        'id_kunjungan',
        'id_tindakan',
        'keterangan',
        'created_at'
    ];



    public function getAllData()
    {
        $query = $this->select('
          tindakan.id,
            tindakan.id_kunjungan,
             tindakan.id_tindakan,  
            tindakan.created_at,
            pasien.nama_lengkap,
            master_tindakan.nama_tindakan,
            master_tindakan.tarif,
            tindakan.keterangan
        ')
            ->join('master_tindakan', 'master_tindakan.id_tindakan = tindakan.id_tindakan')
            ->join('kunjungan', 'kunjungan.id_kunjungan = tindakan.id_kunjungan')
            ->join('pasien', 'pasien.id_pasien = kunjungan.id_pasien')
            ->orderBy('tindakan.created_at', 'DESC')
            ->findAll();

        // Kelompokkan berdasarkan id_kunjungan
        $grouped = [];

        foreach ($query as $row) {
            $id = $row['id_kunjungan'];

            if (!isset($grouped[$id])) {
                $grouped[$id] = [
                    'id_kunjungan' => $row['id_kunjungan'],
                    'nama_lengkap' => $row['nama_lengkap'],
                    'created_at'   => $row['created_at'],
                    'detail_tindakan'       => []
                ];
            }

            $grouped[$id]['detail_tindakan'][] = [
                'id_detail'     => $row['id'],
                'id_tindakan'   => $row['id_tindakan'],
                'nama_tindakan' => $row['nama_tindakan'],
                'tarif'         => $row['tarif'],
                'keterangan'    => $row['keterangan']
            ];
        }

        return $grouped;
    }


    /**
     * Mengambil tindakan berdasarkan ID
     */
    public function getDataById($id)
    {
        return $this->where('id', $id)->first();
    }

    /**
     * Menambahkan data tindakan
     */
    public function tambahData($dataInsert)
    {
        // Jika array multidimensi (banyak baris), gunakan insertBatch
        if (isset($dataInsert[0]) && is_array($dataInsert[0])) {
            return $this->insertBatch($dataInsert);
        }

        // Jika hanya satu baris dataInsert
        return $this->insert($dataInsert);
    }

    /**
     * Update data tindakan
     */
    public function updateData($id, $data)
    {
        return $this->update($id, $data);
    }

    /**
     * Hapus data tindakan
     */
    public function deleteData($id)
    {
        return $this->delete($id);
    }

    /**
     * Ambil semua tindakan berdasarkan ID kunjungan tertentu
     */
    public function getByKunjungan($id_kunjungan)
    {
        return $this->select('tindakan.*, master_tindakan.nama_tindakan')
            ->join('master_tindakan', 'master_tindakan.id_tindakan = tindakan.id_tindakan')
            ->where('tindakan.id_kunjungan', $id_kunjungan)
            ->orderBy('tindakan.created_at', 'ASC')
            ->findAll();
    }
}
