<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Poli extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function index()
    {
        $this->render('registrasi/poli');
    }

    // Get semua poli aktif
    public function get_poli()
    {
        $poli = $this->db
            ->select('poli_id, kode_poli, nama_poli')
            ->where('status_aktif', 1)
            ->order_by('nama_poli', 'ASC')
            ->get('tb_poli')
            ->result();

        $this->output->set_content_type('application/json')->set_output(json_encode($poli));
    }

    // Get dokter berdasarkan poli
    public function get_dokter_by_poli($poli_id = null)
    {
        $this->db->select('d.dokter_id, pt.nama_lengkap as nama_dokter, d.spesialisasi');
        $this->db->from('tb_dokter d');
        $this->db->join('tb_petugas pt', 'd.id_petugas = pt.petugas_id');
        $this->db->join('tb_poli p', 'd.id_poli = p.poli_id');
        $this->db->where('d.status_aktif', 1);
        $this->db->where('pt.status_aktif', 1);

        if ($poli_id && $poli_id != 'all') {
            $this->db->where('d.id_poli', $poli_id);
        }

        $this->db->order_by('pt.nama_lengkap', 'ASC');
        
        $dokter = $this->db->get()->result();

        $this->output->set_content_type('application/json')->set_output(json_encode($dokter));
    }

    // Get antrian poli dengan filter
    public function get_antrian_poli()
    {
        $poli_id = $this->input->get('poli_id');
        $dokter_id = $this->input->get('dokter_id');
        $start_date = $this->input->get('start_date');
        $end_date = $this->input->get('end_date');
        $status = $this->input->get('status');

        $this->db->select('a.*, p.nama_lengkap as nama_pasien, pl.nama_poli, 
                          d.dokter_id, pt.nama_lengkap as nama_dokter, 
                          r.nomor_reservasi, r.tanggal_registrasi, r.jam_registrasi');
        $this->db->from('tb_antrian a');
        $this->db->join('tb_registrasi r', 'a.id_registrasi = r.registrasi_id');
        $this->db->join('tb_pasien p', 'r.id_pasien = p.pasien_id');
        $this->db->join('tb_poli pl', 'a.id_poli = pl.poli_id');
        $this->db->join('tb_dokter d', 'r.id_dokter = d.dokter_id', 'left');
        $this->db->join('tb_petugas pt', 'd.id_petugas = pt.petugas_id', 'left');
        $this->db->where('a.jenis_antrian', 'POLI');

        // Apply filters
        if ($poli_id && $poli_id != 'all') {
            $this->db->where('a.id_poli', $poli_id);
        }

        if ($dokter_id && $dokter_id != 'all') {
            $this->db->where('r.id_dokter', $dokter_id);
        }

        if ($start_date) {
            $this->db->where('DATE(a.created_at) >=', $start_date);
        }

        if ($end_date) {
            $this->db->where('DATE(a.created_at) <=', $end_date);
        }

        if ($status && $status != 'all') {
            $this->db->where('a.status_antrian', $status);
        }

        $this->db->order_by('a.created_at', 'DESC');
        $this->db->order_by('a.nomor_antrian', 'ASC');

        $antrian = $this->db->get()->result();

        $this->output->set_content_type('application/json')->set_output(json_encode($antrian));
    }

    // Panggil antrian poli
    public function panggil_antrian($antrian_id)
    {
        $this->db->trans_begin();

        // Update antrian yang sedang dipanggil sebelumnya (jika ada)
        $this->db
            ->where('status_antrian', 'DIPANGGIL')
            ->where('jenis_antrian', 'POLI')
            ->update('tb_antrian', [
                'status_antrian' => 'MENUNGGU',
                'updated_at' => date('Y-m-d H:i:s')
            ]);

        // Update antrian yang dipanggil
        $updated = $this->db
            ->where('antrian_id', $antrian_id)
            ->where('jenis_antrian', 'POLI')
            ->update('tb_antrian', [
                'status_antrian' => 'DIPANGGIL',
                'waktu_panggil' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);

        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            $response = ['success' => false, 'message' => 'Gagal memanggil antrian'];
        } else {
            $this->db->trans_commit();
            
            // Get updated antrian data
            $antrian = $this->db
                ->select('a.*, p.nama_lengkap, pl.nama_poli')
                ->from('tb_antrian a')
                ->join('tb_registrasi r', 'a.id_registrasi = r.registrasi_id')
                ->join('tb_pasien p', 'r.id_pasien = p.pasien_id')
                ->join('tb_poli pl', 'a.id_poli = pl.poli_id')
                ->where('a.antrian_id', $antrian_id)
                ->get()
                ->row();

            $response = [
                'success' => true,
                'message' => 'Antrian berhasil dipanggil',
                'data' => $antrian
            ];
        }

        $this->output->set_content_type('application/json')->set_output(json_encode($response));
    }

    // Selesaikan antrian poli
    public function selesai_antrian($antrian_id)
    {
        $updated = $this->db
            ->where('antrian_id', $antrian_id)
            ->where('jenis_antrian', 'POLI')
            ->update('tb_antrian', [
                'status_antrian' => 'SELESAI',
                'updated_at' => date('Y-m-d H:i:s')
            ]);

        $response = [
            'success' => (bool)$updated,
            'message' => $updated ? 'Antrian diselesaikan' : 'Gagal menyelesaikan antrian'
        ];

        $this->output->set_content_type('application/json')->set_output(json_encode($response));
    }

    // Batalkan antrian poli
    public function batal_antrian($antrian_id)
    {
        $updated = $this->db
            ->where('antrian_id', $antrian_id)
            ->where('jenis_antrian', 'POLI')
            ->update('tb_antrian', [
                'status_antrian' => 'BATAL',
                'updated_at' => date('Y-m-d H:i:s')
            ]);

        $response = [
            'success' => (bool)$updated,
            'message' => $updated ? 'Antrian dibatalkan' : 'Gagal membatalkan antrian'
        ];

        $this->output->set_content_type('application/json')->set_output(json_encode($response));
    }

    // Get statistik antrian poli
    public function statistik()
    {
        $poli_id = $this->input->get('poli_id');
        $start_date = $this->input->get('start_date') ?: date('Y-m-d');
        $end_date = $this->input->get('end_date') ?: date('Y-m-d');

        $this->db->where('DATE(created_at) >=', $start_date);
        $this->db->where('DATE(created_at) <=', $end_date);
        $this->db->where('jenis_antrian', 'POLI');

        if ($poli_id && $poli_id != 'all') {
            $this->db->where('id_poli', $poli_id);
        }

        $total_antrian = $this->db->count_all_results('tb_antrian');

        // Antrian selesai
        $this->db->where('DATE(created_at) >=', $start_date);
        $this->db->where('DATE(created_at) <=', $end_date);
        $this->db->where('jenis_antrian', 'POLI');
        $this->db->where('status_antrian', 'SELESAI');
        if ($poli_id && $poli_id != 'all') {
            $this->db->where('id_poli', $poli_id);
        }
        $antrian_selesai = $this->db->count_all_results('tb_antrian');

        // Antrian menunggu
        $this->db->where('DATE(created_at) >=', $start_date);
        $this->db->where('DATE(created_at) <=', $end_date);
        $this->db->where('jenis_antrian', 'POLI');
        $this->db->where('status_antrian', 'MENUNGGU');
        if ($poli_id && $poli_id != 'all') {
            $this->db->where('id_poli', $poli_id);
        }
        $antrian_menunggu = $this->db->count_all_results('tb_antrian');

        // Antrian dipanggil
        $this->db->where('DATE(created_at) >=', $start_date);
        $this->db->where('DATE(created_at) <=', $end_date);
        $this->db->where('jenis_antrian', 'POLI');
        $this->db->where('status_antrian', 'DIPANGGIL');
        if ($poli_id && $poli_id != 'all') {
            $this->db->where('id_poli', $poli_id);
        }
        $antrian_dipanggil = $this->db->count_all_results('tb_antrian');

        $response = [
            'total_antrian' => $total_antrian,
            'antrian_selesai' => $antrian_selesai,
            'antrian_menunggu' => $antrian_menunggu,
            'antrian_dipanggil' => $antrian_dipanggil
        ];

        $this->output->set_content_type('application/json')->set_output(json_encode($response));
    }
}
