<?php
    if(!isset($_GET["id"]) || empty($_GET["id"])){
        header("Location: /admin/sellers/list");
        exit();
    }
    $seller_details = $seller->getSeller($_GET["id"]);
    if(empty($seller_details)){
        header("Location: /admin/sellers/list");
        exit();
    }

    $data = array();
    $data['title'] = "Satıcı 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' => "Satıcılar",
        'href' => "/admin/sellers/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Güncelle",
        'href' => "#",
        'index' => 3
    );

    $response = array();
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $title              = $_POST['title'];
        $status             = $_POST['status'];
        $short_name         = $_POST['short_name'];
        $address            = $_POST['address'];
        $phone              = $_POST['phone'];
        $email_             = $_POST['email'];
        $tax_no             = $_POST['tax_no'];
        $tax_office         = $_POST['tax_office'];
        $mersis             = $_POST['mersis'];
        $kep                = $_POST['kep'];
        $trade_registary_number = $_POST['trade_registary_number'];

        $stmt = $db->query("UPDATE {$db->prefix}sellers SET status=:status, title=:title, `short_name`=:short_name, `address`=:address, `phone`=:phone, `email`=:email, `tax_no`=:tax_no, `tax_office`=:tax_office, `mersis`=:mersis, `kep`=:kep, `trade_registary_number`=:trade_registary_number WHERE id=:id",
        array(':id'=>$_GET['id'], ':title'=>$title, ':status'=>$status, ':short_name'=>$short_name, ':address'=>$address, ':phone'=>$phone, ':email'=>$email_, ':tax_no'=>$tax_no, ':tax_office'=>$tax_office, ':mersis'=>$mersis, ':kep'=>$kep, ':trade_registary_number'=>$trade_registary_number));
        if($stmt->error){
            $utils->errorLog("error", "Cannot add seller. 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 Satıcı güncellenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Satıcı başarıyla güncellendi."; 
            header("Refresh: 0;");
            exit();
        }
	}
?>
<?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">Sayfa Detayları</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post" enctype="multipart/form-data">
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="short_name" class="col-form-label fw-bold">Kısa Ad</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="short_name" id="short_name" placeholder="" value="<?php echo $seller_details["short_name"]; ?>" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="title" class="col-form-label fw-bold">Başlık</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="Başlık" value="<?php echo $seller_details["title"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="address" class="col-form-label fw-bold">Adres</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="address" id="address" placeholder="" value="<?php echo $seller_details["address"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="phone" class="col-form-label fw-bold">Telefon</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="phone" id="phone" placeholder="" value="<?php echo $seller_details["phone"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="email" class="col-form-label fw-bold">Email</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="email" id="email" placeholder="" value="<?php echo $seller_details["email"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="tax_no" class="col-form-label fw-bold">Vergi No</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="tax_no" id="tax_no" placeholder="" value="<?php echo $seller_details["tax_no"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="tax_office" class="col-form-label fw-bold">Vergi Dairesi</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="tax_office" id="tax_office" placeholder="" value="<?php echo $seller_details["tax_office"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="mersis" class="col-form-label fw-bold">Mersis No</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="mersis" id="mersis" placeholder="" value="<?php echo $seller_details["mersis"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="kep" class="col-form-label fw-bold">KEP Adresi</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="kep" id="kep" placeholder="" value="<?php echo $seller_details["kep"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="trade_registary_number" class="col-form-label fw-bold">Ticaret Sicil No</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="trade_registary_number" id="trade_registary_number" placeholder="" value="<?php echo $seller_details["trade_registary_number"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <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 $seller_details["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $seller_details["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/sellers/list" class="btn btn-secondary me-2">Geri</a>
                                            <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"; ?>