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

class Pasienbaru extends CI_Controller
{

    public function pasienbaru_data()
    {
        // 1. Dihapus: if (isset($_POST['KD_OPNAME'])) karena memang tidak butuh inputan

        // Mengambil data dari API
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/pasien_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',
            // 2. Dihapus: CURLOPT_POSTFIELDS karena body/inputan kosong sesuai Postman
        ));

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

        // Mengembalikan respons API dalam format JSON
        if ($response) {
            // Cek apakah response benar-benar JSON yang valid dari API-nya
            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 ke View
            }
        } else {
            echo json_encode(['error' => 'Tidak ada data yang ditemukan atau terjadi kesalahan.']);
        }
    }


     public function unduhdatapasienbaru()
    {

        $nm_user = $this->session->userdata('nm_user');
        $kd_user = $this->session->userdata('kd_user');

        // Memanggil API menggunakan cURL
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => '103.255.241.146/api/service_rs/pasien_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',

        ));

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

        //log harian download
        $datai = [
            "NIP_PETUGAS" => $kd_user,
            "CATATAN" => "Akun " . $nm_user . " berhasil download data Pasien Baru Daftar Online."

        ];
        $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 Rekam Medis');
            $sheet->setCellValue('B1', 'Nama Pasien');
            $sheet->setCellValue('C1', 'Jenis Kelamin');
            $sheet->setCellValue('D1', 'Golongan Darah');
            $sheet->setCellValue('E1', 'Tempat Lahir');
            $sheet->setCellValue('F1', 'Tanggal Lahir');
            $sheet->setCellValue('G1', 'Tanggal Masuk');
            $sheet->setCellValue('H1', 'Kode Agama');
            $sheet->setCellValue('I1', 'Kode Pendidikan');
            $sheet->setCellValue('J1', 'Kode Pekerjaan');
            $sheet->setCellValue('K1', 'Status Kawin');
            $sheet->setCellValue('L1', 'Kode Suku');
            $sheet->setCellValue('M1', 'Kode Bahasa');
            $sheet->setCellValue('N1', 'Disabilitas');
            $sheet->setCellValue('O1', 'Jenis Disabilitas');
            $sheet->setCellValue('P1', 'No KTP');
            $sheet->setCellValue('Q1', 'No HP');
            $sheet->setCellValue('R1', 'No Telepon');
            $sheet->setCellValue('S1', 'Alamat');
            $sheet->setCellValue('T1', 'Kode Pos');
            $sheet->setCellValue('U1', 'Kode Provinsi');
            $sheet->setCellValue('V1', 'Kode Kota');
            $sheet->setCellValue('W1', 'Kode Kecamatan');
            $sheet->setCellValue('X1', 'Kode Desa');
            $sheet->setCellValue('Y1', 'RT');
            $sheet->setCellValue('Z1', 'RW');
            $sheet->setCellValue('AA1', 'Kode Pembayaran');

            $sheet->setCellValue('AB1', 'Nama Ayah');
            $sheet->setCellValue('AC1', 'Tgl Lahir Ayah');
            $sheet->setCellValue('AD1', 'Umur Ayah');
            $sheet->setCellValue('AE1', 'Pekerjaan Ayah');
            $sheet->setCellValue('AF1', 'Alamat Ayah');

            $sheet->setCellValue('AG1', 'Nama Ibu');
            $sheet->setCellValue('AH1', 'Tgl Lahir Ibu');
            $sheet->setCellValue('AI1', 'Umur Ibu');
            $sheet->setCellValue('AJ1', 'Pekerjaan Ibu');
            $sheet->setCellValue('AK1', 'Alamat Ibu');

            $sheet->setCellValue('AL1', 'NIP Petugas');
            $sheet->setCellValue('AM1', 'WhatsApp');
            $sheet->setCellValue('AN1', 'Telegram');
            $sheet->setCellValue('AO1', 'BBM');
            $sheet->setCellValue('AP1', 'Line');

            $sheet->setCellValue('AQ1', 'Nama Penanggung Jawab');
            $sheet->setCellValue('AR1', 'HP Penanggung Jawab');
            $sheet->setCellValue('AS1', 'Telp Penanggung Jawab');
            $sheet->setCellValue('AT1', 'No KTP Penanggung Jawab');
            $sheet->setCellValue('AU1', 'Alamat Penanggung Jawab');
            $sheet->setCellValue('AV1', 'Provinsi Penanggung Jawab');
            $sheet->setCellValue('AW1', 'Kota Penanggung Jawab');
            $sheet->setCellValue('AX1', 'Kecamatan Penanggung Jawab');
            $sheet->setCellValue('AY1', 'Desa Penanggung Jawab');
            $sheet->setCellValue('AZ1', 'Pekerjaan Penanggung Jawab');

            $sheet->setCellValue('BA1', 'Alamat Kantor');
            $sheet->setCellValue('BB1', 'Hubungan Penanggung Jawab');
            $sheet->setCellValue('BC1', 'Memiliki Alergi');
            $sheet->setCellValue('BD1', 'Catatan Alergi');
            $sheet->setCellValue('BE1', 'Riwayat Penyakit Terdahulu');
            $sheet->setCellValue('BF1', 'Riwayat Penyakit Keluarga');

            // Isi data
            $row = 2;
            foreach ($data as $item) {
            $sheet->setCellValue('A'.$row, $item['kd_rekam_medis'] ?? '');
            $sheet->setCellValue('B'.$row, $item['nm_pasien'] ?? '');
            $sheet->setCellValue('C'.$row, $item['jenis_kelamin'] ?? '');
            $sheet->setCellValue('D'.$row, $item['kd_gol_darah'] ?? '');
            $sheet->setCellValue('E'.$row, $item['tempat_lahir'] ?? '');
            $sheet->setCellValue('F'.$row, $item['tgl_lahir'] ?? '');
            $sheet->setCellValue('G'.$row, $item['tgl_masuk'] ?? '');
            $sheet->setCellValue('H'.$row, $item['kd_agama'] ?? '');
            $sheet->setCellValue('I'.$row, $item['kd_pendidikan'] ?? '');
            $sheet->setCellValue('J'.$row, $item['kd_pekerjaan'] ?? '');
            $sheet->setCellValue('K'.$row, $item['kd_status_kawin'] ?? '');
            $sheet->setCellValue('L'.$row, $item['kd_suku'] ?? '');
            $sheet->setCellValue('M'.$row, $item['kd_bahasa'] ?? '');
            $sheet->setCellValue('N'.$row, $item['is_disabilitas'] ?? '');
            $sheet->setCellValue('O'.$row, $item['disabilitas'] ?? '');
            $sheet->setCellValue('P'.$row, $item['no_ktp'] ?? '');
            $sheet->setCellValue('Q'.$row, $item['hp'] ?? '');
            $sheet->setCellValue('R'.$row, $item['telp'] ?? '');
            $sheet->setCellValue('S'.$row, $item['alamat'] ?? '');
            $sheet->setCellValue('T'.$row, $item['kd_pos'] ?? '');
            $sheet->setCellValue('U'.$row, $item['kd_prov'] ?? '');
            $sheet->setCellValue('V'.$row, $item['kd_kota'] ?? '');
            $sheet->setCellValue('W'.$row, $item['kd_kec'] ?? '');
            $sheet->setCellValue('X'.$row, $item['kd_desa'] ?? '');
            $sheet->setCellValue('Y'.$row, $item['rt'] ?? '');
            $sheet->setCellValue('Z'.$row, $item['rw'] ?? '');
            $sheet->setCellValue('AA'.$row, $item['kd_pembayaran'] ?? '');

            $sheet->setCellValue('AB'.$row, $item['nm_ayah'] ?? '');
            $sheet->setCellValue('AC'.$row, $item['tgl_lahir_ayah'] ?? '');
            $sheet->setCellValue('AD'.$row, $item['umur_ayah'] ?? '');
            $sheet->setCellValue('AE'.$row, $item['kd_pekerjaan_ayah'] ?? '');
            $sheet->setCellValue('AF'.$row, $item['alamat_ayah'] ?? '');

            $sheet->setCellValue('AG'.$row, $item['nm_ibu'] ?? '');
            $sheet->setCellValue('AH'.$row, $item['tgl_lahir_ibu'] ?? '');
            $sheet->setCellValue('AI'.$row, $item['umur_ibu'] ?? '');
            $sheet->setCellValue('AJ'.$row, $item['kd_pekerjaan_ibu'] ?? '');
            $sheet->setCellValue('AK'.$row, $item['alamat_ibu'] ?? '');

            $sheet->setCellValue('AL'.$row, $item['nip_petugas'] ?? '');
            $sheet->setCellValue('AM'.$row, $item['wa'] ?? '');
            $sheet->setCellValue('AN'.$row, $item['telegram'] ?? '');
            $sheet->setCellValue('AO'.$row, $item['bbm'] ?? '');
            $sheet->setCellValue('AP'.$row, $item['line'] ?? '');

            $sheet->setCellValue('AQ'.$row, $item['nm_pj'] ?? '');
            $sheet->setCellValue('AR'.$row, $item['hp_pj'] ?? '');
            $sheet->setCellValue('AS'.$row, $item['telp_pj'] ?? '');
            $sheet->setCellValue('AT'.$row, $item['no_ktp_pj'] ?? '');
            $sheet->setCellValue('AU'.$row, $item['alamat_pj'] ?? '');
            $sheet->setCellValue('AV'.$row, $item['kd_prov_pj'] ?? '');
            $sheet->setCellValue('AW'.$row, $item['kd_kota_pj'] ?? '');
            $sheet->setCellValue('AX'.$row, $item['kd_kec_pj'] ?? '');
            $sheet->setCellValue('AY'.$row, $item['kd_desa_pj'] ?? '');
            $sheet->setCellValue('AZ'.$row, $item['kd_pekerjaan_pj'] ?? '');

            $sheet->setCellValue('BA'.$row, $item['alamat_kantor'] ?? '');
            $sheet->setCellValue('BB'.$row, $item['kd_hubungan_pj'] ?? '');
            $sheet->setCellValue('BC'.$row, $item['is_alergi'] ?? '');
            $sheet->setCellValue('BD'.$row, $item['catatan_alergi'] ?? '');
            $sheet->setCellValue('BE'.$row, $item['riwayat_penyakit_terdahulu'] ?? '');
            $sheet->setCellValue('BF'.$row, $item['riwayat_penyakit_keluarga'] ?? '');
                $row++;
            }

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

}
