<?php

function parseData($type, $val) {
    if($type == "course_type") {
        switch ($val) {
            case 'car' :
                $course_type = 'รถยนต์';
            break;
            case 'motocycle' :
                $course_type = 'รถจักรยานยนต์';
            break;
            default :
                $course_type = 'รถยนต์';
            break;
        }
        return $course_type;
    }
    if ($type == "course_gear") {
        switch ($val) {
            case 'auto' :
                $course_gear = 'ออโต้';
            break;
            case 'manual' :
                $course_gear = 'ธรรมดา';
            break;
            case 'all' :
                $course_gear = 'ธรรม + ออโต้';
            break;
            default :
                $course_gear = 'ธรรม + ออโต้';
            break;
        }
        return $course_gear;
    }
}

function formatTimeFromDB($time) {
    $t = explode(':', $time);
    $t = $t[0].":".$t[1];
    return $t;
}

function getEndTimeFromHour($starttime, $hour) {
    $e = getHourDigit($starttime);
    $e = $e + $hour;
    return $e.":00";
}

// function formatDateFromDB($date) {
//     $d = explode('-', $date);
//     $d = $d[2]."/".$d[1]."/".($d[0]+543);
//     return $d;
// }

// function formatDateToDB($date) {
//     $d = explode('/', $date);
//     $d = ($d[2]-543)."-".$d[1]."-".($d[0]);
//     return $d;
// }

function formatDateFromDB($date) {
    $d = explode('-', $date);
    $d = $d[2]."/".$d[1]."/".$d[0];
    return $d;
}

function formatDateToDB($date) {
    $d = explode('/', $date);
    $d = $d[2]."-".$d[1]."-".($d[0]);
    return $d;
}

function formatDateTHFromDB($date) {
    $d = explode('-', $date);
    $d = $d[2]."/".$d[1]."/".($d[0] + 543);
    return $d;
}

function calAge($birthDate) {
    $from = new DateTime($birthDate);
    $to   = new DateTime('today');
    return $from->diff($to)->y;
}

function getGender($title) {
    if($title == "นาย") return "ชาย";
    else return "หญิง";
}

function getHourDigit($time) {
    $t = explode(':', $time);
    return $t[0];
}

function calHour($start, $end) {
    $s = explode(':', $start);
    $e = explode(':', $end);
    return $e[0] - $s[0];
}

function dayDiffFromNow($date) {
    $d1 = date_create(date("Y-m-d H:i:s"));
    $d2=date_create($date);
    $diff=date_diff($d1,$d2);
    return $diff->format('%a');
}

function alertPayment($testDate, $remainPayment) {
    if( ( ($testDate!="") && dayDiffFromNow($testDate) <= '3') && ($remainPayment > '0') ) {
        return true;
    } else {
        return false;
    }
}

function showDash($data) {
    if($data=="" || $data==null) {
        return "-";
    } else {
        return $data;
    }
}

function getBlock($start, $end) {
    $arrStartBlock = ['7'=>'1', '8'=>'3', '9'=>'5', '10'=>'7', '11'=>'9', '12'=>'11', '13'=>'13', '14'=>'15', '15'=>'17', '16'=>'19', '17'=>'21', '18'=>'23', '19'=>'25', '20'=>'27'];
    $arrEndBlock = ['7'=>'0', '8'=>'2', '9'=>'4', '10'=>'6', '11'=>'8', '12'=>'10', '13'=>'12', '14'=>'14', '15'=>'16', '16'=>'18', '17'=>'20', '18'=>'22', '19'=>'24', '20'=>'26'];
    return $arrStartBlock[getHourDigit($start)];
}

function getDateBetween2Date($d1, $d2) {
    $array = array();
    $Variable1 = strtotime($d1);
    $Variable2 = strtotime($d2);
    for ($currentDate = $Variable1; $currentDate <= $Variable2; $currentDate += (86400)) {
        $Store = date('Y-m-d', $currentDate);
        $array[] = $Store;
    }
    return $array;
}

function getFirstdayOfWeek($date) {
    $firstday = date($date, strtotime("this week"));
    return $firstday;
}

function getLastdayOfWeek($firstday) {
    $date = strtotime($firstday);
    $lastday = strtotime("+6 day", $date);
    $lastday = date('Y-m-d', $lastday);
    return $lastday;
}

function showDateToCalendar($date) {
    $d = explode('-', $date);
    $d = $d[2]."/".$d[1];
    return $d;
}

// function getFirstname($name) {
//     $f = explode(' ', $name);
//     return $f[0];
// }

function getFirstname($name) {
    $f = explode(',', $name);
    $no = count($f);
    $n = '';

    if($no>1){
        for($i=0; $i<$no; $i++) {
            $fname = explode(' ', $f[$i]);
            $fname = $fname[0];
            if($i==0) {
                $n .= $fname;
            } else {
                $n .= ' , '.$fname;
            }
        }
    } else {
        $fname = explode(' ', $f[0]);
        $n = $fname[0];
    }
    return $n;
}

function check_cancel_booking($time) {
    $minHour = 0;   // min hour can cancel booking
    $now = date('H');
    $t = explode(':', $time);
    $t = $t[0];
    if( ($t > $now) && ( ($t - $now)>=$minHour ) ) {
        return true;
    } else {
        return false;
    }
}

function getModule($value) {
    $t =  explode('-', $value);
    $module = $t[0];
    return $module;
}

function getModuleDrive($value) {
    $t =  explode('-', $value);
    $module = $t[2];
    return $module;
}

function printDate($date) {
    $month_arr=array("ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
    $d = explode('-', $date);
    $m = $d[1] - 1;
    $d = (int)$d[2] . " " . $month_arr[$m] . " " . ($d[0] + 543);
    return $d;
}

function getTime($datetime) {
    $tmp = explode(" ", $datetime);
    $time = $tmp[1];
    return $time;
}

function numberOfStudent($data) {
    $tmp = explode('%^', $data);
    $no = count($tmp);
    return $no;
}

function getStudentAtN($data, $noOfStudent) {
    $tmp = explode('%^', $data);
    $student = $tmp[$noOfStudent];
    return $student;
}

function getVehicleBookingAtN($data, $noOfStudent) {
    $tmp = explode('%^', $data);
    $id = $tmp[$noOfStudent];
    return $id;
}

function getTeacherName($arr, $id) {
    $teacher = '';
    for ($i=0; $i<count($arr); $i++) {
        if($id==$arr[$i]['id']) {
            $teacher = $arr[$i]['firstname'] . " " . $arr[$i]['lastname'];
            break;
        }
    }
    return $teacher;
}

function debug_to_console($data) {
    $output = $data;
    if (is_array($output))
        $output = implode(',', $output);

    echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}

function getDayAndTime($date) {
    $tmp = explode(' ', $date);
    $time = $tmp[1];
    $day = explode('-', $tmp[0]);
    $day = ltrim($day[2], '0');
    return 'วันที่ ' . $day . ' , เวลา ' . $time;
}

function getMontTH($value) {
    $month_arr=array("ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
    return $month_arr[$value];
}

function getGearAbbr($value) {
    if($value=='ออโต้') {
        return 'a';
    }
    else if($value=='ธรรมดา') {
        return 'm';
    }
    else {
        return '0';
    }
}

function getTeacherType($value) {
    $type = "";
    if($value=="a") {
        $type =  "ทั้งสอนและสแกน";
    }
    if($value=="t") {
        $type =  "สอนอย่างเดียว";
    }
    if($value=="s") {
        $type =  "สแกนอย่างเดียว";
    }
    return $type;
}