<?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 TarifDokterRsAwalBros extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        $mapHealthcare = [
            'A' => 'PekanBaru',
            'B' => 'Batam',
            'G' => 'UjungBatu',
            'I' => 'Panam',
            'K' => 'AhmadYani',
            'L' => 'ChevronDuri',
        ];

        $file = fopen(public_path("files/Tarif Konsultasi AwalBros 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 = $codeValue;
                  
                    if ($codeValue == $mapValue) {
                        if ($jadwal->nIDSpesialis == 1) {
                            //umum
                            $jadwal->nBiaya =(int)str_replace(",", "", $value[1]) + (int)str_replace(",", "", $value[2]);
                            $jadwal->nBiayaTC = (int)str_replace(",", "", $value[2]);
                            $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[1]) + (int)str_replace(",", "", $value[4]);
                            $jadwal->nBiayaTC = (int)str_replace(",", "", $value[4]);
                            $jadwal->nBiayaATC = (int)str_replace(",", "", $value[7]);
    
                            $jadwal->save();
                        } else if ($jadwal->nIDSpesialis !== 1) {
                            //spesialis
                            $jadwal->nBiaya = (int)str_replace(",", "", $value[1]) + (int)str_replace(",", "", $value[3]);
                            $jadwal->nBiayaTC = (int)str_replace(",", "", $value[3]);
                            $jadwal->nBiayaATC = (int)str_replace(",", "", $value[6]);
    
                            $jadwal->save();
                        }
                    } else {
                        continue;
                    }
                }
            }
    }
}
