<?php include '../loader.php'; ?>
<?php include 'inc/config.php'; ?>
<?php include 'inc/template_start.php'; ?>
<?php include 'inc/page_head.php'; ?>
<?php
if(!$user->isLogged()) $location->aredirect('login');
//if(!$func->get('reservation_id')) $location->aredirect('orders');


$supports = $db->query("SELECT * FROM ".DBPREFIX."supports ");

$labels['3']['class']   = "label-success";
$labels['3']['text']    = "Başarılı";
$labels['1']['class']   = "label-info";
$labels['1']['text']    = "Beklemede";
$labels['0']['class']   = "label-danger";
$labels['0']['text']    = "Başarısız Ödeme";
$labels['2']['class']   = "label-warning";
$labels['2']['text']    = "İptal Edildi";

$path = '';
$order_info = array();
if($func->get('reservation_id')) {
    $path = '?reservation_id='.$func->get('reservation_id');
    $order_info = $db->query("SELECT * FROM ".DBPREFIX."reservations WHERE reservation_id='".$func->get('reservation_id')."'");
    $order_info = $order_info->num_rows?$order_info->row:array();
}
/*
    $error->set('Sipariş bulunamadı!');
    $location->aredirect('orders');
*/


if($_SERVER['REQUEST_METHOD']=='POST') {
    //$func->prnt($func->post(),true);
    if($func->post('order_status')==1 && !$func->post('bildirim')) {
        $transaction = md5(uniqid(mt_rand(), true));
        $db->query("UPDATE " . DBPREFIX . "order SET order_status ='" . $func->post('order_status') . "', status='" . $func->post('order_status') . "', transaction_id='" . $transaction . "' WHERE id = '" . $func->get('order_id') . "'");

        $order_tickets = $db->query("SELECT * FROM " . DBPREFIX . "order_ticket WHERE order_id = '" . $func->get('order_id') . "'");

        //$func->prnt($order_tickets,true);
        if ($order_tickets->num_rows) {
            $tickets = $func->addTicket($transaction, $order_tickets->rows, $order_info);

            foreach ($order_tickets->rows as $row) {

                $ticket = isset($tickets[$row['ticket_id']]) ? $tickets[$row['ticket_id']] : '';
                if (!empty($ticket)) {
                    $db->query("UPDATE " . DBPREFIX . "order_ticket SET ticket_number='" . $ticket . "' WHERE ticket_id = '" . $row['ticket_id'] . "'");

                    $data['subject'] = html_entity_decode(sprintf('Sayın %s, biletiniz oluşturuldu.', $row['name'] . '' . $row['lastname'], ENT_QUOTES, 'UTF-8'));
                    $data['body'] = '<b>Bilet Numaranız: </b>' . $ticket;
                    $data['name'] = $row['name'] . '' . $row['lastname'];
                    $data['email'] = $row['email'];
                    $mail_return = $func->sendMail($mail, $data);
                    $message = 'Bilet Numaranız: ' . $ticket;
                    $sms_return = $func->sendSms($sms, array($row['telephone']), $message);

                    $db->query("UPDATE " . DBPREFIX . "order_ticket SET sms_notification='" . $sms_return . "', mail_notification='" . $mail_return . "' WHERE ticket_id = '" . $row['ticket_id'] . "'");
                }

            }
        }
    }elseif ($func->post('bildirim')=='ok'){

        $order_tickets = $db->query("SELECT * FROM " . DBPREFIX . "order_ticket WHERE order_id = '" . $func->get('order_id') . "'");

        //$func->prnt($order_tickets,true);
        if ($order_tickets->num_rows) {

            foreach ($order_tickets->rows as $row) {

                if (!empty($row['ticket_number'])) {

                    $data['subject'] = html_entity_decode(sprintf('Sayın %s, biletiniz oluşturuldu.', $row['name'] . '' . $row['lastname'], ENT_QUOTES, 'UTF-8'));
                    $data['body'] = '<b>Bilet Numaranız: </b>' . $row['ticket_number'];
                    $data['name'] = $row['name'] . '' . $row['lastname'];
                    $data['email'] = $row['email'];
                    $mail_return = $func->sendMail($mail, $data);
                    $message = 'Bilet Numaranız: ' . $row['ticket_number'];
                    $sms_return = $func->sendSms($sms, $row['telephone'], $message);
                    $db->query("UPDATE " . DBPREFIX . "order_ticket SET sms_notification='" . $sms_return . "', mail_notification='" . $mail_return . "' WHERE ticket_id = '" . $row['ticket_id'] . "'");
                }

            }
        }

    }else{

        $db->query("UPDATE " . DBPREFIX . "order SET order_status ='".$func->post('order_status')."', status='".$func->post('order_status')."' WHERE id = '" .  $func->get('order_id') . "'");

        $data['subject']    = html_entity_decode(sprintf('Sayın %s, sipariş durumunuz değiştirildi.', $order_info['name'].''.$order_info['lastname'], ENT_QUOTES, 'UTF-8'));
        $data['body']       = '<b>Sipariş Durumunuz: </b>'. $labels[$func->post('order_status')]['text'];
        $data['name']       = $order_info['name'].''.$order_info['lastname'];
        $data['email']      = $order_info['email'];
        $mail_return        = $func->sendMail($mail,$data);
    }

    $success->set('Siparişiniz başarıyla güncellenmiştir.');
    $location->aredirect('orders');
}



?>
<!-- Page content -->
<div id="page-content">


    <div class="row">

        <div class="col-lg-12">
            <!-- Meta Data Block -->
            <div class="block">
                <!-- Meta Data Title -->
                <div class="block-title">
                    <h2><i class="fa fa-google"></i> <strong>Rezervasyon</strong> Ekle/Düzenle</h2>
                </div>
                <!-- END Meta Data Title -->

                <!-- Meta Data Content -->
                <form action="<?php echo SITE_ADMIN; ?>order_edit.php<?php echo $path; ?>" method="post" class="form-horizontal form-bordered">
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">İsim/Soyisim</label>
                        <div class="col-md-9">
                            <input type="text" id="product-meta-title" name="name" value="<?php echo $order_info?$order_info['name']:''; ?>" class="form-control" placeholder="İsim/Soyisim..">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-telephone">Telefon</label>
                        <div class="col-md-9">
                            <input type="text" id="product-telephone" name="telephone" value="<?php echo $order_info?$order_info['telephone']:''; ?>" class="form-control" placeholder="Telefon Numarası">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-point">Puan</label>
                        <div class="col-md-9">
                            <input type="number" id="product-point" name="point" value="<?php echo $order_info?$order_info['point']:''; ?>" class="form-control" placeholder="Puan(1,2 gibi)">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-point">Randevu Tarihi</label>
                        <div class="col-sm-9">
                            <input type="text" name="reservation_date" id="filter-date" value="<?php echo $order_info?$order_info['reservation_date']:''; ?>" placeholder="15-10-2020 gibi" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-point">Hizmet Seçimi</label>
                        <div class="col-sm-9">
                            <select class="form-control" name="supports[]" multiple id="supports">
                                <?php if($supports->num_rows){ ?>
                                    <?php foreach($supports->rows as $support){ ?>
                                        <option value="<?php echo $support['support_id']; ?>"><?php echo $support['name']; ?></option>
                                    <?php  } ?>
                                <?php  } ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-point">Personel Seçimi</label>
                        <div class="col-sm-9">
                            <select class="form-control" name="user_id" id="user_id">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-note">Not</label>
                        <div class="col-md-9">
                            <textarea id="product-note" name="note" class="form-control" rows="6" placeholder="Müşteri Notu.."><?php echo $order_info?$order_info['note']:''; ?></textarea>

                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-md-3 control-label" for="product-meta-title">Rezervasyon Durumu</label>
                        <div class="col-md-9">
                            <select type="text" id="order_status" name="order_status" class="form-control">

                                <?php foreach ($labels as $key=>$label) { ?>
                                    <option value="<?php echo $key; ?>" <?php echo $order_info['status']==$key?'selected="selected"':''; ?>><?php echo $label['text']; ?></option>
                                <?php } ?>
                            </select>
                            <div class="help-block">Sipariş durumunu güncellemek için bir durum seçiniz..</div>
                        </div>
                    </div>


                    <div class="form-group form-actions">
                        <div class="col-md-9 col-md-offset-3">
                            <button type="submit" class="btn btn-sm btn-primary"><i class="fa fa-floppy-o"></i> Save</button>
                            <a href="<?php echo SITE_ADMIN; ?>customers.php" class="btn btn-sm btn-warning"><i class="fa fa-repeat"></i> İptal</a>
                        </div>
                    </div>
                </form>
                <!-- END Meta Data Content -->
            </div>
            <!-- END Meta Data Block -->

        </div>
    </div>


</div>









<?php include 'inc/page_footer.php'; ?>
<?php include 'inc/template_scripts.php'; ?>

<!-- Load and execute javascript code used only in this page -->
<script src="js/pages/ecomProducts.js"></script>
<script>$(function(){ EcomProducts.init(); });</script>

<?php include 'inc/template_end.php'; ?>
