<div class="container mt-5">
	<div class="row">
		<div class="col-xl-3 col-md-6">
			<div class="card card-stats">
				<!-- Card body -->
				<div class="card-body">
					<div class="row">
						<?php 
							date_default_timezone_set("Asia/Bangkok");
							$firstday = date('Y-m-01');
							$endday = date('Y-m-d');
							$query = $this->db->query("SELECT sum(amount) as sum FROM logrefill WHERE logrefill.by != 'แอดมิน' and date(Timestamp) = '$endday'");							
							$data = $query->row_array();
							 ?>
						<div class="col">
							<h5 class="card-title text-uppercase text-muted mb-0">รายรับ (วันนี้)</h5>
							<span class="h2 font-weight-bold mb-0"><?php echo number_format($data['sum']) ?></span>
						</div>
						<div class="col-auto">
							<div class="icon icon-shape bg-gradient-red text-white rounded-circle shadow">
								<i class="fas fa-chart-bar"></i>
							</div>
						</div>
					</div>
					<p class="mt-3 mb-0 text-sm">
					<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-d');
							$query = $this->db->query("SELECT sum(amount) as sum FROM logrefill WHERE logrefill.by = 'Truemoney' and date(Timestamp) = '$endday'");
							$data = $query->row_array();
						?>
						<span class="text-nowrap mr-2">ทรูมันนี่</span>
						<span class="text-success"><?php echo number_format($data['sum']) ?> บาท</span>
					</p>
					<p class="mt-2 mb-0 text-sm">
					<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-d');
							$query = $this->db->query("SELECT sum(amount) as sum FROM logrefill WHERE logrefill.by = 'โอนผ่านธนาคาร' and date(Timestamp) = '$endday'");
							$data = $query->row_array();
						?>
						<span class="text-nowrap mr-2">ธนาคาร</span>
						<span class="text-success"><?php echo number_format($data['sum']) ?> บาท</span>
					</p>
					<!-- <p class="mt-2 mb-0 text-sm">
						<span class="text-nowrap mr-2">แอดมิน</span>
						<span class="text-success">0 บาท</span>
					</p> -->
				</div>
			</div>
		</div>
		<div class="col-xl-3 col-md-6">
			<div class="card card-stats">
				<!-- Card body -->
				<div class="card-body">
					<div class="row">
						<div class="col">
							<h5 class="card-title text-uppercase text-muted mb-0">รายรับ (เดือนนี้)</h5>
							<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-t');
							$query = $this->db->query("SELECT sum(amount) as sum FROM logrefill WHERE logrefill.by != 'แอดมิน' AND date(Timestamp) BETWEEN '$firstday' AND '$endday'");
							$data = $query->row_array();
							 ?>
							<span class="h2 font-weight-bold mb-0"><?php echo number_format($data['sum']) ?></span>
						</div>
						<div class="col-auto">
							<div class="icon icon-shape bg-gradient-red text-white rounded-circle shadow">
								<i class="fas fa-chart-bar"></i>
							</div>
						</div>
					</div>
					<p class="mt-3 mb-0 text-sm">
					<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-t');
							$query = $this->db->query("SELECT sum(amount) as sum FROM logrefill WHERE logrefill.by = 'Truemoney' and date(Timestamp) BETWEEN '$firstday' AND '$endday'");
							$data = $query->row_array();
						?>
						<span class="text-nowrap mr-2">ทรูมันนี่</span>
						<span class="text-success"><?php echo number_format($data['sum']) ?> บาท</span>
					</p>
					<p class="mt-2 mb-0 text-sm">
					<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-t');
							$query = $this->db->query("SELECT sum(amount) as sum FROM logrefill WHERE logrefill.by = 'ธนาคารกสิกร' and date(Timestamp) BETWEEN '$firstday' AND '$endday'");
							$data = $query->row_array();
						?>
						<span class="text-nowrap mr-2">ธนาคาร</span>
						<span class="text-success"><?php echo number_format($data['sum']) ?> บาท</span>
					</p>
					<!-- <p class="mt-2 mb-0 text-sm">
						<span class="text-nowrap mr-2">แอดมิน</span>
						<span class="text-success">0 บาท</span>
					</p> -->
				</div>
			</div>
		</div>
		<div class="col-xl-3 col-md-6">
			<div class="card card-stats">
				<!-- Card body -->
				<div class="card-body">
					<div class="row">
						<div class="col">
							<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-t');
							$query = $this->db->query("SELECT SUM(total-discount) as sum FROM logbuyproduct WHERE date(timestamp) = '$endday'");		
							$data = $query->row_array();					
							 ?>
							<h5 class="card-title text-uppercase text-muted mb-0">ยอดขาย (วันนี้)</h5>
							<span
								class="h2 font-weight-bold mb-0"><?php echo number_format($data['sum']) ?></span>
						</div>
						<div class="col-auto">
							<div class="icon icon-shape bg-gradient-danger text-white rounded-circle shadow">
							<i class="fas fa-chart-bar"></i>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="col-xl-3 col-md-6">
			<div class="card card-stats">
				<!-- Card body -->
				<div class="card-body">
					<div class="row">
						<div class="col">
							<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-t');
							$query = $this->db->query("SELECT SUM(total-discount) as sum FROM logbuyproduct WHERE date(Timestamp) BETWEEN '$firstday' AND '$endday'");	
							$data = $query->row_array();					
							 ?>
							<h5 class="card-title text-uppercase text-muted mb-0">ยอดขาย (เดือน)</h5>
							<span
								class="h2 font-weight-bold mb-0"><?php echo number_format($data['sum']) ?></span>
						</div>
						<div class="col-auto">
							<div class="icon icon-shape bg-gradient-danger text-white rounded-circle shadow">
							<i class="fas fa-chart-bar"></i>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="col-xl-3 col-md-6">
			<div class="card card-stats">
				<!-- Card body -->
				<div class="card-body">
					<div class="row">
						<div class="col">
							<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-t');
							$query = $this->db->query("SELECT * FROM users WHERE date = '$endday'");							
							 ?>
							<h5 class="card-title text-uppercase text-muted mb-0">ผู้ใช้ใหม่ (วันนี้)</h5>
							<span
								class="h2 font-weight-bold mb-0"><?php echo number_format($query->num_rows()) ?></span>
						</div>
						<div class="col-auto">
							<div class="icon icon-shape bg-gradient-danger text-white rounded-circle shadow">
								<i class="fas fa-user-plus"></i>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="col-xl-3 col-md-6">
			<div class="card card-stats">
				<!-- Card body -->
				<div class="card-body">
					<div class="row">
						<div class="col">
							<?php 
							$firstday = date('Y-m-01');
							$endday = date('Y-m-t');
							$query = $this->db->query("SELECT * FROM users WHERE date BETWEEN '$firstday' AND '$endday'");							
							 ?>
							<h5 class="card-title text-uppercase text-muted mb-0">ผู้ใช้ใหม่ (เดือน)</h5>
							<span
								class="h2 font-weight-bold mb-0"><?php echo number_format($query->num_rows()) ?></span>
						</div>
						<div class="col-auto">
							<div class="icon icon-shape bg-gradient-danger text-white rounded-circle shadow">
								<i class="fas fa-user-plus"></i>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<h5>สินค้าขายดี</h5>
	<div class="table-responsive mt-5 mb-5">
			<table id="datatable_card" class="table table-striped table-bordered" style="width:100%">
				<thead>
					<tr>
						<th><span style="font-size: 16px;">ลำดับ</span></th>
						<th><span style="font-size: 16px;">ชื่อสินค้า</span></th>
						<th><span style="font-size: 16px;">จำนวนการซื้อ</span></th>
					</tr>
				</thead>
				<?php 
				$query = $this->db->query("SELECT logbuyproduct.product,SUM(logbuyproduct.amount) as count FROM `logbuyproduct` GROUP BY logbuyproduct.product ORDER BY count desc");
                     ?>
				<tbody>
					<?php $i = 1;
                    foreach($query->result_array() as $data)
                    { ?>
					<tr>
						<td>
							<p><?php echo $i; ?></p>
						</td>
						<td>
							<p><?php echo $data['product']; ?></p>
						</td>
						<td>
							<p><?php echo number_format($data['count']); ?></p>
						</td>
					</tr>
					<?php 
                    $i++;
                    } ?>
				</tbody>
			</table>
		</div>
</div>
