<?php

class RumahSakit2Client extends BackgroundBaseClient
{
    public const FLAG_HANYA_RUJUKAN = 1;
    public const FLAG_RUJUKAN_DAN_NON_RUJUKAN = 0;

    public function filterResponse($body, $result) {
        //NOTE: Logger harus diset dari controller, karena didapat dari DI.
        parent::filterResponse($body, $result);
        if($this->logger != null) {
            if (!empty($result)) {
                foreach ($result as $item) {
                    if(array_key_exists('status', $item)) {
                        if($item['status'] != 200) {
                            $this->logger->addArray('api_result_', $item);
                        }
                    }
                }
            }
        }
    }

	function getTimestamp() {
	    $defaultTimezone = date_default_timezone_get();
	    date_default_timezone_set('UTC');
	    $result = strval(time() - strtotime('1970-01-01 00:00:00'));
	    if($defaultTimezone != 'UTC') date_default_timezone_set($defaultTimezone);
	    return $result;
    }

    public function getDataRS()
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/dataRS";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"]
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_rs");
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getDataTempatTidur($koders)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/rekapDataTT";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "koders" => $koders
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_data_tempat_tidur");
        $this->setLogParams([
            "koders" => $koders
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getDataBorRs($koders, $tanggal, $flagRujukan)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/borRS";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "koders" => $koders,
                "flag" => $flagRujukan,
                "tanggal" => $tanggal
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_data_bor_rs");
        $this->setLogParams([
            "koders" => $koders,
            "flag" => $flagRujukan,
            "tanggal" => $tanggal
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getSemuaSdmRs($koders, $bulan, $tahun)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/sdmRS";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "koders" => $koders,
                "bulan" => $bulan,
                "tahun" => $tahun
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_data_semua_sdm_rs");
        $this->setLogParams([
            "koders" => $koders,
            "bulan" => $bulan,
            "tahun" => $tahun
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getSdmCovid($koders)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/sdmCovid";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "koders" => $koders
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_data_sdm_covid");
        $this->setLogParams([
            "koders" => $koders
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getDataPelayananRs($koders)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/pelayananRS";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "koders" => $koders
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_data_pelayanan_rs");
        $this->setLogParams([
            "koders" => $koders
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getDataLogistikCovid($koders)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/logistikCovid";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "koders" => $koders
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_data_logistik_covid");
        $this->setLogParams([
            "koders" => $koders
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getPasienCovid($bulan, $tahun)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Pasien/konfirmasi_pasien";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "bulan" => $bulan,
                "tahun" => $tahun
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_pasien_covid");
        $this->setLogParams([
            "bulan" => $bulan,
            "tahun" => $tahun
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getRekapPasienMasuk($tglawal, $tglakhir)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/pasienMasuk";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "tglawal" => $tglawal,
                "tglakhir" => $tglakhir
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_rekan_pasien_masuk");
        $this->setLogParams([
            "tglawal" => $tglawal,
            "tglakhir" => $tglakhir
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getRekapPasienDirawatKomorbid($tglawal, $tglakhir)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/pasienDirawatDenganKomorbid";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "tglawal" => $tglawal,
                "tglakhir" => $tglakhir
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_rekan_pasien_dirawat_dengan_komorbid");
        $this->setLogParams([
            "tglawal" => $tglawal,
            "tglakhir" => $tglakhir
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getRekapPasienDirawatTanpaKomorbid($tglawal, $tglakhir)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/pasienDirawatTanpaKomorbid";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "tglawal" => $tglawal,
                "tglakhir" => $tglakhir
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_rekan_pasien_dirawat_tanpa_komorbid");
        $this->setLogParams([
            "tglawal" => $tglawal,
            "tglakhir" => $tglakhir
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }

    public function getRekapPasienKeluar($tglawal, $tglakhir)
    {
		$timestamp = $this->getTimestamp();

        $method = "GET";
        $path = "/fo/index.php/Webservice_data/pasienKeluar";
        $params = [
            "headers" => [
                "X-rs-ID"   => $this->prop["rsid"],
                "X-Timestamp" => $timestamp,
				"X-Pass" => $this->prop["secret_key"],
                "tglawal" => $tglawal,
                "tglakhir" => $tglakhir
            ]
        ];

        //region LOG Definition
        $this->setApiName("rumahsakit2-get_rekan_pasien_keluar");
        $this->setLogParams([
            "tglawal" => $tglawal,
            "tglakhir" => $tglakhir
        ]);
        //endregion

        $exec = $this->execute($method, $path, $params, false);
        return $exec;
    }
}
