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

class Mcudata 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 datareportmcu()
    {
        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/tampil_hasil_mcu',
                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 downloaddatamcu()
    {

        $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/tampil_hasil_mcu',
            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_Peserta_SehatTindakan."

        ];
        $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', 'Nama Pelaksana Fisik');
            $sheet->setCellValue('B1', 'Kode Registrasi');
            $sheet->setCellValue('C1', 'Tanggal MCU');
            $sheet->setCellValue('D1', 'Nama Pasien');
            $sheet->setCellValue('E1', 'Usia');
            $sheet->setCellValue('F1', 'Kode Paket MCU');
            $sheet->setCellValue('G1', 'Tensi');
            $sheet->setCellValue('H1', 'Tinggi/Berat Badan');
            $sheet->setCellValue('I1', 'Suhu Tubuh');
            $sheet->setCellValue('J1', 'Index Masa Tubuh');
            $sheet->setCellValue('K1', 'Klasifikasi IMT');
            $sheet->setCellValue('L1', 'Kesimpulan Radiologi');
            $sheet->setCellValue('M1', 'Hasil EKG');
            $sheet->setCellValue('N1', 'Kesimpulan Darah');
            $sheet->setCellValue('O1', 'Kesimpulan Urine');
            $sheet->setCellValue('P1', 'Kesimpulan Darah Kimia');
            $sheet->setCellValue('Q1', 'Kesimpulan Faeses');
            $sheet->setCellValue('R1', 'Kesimpulan Tumor Marker');
            $sheet->setCellValue('S1', 'Kesimpulan Lab');
            $sheet->setCellValue('T1', 'Kesimpulan Audio');
            $sheet->setCellValue('U1', 'Keterangan Sehat');
            $sheet->setCellValue('V1', 'Saran MCU');
            $sheet->setCellValue('W1', 'Kesimpulan Fisik');
            $sheet->setCellValue('X1', 'Saran Fisik');
            $sheet->setCellValue('Y1', 'Kesimpulan Treadmill');
            $sheet->setCellValue('Z1', 'Saran Treadmill');
            $sheet->setCellValue('AA1', 'Hasil Spirometri');
            $sheet->setCellValue('AB1', 'Kesimpulan Spirometri');
            $sheet->setCellValue('AC1', 'Sehat');
            $sheet->setCellValue('AD1', 'Assesmen Radiologi');
            $sheet->setCellValue('AE1', 'Hasil Lab');
            $sheet->setCellValue('AF1', 'Keluhan');
            $sheet->setCellValue('AG1', 'Riwayat Penyakit');
            $sheet->setCellValue('AH1', 'Riwayat Operasi');
            $sheet->setCellValue('AI1', 'Riwayat Keluarga');
            $sheet->setCellValue('AJ1', 'Lain-lain');
            $sheet->setCellValue('AK1', 'Kepala');
            $sheet->setCellValue('AL1', 'Tyroid');
            $sheet->setCellValue('AM1', 'JVP');
            $sheet->setCellValue('AN1', 'Visus');
            $sheet->setCellValue('AO1', 'Kaca Mata');
            $sheet->setCellValue('AP1', 'Buta Warna');
            $sheet->setCellValue('AQ1', 'Reflex Mata');
            $sheet->setCellValue('AR1', 'Pupil');
            $sheet->setCellValue('AS1', 'Telinga');
            $sheet->setCellValue('AT1', 'Hidung');
            $sheet->setCellValue('AU1', 'Tenggorokan');
            $sheet->setCellValue('AV1', 'Jantung');
            $sheet->setCellValue('AW1', 'Mulut');
            $sheet->setCellValue('AX1', 'Gigi');
            $sheet->setCellValue('AY1', 'Paru');
            $sheet->setCellValue('AZ1', 'Perut');
            $sheet->setCellValue('BA1', 'Kelenjar');
            $sheet->setCellValue('BB1', 'Alat Kelamin');
            $sheet->setCellValue('BC1', 'Anus Rektum');
            $sheet->setCellValue('BD1', 'Gerak Punggung');
            $sheet->setCellValue('BE1', 'Reflex');
            $sheet->setCellValue('BF1', 'Sensibilitas');
            $sheet->setCellValue('BG1', 'Kode Rekam Medis');
            $sheet->setCellValue('BH1', 'Jenis Kelamin');
            $sheet->setCellValue('BI1', 'Golongan Darah');
            $sheet->setCellValue('BJ1', 'Tempat Lahir');
            $sheet->setCellValue('BK1', 'Tanggal Lahir');
            $sheet->setCellValue('BL1', 'Tanggal Masuk');
            $sheet->setCellValue('BM1', 'Agama');
            $sheet->setCellValue('BN1', 'Pendidikan');
            $sheet->setCellValue('BO1', 'Pekerjaan');
            $sheet->setCellValue('BP1', 'Status Kawin');
            $sheet->setCellValue('BQ1', 'Suku');
            $sheet->setCellValue('BR1', 'Bahasa');
            $sheet->setCellValue('BS1', 'Disabilitas');
            $sheet->setCellValue('BT1', 'Keterangan Disabilitas');
            $sheet->setCellValue('BU1', 'No KTP');
            $sheet->setCellValue('BV1', 'HP');
            $sheet->setCellValue('BW1', 'Telepon');
            $sheet->setCellValue('BX1', 'Alamat');
            $sheet->setCellValue('BY1', 'Kode Pos');
            $sheet->setCellValue('BZ1', 'Provinsi');
            $sheet->setCellValue('CA1', 'Kota');
            $sheet->setCellValue('CB1', 'Kecamatan');
            $sheet->setCellValue('CC1', 'Desa');
            $sheet->setCellValue('CD1', 'RT');
            $sheet->setCellValue('CE1', 'RW');
            $sheet->setCellValue('CF1', 'Kode Pembayaran');
            $sheet->setCellValue('CG1', 'Nama Ayah');
            $sheet->setCellValue('CH1', 'Tanggal Lahir Ayah');
            $sheet->setCellValue('CI1', 'Umur Ayah');
            $sheet->setCellValue('CJ1', 'Pekerjaan Ayah');
            $sheet->setCellValue('CK1', 'Alamat Ayah');
            $sheet->setCellValue('CL1', 'Nama Ibu');
            $sheet->setCellValue('CM1', 'Tanggal Lahir Ibu');
            $sheet->setCellValue('CN1', 'Umur Ibu');
            $sheet->setCellValue('CO1', 'Pekerjaan Ibu');
            $sheet->setCellValue('CP1', 'Alamat Ibu');
            $sheet->setCellValue('CQ1', 'NIP Petugas');
            $sheet->setCellValue('CR1', 'WhatsApp');
            $sheet->setCellValue('CS1', 'Telegram');
            $sheet->setCellValue('CT1', 'BBM');
            $sheet->setCellValue('CU1', 'Line');
            $sheet->setCellValue('CV1', 'Nama PJ');
            $sheet->setCellValue('CW1', 'HP PJ');
            $sheet->setCellValue('CX1', 'Telepon PJ');
            $sheet->setCellValue('CY1', 'No KTP PJ');
            $sheet->setCellValue('CZ1', 'Alamat PJ');
            $sheet->setCellValue('DA1', 'Provinsi PJ');
            $sheet->setCellValue('DB1', 'Kota PJ');
            $sheet->setCellValue('DC1', 'Kecamatan PJ');
            $sheet->setCellValue('DD1', 'Desa PJ');
            $sheet->setCellValue('DE1', 'Pekerjaan PJ');
            $sheet->setCellValue('DF1', 'Alamat Kantor');
            $sheet->setCellValue('DG1', 'Hubungan PJ');
            $sheet->setCellValue('DH1', 'Alergi');
            $sheet->setCellValue('DI1', 'Catatan Alergi');
            $sheet->setCellValue('DJ1', 'Riwayat Penyakit Terdahulu');
            $sheet->setCellValue('DK1', 'Riwayat Penyakit Keluarga');
            $sheet->setCellValue('DL1', 'Kode Pendaftaran');
            $sheet->setCellValue('DM1', 'Cara Masuk');
            $sheet->setCellValue('DN1', 'Tanggal Registrasi');
            $sheet->setCellValue('DO1', 'Jam Registrasi');
            $sheet->setCellValue('DP1', 'Tanggal Keluar');
            $sheet->setCellValue('DQ1', 'Jam Keluar');
            $sheet->setCellValue('DR1', 'Jenis Pasien');
            $sheet->setCellValue('DS1', 'Poliklinik');
            $sheet->setCellValue('DT1', 'NIP Dokter');
            $sheet->setCellValue('DU1', 'Kode Ruang');
            $sheet->setCellValue('DV1', 'Status Periksa');
            $sheet->setCellValue('DW1', 'Total');
            $sheet->setCellValue('DX1', 'Total Pribadi');
            $sheet->setCellValue('DY1', 'Total Penjamin');
            $sheet->setCellValue('DZ1', 'No Antri');
            $sheet->setCellValue('EA1', 'NIP Perawat');
            $sheet->setCellValue('EB1', 'NIP Dokter Tuju');
            $sheet->setCellValue('EC1', 'NIP Update');
            $sheet->setCellValue('ED1', 'Status Izin Pulang');
            $sheet->setCellValue('EE1', 'Status Bayar Pulang');
            $sheet->setCellValue('EF1', 'Status Pulang');
            $sheet->setCellValue('EG1', 'Tanggal Izin Pulang');
            $sheet->setCellValue('EH1', 'Tanggal Bayar Pulang');
            $sheet->setCellValue('EI1', 'Tanggal Pulang');
            $sheet->setCellValue('EJ1', 'NIP Izin Pulang');
            $sheet->setCellValue('EK1', 'NIP Pulang');
            $sheet->setCellValue('EL1', 'Kode Tiket');
            $sheet->setCellValue('EM1', 'Kode Group MCU');
            $sheet->setCellValue('EN1', 'Keterangan');
            $sheet->setCellValue('EO1', 'Catatan');
            $sheet->setCellValue('EP1', 'Status');
            $sheet->setCellValue('EQ1', 'NIP');
            $sheet->setCellValue('ER1', 'Divisi');
            $sheet->setCellValue('ES1', 'Index MCU');
            $sheet->setCellValue('ET1', 'Saran Lab');
            $sheet->setCellValue('EU1', 'Saran Radiologi');
            $sheet->setCellValue('EV1', 'Status Sehat');
            $sheet->setCellValue('EW1', 'Audio Dengar Di');
            $sheet->setCellValue('EX1', 'Kesimpulan EKG');
            $sheet->setCellValue('EY1', 'Saran EKG');
            $sheet->setCellValue('EZ1', 'Kesimpulan Refrakto');
            $sheet->setCellValue('FA1', 'Saran Refrakto');
            $sheet->setCellValue('FB1', 'Audio Daun Telinga Kiri');
            $sheet->setCellValue('FC1', 'Audio Saluran Telinga Kiri');
            $sheet->setCellValue('FD1', 'Audio Gendang Telinga Kiri');
            $sheet->setCellValue('FE1', 'Audio Hantaran Kiri');
            $sheet->setCellValue('FF1', 'Audio Tanda Rinne Kiri');
            $sheet->setCellValue('FG1', 'Audio Telinga Ditutup Kiri');
            $sheet->setCellValue('FH1', 'Audio Tanda Bing Kiri');
            $sheet->setCellValue('FI1', 'Audio Daun Telinga Kanan');
            $sheet->setCellValue('FJ1', 'Audio Saluran Telinga Kanan');
            $sheet->setCellValue('FK1', 'Audio Gendang Telinga Kanan');
            $sheet->setCellValue('FL1', 'Audio Hantaran Kanan');
            $sheet->setCellValue('FM1', 'Audio Tanda Rinne Kanan');
            $sheet->setCellValue('FN1', 'Audio Telinga Ditutup Kanan');
            $sheet->setCellValue('FO1', 'Audio Tanda Bing Kanan');
            $sheet->setCellValue('FP1', 'NIP Dokter Audio');
            $sheet->setCellValue('FQ1', 'NIP Dokter EKG');
            $sheet->setCellValue('FR1', 'NIP Dokter Refrakto');
            $sheet->setCellValue('FS1', 'Hasil Refrakto');
            $sheet->setCellValue('FT1', 'Saran Spirometri');
            $sheet->setCellValue('FU1', 'NIP Dokter Spirometri');
            $sheet->setCellValue('FV1', 'Kesimpulan Telinga Kanan');
            $sheet->setCellValue('FW1', 'Kesimpulan Telinga Kiri');
            $sheet->setCellValue('FX1', 'Saran Audio');
            $sheet->setCellValue('FY1', 'Hasil Treadmill');
            $sheet->setCellValue('FZ1', 'NIP Treadmill');
            $sheet->setCellValue('GA1', 'NIP Echo');
            $sheet->setCellValue('GB1', 'Hasil Echo');
            $sheet->setCellValue('GC1', 'Kesimpulan Echo');
            $sheet->setCellValue('GD1', 'Saran Echo');
            $sheet->setCellValue('GE1', 'Tindakan Lainnya');
            $sheet->setCellValue('GF1', 'Tanggal Pemeriksaan');
            $sheet->setCellValue('GG1', 'NIP Pelaksana');
            $sheet->setCellValue('GH1', 'Keadaan Umum');
            $sheet->setCellValue('GI1', 'Move 1');
            $sheet->setCellValue('GJ1', 'Move 2');
            $sheet->setCellValue('GK1', 'Motorik 1');
            $sheet->setCellValue('GL1', 'Motorik 2');
            $sheet->setCellValue('GM1', 'Verbal 1');
            $sheet->setCellValue('GN1', 'Verbal 2');
            $sheet->setCellValue('GO1', 'Diastolik');
            $sheet->setCellValue('GP1', 'MAP');
            $sheet->setCellValue('GQ1', 'Nadi');
            $sheet->setCellValue('GR1', 'Laju Pernafasan');
            $sheet->setCellValue('GS1', 'SPO2');
            $sheet->setCellValue('GT1', 'Tinggi Badan');
            $sheet->setCellValue('GU1', 'Berat Badan');
            $sheet->setCellValue('GV1', 'Is G');
            $sheet->setCellValue('GW1', 'Is KG');
            $sheet->setCellValue('GX1', 'Status Underweight');
            $sheet->setCellValue('GY1', 'Status Normal');
            $sheet->setCellValue('GZ1', 'Status Overweight');
            $sheet->setCellValue('HA1', 'Status Obese');
            $sheet->setCellValue('HB1', 'Status Baik');
            $sheet->setCellValue('HC1', 'Status Tampak Sakit');
            $sheet->setCellValue('HD1', 'Status Sesak');
            $sheet->setCellValue('HE1', 'Status Pucat');
            $sheet->setCellValue('HF1', 'Status Lemah');
            $sheet->setCellValue('HG1', 'Status Kejang');
            $sheet->setCellValue('HH1', 'Status Kondisi Lainnya');
            $sheet->setCellValue('HI1', 'Deskripsi Kondisi');
            $sheet->setCellValue('HJ1', 'Status Composmentis');
            $sheet->setCellValue('HK1', 'Status Apatis');
            $sheet->setCellValue('HL1', 'Status Somnolen');
            $sheet->setCellValue('HM1', 'Status Soporocoma');
            $sheet->setCellValue('HN1', 'Status Sadar Lainnya');
            $sheet->setCellValue('HO1', 'Deskripsi Sadar');
            $sheet->setCellValue('HP1', 'Lingkar Pinggang');
            $sheet->setCellValue('HQ1', 'Tanggal');
            $sheet->setCellValue('HR1', 'Alamat Pelaksanaan');
            $sheet->setCellValue('HS1', 'PJ Institusi');
            $sheet->setCellValue('HT1', 'PJ RS');
            $sheet->setCellValue('HU1', 'Jumlah MCU');
            $sheet->setCellValue('HV1', 'Kode Dokter');
            $sheet->setCellValue('HW1', 'Nama');
            $sheet->setCellValue('HX1', 'Kode Spesialis');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
                // Kolom A-Z
                $sheet->setCellValue('A' . $row, $item['nm_pelaksana_fisik']);
                $sheet->setCellValue('B' . $row, $item['kd_registrasi']);
                $sheet->setCellValue('C' . $row, $item['tgl_mcu']);
                $sheet->setCellValue('D' . $row, $item['nm_pasien']);
                $sheet->setCellValue('E' . $row, $item['usia']);
                $sheet->setCellValue('F' . $row, $item['kd_paket_mcu']);
                $sheet->setCellValue('G' . $row, $item['tensi']);
                $sheet->setCellValue('H' . $row, $item['tbbb']);
                $sheet->setCellValue('I' . $row, $item['suhu_tubuh']);
                $sheet->setCellValue('J' . $row, $item['index_masa_tubuh']);
                $sheet->setCellValue('K' . $row, $item['klasifikasi_imt']);
                $sheet->setCellValue('L' . $row, $item['kesimpulan_rad']);
                $sheet->setCellValue('M' . $row, $item['hasil_ekg']);
                $sheet->setCellValue('N' . $row, $item['kesimpulan_darah']);
                $sheet->setCellValue('O' . $row, $item['kesimpulan_urine']);
                $sheet->setCellValue('P' . $row, $item['kesimpulan_darah_kimia']);
                $sheet->setCellValue('Q' . $row, $item['kesimpulan_faeses']);
                $sheet->setCellValue('R' . $row, $item['kesimpulan_tumor_marker']);
                $sheet->setCellValue('S' . $row, $item['kesimpulan_lab']);
                $sheet->setCellValue('T' . $row, $item['kesimpulan_audio']);
                $sheet->setCellValue('U' . $row, $item['keterangan_sehat']);
                $sheet->setCellValue('V' . $row, $item['saran_mcu']);
                $sheet->setCellValue('W' . $row, $item['kesimpulan_fisik']);
                $sheet->setCellValue('X' . $row, $item['saran_fisik']);
                $sheet->setCellValue('Y' . $row, $item['kesimpulan_treadmill']);
                $sheet->setCellValue('Z' . $row, $item['saran_treadmill']);

                // Kolom AA-AZ
                $sheet->setCellValue('AA' . $row, $item['hasil_spirometri']);
                $sheet->setCellValue('AB' . $row, $item['kesimpulan_spirometri']);
                $sheet->setCellValue('AC' . $row, $item['sehat']);
                $sheet->setCellValue('AD' . $row, $item['assesmen_rad']);
                $sheet->setCellValue('AE' . $row, $item['hasil_lab']);
                $sheet->setCellValue('AF' . $row, $item['keluhan']);
                $sheet->setCellValue('AG' . $row, $item['riwayat_penyakit']);
                $sheet->setCellValue('AH' . $row, $item['riwayat_operasi']);
                $sheet->setCellValue('AI' . $row, $item['riwayat_keluarga']);
                $sheet->setCellValue('AJ' . $row, $item['lain_lain']);
                $sheet->setCellValue('AK' . $row, $item['kepala']);
                $sheet->setCellValue('AL' . $row, $item['tyroid']);
                $sheet->setCellValue('AM' . $row, $item['jvp']);
                $sheet->setCellValue('AN' . $row, $item['visus']);
                $sheet->setCellValue('AO' . $row, $item['kaca_mata']);
                $sheet->setCellValue('AP' . $row, $item['buta_warna']);
                $sheet->setCellValue('AQ' . $row, $item['reflex_mata']);
                $sheet->setCellValue('AR' . $row, $item['pupil']);
                $sheet->setCellValue('AS' . $row, $item['telinga']);
                $sheet->setCellValue('AT' . $row, $item['hidung']);
                $sheet->setCellValue('AU' . $row, $item['tenggorokan']);
                $sheet->setCellValue('AV' . $row, $item['jantung']);
                $sheet->setCellValue('AW' . $row, $item['mulut']);
                $sheet->setCellValue('AX' . $row, $item['gigi']);
                $sheet->setCellValue('AY' . $row, $item['paru']);
                $sheet->setCellValue('AZ' . $row, $item['perut']);

                // Kolom BA-BZ
                $sheet->setCellValue('BA' . $row, $item['kelenjar']);
                $sheet->setCellValue('BB' . $row, $item['alat_kelamin']);
                $sheet->setCellValue('BC' . $row, $item['anus_rektum']);
                $sheet->setCellValue('BD' . $row, $item['gerak_punggung']);
                $sheet->setCellValue('BE' . $row, $item['reflex']);
                $sheet->setCellValue('BF' . $row, $item['sensibilitas']);
                $sheet->setCellValue('BG' . $row, $item['kd_rekam_medis']);

                // --- LOGIKA BARU UNTUK MENGAMBIL JENIS KELAMIN DARI ALAMAT KANTOR ---
                $jenisKelamin = ''; // Siapkan variabel dengan nilai default kosong
                // Cek apakah field 'alamat_kantor' ada dan tidak kosong
                if (!empty($item['alamat_kantor'])) {
                    // Pisahkan string menjadi beberapa baris berdasarkan karakter newline
                    $lines = preg_split("/\r\n|\n|\r/", $item['alamat_kantor']);

                    // Cek apakah baris ketiga (index 2) ada di dalam hasil pemisahan
                    if (isset($lines[2])) {
                        // Pisahkan baris ketiga berdasarkan karakter '/'
                        $parts = explode('/', $lines[2]);
                        // Ambil bagian pertama (sebelum '/') dan hapus spasi di awal/akhir
                        $jenisKelamin = trim($parts[0]);
                    }
                }
                // Jika setelah proses di atas $jenisKelamin masih kosong,
                // gunakan nilai dari field 'jenis_kelamin' sebagai cadangan
                if (empty($jenisKelamin) && !empty($item['jenis_kelamin'])) {
                    $jenisKelamin = $item['jenis_kelamin'];
                }
                // Masukkan nilai jenis kelamin yang sudah didapat ke sel Excel
                $sheet->setCellValue('BH' . $row, $jenisKelamin);
                // --- AKHIR LOGIKA BARU ---

                $sheet->setCellValue('BI' . $row, $item['kd_gol_darah']);
                $sheet->setCellValue('BJ' . $row, $item['tempat_lahir']);
                $sheet->setCellValue('BK' . $row, $item['tgl_lahir']);
                $sheet->setCellValue('BL' . $row, $item['tgl_masuk']);
                $sheet->setCellValue('BM' . $row, $item['kd_agama']);
                $sheet->setCellValue('BN' . $row, $item['kd_pendidikan']);
                $sheet->setCellValue('BO' . $row, $item['kd_pekerjaan']);
                $sheet->setCellValue('BP' . $row, $item['kd_status_kawin']);
                $sheet->setCellValue('BQ' . $row, $item['kd_suku']);
                $sheet->setCellValue('BR' . $row, $item['kd_bahasa']);
                $sheet->setCellValue('BS' . $row, $item['is_disabilitas']);
                $sheet->setCellValue('BT' . $row, $item['disabilitas']);
                $sheet->setCellValue('BU' . $row, $item['no_ktp']);
                $sheet->setCellValue('BV' . $row, $item['hp']);
                $sheet->setCellValue('BW' . $row, $item['telp']);
                $sheet->setCellValue('BX' . $row, $item['alamat']);
                $sheet->setCellValue('BY' . $row, $item['kd_pos']);
                $sheet->setCellValue('BZ' . $row, $item['kd_prov']);

                // Kolom CA-CZ
                $sheet->setCellValue('CA' . $row, $item['kd_kota']);
                $sheet->setCellValue('CB' . $row, $item['kd_kec']);
                $sheet->setCellValue('CC' . $row, $item['kd_desa']);
                $sheet->setCellValue('CD' . $row, $item['rt']);
                $sheet->setCellValue('CE' . $row, $item['rw']);
                $sheet->setCellValue('CF' . $row, $item['kd_pembayaran']);
                $sheet->setCellValue('CG' . $row, $item['nm_ayah']);
                $sheet->setCellValue('CH' . $row, $item['tgl_lahir_ayah']);
                $sheet->setCellValue('CI' . $row, $item['umur_ayah']);
                $sheet->setCellValue('CJ' . $row, $item['kd_pekerjaan_ayah']);
                $sheet->setCellValue('CK' . $row, $item['alamat_ayah']);
                $sheet->setCellValue('CL' . $row, $item['nm_ibu']);
                $sheet->setCellValue('CM' . $row, $item['tgl_lahir_ibu']);
                $sheet->setCellValue('CN' . $row, $item['umur_ibu']);
                $sheet->setCellValue('CO' . $row, $item['kd_pekerjaan_ibu']);
                $sheet->setCellValue('CP' . $row, $item['alamat_ibu']);
                $sheet->setCellValue('CQ' . $row, $item['nip_petugas']);
                $sheet->setCellValue('CR' . $row, $item['wa']);
                $sheet->setCellValue('CS' . $row, $item['telegram']);
                $sheet->setCellValue('CT' . $row, $item['bbm']);
                $sheet->setCellValue('CU' . $row, $item['line']);
                $sheet->setCellValue('CV' . $row, $item['nm_pj']);
                $sheet->setCellValue('CW' . $row, $item['hp_pj']);
                $sheet->setCellValue('CX' . $row, $item['telp_pj']);
                $sheet->setCellValue('CY' . $row, $item['no_ktp_pj']);
                $sheet->setCellValue('CZ' . $row, $item['alamat_pj']);

                // Kolom DA-DZ
                $sheet->setCellValue('DA' . $row, $item['kd_prov_pj']);
                $sheet->setCellValue('DB' . $row, $item['kd_kota_pj']);
                $sheet->setCellValue('DC' . $row, $item['kd_kec_pj']);
                $sheet->setCellValue('DD' . $row, $item['kd_desa_pj']);
                $sheet->setCellValue('DE' . $row, $item['kd_pekerjaan_pj']);
                $sheet->setCellValue('DF' . $row, $item['alamat_kantor']);
                $sheet->setCellValue('DG' . $row, $item['kd_hubungan_pj']);
                $sheet->setCellValue('DH' . $row, $item['is_alergi']);
                $sheet->setCellValue('DI' . $row, $item['catatan_alergi']);
                $sheet->setCellValue('DJ' . $row, $item['riwayat_penyakit_terdahulu']);
                $sheet->setCellValue('DK' . $row, $item['riwayat_penyakit_keluarga']);
                $sheet->setCellValue('DL' . $row, $item['kd_pendaftaran']);
                $sheet->setCellValue('DM' . $row, $item['kd_cara_masuk']);
                $sheet->setCellValue('DN' . $row, $item['tgl_registrasi']);
                $sheet->setCellValue('DO' . $row, $item['jam_registrasi']);
                $sheet->setCellValue('DP' . $row, $item['tgl_keluar']);
                $sheet->setCellValue('DQ' . $row, $item['jam_keluar']);
                $sheet->setCellValue('DR' . $row, $item['kd_jenis_pasien']);
                $sheet->setCellValue('DS' . $row, $item['kd_poliklinik']);
                $sheet->setCellValue('DT' . $row, $item['nip_dokter']);
                $sheet->setCellValue('DU' . $row, $item['kd_ruang']);
                $sheet->setCellValue('DV' . $row, $item['kd_status_periksa']);
                $sheet->setCellValue('DW' . $row, $item['total']);
                $sheet->setCellValue('DX' . $row, $item['total_pribadi']);
                $sheet->setCellValue('DY' . $row, $item['total_penjamin']);
                $sheet->setCellValue('DZ' . $row, $item['no_antri']);

                // Kolom EA-EZ
                $sheet->setCellValue('EA' . $row, $item['nip_perawat']);
                $sheet->setCellValue('EB' . $row, $item['nip_dokter_tuju']);
                $sheet->setCellValue('EC' . $row, $item['nip_update']);
                $sheet->setCellValue('ED' . $row, $item['status_izin_pulang']);
                $sheet->setCellValue('EE' . $row, $item['status_bayar_pulang']);
                $sheet->setCellValue('EF' . $row, $item['status_pulang']);
                $sheet->setCellValue('EG' . $row, $item['tgl_izin_pulang']);
                $sheet->setCellValue('EH' . $row, $item['tgl_bayar_pulang']);
                $sheet->setCellValue('EI' . $row, $item['tgl_pulang']);
                $sheet->setCellValue('EJ' . $row, $item['nip_izin_pulang']);
                $sheet->setCellValue('EK' . $row, $item['nip_pulang']);
                $sheet->setCellValue('EL' . $row, $item['kode_tiket']);
                $sheet->setCellValue('EM' . $row, $item['kd_group_mcu']);
                $sheet->setCellValue('EN' . $row, $item['keterangan']);
                $sheet->setCellValue('EO' . $row, $item['catatan']);
                $sheet->setCellValue('EP' . $row, $item['status']);
                $sheet->setCellValue('EQ' . $row, $item['nip']);
                $sheet->setCellValue('ER' . $row, $item['divisi']);
                $sheet->setCellValue('ES' . $row, $item['index_mcu']);
                $sheet->setCellValue('ET' . $row, $item['saran_lab']);
                $sheet->setCellValue('EU' . $row, $item['saran_rad']);
                $sheet->setCellValue('EV' . $row, $item['is_sehat']);
                $sheet->setCellValue('EW' . $row, $item['audio_dengar_di']);
                $sheet->setCellValue('EX' . $row, $item['kesimpulan_ekg']);
                $sheet->setCellValue('EY' . $row, $item['saran_ekg']);
                $sheet->setCellValue('EZ' . $row, $item['kesimpulan_refrakto']);

                // Kolom FA-FZ
                $sheet->setCellValue('FA' . $row, $item['saran_refrakto']);
                $sheet->setCellValue('FB' . $row, $item['audio_daun_telinga_kiri']);
                $sheet->setCellValue('FC' . $row, $item['audio_saluran_telinga_kiri']);
                $sheet->setCellValue('FD' . $row, $item['audio_gendang_telinga_kiri']);
                $sheet->setCellValue('FE' . $row, $item['audio_hantaran_kiri']);
                $sheet->setCellValue('FF' . $row, $item['audio_tanda_rinne_kiri']);
                $sheet->setCellValue('FG' . $row, $item['audio_telinga_ditutup_kiri']);
                $sheet->setCellValue('FH' . $row, $item['audio_tanda_bing_kiri']);
                $sheet->setCellValue('FI' . $row, $item['audio_daun_telinga_kanan']);
                $sheet->setCellValue('FJ' . $row, $item['audio_saluran_telinga_kanan']);
                $sheet->setCellValue('FK' . $row, $item['audio_gendang_telinga_kanan']);
                $sheet->setCellValue('FL' . $row, $item['audio_hantaran_kanan']);
                $sheet->setCellValue('FM' . $row, $item['audio_tanda_rinne_kanan']);
                $sheet->setCellValue('FN' . $row, $item['audio_telinga_ditutup_kanan']);
                $sheet->setCellValue('FO' . $row, $item['audio_tanda_bing_kanan']);
                $sheet->setCellValue('FP' . $row, $item['nip_dokter_audio']);
                $sheet->setCellValue('FQ' . $row, $item['nip_dokter_ekg']);
                $sheet->setCellValue('FR' . $row, $item['nip_dokter_refrakto']);
                $sheet->setCellValue('FS' . $row, $item['hasil_refrakto']);
                $sheet->setCellValue('FT' . $row, $item['saran_spirometri']);
                $sheet->setCellValue('FU' . $row, $item['nip_dokter_spirometri']);
                $sheet->setCellValue('FV' . $row, $item['kesimpulan_telinga_kanan']);
                $sheet->setCellValue('FW' . $row, $item['kesimpulan_telinga_kiri']);
                $sheet->setCellValue('FX' . $row, $item['saran_audio']);
                $sheet->setCellValue('FY' . $row, $item['hasil_treadmill']);
                $sheet->setCellValue('FZ' . $row, $item['nip_treadmill']);

                // Kolom GA-GZ
                $sheet->setCellValue('GA' . $row, $item['nip_echo']);
                $sheet->setCellValue('GB' . $row, $item['hasil_echo']);
                $sheet->setCellValue('GC' . $row, $item['kesimpulan_echo']);
                $sheet->setCellValue('GD' . $row, $item['saran_echo']);
                $sheet->setCellValue('GE' . $row, $item['tindakan_lainnya']);
                $sheet->setCellValue('GF' . $row, $item['tgl_pemeriksaan']);
                $sheet->setCellValue('GG' . $row, $item['nip_pelaksana']);
                $sheet->setCellValue('GH' . $row, $item['keadaan_umum']);
                $sheet->setCellValue('GI' . $row, $item['move_1']);
                $sheet->setCellValue('GJ' . $row, $item['move_2']);
                $sheet->setCellValue('GK' . $row, $item['motorik_1']);
                $sheet->setCellValue('GL' . $row, $item['motorik_2']);
                $sheet->setCellValue('GM' . $row, $item['verbal_1']);
                $sheet->setCellValue('GN' . $row, $item['verbal_2']);
                $sheet->setCellValue('GO' . $row, $item['diastolik']);
                $sheet->setCellValue('GP' . $row, $item['map']);
                $sheet->setCellValue('GQ' . $row, $item['nadi']);
                $sheet->setCellValue('GR' . $row, $item['laju_pernafasan']);
                $sheet->setCellValue('GS' . $row, $item['spo2']);
                $sheet->setCellValue('GT' . $row, $item['tinggi_badan']);
                $sheet->setCellValue('GU' . $row, $item['berat_badan']);
                $sheet->setCellValue('GV' . $row, $item['is_g']);
                $sheet->setCellValue('GW' . $row, $item['is_kg']);
                $sheet->setCellValue('GX' . $row, $item['is_underweight']);
                $sheet->setCellValue('GY' . $row, $item['is_normal']);
                $sheet->setCellValue('GZ' . $row, $item['is_overweight']);

                // Kolom HA-HZ
                $sheet->setCellValue('HA' . $row, $item['is_obese']);
                $sheet->setCellValue('HB' . $row, $item['is_baik']);
                $sheet->setCellValue('HC' . $row, $item['is_tampak_sakit']);
                $sheet->setCellValue('HD' . $row, $item['is_sesak']);
                $sheet->setCellValue('HE' . $row, $item['is_pucat']);
                $sheet->setCellValue('HF' . $row, $item['is_lemah']);
                $sheet->setCellValue('HG' . $row, $item['is_kejang']);
                $sheet->setCellValue('HH' . $row, $item['is_kondisi_lainnya']);
                $sheet->setCellValue('HI' . $row, $item['deskripsi_kondisi']);
                $sheet->setCellValue('HJ' . $row, $item['is_composmentis']);
                $sheet->setCellValue('HK' . $row, $item['is_apatis']);
                $sheet->setCellValue('HL' . $row, $item['is_somnolen']);
                $sheet->setCellValue('HM' . $row, $item['is_soporocoma']);
                $sheet->setCellValue('HN' . $row, $item['is_sadar_lainnya']);
                $sheet->setCellValue('HO' . $row, $item['deskripsi_sadar']);
                $sheet->setCellValue('HP' . $row, $item['lingkar_pinggang']);
                $sheet->setCellValue('HQ' . $row, $item['tgl']);
                $sheet->setCellValue('HR' . $row, $item['alamat_pelaksaan']);
                $sheet->setCellValue('HS' . $row, $item['pj_institusi']);
                $sheet->setCellValue('HT' . $row, $item['pj_rs']);
                $sheet->setCellValue('HU' . $row, $item['jumlah_mcu']);
                $sheet->setCellValue('HV' . $row, $item['kd_dokter']);
                $sheet->setCellValue('HW' . $row, $item['nama']);
                $sheet->setCellValue('HX' . $row, $item['kd_spesialis']);
                $row++;
            }

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