<?php

namespace Database\Seeders;

use App\Models\OLDLMS\Healthcare;
use App\Models\OLDLMS\JadwalDokter;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class PricesJadwalDokter extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // $healthcare_primaya = Healthcare::where('nIDHealthCareGroup', 1)->get();
        // PHSM = semarang
        // PHBU = Bekasi Utara
        // PHMA = makasar
        // PHPK = pasar kemis
        // PHPC = cikini
        // PHBW = Bakti wara
        // PEVH = evasari
        // PHTA = tangerang
        // PHSB = sukabumi
        // PHKA = karawang
        // PHBB = bekasi barat
        // PHBT = bekasi timur

        $mapHealthcare = [
            'SG' => 'PHSM',
            'P' => 'PHBU',
            'F' => 'PHMA',
            'PK' => 'PHPK',
            'CK' => 'PHPC',
            'BW' => 'PHBW',
            'E' => 'PEVH',
            'C' => 'PHTA',
            'SK' => 'PHSB',
            'Q' => 'PHKA',
            'D' => 'PHBB',
            'M' => 'PHBT',
        ];

        $file = fopen(public_path("files/Tarif Konsultasi Primaya 2023.csv"), "r");
        $data = [];

        while ($row = fgetcsv($file)) {
            for ($i = 0; $i < count($row); $i++) {
                $data[$i][] = $row[$i];
            }
        }
        fclose($file);


        // foreach ($healthcare_primaya as $healthcare) {
        $jadwalDokter = JadwalDokter::get();
        foreach ($jadwalDokter as $jadwal) {
            $id_healthcare_jadwal = $jadwal->nIDHealthCare;
            $healthcare = Healthcare::where('nID', $id_healthcare_jadwal)->first() ?? null;
            if ($healthcare == null) {
                continue;
            }

            $mapValue = $mapHealthcare[$healthcare->sKodeRS] ?? null;
            if ($mapValue == null) {
                continue;
            }

            foreach ($data as $value) {
                if ($value[0] == "") {
                    continue;
                }
                $codeValue = $value[0];
                $codeValue = substr($codeValue, 1, 4);

                if ($codeValue == $mapValue) {
                    if ($jadwal->nIDSpesialis == 1) {
                        //umum
                        $jadwal->nBiaya = (int)str_replace(",", "", $value[2]) + (int)str_replace(",", "", $value[1]);
                        $jadwal->nBiayaTC = (int)str_replace(",", "", $value[5]);
                        $jadwal->nBiayaATC = (int)str_replace(",", "", $value[5]);

                        $jadwal->save();
                    } else if ($jadwal->nIDSpesialis !== 1 && $jadwal->sIsSubSpesialis == 1) {
                        //sub spesialis
                        $jadwal->nBiaya = (int)str_replace(",", "", $value[4]) + (int)str_replace(",", "", $value[1]);
                        $jadwal->nBiayaTC = (int)str_replace(",", "", $value[7]);
                        $jadwal->nBiayaATC = (int)str_replace(",", "", $value[7]);

                        $jadwal->save();
                    } else if ($jadwal->nIDSpesialis !== 1) {
                        //spesialis
                        $jadwal->nBiaya = (int)str_replace(",", "", $value[3]) + (int)str_replace(",", "", $value[1]);
                        $jadwal->nBiayaTC = (int)str_replace(",", "", $value[6]);
                        $jadwal->nBiayaATC = (int)str_replace(",", "", $value[6]);

                        $jadwal->save();
                    }
                } else {
                    continue;
                }
            }
        }
        // }
    }
}
