<?php

if(isset($_POST['name_submit']) && !empty($_POST['name'])){
    $name   = $db->escape_var($_POST['name']);

    $q 		=  "SELECT customers.*,currencies.`unit`,transactions.*
    			FROM customers,transactions,customer_balances,currencies
    			WHERE name='$name'
    			AND customers.`id`=transactions.`customers_id`
    			AND customers.`id`=customer_balances.`customers_id`
    			AND customer_balances.`currencies_id`=currencies.`id`
				AND customer_balances.`customers_id`=transactions.`customers_id`";
    
    $result = $db->query($q);
    if($result->num_rows > 0){
		$i = 0;
		while($row = $result->fetch_assoc()){

			echo "<tr>";
			echo "<td>" . ++$i . "</td>";
			echo "<td>" . $row['name'] ."</td>";
			echo "<td>" . $row['bc_id'] . "</td>";
			echo "<td>" . $row['phone'] . "</td>";
			echo "<td>" . $row['unit'] . "</td>";
			echo "<td>" . $row['amount'] . "</td>";
			echo "<td>" . $row['total_dollars'] . "</td>";
			if ($row['type'] == "in") {	
				echo "<td>رسیدگی</td>";
			}else {
				echo "<td>بردگی</td>";
			}
			echo "<td>" . $row['date'] . "</td>";
			echo "<td class='table-operation'>" . "<a href='customer_edit.php?id={$row['id']}' class='fa fa-edit'></a>" . "</td>";
			echo "<td class='table-operation'>" . "<a href='javascript:void(0);' class='fa fa-times text-danger'></a>" . "</td>";
			echo "</tr>";
		}
		$result->free();
	}else{
		//Search customers which did not do any transactions
    	$q		= "SELECT * FROM customers WHERE name='$name'";
    	$result = $db->query($q);

		if($result->num_rows > 0){
			$i = 0;
			while($row = $result->fetch_assoc()){
				echo "<tr>";
				echo "<td>" . ++$i . "</td>";
				echo "<td>" . $row['name'] ."</td>";
				echo "<td>" . $row['bc_id'] . "</td>";
				echo "<td>" . $row['phone'] . "</td>";
				echo "<td>" . "حساب وجود ندارد" . "</td>";
				echo "<td>" . "موجودی ندارد" . "</td>";
				echo "<td>" . "0" . "</td>";
				echo "<td>" . "معامله صورت نگرفته" . "</td>";
				echo "<td>" . " " . "</td>";
				echo "<td class='table-operation'>" . "<a href='customer_edit.php?id={$row['id']}' class='fa fa-edit'></a>" . "</td>";
				echo "<td class='table-operation'>" . "<a href='customer_del.php?id={$row['id']}' class='fa fa-times'></a>" . "</td>";
				echo "</tr>";
			}
			$result->free();
		}
	}



}
/* END of query based on customer's name */

if(isset($_POST['unit_submit']) && !empty($_POST['unit'])){
    $unit   = $db->escape_var($_POST['unit']);
    $q 		= "SELECT customers.*,currencies.`unit`,transactions.*
    			FROM customers,transactions,customer_balances,currencies
    			WHERE customers.`id`=transactions.`customers_id`
    			AND customers.`id`=customer_balances.`customers_id`
    			AND customer_balances.`currencies_id`=currencies.`id`
    			AND currencies.`id`=$unit";

    $result = $db->query($q);
    if($result->num_rows > 0){
		$i = 0;
		while($row = $result->fetch_assoc()){
			
			echo "<tr>";
			echo "<td>" . ++$i . "</td>";
			echo "<td>" . $row['name'] ."</td>";
			echo "<td>" . $row['bc_id'] . "</td>";
			echo "<td>" . $row['phone'] . "</td>";
			echo "<td>" . $row['unit'] . "</td>";
			echo "<td>" . $row['amount'] . "</td>";
			echo "<td>" . $row['total_dollars'] . "</td>";
			if ($row['type'] == "in") {	
				echo "<td>رسیدگی</td>";
			}else {
				echo "<td>بردگی</td>";
			}
			echo "<td>" . $row['date'] . "</td>";
			echo "<td class='table-operation'>" . "<a href='customer_edit.php?id={$row['id']}' class='fa fa-edit'></a>" . "</td>";
			echo "<td class='table-operation'>" . "<a href='javascript:void(0);' class='fa fa-times text-danger'></a>" . "</td>";
			echo "</tr>";
		}
		$result->free();
	}
}
/* END of query based on currency unit */
?>