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

/**
 * @property CI_Form_validation $form_validation
 * @property CI_Db $db
 * @property CI_Session $session
 * @property CI_Input $input
 * @property CI_Upload $upload
 * @property CI_Wali_santri_model $wali_santri_model
 * @property CI_Santri_model $santri_model
 * @property CI_Chat_model $chat_model
 */

class Chat extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        is_logged_in();
        $this->load->model('chat_model');
        $this->load->model('wali_santri_model'); // Untuk relasi nama wali jika diperlukan
    }

    // ✅ Fungsi kirim chat (digunakan oleh aplikasi Android misalnya)
    public function sendChat()
    {
        $id_wali_santri = $this->input->post('id_wali_santri');
        $sender = $this->input->post('sender');
        $message = $this->input->post('message');

        if ($id_wali_santri && $sender && $message) {
            $this->chat_model->insertChat($id_wali_santri, $sender, $message);
            echo json_encode(['status' => 'success', 'message' => 'Chat sent']);
        } else {
            echo json_encode(['status' => 'error', 'message' => 'Missing required data']);
        }
    }

    // ✅ Ambil riwayat chat berdasarkan id_wali_santri
    public function getChatByWali($id_wali_santri)
    {
        $chat = $this->chat_model->getChatByWali($id_wali_santri);
        echo json_encode(['status' => 'success', 'data' => $chat]);
    }

    // ✅ Halaman admin untuk melihat dan membalas chat
    public function chatAdmin($id_wali_santri = null)
    {
        $data['title'] = 'Pesan';
        $data['admin'] = $this->db->get_where('admin', ['email' => $this->session->userdata('email')])->row_array();
        $data['chat'] = $this->chat_model->getChatByWali($id_wali_santri);
        $data['id_wali_santri'] = $id_wali_santri;

        $this->load->view('templates/header', $data);
        $this->load->view('templates/sidebar', $data);
        $this->load->view('admin/chat_admin', $data);
        $this->load->view('templates/footer');
    }


    // ✅ Proses kirim chat dari sisi admin (form di web)
    public function sendChatFromAdmin()
    {
        $id_wali_santri = $this->input->post('id_wali_santri');
        $message = $this->input->post('message');
        $sender = 'admin';

        if ($id_wali_santri && $message) {
            $this->chat_model->insertChat($id_wali_santri, $sender, $message);
            // Tambahkan flashdata sebelum redirect
            $this->session->set_flashdata('pesan', '
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                Pesan berhasil dikirim!
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        ');
            redirect('chat/chatadmin/' . $id_wali_santri);
        } else {
            show_error('Input tidak lengkap!');
        }
    }
}
