<?php
include_once 'header.php';

//$func->prnt($func->post(),true);
//$func->prnt($session->get('cart_info'),true);
if($_SERVER['REQUEST_METHOD']=='POST') {
    $payment_info = array(
        'name' =>$func->post('name'),
        'lastname' =>$func->post('lastname'),
        'telephone' =>$func->post('telephone'),
        'tc_no' =>$func->post('tc_no'),
        'email' =>$func->post('email'),
    );
    $total = 0;
    $session->set('payment_info',$payment_info);
    $quantity = count($session->get('cart_info'));
    foreach ($session->get('cart_info') as $item) {

        if($item['kart_type']=='adult'){
            $price = ($session->get('coupon') && $session->get('coupon_type'))?CHILD_PRICE:ADULT_PRICE;
        }else{
            $price = CHILD_PRICE;
        }
        $total += $price;
    }


    if(!$session->get('order_id')) {
        $order_id = $db->newOrderId();
        //$func->prnt($session->get('cart_info'),true);
        $db->query("INSERT INTO " . DBPREFIX . "order SET 
                    order_id='" . $order_id . "', 
                    price='" . ADULT_PRICE . "', 
                    quantity='" . $quantity . "', 
                    order_status ='0',
                    total='" . $total . "', 
                    `name` ='" . $func->post('name') . "',
                    tc_no ='".$func->post('tc_no')."',
                    lastname ='" . $func->post('lastname') . "',
                    telephone ='" . $func->post('telephone') . "',                    
                    payment_type ='" . $func->post('payment_type') . "',                    
                    email ='" . $func->post('email') . "',
                    address ='" . $func->post('address') . "',
                    date_added= NOW()
                    ");
        $id = $db->getLastId();
        $session->set('order_id', $id);
        if($session->get('cart_info')) {
            foreach ($session->get('cart_info') as $item) {
                if($item['kart_type']=='adult'){
                    $price = ($session->get('coupon') && $session->get('coupon_type'))?CHILD_PRICE:ADULT_PRICE;
                }else{
                    $price = CHILD_PRICE;
                }

                $db->query("INSERT INTO " . DBPREFIX . "order_ticket SET 
                    order_id='" . $id . "', 
                    price='" . $price . "',                                                
                    name ='" . $item['name'] . "',
                    lastname ='" . $item['lastname'] . "',
                    telephone ='" . $item['telephone'] . "',
                    tc_no ='" . $item['tc_no'] . "',
                    addres ='" . $item['addres'] . "',
                    home_telephone ='" . $item['home_telephone'] . "',
                    birthdate ='" . $item['birthdate'] . "',
                    kart_type ='" . $item['kart_type'] . "',                                        
                    email ='" . $item['email'] . "',                    
                    date_added= NOW()
                    ");
            }
        }
     }else{
        $id = $session->get('order_id');
        $order_id = $db->newOrderId();
        $db->query("UPDATE " . DBPREFIX . "order SET                  
                    price='" . ADULT_PRICE . "', 
                    order_id='".$order_id."',
                    quantity='" . $quantity . "',                     
                    total='" . $total . "', 
                    `name` ='" . $func->post('name') . "',
                    tc_no ='".$func->post('tc_no')."',
                    lastname ='" . $func->post('lastname') . "',
                    telephone ='" . $func->post('telephone') . "',                    
                    payment_type ='" . $func->post('payment_type') . "',                    
                    email ='" . $func->post('email') . "',
                    address ='" . $func->post('address') . "'
                    WHERE id = '".$id."'
                    ");
        if($session->get('cart_info')) {
            $db->query("DELETE FROM ".DBPREFIX."order_ticket WHERE order_id='".$id."'");
            foreach ($session->get('cart_info') as $item) {
                if($item['kart_type']=='adult'){
                    $price = ($session->get('coupon') && $session->get('coupon_type'))?CHILD_PRICE:ADULT_PRICE;
                }else{
                    $price = CHILD_PRICE;
                }
                $db->query("INSERT INTO " . DBPREFIX . "order_ticket SET 
                    order_id='" . $id . "', 
                    price='" . $price . "',                                                
                    name ='" . $item['name'] . "',
                    lastname ='" . $item['lastname'] . "',
                    telephone ='" . $item['telephone'] . "',                    
                    email ='" . $item['email'] . "',                    
                    date_added= NOW()
                    ");
            }
        }
    }

    if($session->get('coupon')){
        $db->query("DELETE FROM ".DBPREFIX."order_coupon WHERE order_id='".$id."'");
        $db->query("INSERT INTO ".DBPREFIX."order_coupon SET order_id='".$id."',coupon_id='".$session->get('coupon')."'");
    }

    include_once PAYMENT_METHODS.$func->post('payment_type').'.php';

    $func->prnt('ödeme yapıldı..');
}else{

    $error->set('Geçersiz istek!');
    $location->redirect('payment');

}

include_once 'footer.php'; ?>