<?php
session_start();
// include mysql database configuration file
include '../../../config/koneksi.php';
  $fileuploads   = $_POST['file'];
  if (!empty($fileuploads)){
  //mysqli_query($koneksi, "delete from renja ");
 // mysqli_query($koneksi, "delete from sumber_dana");
 //  mysqli_query($koneksi, "delete from urusan");
 //  mysqli_query($koneksi, "delete from skpd");
 //  mysqli_query($koneksi, "delete from bidang_urusan");
 //  mysqli_query($koneksi, "delete from sub_skpd");
 //  mysqli_query($koneksi, "delete from program");
 //  mysqli_query($koneksi, "delete from kegiatan");
 //  mysqli_query($koneksi, "delete from sub_kegiatan");
   //mysqli_query($koneksi, "delete from rekening");
 //  mysqli_query($koneksi, "delete from bidang_skpd");

  
 
    // Allowed mime types
    $fileMimes = array(
        'text/x-comma-separated-values',
        'text/comma-separated-values',
        'application/octet-stream',
        'application/vnd.ms-excel',
        'application/x-csv',
        'text/x-csv',
        'text/csv',
        'application/csv',
        'application/excel',
        'application/vnd.msexcel',
        'text/plain'
    );
 
    // Validate whether selected file is a CSV file
    if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $fileMimes))
    
    {
 
            // Open uploaded CSV file with read-only mode
            $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
 
            // Skip the first line
            fgetcsv($csvFile);
 
            // Parse data from CSV file line by line
             // Parse data from CSV file line by line
            while (($getData = fgetcsv($csvFile, 1000, ";")) !== FALSE)
            {
                if($getData[0]==''){

                }
                else
                {
                                // Get row data
                $no = $getData[0] ;
                $tahun =  $getData[1];

                $kode_urusan = $getData[2] ;
                $urusan = $getData[3];
                
                $kode_skpd = $getData[4] ;
                $skpd = $getData[5];
                
                $kode_sub_skpd = $getData[6] ;
                $sub_skpd = $getData[7];
                
                $kode_bidang_urusan = $getData[8] ;
                $bidang_urusan = $getData[9];

                
                $kode_program = $getData[10] ;
                $program = $getData[11];
             
                $kode_kegiatan = $getData[12];
                $kegiatan = $getData[13];

                $kode_sub_kegiatan = $getData[14] ;
                $sub_kegiatan = $getData[15];
               
                $kode_sumber_dana = $getData[16];
                $sumber_dana = $getData[17];

                $kode_rekening = $getData[18];
                $rekening = $getData[19];

              
                
                

                $rincian = str_replace(',', '.',  $getData[22]) ;
                //echo($rincian);

                $kode_bidang_skpd =$kode_urusan.".". $kode_bidang_urusan ."." . $kode_skpd .".".$kode_sub_skpd;
               

mysqli_query($koneksi, "replace INTO sumber_dana (sumber_dana,kode_sumber_dana) VALUES ('$sumber_dana','$kode_sumber_dana')");
mysqli_query($koneksi, "replace INTO urusan (kode_urusan,urusan) VALUES ('$kode_urusan','$urusan')");
mysqli_query($koneksi, "replace INTO skpd (skpd,kode_skpd) VALUES ('$skpd','$kode_skpd')");
mysqli_query($koneksi, "replace INTO bidang_urusan (bidang_urusan,kode_urusan,kode_bidang_urusan) VALUES ('$bidang_urusan','$kode_urusan','$kode_bidang_urusan')");
mysqli_query($koneksi, "replace INTO sub_skpd (sub_skpd,kode_skpd,kode_sub_skpd) VALUES ('$sub_skpd','$kode_skpd','$kode_sub_skpd')");
mysqli_query($koneksi, "replace INTO program (program,kode_bidang_urusan,kode_program) VALUES ('$program','$kode_bidang_urusan','$kode_program')");
mysqli_query($koneksi, "replace INTO kegiatan (kode_kegiatan,kode_program,kegiatan) VALUES ('$kode_kegiatan','$kode_program','$kegiatan')");
mysqli_query($koneksi, "replace INTO sub_kegiatan (kode_sub_kegiatan,kode_kegiatan,sub_kegiatan) VALUES ('$kode_sub_kegiatan','$kode_kegiatan','$sub_kegiatan')");
//mysqli_query($koneksi, "replace INTO rekening (kode_rekening,rekening) VALUES ('$kode_rekening','$rekening')");
mysqli_query($koneksi, "replace INTO bidang_skpd (kode_bidang_skpd,kode_bidang_urusan,kode_skpd,kode_sub_skpd,kode_urusan) VALUES ('$kode_bidang_skpd','$kode_bidang_urusan','$kode_skpd','$kode_sub_skpd','$kode_urusan')");

mysqli_query($koneksi, "replace INTO renja 
(id_renja
,no
,tahun
,kode_urusan
,urusan
,kode_skpd
,skpd
,kode_sub_skpd
,sub_skpd
,kode_bidang_urusan
,bidang_urusan
,kode_program
,program
,kode_kegiatan
,kegiatan
,kode_sub_kegiatan
,sub_kegiatan
,kode_sumber_dana
,sumber_dana
,kode_rekening
,rekening
,rincian
) 
VALUES 
('$no'
,'$no'
,'$tahun'
,'$kode_urusan'
,'$urusan'
,'$kode_skpd'
,'$skpd'
,'$kode_sub_skpd'
,'$sub_skpd'
,'$kode_bidang_urusan'
,'$bidang_urusan'
,'$kode_program'
,'$program'
,'$kode_kegiatan'
,'$kegiatan'
,'$kode_sub_kegiatan'
,'$sub_kegiatan'
,'$kode_sumber_dana'
,'$sumber_dana'
,'$kode_rekening'
,'$rekening'
,'$rincian'
)");
 
 

                }
            }
 
            // Close opened CSV file
            fclose($csvFile);
 
          //  header("Location: index.php");
            echo "Data Berhasil DiUnggah Ke Database";
         
    }
    else
    {
        echo "Please select valid file";
    }
}
else
{
    echo"Belum Memilih data Untuk Diupload Ke Database ";
}