<?php require view('theme/header'); ?>
<main class="app-main">
	<div class="page-content">
		<div class="container-fluid">

			<div class="row mt-4 pt-2">
				<div class="col-12">
					<form action="/orders" method="get" id="history-search">
						<div class="fg fg-dark">
							<div class="fg-icon" style="background-color: transparent;"><i class="far fa-search"></i></div>
							<input type="text" name="search" id="searchService" class="fg-control" placeholder="Sipariş Ara">
					</form>
				</div>

				<ul class="order-navs mt-3">
					<li class="<?php if (!array_key_exists(2, $uri)) { ?>active <?php } ?>">
						<a href="/orders">
							<i class="far fa-filter"></i>
							<div class="onav-text">Tüm Siparişler</div>
						</a>
					</li>
					<li class="<?php if (array_key_exists(2, $uri) && $uri[2] == "pending") { ?>active <?php } ?>">
						<a href="/orders/pending">
							<i class="fas fa-spinner"></i>
							<div class="onav-text">Sipariş Alındı</div>
						</a>
					</li>
					<li class="<?php if (array_key_exists(2, $uri) && $uri[2] == "inprogress") { ?>active <?php } ?>">
						<a href="/orders/inprogress">
							<i class="far fa-tasks"></i>
							<div class="onav-text">Yükleniyor</div>
						</a>
					</li>
					<li class="<?php if (array_key_exists(2, $uri) && $uri[2] == "completed") { ?>active <?php } ?>">
						<a href="/orders/completed">
							<i class="far fa-check-circle"></i>
							<div class="onav-text">Tamamlandı</div>
						</a>
					</li>
					<li class="<?php if (array_key_exists(2, $uri) && $uri[2] == "partial") { ?>active <?php } ?>">
						<a href="/orders/partial">
							<i class="far fa-project-diagram"></i>
							<div class="onav-text">Kısmen Tamamlandı</div>
						</a>
					</li>
					<li class="<?php if (array_key_exists(2, $uri) && $uri[2] == "processing") { ?>active <?php } ?>">
						<a href="/orders/processing">
							<i class="far fa-sync"></i>
							<div class="onav-text">Sırada</div>
						</a>
					</li>
					<li class="<?php if (array_key_exists(2, $uri) && $uri[2] == "canceled") { ?>active <?php } ?>">
						<a href="/orders/canceled">
							<i class="far fa-times-circle"></i>
							<div class="onav-text">İptal Edildi</div>
						</a>
					</li>
				</ul>
			</div>
		</div>


		<div class="order-list">

			<?php
			if ($orders != NULL) {
				foreach ($orders as $order) {
					$service_name = json_decode($order["service_name"], true);

					foreach ($service_name as $details) {



						if ($details["lang"] == 1) {

							$name = $details["name"];
						}

						if ($details["lang"] == $lang) {

							$name = $details["name"];

							break;
						}
					}
			?>
					<div class="card card-order mt-5 service-item">
						<div class="card-body">
							<div class="row">
								<div class="col-lg-auto col-md-auto col-12 co-part">
									<div class="coh-icon">
										<?= $order['order_id'] ?>
									</div>
								</div>
								<div class="col-lg col mb-lg-0 mb-3 co-part">
									<h4 class="co-text"><?= $name ?></h4>
								</div>
								<div class="col-lg-auto pe-lg-0 co-part mb-lg-0 mb-4">
									<div class="order-input">
										<button class="btn btn-transparent btn-sm" onclick="copyToClipboard('<?= $order['order_link'] ?>')">
											<i class="far fa-copy"></i>
										</button>
										<a class="oi-control" href="<?= $order['order_link'] ?>" target="_blank"><?= $order['order_link'] ?></a>
									</div>
								</div>
								<div class="col-lg-auto col-12 co-part co-right">
									<div>

										<div class="order-status d-block 
								    <?php if ($order['order_status'] == "Completed") { ?>os-completed<?php  } elseif ($order['order_status'] == "Processing") { ?>os-processing<?php  } elseif ($order['order_status'] == "Pending") { ?>os-pending <?php  } elseif ($order['order_status'] == "Partial") { ?>os-partial<?php  } elseif ($order['order_status'] == "In progress") { ?>os-iprogress<?php  } elseif ($order['order_status'] == "Canceled") { ?>os-cancel<?php } ?>">
											<?php if ($order['order_status'] == "Completed") { ?>{ORDERS_COMPLETED}<?php } elseif ($order['order_status'] == "Canceled") { ?>{ORDERS_CANCELED}<?php } elseif ($order['order_status'] == "Partial") { ?>{ORDERS_PARTIAL}<?php } elseif ($order['order_status'] == "Pending") { ?>{ORDERS_PENDING}<?php } elseif ($order['order_status'] == "In progress") { ?>{ORDERS_INPROGRESS}<?php } elseif ($order['order_status'] == "Processing") { ?>{ORDERS_PROCESSING}<?php } ?></div>
									</div>
									<div class="co-right-right">
										<button class="btn btn-dark orderToggle"><i class="fas fa-chevron-down"></i></button>
									</div>
								</div>
							</div>
							<div class="co-hidden" style="display: none;">
								<div class="row">
									<div class="col-xl-2 col-6 mb-4 mb-xl-0">
										<div class="chid-item">
											<div class="d-flex align-items-center">
												<div class="coh-icon">
													<i class="far fa-chart-line"></i>
												</div>
												<div class="coh-details ps-2">
													<h3 class="coh-m-title"><?= $order['order_amount'] ?> {CURRENCY_SYMBOL}</h3>
													<div class="coh-count">{ORDERS_AMOUNT}</div>
												</div>
											</div>
										</div>
									</div>
									<div class="col-xl-2 col-6 mb-4 mb-xl-0">
										<div class="chid-item">
											<div class="d-flex align-items-center">
												<div class="coh-icon">
													<i class="far fa-chart-line"></i>
												</div>
												<div class="coh-details ps-2">
													<h3 class="coh-m-title"><?= $order['order_quantity'] ?></h3>
													<div class="coh-count">{ORDERS_QUANTITY}</div>
												</div>
											</div>
										</div>
									</div>
									<div class="col-xl-2 col-lg-6 col-md-12 mb-4 mb-xl-0">
										<div class="chid-item">
											<div class="d-flex align-items-center">
												<div class="coh-icon">
													<i class="far fa-chart-line"></i>
												</div>
												<div class="coh-details ps-2">
													<h3 class="coh-m-title"><?= $order['order_start_count'] ?></h3>
													<div class="coh-count">{ORDERS_START_COUNT}</div>
												</div>
											</div>
										</div>
									</div>
									<div class="col-xl-2 col-lg-6 col-md-12 mb-4 mb-xl-0">
										<div class="chid-item">
											<div class="d-flex align-items-center">
												<div class="coh-icon">
													<i class="far fa-chart-line"></i>
												</div>
												<div class="coh-details ps-2">
													<h3 class="coh-m-title"><?= $order['order_remains'] ?></h3>
													<div class="coh-count">{ORDERS_REMAINS}</div>
												</div>
											</div>
										</div>
									</div>
									<div class="col-xl-auto col-lg-6 col-md-12 mb-4 mb-xl-0">
										<div class="chid-item">
											<div class="d-flex align-items-center">
												<div class="coh-icon">
													<i class="far fa-chart-line"></i>
												</div>
												<div class="coh-details ps-2">
													<h3 class="coh-m-title"><?= str_date(date("y-m-d H:i:s", $order["order_date"])) ?></h3>
													<div class="coh-count">{ORDERS_DATE}</div>
												</div>
											</div>
										</div>
									</div>
									<div class="col-xl-auto col-md-12 col-12 col-4">
										<button class="btn btn-primary ahbtn btn-block bs-3" data-bs-toggle="modal" data-bs-target="#report<?= $order['order_id'] ?>">
											<i class="fas fa-headset"></i>
											<span class="ms-3 d-inline-block d-xl-none">Create Ticket</span>
										</button>
									</div>
								</div>
								<div class="row mt-3">
									<?php



									$taskds = $db->from('tasks')

										->where('order_id', $order['order_id'])

										->orderby('task_date', 'DESC')

										->first();

									if ($taskds) {

										if ($taskds['task_status'] == 0) {
											$taskds = '<div class=\"col-lg-6\"><button class="btn btn-dark btn-block disabled" data-toggle="tooltip" data-placement="top" >Onay Bekliyor</button></div>';
										} else {

											if ($taskds['task_type'] == 1) {


												$is_refill_gun_sinir = 0;
												$refill_gun_sinir = $order['refill_day'];
												if ($refill_gun_sinir > 0) {
													$date_now = date("Y-m-d H:i:s");
													$refill_gun_trh = strtotime('+' . $refill_gun_sinir . ' days', $order['order_date']);
													$refill_gun_trh = date('Y-m-d H:i:s', $refill_gun_trh);
													if ($date_now > $refill_gun_trh) {
														$is_refill_gun_sinir = false;
													} else {
														$is_refill_gun_sinir = true;
													}
												} else {
													$is_refill_gun_sinir = false;
												}

												if ($is_refill_gun_sinir) {

													$is_refill_bekleme = 0;
													$refill_bekleme_suresi = $order['refill_bekleme'];
													if ($refill_bekleme_suresi > 0) {
														$date_now = date("Y-m-d H:i:s");
														$refill_bekleme_trh = strtotime('+' . $refill_bekleme_suresi . ' hours', strtotime($taskds['task_updatedate']));
														$refill_bekleme_trh = date('Y-m-d H:i:s', $refill_bekleme_trh);
														if ($date_now > $refill_bekleme_trh) {
															$is_refill_bekleme = true;
														} else {
															$is_refill_bekleme = false;
														}
													} else {
														$is_refill_bekleme = true;
													}

													if ($is_refill_bekleme) {
														$taskds = "<div class=\"col-lg-6\"><a onclick='refill_order(" . $order['order_id'] . ")' class='btn btn-dark btn-block'>Telafi Başlat</a></div>";
													} else {
														if ($taskds['task_updatedate'] != null) {

															$refilkalantrh = tarih_farki($refill_bekleme_trh);

															$refill_status_msg = "";
															$refill_status = $taskds['task_status'];
															if ($refill_status == 1) {
																$refill_status_msg = "<div class=\"col-lg-6\"><button class=\"btn btn-dark btn-block disabled\" data-toggle=\"tooltip\" data-placement=\"top\" >Onaylandı</button></div>";
															} else if ($refill_status == 2) {
																$refill_status_msg = "<div class=\"col-lg-6\"><button class=\"btn btn-dark btn-block disabled\" data-toggle=\"tooltip\" data-placement=\"top\" >Reddedildi</button></div>";
															}

															$taskds = "
                        									" . $refill_status_msg . "
                        									<div class=\"col-lg-6\"><button class=\"btn btn-dark btn-block disabled\" data-toggle=\"tooltip\" data-placement=\"top\" >Yeniden refill yapabilmek için kalan süre: " . $refilkalantrh['saat'] . " saat " . $refilkalantrh['dakika'] . " dakika " . $refilkalantrh['saniye'] . " saniye</button></div>
                        									";
														} else {
															$taskds = "<div class=\"col-lg-6\"><button class=\"btn btn-dark btn-block disabled\" data-toggle=\"tooltip\" data-placement=\"top\" >İstek Gönderildi</button></div>";
														}
													}
												} else {
												}
											} elseif ($taskds['task_type'] == 2) {
												$taskds = "<div class=\"col-lg-6\"><a onclick='customer_cancel_order(" . $order['order_id'] . ")' class='btn btn-primary btn-block'>İptal Et</a></div>";
											}
										}
									} else {

										if ($order['cancel_button'] == 1) {

											$taskds = "<div class=\"col-lg-6\"><a onclick='customer_cancel_order(" . $order['order_id'] . ")' class='btn btn-primary btn-block'>İptal Et</a></div>";
										} elseif ($order['refill_button'] == 1) {


											$refill_bekleme_trh = "";
											$is_refill_bekleme = 0;
											$refill_bekleme_suresi = $order['refill_bekleme'];
											if ($refill_bekleme_suresi > 0) {
												$date_now = date("Y-m-d H:i:s");
												$refill_bekleme_trh = strtotime('+' . $refill_bekleme_suresi . ' hours', $order['update_date']);
												$refill_bekleme_trh = date('Y-m-d H:i:s', $refill_bekleme_trh);
												if ($date_now > $refill_bekleme_trh) {
													$is_refill_bekleme = true;
												} else {
													$is_refill_bekleme = false;
												}
											} else {
												$is_refill_bekleme = true;
											}

											if ($is_refill_bekleme) {
												$taskds = "<div class=\"col-lg-6\"><a onclick='refill_order(" . $order['order_id'] . ")' class='btn btn-dark btn-block'>Telafi Başlat</a></div>";
											} else {
												if ($order['order'] != "0") {

													$refilkalantrh = tarih_farki($refill_bekleme_trh);

													$taskds = "
                        							<div class=\"col-lg-6\"><button class=\"btn btn-dark btn-block disabled\" data-toggle=\"tooltip\" data-placement=\"top\" >Yeniden refill yapabilmek için kalan süre: " . $refilkalantrh['saat'] . " saat " . $refilkalantrh['dakika'] . " dakika " . $refilkalantrh['saniye'] . " saniye</button></div>";
												} else {
													$taskds = "<div class=\"col-lg-6\"><button class=\"btn btn-dark btn-block disabled\" data-toggle=\"tooltip\" data-placement=\"top\" >İstek Gönderildi</button></div>";
												}
											}
										} else {
										}
									} ?>



									<!---------------------------------------------------------------------------------->

									<?php if ($order['cancel_button'] == 1) :

										if ($order['order_status'] == "Pending") :

											echo $taskds;

										elseif ($order['order_status'] == "Processing") :

											echo $taskds;

										else :

										endif;

									elseif ($order['refill_button'] == 1) :

										if ($order['order_status'] == "Completed") :

											echo $taskds;

										else :

										endif;

									else :

									endif; ?>

								</div>
								<!------------------------------------------------------------------------------------->

							</div>
						</div>
					</div><!-- card-order -->

					<div class="modal fade" id="report<?= $order['order_id'] ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
						<div class="modal-dialog modal-dialog-centered">
							<div class="modal-content">
								<!-- Modal Header -->
								<div class="modal-header">
									<h4 class="modal-title">Ticket Oluştur</h4>
									<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
								</div>
								<!-- Modal body -->
								<div class="modal-body">
									<div class="payout">

										<form id="create_ticket" onsubmit="return false;">
											<div id="sonuc"></div>
											<div class="form-group mb-3">
												<label for="title" class="control-label">{TICKET_FORM_TITLE}</label>
												<select class="form-control" id="title" name="title">
													<option value="Siparişimi iptal etmek istiyorum #<?= $order['order_id'] ?>">Siparişimi iptal etmek istiyorum</option>
													<option value="Gönderilen miktar düştü #<?= $order['order_id'] ?>">Gönderilen miktar düştü</option>
													<option value="Siparişim beklemede kaldı #<?= $order['order_id'] ?>">Siparişim beklemede kaldı</option>
													<option value="Siparişim neden iptal edildi? #<?= $order['order_id'] ?>">Siparişim neden iptal edildi?</option>
													<option value="Diğer #<?= $order['order_id'] ?>">Diğer</option>
												</select>
											</div>
											<div class="form-group">
												<label for="message" class="control-label">{TICKET_FORM_MESSAGE}</label>
												<textarea class="form-control" rows="7" id="message" name="message" required></textarea>
											</div>
											<input name="_csrf" value="{{csrftoken}}" type="hidden">
											<input id="hmessage" name="TicketForm[message]" type="hidden">
											<input type="hidden" name="_csrf" value="{{csrftoken}}">
											<button id="buton{{ order['id'] }}" class="btn btn-primary btn-block mt-3">{TICKET_SUBMIT}</button>
										</form>

									</div>

								</div>
							</div>
						</div>
					</div>

				<?php }
			} else { ?>
				<div class="card mt-3">
					<div class="card-body">
						<div class="noorders text-center">
							<img src="<?= theme("assets/images/smiley.svg"); ?>" class="img-fluid smiley my-5" />
							<p>Merhaba <strong>{USER_USERNAME}</strong>, daha önce hiç sipariş vermedin.</p>
							<p>Bakiye Ekleyip <a href="/">Yeni Sipariş</a> Sayfasından Dilediğin Hizmeti Verebilirsin...</p>
							<div class="d-flex justify-content-center mt-5">
								<a href="/addfunds"><button class="btn btn-primary btn-lg mb-4 mx-1">Bakiye Yükle</button></a>
								<a href="/"><button class="btn btn-dark btn-lg mb-4 mx-1">Sipariş Ver</button></a>
							</div>
						</div>
					</div>
				</div>
			<?php } ?>




		</div>



	</div>
	</div>
</main>

<?php require view('theme/footer'); ?>