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

class Tampiligd extends CI_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     *	- or -
     * 		http://example.com/index.php/welcome/index
     *	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/userguide3/general/urls.html
     */
    public function filter_dataigd()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }

        $akses = $this->session->userdata("kd_akses");
        $app = $this->session->userdata("kd_app");

        // Ambil tanggal awal dari input POST
        $tgl_awal = $_POST['tanggal_awal'];

        // Atur tanggal akhir secara otomatis (misalnya, 1 hari setelah tanggal awal)
        $tgl_akhir = date('Y-m-d', strtotime($tgl_awal));

        // Format tanggal sesuai kebutuhan API
        $tanggal_awal = date('d/m/Y', strtotime($tgl_awal));
        $tanggal_akhir = date('d/m/Y', strtotime($tgl_awal));

        // Siapkan data untuk dikirim ke API
        $datai["TANGGAL_AWAL"] = $tanggal_awal;
        $data["TANGGAL_PERIKSA"] = $tanggal_awal;
        $datae["TANGGAL_AKHIR"] = $tanggal_akhir;


        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd_lama_baru',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awal,
                'TANGGAL_AKHIR' => $tanggal_akhir // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsona =  str_replace('\r\n     ', "---0", $response);
        $jsona =  str_replace('\r\n', "---", $jsona);
        $jsona = json_decode($jsona);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd_sum',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awal,
                'TANGGAL_AKHIR' => $tanggal_akhir // Tambahkan tanggal akhir ke body request
            ),

        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonb =  str_replace('\r\n     ', "---0", $response);
        $jsonb =  str_replace('\r\n', "---", $jsonb);
        $jsonb = json_decode($jsonb);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awal,
                'TANGGAL_AKHIR' => $tanggal_akhir // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonc =  str_replace('\r\n     ', "---0", $response);
        $jsonc =  str_replace('\r\n', "---", $jsonc);
        $jsonc = json_decode($jsonc);


        $data["igdlamabaru"]                = $jsona;
        $data["igdhariinitotal"]               = $jsonb;
        $data["igdtampilhariini"]               = $jsonc;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tgl_awal"]        = $tgl_akhir;
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/dashboardpasienigd', $data);
    }

    public function pasienigdtotal()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AWAL'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/igd_all',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadpasienigdtotal()
    {
        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/igd_all',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download data IGD Total."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);

        $responsee = curl_exec($curl);
        curl_close($curl);

        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Kode Registrasi');
            $sheet->setCellValue('B1', 'Kode Rekam Medis');
            $sheet->setCellValue('C1', 'Kode Pendaftaran');
            $sheet->setCellValue('D1', 'Tanggal Registrasi');
            $sheet->setCellValue('E1', 'Jam Registrasi');
            $sheet->setCellValue('F1', 'Tanggal Keluar');
            $sheet->setCellValue('G1', 'Jam Keluar');
            $sheet->setCellValue('H1', 'Kode Poliklinik');
            $sheet->setCellValue('I1', 'NIP Dokter');
            $sheet->setCellValue('J1', 'Kode Ruang');
            $sheet->setCellValue('K1', 'Total');
            $sheet->setCellValue('L1', 'Total Pribadi');
            $sheet->setCellValue('M1', 'Total Penjamin');
            $sheet->setCellValue('N1', 'NIP Petugas');
            $sheet->setCellValue('O1', 'No Antri');
            $sheet->setCellValue('P1', 'NIP Perawat');
            $sheet->setCellValue('Q1', 'NIP Dokter Tuju');
            $sheet->setCellValue('R1', 'Status Izin Pulang');
            $sheet->setCellValue('S1', 'Status Bayar Pulang');
            $sheet->setCellValue('T1', 'Status Pulang');
            $sheet->setCellValue('U1', 'Tanggal Izin Pulang');
            $sheet->setCellValue('V1', 'Tanggal Pulang');
            $sheet->setCellValue('W1', 'NIP Izin Pulang');
            $sheet->setCellValue('X1', 'NIP Pulang');
            $sheet->setCellValue('Y1', 'Kode Tiket');
            $sheet->setCellValue('Z1', 'Nama Jenis Pasien');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('B' . $row, $item['kd_rekam_medis']);
                $sheet->setCellValue('C' . $row, $item['kd_pendaftaran']);
                $sheet->setCellValue('D' . $row, $item['tgl_registrasi']);
                $sheet->setCellValue('E' . $row, $item['jam_registrasi']);
                $sheet->setCellValue('F' . $row, $item['tgl_keluar']);
                $sheet->setCellValue('G' . $row, $item['jam_keluar']);
                $sheet->setCellValue('H' . $row, $item['kd_poliklinik']);
                $sheet->setCellValue('I' . $row, $item['nip_dokter']);
                $sheet->setCellValue('J' . $row, $item['kd_ruang']);
                $sheet->setCellValue('K' . $row, $item['total']);
                $sheet->setCellValue('L' . $row, $item['total_pribadi']);
                $sheet->setCellValue('M' . $row, $item['total_penjamin']);
                $sheet->setCellValue('N' . $row, $item['nip_petugas']);
                $sheet->setCellValue('O' . $row, $item['no_antri']);
                $sheet->setCellValue('P' . $row, $item['nip_perawat']);
                $sheet->setCellValue('Q' . $row, $item['nip_dokter_tuju']);
                $sheet->setCellValue('R' . $row, $item['status_izin_pulang']);
                $sheet->setCellValue('S' . $row, $item['status_bayar_pulang']);
                $sheet->setCellValue('T' . $row, $item['status_pulang']);
                $sheet->setCellValue('U' . $row, $item['tgl_izin_pulang']);
                $sheet->setCellValue('V' . $row, $item['tgl_pulang']);
                $sheet->setCellValue('W' . $row, $item['nip_izin_pulang']);
                $sheet->setCellValue('X' . $row, $item['nip_pulang']);
                $sheet->setCellValue('Y' . $row, $item['kode_tiket']);
                $sheet->setCellValue('Z' . $row, $item['nm_jenis_pasien']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_pasien_igd_total.xlsx"');
            header('Cache-Control: max-age=0');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }


    public function pasienigdbatal()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AKHIR'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/igd_batal',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadpasienigdbatal()
    {

        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/igd_batal',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download data IGD Batal."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);


        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Kode Registrasi');
            $sheet->setCellValue('B1', 'Kode Rekam Medis');
            $sheet->setCellValue('C1', 'Kode Pendaftaran');
            $sheet->setCellValue('D1', 'Tanggal Registrasi');
            $sheet->setCellValue('E1', 'Jam Registrasi');
            $sheet->setCellValue('F1', 'Tanggal Keluar');
            $sheet->setCellValue('G1', 'Jam Keluar');
            $sheet->setCellValue('H1', 'Kode Poliklinik');
            $sheet->setCellValue('I1', 'NIP Dokter');
            $sheet->setCellValue('J1', 'Kode Ruang');
            $sheet->setCellValue('K1', 'Total');
            $sheet->setCellValue('L1', 'Total Pribadi');
            $sheet->setCellValue('M1', 'Total Penjamin');
            $sheet->setCellValue('N1', 'NIP Petugas');
            $sheet->setCellValue('O1', 'No Antri');
            $sheet->setCellValue('P1', 'NIP Perawat');
            $sheet->setCellValue('Q1', 'NIP Dokter Tuju');
            $sheet->setCellValue('R1', 'Status Izin Pulang');
            $sheet->setCellValue('S1', 'Status Bayar Pulang');
            $sheet->setCellValue('T1', 'Status Pulang');
            $sheet->setCellValue('U1', 'Tanggal Izin Pulang');
            $sheet->setCellValue('V1', 'Tanggal Pulang');
            $sheet->setCellValue('W1', 'NIP Izin Pulang');
            $sheet->setCellValue('X1', 'NIP Pulang');
            $sheet->setCellValue('Y1', 'Kode Tiket');
            $sheet->setCellValue('Z1', 'Nama Jenis Pasien');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('B' . $row, $item['kd_rekam_medis']);
                $sheet->setCellValue('C' . $row, $item['kd_pendaftaran']);
                $sheet->setCellValue('D' . $row, $item['tgl_registrasi']);
                $sheet->setCellValue('E' . $row, $item['jam_registrasi']);
                $sheet->setCellValue('F' . $row, $item['tgl_keluar']);
                $sheet->setCellValue('G' . $row, $item['jam_keluar']);
                $sheet->setCellValue('H' . $row, $item['kd_poliklinik']);
                $sheet->setCellValue('I' . $row, $item['nip_dokter']);
                $sheet->setCellValue('J' . $row, $item['kd_ruang']);
                $sheet->setCellValue('K' . $row, $item['total']);
                $sheet->setCellValue('L' . $row, $item['total_pribadi']);
                $sheet->setCellValue('M' . $row, $item['total_penjamin']);
                $sheet->setCellValue('N' . $row, $item['nip_petugas']);
                $sheet->setCellValue('O' . $row, $item['no_antri']);
                $sheet->setCellValue('P' . $row, $item['nip_perawat']);
                $sheet->setCellValue('Q' . $row, $item['nip_dokter_tuju']);
                $sheet->setCellValue('R' . $row, $item['status_izin_pulang']);
                $sheet->setCellValue('S' . $row, $item['status_bayar_pulang']);
                $sheet->setCellValue('T' . $row, $item['status_pulang']);
                $sheet->setCellValue('U' . $row, $item['tgl_izin_pulang']);
                $sheet->setCellValue('V' . $row, $item['tgl_pulang']);
                $sheet->setCellValue('W' . $row, $item['nip_izin_pulang']);
                $sheet->setCellValue('X' . $row, $item['nip_pulang']);
                $sheet->setCellValue('Y' . $row, $item['kode_tiket']);
                $sheet->setCellValue('Z' . $row, $item['nm_jenis_pasien']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_pasien_igd_batal.xlsx"');
            header('Cache-Control: max-age=0');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }

    public function pasienigddilayani()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AKHIR'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/igd_dilayani',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadpasienigddilayani()
    {

        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/igd_dilayani',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download data IGD Dilayani."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);


        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Kode Registrasi');
            $sheet->setCellValue('B1', 'Kode Rekam Medis');
            $sheet->setCellValue('C1', 'Kode Pendaftaran');
            $sheet->setCellValue('D1', 'Tanggal Registrasi');
            $sheet->setCellValue('E1', 'Jam Registrasi');
            $sheet->setCellValue('F1', 'Tanggal Keluar');
            $sheet->setCellValue('G1', 'Jam Keluar');
            $sheet->setCellValue('H1', 'Kode Poliklinik');
            $sheet->setCellValue('I1', 'NIP Dokter');
            $sheet->setCellValue('J1', 'Kode Ruang');
            $sheet->setCellValue('K1', 'Total');
            $sheet->setCellValue('L1', 'Total Pribadi');
            $sheet->setCellValue('M1', 'Total Penjamin');
            $sheet->setCellValue('N1', 'NIP Petugas');
            $sheet->setCellValue('O1', 'No Antri');
            $sheet->setCellValue('P1', 'NIP Perawat');
            $sheet->setCellValue('Q1', 'NIP Dokter Tuju');
            $sheet->setCellValue('R1', 'Status Izin Pulang');
            $sheet->setCellValue('S1', 'Status Bayar Pulang');
            $sheet->setCellValue('T1', 'Status Pulang');
            $sheet->setCellValue('U1', 'Tanggal Izin Pulang');
            $sheet->setCellValue('V1', 'Tanggal Pulang');
            $sheet->setCellValue('W1', 'NIP Izin Pulang');
            $sheet->setCellValue('X1', 'NIP Pulang');
            $sheet->setCellValue('Y1', 'Kode Tiket');
            $sheet->setCellValue('Z1', 'Nama Jenis Pasien');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('B' . $row, $item['kd_rekam_medis']);
                $sheet->setCellValue('C' . $row, $item['kd_pendaftaran']);
                $sheet->setCellValue('D' . $row, $item['tgl_registrasi']);
                $sheet->setCellValue('E' . $row, $item['jam_registrasi']);
                $sheet->setCellValue('F' . $row, $item['tgl_keluar']);
                $sheet->setCellValue('G' . $row, $item['jam_keluar']);
                $sheet->setCellValue('H' . $row, $item['kd_poliklinik']);
                $sheet->setCellValue('I' . $row, $item['nip_dokter']);
                $sheet->setCellValue('J' . $row, $item['kd_ruang']);
                $sheet->setCellValue('K' . $row, $item['total']);
                $sheet->setCellValue('L' . $row, $item['total_pribadi']);
                $sheet->setCellValue('M' . $row, $item['total_penjamin']);
                $sheet->setCellValue('N' . $row, $item['nip_petugas']);
                $sheet->setCellValue('O' . $row, $item['no_antri']);
                $sheet->setCellValue('P' . $row, $item['nip_perawat']);
                $sheet->setCellValue('Q' . $row, $item['nip_dokter_tuju']);
                $sheet->setCellValue('R' . $row, $item['status_izin_pulang']);
                $sheet->setCellValue('S' . $row, $item['status_bayar_pulang']);
                $sheet->setCellValue('T' . $row, $item['status_pulang']);
                $sheet->setCellValue('U' . $row, $item['tgl_izin_pulang']);
                $sheet->setCellValue('V' . $row, $item['tgl_pulang']);
                $sheet->setCellValue('W' . $row, $item['nip_izin_pulang']);
                $sheet->setCellValue('X' . $row, $item['nip_pulang']);
                $sheet->setCellValue('Y' . $row, $item['kode_tiket']);
                $sheet->setCellValue('Z' . $row, $item['nm_jenis_pasien']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_pasien_igd_batal.xlsx"');
            header('Cache-Control: max-age=0');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }

    public function pasienigdkeinap()
    {
        if (isset($_POST['TANGGAL_AWAL']) && isset($_POST['TANGGAL_AKHIR'])) {
            // Mengambil input tanggal dari form
            $tanggalawal = $_POST['TANGGAL_AWAL'];
            $tanggalakhir = $_POST['TANGGAL_AKHIR'];

            // Mengonversi format tanggal ke dd/mm/yyyy
            $tanggal_awal = date('d/m/Y', strtotime($tanggalawal));
            $tanggal_akhir = date('d/m/Y', strtotime($tanggalakhir));

            // Mengambil data dari API
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => '103.255.241.146/api/service_rs/igd_ke_inap',
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => '',
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => 'POST',
                CURLOPT_POSTFIELDS => array(
                    'TANGGAL_AWAL' => $tanggal_awal,
                    'TANGGAL_AKHIR' => $tanggal_akhir
                ),
            ));

            $response = curl_exec($curl);
            curl_close($curl);

            // Mengembalikan respons API dalam format JSON
            if ($response) {
                // Jika response bukan JSON, kirimkan error
                json_decode($response);
                if (json_last_error() !== JSON_ERROR_NONE) {
                    echo json_encode(['error' => 'Data tidak valid.']);
                } else {
                    echo $response; // Kirimkan kembali data yang benar
                }
            } else {
                echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
            }
        }
    }

    public function downloadpasienigdkeinap()
    {

        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');
        $tanggal_awal = $this->input->get('TANGGAL_AWAL');
        $tanggal_akhir = $this->input->get('TANGGAL_AKHIR');

        // Ubah format tanggal jika diperlukan
        $tanggal_awali = date('d/m/Y', strtotime($tanggal_awal));
        $tanggal_akhiri = date('d/m/Y', strtotime($tanggal_akhir));

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/igd_ke_inap',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awali,
                'TANGGAL_AKHIR' => $tanggal_akhiri
            ),
        ));

        $response = curl_exec($curl);
        curl_close($curl);

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download data IGD ke INAP."

        ];
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => '103.255.241.146/api/service_rs/sinaran_download',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $datai,
        ]);


        if ($response) {
            $data = json_decode($response, true)['response'];

            // Load library PhpSpreadsheet
            //require_once APPPATH . 'third_party/PhpSpreadsheet/src/Bootstrap.php';

            // Load library PhpSpreadsheet
            require_once APPPATH . '../vendor/autoload.php';

            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();

            // Set header kolom
            $sheet->setCellValue('A1', 'Kode Registrasi');
            $sheet->setCellValue('B1', 'Kode Rekam Medis');
            $sheet->setCellValue('C1', 'Kode Pendaftaran');
            $sheet->setCellValue('D1', 'Tanggal Registrasi');
            $sheet->setCellValue('E1', 'Jam Registrasi');
            $sheet->setCellValue('F1', 'Tanggal Keluar');
            $sheet->setCellValue('G1', 'Jam Keluar');
            $sheet->setCellValue('H1', 'Kode Poliklinik');
            $sheet->setCellValue('I1', 'NIP Dokter');
            $sheet->setCellValue('J1', 'Kode Ruang');
            $sheet->setCellValue('K1', 'Total');
            $sheet->setCellValue('L1', 'Total Pribadi');
            $sheet->setCellValue('M1', 'Total Penjamin');
            $sheet->setCellValue('N1', 'NIP Petugas');
            $sheet->setCellValue('O1', 'No Antri');
            $sheet->setCellValue('P1', 'NIP Perawat');
            $sheet->setCellValue('Q1', 'NIP Dokter Tuju');
            $sheet->setCellValue('R1', 'Status Izin Pulang');
            $sheet->setCellValue('S1', 'Status Bayar Pulang');
            $sheet->setCellValue('T1', 'Status Pulang');
            $sheet->setCellValue('U1', 'Tanggal Izin Pulang');
            $sheet->setCellValue('V1', 'Tanggal Pulang');
            $sheet->setCellValue('W1', 'NIP Izin Pulang');
            $sheet->setCellValue('X1', 'NIP Pulang');
            $sheet->setCellValue('Y1', 'Kode Tiket');
            $sheet->setCellValue('Z1', 'Asal PPK');


            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('B' . $row, $item['kd_rekam_medis']);
                $sheet->setCellValue('C' . $row, $item['kd_pendaftaran']);
                $sheet->setCellValue('D' . $row, $item['tgl_registrasi']);
                $sheet->setCellValue('E' . $row, $item['jam_registrasi']);
                $sheet->setCellValue('F' . $row, $item['tgl_keluar']);
                $sheet->setCellValue('G' . $row, $item['jam_keluar']);
                $sheet->setCellValue('H' . $row, $item['kd_poliklinik']);
                $sheet->setCellValue('I' . $row, $item['nip_dokter']);
                $sheet->setCellValue('J' . $row, $item['kd_ruang']);
                $sheet->setCellValue('K' . $row, $item['total']);
                $sheet->setCellValue('L' . $row, $item['total_pribadi']);
                $sheet->setCellValue('M' . $row, $item['total_penjamin']);
                $sheet->setCellValue('N' . $row, $item['nip_petugas']);
                $sheet->setCellValue('O' . $row, $item['no_antri']);
                $sheet->setCellValue('P' . $row, $item['nip_perawat']);
                $sheet->setCellValue('Q' . $row, $item['nip_dokter_tuju']);
                $sheet->setCellValue('R' . $row, $item['status_izin_pulang']);
                $sheet->setCellValue('S' . $row, $item['status_bayar_pulang']);
                $sheet->setCellValue('T' . $row, $item['status_pulang']);
                $sheet->setCellValue('U' . $row, $item['tgl_izin_pulang']);
                $sheet->setCellValue('V' . $row, $item['tgl_pulang']);
                $sheet->setCellValue('W' . $row, $item['nip_izin_pulang']);
                $sheet->setCellValue('X' . $row, $item['nip_pulang']);
                $sheet->setCellValue('Y' . $row, $item['kode_tiket']);
                $sheet->setCellValue('Z' . $row, $item['asal_ppk']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_pasien_igd_keInap.xlsx"');
            header('Cache-Control: max-age=0');

            // Output ke file
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
            $writer->save('php://output');
            exit();
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan.']);
        }
    }

    public function tampildatarekapigd()
    {
        if (!$this->session->userdata('berhasil_loginu')) {
            redirect(''); // Jika tidak login, redirect ke halaman login
        }

        $akses = $this->session->userdata("kd_akses");
        $app = $this->session->userdata("kd_app");

        // Ambil tanggal awal dari input POST
        $tgl_awal = $_POST['tanggal_awal'];
        $tgl_akhir = $_POST['tanggal_akhir'];

        // Atur tanggal akhir secara otomatis (misalnya, 1 hari setelah tanggal awal)
        //$tgl_akhir = date('Y-m-d', strtotime($tgl_awal));

        // Format tanggal sesuai kebutuhan API
        $tanggal_awal = date('d/m/Y', strtotime($tgl_awal));
        $tanggal_akhir = date('d/m/Y', strtotime($tgl_akhir));

        // Siapkan data untuk dikirim ke API
        $datai["TANGGAL_AWAL"] = $tanggal_awal;
        $data["TANGGAL_PERIKSA"] = $tanggal_awal;
        $datae["TANGGAL_AKHIR"] = $tanggal_akhir;


        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/tampil_igd',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => array(
                'TANGGAL_AWAL' => $tanggal_awal,
                'TANGGAL_AKHIR' => $tanggal_akhir // Tambahkan tanggal akhir ke body request
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsona =  str_replace('\r\n     ', "---0", $response);
        $jsona =  str_replace('\r\n', "---", $jsona);
        $jsona = json_decode($jsona);

        $data["tampilrekapigdpasien"]                = $jsona;
        $data["akses"]               = $akses;
        $data["app"]               = $app;
        $data["tgl_awal"]        = $tgl_awal;
        $data["tgl_akhir"]        = $tgl_akhir;
        $this->load->view('dashboard/header', $data);
        $this->load->view('dashboard/datarekapigd', $data);
    }

    public function download() {}
}
