<?php 
    if(!isset($_GET["id"]) || empty($_GET["id"])){
        header("Location: /admin/orders/list");
        exit();
    }
    $order_details       = $order->getOrder($_GET['id']);
    if(empty($order_details)) {
        header("Location: /admin/orders/list");
        exit();
    }
    $data = array();
    $data['title'] = "Sipariş Detayları | ".$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' => "Siparişler",
        'href' => "/admin/orders/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Sipariş Detayları",
        'href' => "#",
        'index' => 3
    );

    $redirect_url             = isset($_REQUEST["redirect"]) ? $_REQUEST["redirect"] : "/admin/orders/list";

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if(isset($_POST['action']) && !empty($_POST['action'])) {
            switch($_POST['action']) {
                case 'order':
                    $status                 = $_POST['status'];
                    $shipping_agency_id     = $_POST['shipping_agency_id'];
                    $tracking_code          = $_POST['tracking_code'];
                    $inv_no                 = $_POST['inv_no'];
                    $stmt = $db->query("UPDATE {$db->prefix}orders SET status=:status, shipping_agency_id=:shipping_agency_id, tracking_code=:tracking_code, inv_no=:inv_no WHERE id=:id", array(':id'=>$order_details['id'], ':status'=>$status, ':shipping_agency_id'=>$shipping_agency_id, ':tracking_code'=>$tracking_code, ':inv_no'=>$inv_no));
                    if($stmt->error) {
                        $utils->errorLog("error", "Cannot update order. Error: ".$stmt->error_info['message'], $session->data['user_id'], $_SERVER['REQUEST_URI']);
                        $session->data['messageType'] = "error"; 
                        $session->data['messageTitle'] = "Hata!"; 
                        $session->data['message'] = "Bir hata oluştu ve sipariş güncellenemedi."; 
                        header("Refresh: 0;");
                        exit();
                    }else{
                        $session->data['messageType'] = "success"; 
                        $session->data['messageTitle'] = "Başarılı!"; 
                        $session->data['message'] = "Sipariş başarıyla güncellendi."; 

                        if($order_details['status'] != $status) {
                            $new_status = $order->getOrderStatus($status);
                            $message = "<strong>".$session->data['user_name']."</strong> sipariş durumunu <strong>".$new_status."</strong> olarak güncelledi.";
                            $order->addOrderHistory($session->data['user_id'], $order_details['id'], $message);

                            /*$netGSM	 = new NetGSM($db, $utils);
                            $sms_template = $netGSM->getSMSTemplate($status);

                            if(!empty($sms_template)) {
                                $new_shipping_agency = $order->getShippingAgency($shipping_agency_id);

                                $message = $sms_template['message'];
                                $message = str_replace('{BASE_URL}', $config->get('site_url'), $message);
                                $message = str_replace('{CUSTOMER_NAME}', $order_details['name'], $message);
                                $message = str_replace('{CUSTOMER_EMAIL}', $order_details['email'], $message);
                                $message = str_replace('{ORDER_NO}', $order_details['order_id'], $message);
                                $message = str_replace('{SHIPPING_AGENCY}', $new_shipping_agency['title'], $message);
                                $message = str_replace('{TRACKING_CODE}', $tracking_code, $message);

                                $netGSM->sendSMS($order_details['phone'], $message);
                            }*/
                        }
                        if($order_details['shipping_agency_id'] != $shipping_agency_id) {
                            $new_shipping_agency = $order->getShippingAgency($shipping_agency_id);
                            $message = "<strong>".$session->data['user_name']."</strong> sipariş kargo şirketini <strong>".$new_shipping_agency['title']."</strong> olarak güncelledi.";
                            $order->addOrderHistory($session->data['user_id'], $order_details['id'], $message);
                        }
                        if($order_details['tracking_code'] != $tracking_code) {
                            $message = "<strong>".$session->data['user_name']."</strong> kargo takip numarasını <strong>".$tracking_code."</strong> olarak güncelledi.";
                            $order->addOrderHistory($session->data['user_id'], $order_details['id'], $message);
                        }
                        if($order_details['inv_no'] != $inv_no) {
                            $message = "<strong>".$session->data['user_name']."</strong> fatura numarasını <strong>".$inv_no."</strong> olarak güncelledi.";
                            $order->addOrderHistory($session->data['user_id'], $order_details['id'], $message);
                        }

                        header("Refresh: 0;");
                        exit();
                    }
                break;
                case 'address':
                    $name               = $_POST['name'];
                    $email_             = $_POST['email'];
                    $phone              = $_POST['phone'];
                    $address            = $_POST['address'];
                    $city_code          = $_POST['city_code'];
                    $city               = $utils->getCityName($city_code);
                    $town_code          = $_POST['town_code'];
                    $town               = $utils->getTownName($town_code);
                    $tckno              = $_POST['tckno'];
                    $company            = $_POST['company'];
                    $tax_no             = $_POST['tax_no'];
                    $tax_office         = $_POST['tax_office'];

                    $stmt = $db->query("UPDATE {$db->prefix}orders SET name=:name, email=:email, phone=:phone, address=:address, city_code=:city_code, town_code=:town_code, city=:city, town=:town, tckno=:tckno, company=:company, tax_no=:tax_no, tax_office=:tax_office
                     WHERE id=:id", array(':id'=>$order_details['id'], ':name'=>$name, ':email'=>$email_, ':phone'=>$phone, ':address'=>$address, ':city_code'=>$city_code, ':town_code'=>$town_code, ':city'=>$city, ':town'=>$town, ':tckno'=>$tckno, ':company'=>$company, ':tax_no'=>$tax_no, ':tax_office'=>$tax_office));
                    if($stmt->error) {
                        $utils->errorLog("error", "Cannot update order delivery info. Error: ".$stmt->error_info['message'], $session->data['user_id'], $_SERVER['REQUEST_URI']);
                        $session->data['messageType'] = "error"; 
                        $session->data['messageTitle'] = "Hata!"; 
                        $session->data['message'] = "Bir hata oluştu ve sipariş alıcı bilgileri güncellenemedi."; 
                        header("Refresh: 0;");
                        exit();
                    }else{
                        $session->data['messageType'] = "success"; 
                        $session->data['messageTitle'] = "Başarılı!"; 
                        $session->data['message'] = "Sipariş alıcı bilgileri başarıyla güncellendi."; 

                        header("Refresh: 0;");
                        exit();
                    }
                break;
            }
        }
        
	}

    $order_status_list   = $order->getOrderStatusList();
    $order_items         = $order->getOrderItems($order_details["id"]);
    $shipping_agency     = $order->getShippingAgency($order_details["shipping_agency_id"]);
    $shipping_agencies   = $order->getShippingAgenciesList();
    $cities              = $utils->getCities();
    $towns               = $utils->getTowns($order_details["shipping_city_code"]);
    $order_history       = $order->getOrderHistory($order_details["id"]);
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <form action="" method="post">
                    <div class="row">
                        <div class="col-md-8 col-xl-9">
                            <div class="tab-content" id="tab-content">
                                <div class="tab-pane fade active show" id="accountTab" role="tabpanel">
                                    <div class="card">
                                        <div class="card-header">
                                            <h5 class="card-title"><?php echo $order_details["order_id"]; ?></h5>
                                        </div>
                                        <div class="card-body p-0">
                                            <div class="row">
                                                <div class="table-responsive">
                                                    <table class="table table-condensed table-border">
                                                        <thead>
                                                            <tr>
                                                                <th>Ürün</th>
                                                                <th>Adet x Fiyat</th>
                                                                <th>Toplam</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <?php foreach ($order_items as $item): ?>
                                                                <tr>
                                                                    <td><a href="/<?php echo $item["product_url"]; ?>" target="_blank"><?php echo $item["product_title"]; ?></a></td>
                                                                    <td><?php echo $item["quantity"] . " x " . $utils->moneyFormatter($item["product_price"]); ?> ₺</td>
                                                                    <td><?php echo $utils->moneyFormatter($item["total_price"]); ?> ₺</td>
                                                                </tr>
                                                            <?php endforeach; ?>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                            <div class="p-3">
                                                <div class="row mb-3 justify-content-end">
                                                    <div class="col-md-4">
                                                        <table class="table">
                                                            <tbody>
                                                                <tr>
                                                                    <td>Sipariş Tutarı</td>
                                                                    <td class="text-end fw-bold"><?php echo $utils->moneyFormatter($order_details["gross_total"]); ?> TL</td>
                                                                </tr>
                                                                <tr>
                                                                    <td>İndirim Tutarı</td>
                                                                    <td class="text-end fw-bold"><?php echo $utils->moneyFormatter($order_details["discount_total"]); ?> TL</td>
                                                                </tr>
                                                                <tr>
                                                                    <td>Kargo Bedeli</td>
                                                                    <td class="text-end fw-bold"><?php echo $utils->moneyFormatter($order_details["shipping"]); ?> TL</td>
                                                                </tr>
                                                                <tr>
                                                                    <td>Taksit Sayısı</td>
                                                                    <td class="text-end fw-bold"><?php echo $order_details["installment"]; ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <td>Vade Farkı</td>
                                                                    <td class="text-end fw-bold"><?php echo $utils->moneyFormatter($order_details["commission"]); ?> TL</td>
                                                                </tr>
                                                                <tr>
                                                                    <td>Genel Toplam</td>
                                                                    <td class="text-end fw-bold"><?php echo $utils->moneyFormatter($order_details["order_total"]); ?> TL</td>
                                                                </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                            </div>    
                                        </div>
                                    </div>

                                    <div class="card">
                                        <div class="card-header">
                                            <h5 class="card-title">Sipariş Notları</h5>
                                        </div>
                                        <div class="card-body p-0">
                                            <div class="p-3">
                                                <div class="row mb-3">
                                                    <div class="col-md-6 mb-3">
                                                        <label for="order_note" class="col-form-label">Sipariş Notu</label>
                                                        <textarea name="order_note" id="order_note" rows="3" class="form-control" disabled><?php echo $order_details["order_note"]; ?></textarea>
                                                    </div>
                                                    <div class="col-md-6 mb-3">
                                                        <label for="gift_note" class="col-form-label">Hediye Notu <a href="/admin/orders/gift-note?id=<?php echo $order_details["id"]; ?>" target="_blank" title="Yazdır" class="ms-2"><i class="bi bi-printer"></i></a></label>
                                                        <textarea name="gift_note" id="gift_note" rows="3" class="form-control" disabled><?php echo $order_details["gift_note"]; ?></textarea>
                                                    </div>
                                                </div>
                                            </div>    
                                        </div>
                                    </div>

                                    <div class="card">
                                        <div class="card-header">
                                            <h5 class="card-title">Sipariş Hareketleri</h5>
                                        </div>
                                        <div class="card-body p-0">
                                            <div class="table-reponsive">
                                                <table class="table table-condensed table-hover table-bordered">
                                                    <thead>
                                                        <tr>
                                                            <th>Tarih</th>
                                                            <th>Hareket</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php foreach ($order_history as $row): ?>
                                                            <tr>
                                                                <td style="width: 130px;"><?php echo date('d/m/Y H:i', strtotime($row["date_added"])); ?></td>
                                                                <td><?php echo $row["message"]; ?></td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    </tbody>
                                                </table>
                                            </div>    
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4 col-xl-3">
                            <div class="card mb-3">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">Sipariş Güncelle</h5>
                                </div>
                                <div class="card-body p-3 pt-1">
                                    <div class="row mb-2">
                                        <div class="col-12">
                                            <label for="status" class="col-form-label">Durum</label>
                                            <select name="status" id="status" class="form-control form-select">
                                                <?php foreach ($order_status_list as $status): ?>
                                                    <option value="<?php echo $status['id']; ?>" <?php echo $status['id'] == $order_details["status"] ? 'selected' : ''; ?>><?php echo $status['title']; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-2">
                                        <div class="col-12">
                                            <label for="shipping_agency_id" class="col-form-label">Kargo Şirketi</label>
                                            <select name="shipping_agency_id" id="shipping_agency_id" class="form-control form-select">
                                                <?php foreach ($shipping_agencies as $agency): ?>
                                                    <option value="<?php echo $agency['id']; ?>" <?php echo $agency['id'] == $order_details["shipping_agency_id"] ? 'selected' : ''; ?>><?php echo $agency['title']; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-2">
                                        <div class="col-12">
                                            <label for="tracking_code" class="col-form-label">Kargo Takip No</label>
                                            <input type="text" name="tracking_code" id="tracking_code" class="form-control" value="<?php echo $order_details['tracking_code']; ?>">
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-12">
                                            <label for="inv_no" class="col-form-label">Fatura No</label>
                                            <input type="text" name="inv_no" id="inv_no" class="form-control" value="<?php echo $order_details['inv_no']; ?>">
                                        </div>
                                    </div>
                                </div>
                                <div class="card-footer text-center">
                                    <a href="<?php echo $redirect_url; ?>" class="btn btn-sm btn-primary me-2"><i class="bi bi-reply"></i> Geri</a>
                                    <button type="submit" name="update" id="update" class="btn btn-sm btn-success px-3">Güncelle</button>
                                </div>
                            </div>
                            
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title">Teslimat Bilgileri</h5>
                                    <div>
                                        <a href="#update-address-modal" data-id="<?php echo $order_details["id"]; ?>" data-bs-toggle="modal" title="Düzenle" class="ms-2"><i class="bi bi-pencil-square"></i></a>
                                        <a href="/admin/orders/shipping-label?id=<?php echo $order_details["id"]; ?>" target="_blank" title="Yazdır" class="ms-2 text-secondary"><i class="bi bi-printer"></i></a>
                                    </div>
                                </div>
                                <div class="card-body p-2">
                                    <address class="mb-0">
                                        <strong>Alıcı Adı:</strong> <?php echo $order_details["shipping_name"]." ".$order_details["shipping_surname"]; ?><br>
                                        <strong>Tel:</strong> <?php echo $order_details["shipping_phone"]; ?><br>
                                        <strong>Adres:</strong> <?php echo $order_details["shipping_address"]." ".$order_details["shipping_city"]." / ".$order_details["shipping_town"]; ?>
                                        <hr>
                                        <?php if($order_details["invoice_company"] != ""): ?>
                                            <?php echo $order_details["invoice_company"]; ?>
                                            <br>
                                            <?php echo $order_details["invoice_tax_office"]; ?> / <?php echo $order_details["invoice_tax_no"]; ?><br>
                                        <?php else: ?>
                                            <strong>Unvan:</strong> <?php echo $order_details["invoice_name"]." ".$order_details["invoice_surname"]; ?><br>
                                            <strong>TC Kimlik No:</strong> <?php echo $order_details["invoice_tckno"]; ?><br>
                                        <?php endif; ?>
                                        <strong>Tel:</strong> <?php echo $order_details["invoice_phone"]; ?><br>
                                        <strong>Adres:</strong> <?php echo $order_details["invoice_address"]." ".$order_details["invoice_city"]." / ".$order_details["invoice_town"]; ?>
                                        <br>
                                    </address>
                                </div>
                            </div>

                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title">Sipariş Ayrıntıları</h5>
                                </div>
                                <div class="card-body p-1">
                                    <div class="table-responsive">
                                        <table class="table">
                                            <tbody>
                                                <tr>
                                                    <td class="fw-bold">Kullanıcı Adı</td>
                                                    <?php if($order_details["user_id"] != 0): ?>
                                                        <td class="text-end"><a href="/admin/users/edit?user_id=<?php echo $order_details["user_id"]; ?>" target="_blank"><?php echo $order_details["name"]; ?></a></td>
                                                    <?php else: ?>
                                                        <td class="text-end"><?php echo $order_details["name"]; ?></td>
                                                    <?php endif; ?>
                                                    
                                                </tr>
                                                <tr>
                                                    <td class="fw-bold">Telefon</td>
                                                    <td class="text-end"><?php echo $order_details["phone"]; ?></td>
                                                </tr>
                                                <tr>
                                                    <td class="fw-bold">Email</td>
                                                    <td class="text-end"><?php echo $order_details["email"]; ?></td>
                                                </tr>
                                                <tr>
                                                    <td class="fw-bold">IP</td>
                                                    <td class="text-end"><?php echo $order_details["ip"]; ?></td>
                                                </tr>
                                                <tr>
                                                    <td class="fw-bold">Sipariş Tarihi</td>
                                                    <td class="text-end"><?php echo $order_details["date_added"]; ?></td>
                                                </tr>
                                                <tr>
                                                    <td class="text-center"><a data-bs-toggle="modal" href="#sales-contract-modal" role="button">Satış Sözleşmesi</a></td>
                                                    <td class="text-center"><a data-bs-toggle="modal" href="#sales-contract-modal-2" role="button">Ön Bilgilendirme Formu</a></td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title">Banka Ayrıntıları</h5>
                                </div>
                                <div class="card-body p-1">
                                    <table class="table">
                                        <tbody>
                                            <tr>
                                                <td class="fw-bold">Sipariş No</td>
                                                <td class="text-end"><?php echo $order_details["bank_order_id"]; ?></td>
                                            </tr>
                                            <tr>
                                                <td class="fw-bold">Auth Code</td>
                                                <td class="text-end"><?php echo $order_details["bank_auth_code"]; ?></td>
                                            </tr>
                                            <tr>
                                                <td class="fw-bold">Referans No</td>
                                                <td class="text-end"><?php echo $order_details["bank_reference_code"]; ?></td>
                                            </tr>
                                            <tr>
                                                <td class="fw-bold">Kredi Kartı</td>
                                                <td class="text-end"><?php echo $order_details["bank_ccprefix"]; ?></td>
                                            </tr>
                                            <tr>
                                                <td class="fw-bold">Ödeme Geçidi</td>
                                                <td class="text-end"><?php echo $order_details["payment_gateway"]; ?></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <input type="hidden" name="action" value="order">
                </form>
            </div>

            <div class="modal fade" id="sales-contract-modal" tabindex="-1" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Satış Sözleşmesi</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <?php echo $order_details["sales_contract"]; ?>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" data-bs-dismiss="modal">Tamam</button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="sales-contract-modal-2" tabindex="-1" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Ön Bilgilendirme Formu</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <?php echo $order_details["preliminary_information_form"]; ?>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" data-bs-dismiss="modal">Tamam</button>
                        </div>
                    </div>
                </div>
            </div>

            <form action="" method="post">
                <div class="modal fade" id="update-address-modal" tabindex="-1" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title fw-bold">Adres Güncelle</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                            </div>
                            <div class="modal-body">
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="shipping_name" class="col-form-label fw-bold">Alıcı Adı</label>
                                        <input type="text" class="form-control" id="shipping_name" name="shipping_name" value="<?php echo $order_details["shipping_name"]; ?>">
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="shipping_surname" class="col-form-label fw-bold">Alıcı Soyadı</label>
                                        <input type="text" class="form-control" id="shipping_surname" name="shipping_surname" value="<?php echo $order_details["shipping_surname"]; ?>">
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="shipping_phone" class="col-form-label fw-bold">Telefon</label>
                                        <input type="tel" class="form-control" id="shipping_phone" name="shipping_phone" value="<?php echo $order_details["shipping_phone"]; ?>">
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="email" class="col-form-label fw-bold">Eposta</label>
                                        <input type="email" class="form-control" id="email" name="email" value="<?php echo $order_details["email"]; ?>">
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="shipping_address" class="col-form-label fw-bold">Adres</label>
                                        <textarea name="shipping_address" id="shipping_address" rows="2" class="form-control"><?php echo $order_details["shipping_address"]; ?></textarea>
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="shipping_city_code" class="col-form-label fw-bold">Şehir</label>
                                        <select name="shipping_city_code" id="shipping_city_code" class="form-control form-select">
                                            <?php foreach($cities as $row): ?>
                                                <option value="<?php echo $row["plate_no"]; ?>" <?php echo $row["plate_no"] == $order_details["shipping_city_code"] ? "selected" : ""; ?>><?php echo $row["name"]; ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="shipping_town_code" class="col-form-label fw-bold">İlçe</label>
                                        <select name="shipping_town_code" id="shipping_town_code" class="form-control form-select">
                                            <?php foreach($towns as $row): ?>
                                                <option value="<?php echo $row["id"]; ?>" <?php echo $row["id"] == $order_details["shipping_town_code"] ? "selected" : ""; ?>><?php echo $row["name"]; ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Vazgeç</button>
                                <button type="submit" class="btn btn-primary">Tamam</button>
                            </div>
                        </div>
                    </div>
                </div>
                <input type="hidden" name="action" value="address">
            </form>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>
<script>
    document.getElementById('city_code').addEventListener('change', (e) => {
        var city = e.currentTarget.value;
        get_towns(city, 'town_code')
    });
</script>