<?php

$bugun_alinan_baslangic_1 = new DateTime(date('Y-m-d')); 
$bugun_alinan_baslangic = $bugun_alinan_baslangic_1->format('U');
$bugun_alinan = $db->query("SELECT sum(toplam_tutar) as tutar FROM siparis  WHERE durum != 4 AND siparis_tarihi > '{$bugun_alinan_baslangic}' ")->fetch(PDO::FETCH_ASSOC);

$strtotime = date("o-\WW");
$start = strtotime($strtotime);
$end = strtotime("+6 days 23:59:59", $start);
$buhafta_alinan = $db->query("SELECT sum(toplam_tutar) as tutar FROM siparis  WHERE durum != 4 AND siparis_tarihi > '{$start}' AND siparis_tarihi < '{$end}' ")->fetch(PDO::FETCH_ASSOC);

$month_ini = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y", strtotime("-1 month"))));
$start = strtotime($month_ini);
$month_end = date("Y-m-d", mktime(0, 0, 0, date("m"), date("t", strtotime("+1 month")), date("Y", strtotime("+1 month"))));
$end = strtotime($month_end);
$buay_alinan = $db->query("SELECT sum(toplam_tutar) as tutar FROM siparis  WHERE durum != 4 AND siparis_tarihi BETWEEN '{$start}' AND '{$end}' ")->fetch(PDO::FETCH_ASSOC);

$toplam_tutar = $db->query("SELECT sum(toplam_tutar) as tutar FROM siparis  WHERE durum != 4 ")->fetch(PDO::FETCH_ASSOC);

?>

<div class="row">
	<div class="col-xl-9 col-md-12 col-lg-12">
		<div class="row">
			<div class="col-xl-4 col-lg-4 col-md-12">
				<div class="card bg-gradient-info overflow-hidden text-white">
					<div class="card-body pb-0">
						<p class=" mb-1 ">Bugün Toplam Satış</p>
						<h2 class="mb-1 font-weight-bold fs-30"><?php echo 0 + $bugun_alinan['tutar']; ?> TL</h2>
					</div>
					<div class="chart-wrapper overflow-hidden">
						<canvas id="area-chart1" class="area-chart chart-dropshadow-dark"></canvas>
					</div>
				</div>
			</div>
			<div class="col-xl-4 col-lg-4 col-md-12">
				<div class="card bg-gradient-danger overflow-hidden text-white">
					<div class="card-body pb-0">
						<p class=" mb-1 ">Bu Hafta Toplam Satış</p>
						<h2 class="mb-1 font-weight-bold fs-30"><?php echo 0 + $buhafta_alinan['tutar']; ?> TL</h2>
					</div>
					<div class="chart-wrapper overflow-hidden">
						<canvas id="area-chart2" class="area-chart chart-dropshadow-dark"></canvas>
					</div>
				</div>
			</div>
			<div class="col-xl-4 col-lg-4 col-md-12">
				<div class="card bg-gradient-teal overflow-hidden text-white">
					<div class="card-body pb-0">
						<p class=" mb-1 ">Bu Ay Toplam Satış</p>
						<h2 class="mb-1 font-weight-bold fs-30"><?php echo 0 + $buay_alinan['tutar']; ?> TL</h2>
					</div>
					<div class="chart-wrapper overflow-hidden">
						<canvas id="area-chart3" class="area-chart chart-dropshadow-dark"></canvas>
					</div>
				</div>
			</div>

			<div class="col-12">
				<div class="card">
					<div class="card-header">
						<div class="card-title"><?php echo $siparis_durum[0]; ?></div>
					</div>
					<div class="card-body">
				<div class="table-responsive">
					<table class="table table-bordered text-nowrap" id="example2">
						<thead>
							<tr>
								<th class="wd-15p border-bottom-0">ID</th>
								<th class="wd-15p border-bottom-0">Ad</th>
								<th class="wd-15p border-bottom-0">Soyad</th>
								<th class="wd-15p border-bottom-0">Telefon</th>
								<th class="wd-15p border-bottom-0">Email</th>
								<th class="wd-15p border-bottom-0">Ödeme Yöntemi</th>
								<th class="wd-15p border-bottom-0">Kredi Kartı Ödeme Durumu</th>
								<th class="wd-15p border-bottom-0">Referans Kodu</th>
								<th class="wd-15p border-bottom-0">Toplam Tutar</th>
								<th class="wd-15p border-bottom-0">Sipariş Tarihi</th>
								<th class="wd-15p border-bottom-0">Adres</th>
								<th class="wd-15p border-bottom-0">İşlem</th>
							</tr>
						</thead>
						<tbody>
							<?php
		                      $query = $db->query("SELECT * FROM siparis WHERE durum = 0 ORDER BY id DESC", PDO::FETCH_ASSOC);
		                      if($query->rowCount()){
		                        foreach( $query as $row ){
		                          echo '
		                            <tr>
		                              <td>'.$row['id'].'</td>
		                              <td>'.$row['ad'].'</td>
		                              <td>'.$row['soyad'].'</td>
		                              <td>'.$row['telefon'].'</td>
		                              <td>'.$row['email'].'</td>
		                              <td>'.$odeme_yontemi[$row['odeme_yontemi']].'</td>
		                              <td>';
		                              if($row['odeme_yontemi'] == 1){
		                              	echo $kredi_karti_odendi[$row['kredi_karti_odendi']];
		                              }
		                              echo '</td>
		                              <td>'.$row['siparis_key'].'</td>
		                              <td>'.$row['toplam_tutar'].' TL</td>
		                              <td>'.date('Y-m-d H:i:s',$row['siparis_tarihi']).'</td>
		                              <td>'.$row['adres'].'</td>
		                              <td>
		                                <a href="siparis-detay/'.$row['id'].'" class="badge badge-success" style="margin-right:10px">Düzenle</span>
		                              </td>
		                            </tr>
		                          ';
		                        }
		                      }
		                    ?>
						</tbody>
					</table>
				</div>
			</div>
				</div>
			</div>

		</div>
	</div>
	<div class="col-xl-3 col-md-12 col-lg-12">
		<div class="card">
			<div class="d-block mt-4 card-header border-0 text-center">
				<h2 class="text-center fs-22">Toplam Satış</h2>
			</div>
			<div class="card-body">
				<div class="row text-center">
					<div class="col-md-12">
						<img src="assets/images/png/Trofi.png" alt="img" class="sales-img">
						<h2 class="mb-0 mt-6 fs-40 counter font-weight-bold"><?php echo 0 + $toplam_tutar['tutar']; ?> TL</h2>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

