<?php include_once 'header.php'; ?>
<?php

$payment_info = $session->get('payment_info');

if($_SERVER['REQUEST_METHOD']=='POST' && $func->post('cart_info')) {
    $session->set('cart_info',$func->post('cart_info'));

}else{
    if(!$payment_info) {
        $error->set('Lütfen bilet bilgilerinizi kontrol edin!');
        $location->redirect('cart');
    }

}

$cart_info = $func->post('cart_info')?$func->post('cart_info'):$session->get('cart_info');

?>

<?php //$func->prnt($func->post()); ?>
    <section id="satin-al" class="content-section text-center">
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <form action="<?php echo SITE; ?>confirm.php" method="POST" class="form-horizontal" role="form" id="SaleForm">
                        <div class="row">
                            <div class="col-md-6">
                                <h5>Sipariş Bilgileri</h5>
                                <div class="form-row mb-3">
                                    <div class="col-sm-4"><label>Adınız:</label></div>
                                    <div class="col-sm-8"><input type="text" name="name" placeholder="Adı" id="name" required class="form-control" value="<?php echo (isset($payment_info['name']))?$payment_info['name']:''; ?>"/></div>
                                </div>
                                <div class="form-row mb-3">
                                    <div class="col-sm-4"><label>Soyadınız:</label></div>
                                    <div class="col-sm-8"><input type="text" name="lastname" placeholder="Soyadı" required id="lastname" class="form-control" value="<?php echo (isset($payment_info['lastname']))?$payment_info['lastname']:''; ?>"/></div>
                                </div>

                                <div class="form-row mb-3">
                                    <div class="col-sm-4"><label>TC Kimlik Numarası:</label></div>
                                    <div class="col-sm-8"><input type="text" name="tc_no" maxlength="11" placeholder="TC No" required id="tc_no" class="form-control" value="<?php echo (isset($payment_info['tc_no']))?$payment_info['tc_no']:''; ?>"/></div>
                                </div>

                                <div class="form-row mb-3">
                                    <div class="col-sm-4"><label>Telefon Numaranız:</label></div>
                                    <div class="col-sm-8"><input type="tel" name="telephone" placeholder="Telefon No" required id="telephone" class="form-control" value="<?php echo (isset($payment_info['telephone']))?$payment_info['telephone']:''; ?>"/></div>
                                </div>

                                <div class="form-row mb-3">
                                    <div class="col-sm-4"><label>E-Mail Adresiniz:</label></div>
                                    <div class="col-sm-8"><input type="email" name="email" placeholder="Email" required id="email" class="form-control" value="<?php echo (isset($payment_info['email']))?$payment_info['email']:''; ?>"/></div>
                                </div>

                            </div>

                            <div class="col-md-6">

                                <div class="row boxi pb-3">


                                    <div class="col-12">

                                        <div class="row no-gutters spt1">
                                            <div class="col pb-3"><h4>SEPET ÖZETİ</h4></div>
                                            <div class="w-100"></div>
                                            <?php $total = 0; ?>
                                            <?php foreach ($cart_info as $item) { ?>
                                                <?php $kart_type = $item['kart_type']=='adult'?'YETİŞKİN':'ÖĞRENCİ'; ?>
                                                <?php
                                                if($item['kart_type']=='adult'){
                                                    $price = ($session->get('coupon') && $session->get('coupon_type'))?CHILD_PRICE:ADULT_PRICE;
                                                }else{
                                                    $price = CHILD_PRICE;
                                                }

                                                ?>
                                                <div class="col"><?php echo $item['name'].' '.$item['lastname']; ?></div>
                                                <div class="col" id="koten1"><?php echo $kart_type; ?></div>
                                                <div class="col" id="koten2"><?=number_format($price, 2, ',', '.')?> TL</div>
                                                <div class="w-100"></div>
                                                <?php $total +=  $price; ?>
                                            <?php } ?>




                                            <div class="col spt2"><strong>Toplam</strong></div>
                                            <div class="col"></div>
                                            <div class="col" id="koten3"><?php echo number_format($total, 2, ',', '.'); ?> TL</div>
                                        </div>

                                    </div>
                                </div>
                                <div class="form-group">
                                    <?php if($session->get('coupon')){ ?>
                                        <div class="col-sm-12">
                                            <button type="button" onclick="ajax()" class="btn button" >KUPONU KALDIR</button>
                                        </div>
                                    <?php }else{ ?>
                                        <div class="col-sm-8">
                                            <input name="coupon" placeholder="kupon kodu girin.." type="text" id="coupon" class="form-control" />
                                        </div>
                                        <div class="col-sm-4">
                                            <button type="button" onclick="ajax($('#coupon').val())" class="btn button" >GÖNDER</button>
                                        </div>
                                    <?php } ?>

                                </div>

                                <div class="form-group">
                                    <div class="col-sm-12">
                                    <select name="payment_type" id="payment_type" class="form-control">
                                        <option value="0">Ödeme Yöntemi Seçiniz</option>
                                        <option value="credit_cart">Kredi Kartı İle Ödeme</option>
                                        <option value="garanti_pay">Garanti Pay</option>
                                        <option value="havale">HAvele İle Ödeme</option>
                                    </select>
                                    </div>
                                </div>
                                <div id="credit_cart" class="payment_types" style="display: none">
                                    <div class="form-group">
                                        <div class="col-sm-12">
                                            <label>3 Taksit kullanmak istiyormusunuz?</label>
                                            <input type="checkbox" name="installment" id="installment" value="3" class="form-control">

                                        </div>
                                    </div>
                                    <div class="form-row mb-3">
                                        <div class="col-sm-4"><label>Kredi Kartı Sahibi:</label></div>
                                        <div class="col-sm-8"><input type="text" name="card_name" placeholder="Kart Sahibi" id="card_name" required class="form-control"/></div>
                                    </div>
                                    <div class="form-row mb-3">
                                        <div class="col-sm-4"><label>Kart Numarası:</label></div>
                                        <div class="col-sm-8"><input type="text" name="card_number" placeholder="Kart Numarası" required id="card_number" class="form-control credit" /></div>
                                    </div>
                                    <div class="form-row">
                                        <div class="col-sm-4 mb-3"><label>Ay - Yıl / CCV:</label></div>
                                        <div class="col-sm-8 mb-6">
                                            <div class="form-row">
                                                <div class="col-sm-4 mb-3">
                                                    <select class="form-control" name="month" id="month" required>
                                                        <option value="">Seçiniz</option>
                                                        <?php for($i=1;$i<13;$i++){ ?>
                                                            <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                                        <?php  } ?>
                                                    </select>
                                                </div>
                                                <div class="col-sm-4 mb-3">
                                                    <select class="form-control" name="year" id="year" required>
                                                        <option value="">Seçiniz</option>
                                                        <?php for($i=date('Y');$i<(date('Y')+15);$i++){ ?>
                                                            <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                                        <?php  } ?>
                                                    </select>
                                                </div>
                                                <div class="col-sm-4 mb-3">
                                                    <input type="text" name="ccv" placeholder="CCV" required id="ccv" class="form-control"  maxlength="3"/>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div id="garanti_pay" class="payment_types" style="display:none;"> Garanti Pay ile ödeme...</div>
                                <div id="havale" class="payment_types" style="display:none;"> Havale ile ödeme...</div>

                                <div class="form-group">
                                    <div class="col-sm-12">

                                        <input type="checkbox" checked> <a data-fancybox="model" data-src="#mesefeli-satis-sozlesmesi" href="javascript:;">Mesafeli Satış Sözleşmesi</a>'ni ve <a data-fancybox="model" data-src="#on-bilgilendirme-formu" href="javascript:;">Ön Bilgilendirme Formu</a>'nu okudum ve kabul ediyorum.

                                    </div>
                                </div>


                                <div class="form-group">
                                    <div class="col-sm-12">
                                        <div class="pull-right">
                                            <button type="button" onclick="validateSubmit()" class="btn btn-primary" id="qSend">SİPARİŞİ TAMAMLA</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </section>

<?php include_once 'footer.php'; ?>
<script type="text/javascript">
    $('#payment_type').on('change',function () {
        console.log('PAY TYPE : '+ $(this).val());
        if($(this).val()!=0){
            $('.payment_types').hide();
            $('#'+$(this).val()).show();
        }else{
            $('.payment_types').hide();
        }
    });

    function validateSubmit() {
        if($('#payment_type').val()!=0){
            if($('#payment_type').val()=='credit_cart'){

                if(!$('#card_name').val()){
                    alert('Kart sahibinin adını giriniz');
                    return false;
                }
                if(!$('#card_number').val()){
                    alert('Kart numarasını giriniz');
                    return false;
                }
                if(!$('#month').val()){
                    alert('Kart son kullanma ayını seçiniz');
                    return false;
                }
                if(!$('#year').val()){
                    alert('Kart son kullanma yılını seçiniz');
                    return false;
                }
                if(!$('#ccv').val()){
                    alert('Kartın 3 haneli ccv numarasını giriniz');
                    return false;
                }
                $('#SaleForm').submit();
            }else{
                $('#SaleForm').submit();
            }

        }else{
            alert('Bir ödeme yöntemi seçiniz!');
        }
    }

    function ajax(coupon_code) {
        if(coupon_code){
            url = 'type=coupon&coupon_code='+coupon_code
        }else{
            url = 'type=coupon_clear';
        }
        $.ajax({
            url: 'ajax.php?'+url,
            dataType: 'json',
            beforeSend: function () {
                //$('#button-cart').button('loading');
            },
            complete: function () {
                //$('#button-cart').button('reset');
            },
            success: function (json) {
                if(json.success){
                    alert(json.success);
                    location.reload();
                }else{
                    alert('Girmiş olduğunuz kupon kodu bulunamadı!');
                }
            }
        });
    }
</script>