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

class Dataikbs 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 jadwaloperasiikbs()
    {
        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/d_reg_online/tampil_jadwal_operasi',
                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 downloadjadwaloperasiikbs()
    {

        $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/d_reg_online/tampil_jadwal_operasi',
            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 Operasi IKBS."

        ];
        $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', 'No Index OP');
            $sheet->setCellValue('B1', 'Tanggal OP');
            $sheet->setCellValue('C1', 'Kode Registrasi');
            $sheet->setCellValue('D1', 'Kode Bed');
            $sheet->setCellValue('E1', 'Kode Pembayaran');
            $sheet->setCellValue('F1', 'Kode Ruang OP');
            $sheet->setCellValue('G1', 'NIP Dokter Rencana');
            $sheet->setCellValue('H1', 'Kode Jadwal Mulai');
            $sheet->setCellValue('I1', 'Kode Jadwal Selesai');
            $sheet->setCellValue('J1', 'No Index Tindakan');
            $sheet->setCellValue('K1', 'Mulai Anestesi');
            $sheet->setCellValue('L1', 'Selesai Anestesi');
            $sheet->setCellValue('M1', 'Mulai Tindakan');
            $sheet->setCellValue('N1', 'Selesai Tindakan');
            $sheet->setCellValue('O1', 'Keterangan');
            $sheet->setCellValue('P1', 'Catatan');
            $sheet->setCellValue('Q1', 'Status');
            $sheet->setCellValue('R1', 'Jenis OP');
            $sheet->setCellValue('S1', 'Alergi');
            $sheet->setCellValue('T1', 'Jenis Anestesi');
            $sheet->setCellValue('U1', 'Pendarahan');
            $sheet->setCellValue('V1', 'Transfusi');
            $sheet->setCellValue('W1', 'Jumlah Darah Hilang');
            $sheet->setCellValue('X1', 'Tanggal Insert');
            $sheet->setCellValue('Y1', 'Tanggal Update');
            $sheet->setCellValue('Z1', 'Pencatat Awal');
            $sheet->setCellValue('AA1', 'Pencatat Akhir');
            $sheet->setCellValue('AB1', 'Kode Rekam Medis');
            $sheet->setCellValue('AC1', 'Tanggal Registrasi');
            $sheet->setCellValue('AD1', 'Tanggal Keluar');
            $sheet->setCellValue('AE1', 'Kode Jenis Pasien');
            $sheet->setCellValue('AF1', 'Nama Jenis Pasien');
            $sheet->setCellValue('AG1', 'Nama Poliklinik');
            $sheet->setCellValue('AH1', 'Kode Pembayaran Reg');
            $sheet->setCellValue('AI1', 'Nama Pembayaran Reg');
            $sheet->setCellValue('AJ1', 'Kode Status Periksa');
            $sheet->setCellValue('AK1', 'Nama Status Periksa');
            $sheet->setCellValue('AL1', 'Nama Pasien');
            $sheet->setCellValue('AM1', 'Alamat');
            $sheet->setCellValue('AN1', 'Dokter OP');
            $sheet->setCellValue('AO1', 'Nama Tindakan');
            $sheet->setCellValue('AP1', 'Nama Ruang OP');
            $sheet->setCellValue('AQ1', 'Jam Mulai');
            $sheet->setCellValue('AR1', 'Jam Selesai');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                $sheet->setCellValue('A' . $row, $item['no_index_op']);
                $sheet->setCellValue('B' . $row, $item['tgl_op']);
                $sheet->setCellValue('C' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('D' . $row, $item['kd_bed']);
                $sheet->setCellValue('E' . $row, $item['kd_pembayaran']);
                $sheet->setCellValue('F' . $row, $item['kd_ruang_op']);
                $sheet->setCellValue('G' . $row, $item['nip_dokter_rencana']);
                $sheet->setCellValue('H' . $row, $item['kd_jadwal_mulai']);
                $sheet->setCellValue('I' . $row, $item['kd_jadwal_selesai']);
                $sheet->setCellValue('J' . $row, $item['no_index_tindakan']);
                $sheet->setCellValue('K' . $row, $item['mulai_anestesi']);
                $sheet->setCellValue('L' . $row, $item['selesai_anestesi']);
                $sheet->setCellValue('M' . $row, $item['mulai_tindakan']);
                $sheet->setCellValue('N' . $row, $item['selesai_tindakan']);
                $sheet->setCellValue('O' . $row, $item['keterangan']);
                $sheet->setCellValue('P' . $row, $item['catatan']);
                $sheet->setCellValue('Q' . $row, $item['status']);
                $sheet->setCellValue('R' . $row, $item['jenis_op']);
                $sheet->setCellValue('S' . $row, $item['alergi']);
                $sheet->setCellValue('T' . $row, $item['jenis_anestesi']);
                $sheet->setCellValue('U' . $row, $item['pendarahan']);
                $sheet->setCellValue('V' . $row, $item['transfusi']);
                $sheet->setCellValue('W' . $row, $item['jumlah_darah_hilang']);
                $sheet->setCellValue('X' . $row, $item['tgl_insert']);
                $sheet->setCellValue('Y' . $row, $item['tgl_update']);
                $sheet->setCellValue('Z' . $row, $item['pencatat_awal']);
                $sheet->setCellValue('AA' . $row, $item['pencatat_akhir']);
                $sheet->setCellValue('AB' . $row, $item['kd_rekam_medis']);
                $sheet->setCellValue('AC' . $row, $item['tgl_registrasi']);
                $sheet->setCellValue('AD' . $row, $item['tgl_keluar']);
                $sheet->setCellValue('AE' . $row, $item['kd_jenis_pasien']);
                $sheet->setCellValue('AF' . $row, $item['nm_jenis_pasien']);
                $sheet->setCellValue('AG' . $row, $item['nm_poliklinik']);
                $sheet->setCellValue('AH' . $row, $item['kd_pembayaran_reg']);
                $sheet->setCellValue('AI' . $row, $item['nm_pembayaran_reg']);
                $sheet->setCellValue('AJ' . $row, $item['kd_status_periksa']);
                $sheet->setCellValue('AK' . $row, $item['nm_status_periksa']);
                $sheet->setCellValue('AL' . $row, $item['nm_pasien']);
                $sheet->setCellValue('AM' . $row, $item['alamat']);
                $sheet->setCellValue('AN' . $row, $item['dokter_op']);
                $sheet->setCellValue('AO' . $row, $item['nm_tindakan']);
                $sheet->setCellValue('AP' . $row, $item['nm_ruang_op']);
                $sheet->setCellValue('AQ' . $row, $item['jam_mulai']);
                $sheet->setCellValue('AR' . $row, $item['jam_selesai']);
                $row++;
            }

            // Mengatur header untuk unduhan
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="data_jadwaloperasi_ikbs.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 download() {}
}
