<?php
session_start();

include_once 'config.php';
include_once 'function.php';

if (isset($_POST['submit_import']))
{
 
    // Allowed mime types
    $csvMimes = 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', 'text/vcard', 'text/x-vcard', 'text/directory');
    
    // Validate whether selected file is a CSV file
    $ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
    
    if(!empty($_FILES['file']['name']) && (in_array($_FILES['file']['type'], $csvMimes) || $ext == 'vcf')){
        
        // If the file is uploaded
        if(is_uploaded_file($_FILES['file']['tmp_name'])){
            
            $active_event_id = get_active_event_id();
            
            if ($ext == 'vcf') {
                $vcfContent = file_get_contents($_FILES['file']['tmp_name']);
                $cards = explode("BEGIN:VCARD", $vcfContent);
                $tgl = date('d-m-Y');
                $jam = date('H:i');
                foreach ($cards as $card) {
                    if (trim($card) == '') continue;
                    $nama = '';
                    $whatsapp = '';
                    if (preg_match('/FN:(.*)/i', $card, $m_fn)) {
                        $nama = trim($m_fn[1]);
                    } elseif (preg_match('/N:(.*)/i', $card, $m_n)) {
                        $nama = str_replace(';', ' ', trim($m_n[1]));
                    }
                    if (preg_match('/TEL[^:]*:(.*)/i', $card, $m_tel)) {
                        $whatsapp = trim($m_tel[1]);
                    }
                    if ($nama) {
                         $whatsapp = preg_replace('/[^0-9]/', '', $whatsapp);
                         if(substr($whatsapp, 0, 1) === '0') $whatsapp = '62' . substr($whatsapp, 1);
                         else if(substr($whatsapp, 0, 1) === '8') $whatsapp = '628' . substr($whatsapp, 1);
                         $koneksi->query("INSERT INTO comment_id (nama, tgl, jam, whatsapp, tamu, event_id) VALUES ('".$koneksi->real_escape_string($nama)."', '".$tgl."', '".$jam."', '".$koneksi->real_escape_string($whatsapp)."','Umum', '".$active_event_id."')");
                    }
                }
                $qstring = '?status=succ';
            } else {
                // 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
                while(($line = fgetcsv($csvFile)) !== FALSE){
                    // Get row data
                    $nama   = $line[0];
                    $tgl  = $line[1];
                    $jam  = $line[2];
                    $whatsapp = $line[3];
                    $tamu = $line[4];
                    
                    // Insert member data in the database
                    $koneksi->query("INSERT INTO comment_id (nama, tgl, jam, whatsapp, tamu, event_id) VALUES ('".$nama."', '".$tgl."', '".$jam."', '".$whatsapp."','".$tamu."', '".$active_event_id."')");
                }
                
                // Close opened CSV file
                fclose($csvFile);
                
                $qstring = '?status=succ';
            }
        }else{
            $qstring = '?status=err';
        }
    }else{
        $qstring = '?status=invalid_file';
    }
}

// Redirect to the listing page
header("Location: dashboard.php".$qstring);