<?php
class Registrasi_model extends CI_Model {
   public function insert($data)
    {
        $this->db->insert('tb_registrasi',$data);
        return $this->db->insert_id();
    }

	  public function get_all()
    {
        $this->db->select('r.*, p.nama_lengkap, pl.nama_poli, d.nama_dokter');
        $this->db->from('tb_registrasi r');
        $this->db->join('tb_pasien p','r.id_pasien=p.id_pasien');
        $this->db->join('tb_poli pl','r.id_poli=pl.id_poli','left');
        $this->db->join('tb_dokter d','r.id_dokter=d.id_dokter','left');
        return $this->db->get()->result();
    }

	  public function generate_nomor_reservasi() {
        $prefix = 'RSV';
        $date = date('Ymd');
        
        $this->db->where('DATE(created_at)', date('Y-m-d'));
        $this->db->order_by('nomor_reservasi', 'DESC');
        $last = $this->db->get('tb_registrasi')->row();
        
        if ($last && !empty($last->nomor_reservasi)) {
            $last_number = intval(substr($last->nomor_reservasi, -4));
            $next_number = $last_number + 1;
        } else {
            $next_number = 1;
        }
        
        return $prefix . $date . str_pad($next_number, 4, '0', STR_PAD_LEFT);
    }

    public function generate_kode_verifikasi() {
        return strtoupper(substr(md5(uniqid()), 0, 6));
    }

    public function generate_uuid() {
        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff), mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
        );
    }

	
}
