<?php 
    $data = array();
    $data['title'] = "SMS Güncelle | ".$config->get('site_title');
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $config->get('version');
    $data['csrf_token'] = $csrf_generator->create();
    $data['sidebar_menu'] = $menu->get(array('position'=>'sidebar'));
    $data['sidebar_active_menu'] = $menu->getActiveMenuItemId(trim($_SERVER['REQUEST_URI']));
    $data['footer']['scripts'] = [];
    $menu_items = $menu->get(array('position'=>'footer'));
    foreach($menu_items as $item){
        if($item['col'] == "1"){
            $data['menu']['footer']["1"][] = $item;
        }
        if($item['col'] == "2"){
            $data['menu']['footer']["2"][] = $item;
        }
        if($item['col'] == "3"){
            $data['menu']['footer']["3"][] = $item;
        }
        if($item['col'] == "4"){
            $data['menu']['footer']["4"][] = $item;
        }
    }

    $data['breadcrumbs'] = array();
    $data['breadcrumbs'][] = array(
        'text' => "Pano",
        'href' => "/admin",
        'index' => 1
    );
    $data['breadcrumbs'][] = array(
        'text' => "SMS Ayarları",
        'href' => "/admin/sms/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "SMS Güncelle",
        'href' => "#",
        'index' => 3
    );

    if(isset($_GET['id']) && !empty($_GET['id'])) {
        $stmt = $db->query("SELECT * FROM {$db->prefix}sms_templates WHERE id=:id", array(':id'=>$_GET['id']));
        $sms_details = $stmt->row;
    }else{
        header("Location: /admin/sms/list");
        exit();
    }


    $response = array();
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $id             = $_GET['id'];
        $order_status_   = $_POST['order_status'];
        $status         = $_POST['status'];
        $message        = $_POST['message'];

        $stmt = $db->query("UPDATE {$db->prefix}sms_templates SET `order_status`=:order_status, `status`=:status, message=:message WHERE `id`=:id", array(':order_status'=>$order_status_, ':status'=>$status, ':message'=>$message, ':id'=>$id));
        
        if($stmt->error) {
            $session->data['messageType'] = "error"; 
            $session->data['messageTitle'] = "Hata!"; 
            $session->data['message'] = 'Bir hata oluştu ve isteğiniz yerine getirilemedi. Lütfen daha sonra tekrar deneyiniz.'; 
            header("Refresh: 0;");
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "SMS başarıyla güncellendi."; 
            header("Refresh: 0;");
        }
	}
    $order_status 	    = $order->getOrderStatusList();
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h5 class="card-title">SMS Ekle</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post">
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="status" class="col-form-label fw-bold">Durum</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="status" id="status" class="form-control form-select">
                                                <option value="1" <?php echo $sms_details["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $sms_details["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="order_status" class="col-form-label fw-bold">Sipariş Durumu</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="order_status" id="order_status" class="form-control form-select">
                                                <option value="">Seçiniz</option>
                                                <?php foreach ($order_status as $u): ?>
                                                    <option value="<?php echo $u['id']; ?>" <?php echo $sms_details["order_status"] == $u['id'] ? 'selected' : ''; ?>><?php echo $u['title']; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="message" class="col-form-label fw-bold">Mesaj</label>
                                        </div>
                                        <div class="col-md-9">
                                            <textarea name="message" id="message" rows="5" class="form-control"><?php echo $sms_details["message"]; ?></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>