<?php
require_once('../../config/config.inc.php');
require(DB);
include('../includes/functions.php');

	function getFormattedDateHeader($datetime) {
		$phpDate = strtotime($datetime);
		$FormattedPhpDate = date('d-m-Y', $phpDate);
		return $FormattedPhpDate;
	}
	function getUserNameHeader($id) {
		$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
		mysqli_select_db($con, DB_NAME) or die(mysql_error());
		if($id !='') {
			$res = mysqli_fetch_array(mysqli_query($con, "SELECT name FROM tbl_user WHERE id = '$id'"));
			return $res['name'];
		}
	}
	function getTechNameHeader($id) {
		$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
		mysqli_select_db($con, DB_NAME) or die(mysql_error());
		if($id !='') {
			$res = mysqli_fetch_array(mysqli_query($con, "SELECT name FROM tbl_technician WHERE id = '$id'"));
			return $res['name'];
		}
	}
	function getShopNameHeader($id) {
		$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
		mysqli_select_db($con, DB_NAME) or die(mysql_error());
		if($id !='') {
			$res = mysqli_fetch_array(mysqli_query($con, "SELECT building_name FROM tbl_purchase_partner WHERE id = '$id'"));
			return $res['building_name'];
		}
	}
	

//get unread counts (count of user, tech ... not viewed by admin)
$unreadTechRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_technician WHERE viewed != 'yes'"));
$unreadTechCount = $unreadTechRec['count'];
$unreadUserRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_user WHERE viewed != 'yes'"));
$unreadUserCount = $unreadUserRec['count'];
$unreadShopRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_purchase_partner WHERE viewed != 'yes'"));
$unreadShopCount = $unreadShopRec['count'];
$unreadTaskRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_job WHERE viewed != 'yes'"));
$unreadTaskCount = $unreadTaskRec['count'];
$unreadOrdersRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_purchase_order WHERE partner_id = '0'"));
$unreadOrderCount = $unreadOrdersRec['count'];
$unassignedExtraTechRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM `tbl_job_extra_tech` WHERE `technician_id` = '0'"));
$unassignedExtraTechCount = $unassignedExtraTechRec['count'];

//checking for notifications
	$notifyTaskRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_job WHERE viewed != 'yes'"));
	$notifyTaskCount = $notifyTaskRec['count'];	
	//get new task not yet viewed by admin
	$notifyTaskRs = mysqli_query($con, "SELECT id, is_project, user_id, date FROM tbl_job WHERE status = 'New' AND viewed != 'yes' ORDER BY id DESC LIMIT 10");
	//get new user notifications
	$notifyUserRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_user WHERE viewed != 'yes'"));
	$notifyUserCount = $notifyUserRec['count'];
	$notifyUserRs = mysqli_query($con, "SELECT id, name, date FROM tbl_user WHERE viewed != 'yes' ORDER BY id DESC LIMIT 10");
	//get new tech notifications
	$notifyTechRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_technician WHERE viewed != 'yes'"));
	$notifyTechCount = $notifyTechRec['count'];
	$notifyTechRs = mysqli_query($con, "SELECT id, name, date FROM tbl_technician WHERE viewed != 'yes' ORDER BY id DESC LIMIT 10");
	//get new PP notifications
	$notifyPPRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_purchase_partner WHERE viewed != 'yes'"));
	$notifyPPCount = $notifyPPRec['count'];
	$notifyPPRs = mysqli_query($con, "SELECT id, name, date FROM tbl_purchase_partner WHERE viewed != 'yes' ORDER BY id DESC LIMIT 10");
	//get new PP orders
	$notifyOrderRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_purchase_order WHERE status = 'new' AND partner_id = '0'"));
	$notifyOrderCount = $notifyOrderRec['count'];
	$notifyOrderRs = mysqli_query($con, "SELECT id, technician_id, date FROM tbl_purchase_order WHERE partner_id = '0' ORDER BY id DESC LIMIT 10");
	//extra tech requests
	$notifyExtraTechRec = mysqli_fetch_array(mysqli_query($con, "SELECT count(id) AS count FROM tbl_job_extra_tech WHERE technician_id = '0' AND status = ''"));
	$notifyExtraTechCount = $notifyExtraTechRec['count'];
	$notifyExtraTechRs = mysqli_query($con, "SELECT id, job_id, tech_main_id, date FROM tbl_job_extra_tech WHERE technician_id = '0' AND status = '' ORDER BY id LIMIT 20");
	$notifyDisputeRs = mysqli_query($con, "SELECT `from`, user_id, technician_id FROM tbl_job_disputes WHERE status = '' ORDER BY id LIMIT 10");
	$notifyCatReqRs = mysqli_query($con, "SELECT technician_id FROM tbl_technician_category WHERE verified = '' ORDER BY id LIMIT 10");
	$notifyLeadsRs = mysqli_query($con, "SELECT technician_id, date FROM tbl_new_leads WHERE status = 'New'");
?>	
										<?php if(mysqli_num_rows($notifyTaskRs)>0){ ?>
                                    	<?php while($notifyTaskRec = mysqli_fetch_array($notifyTaskRs)){ ?>
										<li>
                                        	<?php if($notifyTaskRec['is_project']=='yes'){ ?>
                                            	<a href="index.php?page=view_task_project&id=<?php echo $notifyTaskRec['id'];?>">
                                            <?php } else { ?>	
                                            	<a href="index.php?page=view_task&id=<?php echo $notifyTaskRec['id'];?>">
                                            <?php } ?>
                                                <span class="time"><?php echo getFormattedDateHeader($notifyTaskRec['date']);?></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span> Task <?php echo '#'.$notifyTaskRec['id'].' from '.getUserNameHeader($notifyTaskRec['user_id']); ?> </span>
                                            </a>
                                        </li>
                                        <?php } } ?>
                                        <?php while($notifyTechRec = mysqli_fetch_array($notifyTechRs)){ ?>
                                        <li>                                        	
                                            <a href="index.php?page=view_technician&id=<?php echo $notifyTechRec['id'];?>">                                            
                                                <span class="time"><?php echo getFormattedDateHeader($notifyTechRec['date']);?></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span> New technician <?php echo getTechNameHeader($notifyTechRec['id']); ?> </span>
                                            </a>
                                        </li>
                                        <?php } ?>
                                        <?php while($notifyUserRec = mysqli_fetch_array($notifyUserRs)){ ?>
                                        <li>                                        	
                                            <a href="index.php?page=view_user&id=<?php echo $notifyUserRec['id'];?>">                                            
                                                <span class="time"><?php echo getFormattedDateHeader($notifyUserRec['date']);?></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span> New user <?php echo getUserNameHeader($notifyUserRec['id']); ?> </span>
                                            </a>
                                        </li>
                                        <?php } ?>
                                        <?php while($notifyPPRec = mysqli_fetch_array($notifyPPRs)){ ?>
                                        <li>                                        	
                                            <a href="index.php?page=view_purchase_partner&id=<?php echo $notifyPPRec['id'];?>">                                            
                                                <span class="time"><?php echo getFormattedDateHeader($notifyPPRec['date']);?></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span> New partner <?php echo getShopNameHeader($notifyPPRec['id']); ?> </span>
                                            </a>
                                        </li>
                                        <?php } ?>
                                        <?php while($notifyOrderRec = mysqli_fetch_array($notifyOrderRs)){ ?>
                                        <li>                                        	
                                            <a href="index.php?page=view_purchase_order&id=<?php echo $notifyOrderRec['id'];?>">                                            
                                                <span class="time"><?php echo getFormattedDateHeader($notifyOrderRec['date']);?></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span> Purchase order from <?php echo getTechnicianName($notifyOrderRec['technician_id']); ?> </span>
                                            </a>
                                        </li>
                                       <?php } ?>
                                       <?php while($notifyExtraTechRec = mysqli_fetch_array($notifyExtraTechRs)){ ?>
                                        <li>                                        	
                                            <a href="index.php?page=task_assign_technician&task_id=<?php echo $notifyExtraTechRec['job_id'];?>&extra=yes">                                            
                                                <span class="time"><?php echo getFormattedDateHeader($notifyExtraTechRec['date']);?></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span> Extra tech request from <?php echo getTechnicianName($notifyExtraTechRec['tech_main_id']); ?> </span>
                                            </a>
                                        </li>
                                       <?php } ?>
									    <?php while($notifyDisputeRec = mysqli_fetch_array($notifyDisputeRs)){ ?>
                                        <li>                                        	
                                            <a href="index.php?page=tech_disputes">                                            
                                                <span class="time"><?php //echo getFormattedDateHeader($notifyDisputeRec['date']);?></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span> 
                                                <?php if($notifyDisputeRec['from']=='user'){ ?>
                                                	Dispute from <?php echo getUserName($notifyDisputeRec['user_id']); ?>
                                                <?php } else { ?>
                                                	Dispute from <?php echo getTechnicianName($notifyDisputeRec['technician_id']); ?> 
                                                <?php } ?>
                                                </span>
                                            </a>
                                        </li>
                                       <?php } ?>
									   <?php while($notifyLeadsRec = mysqli_fetch_array($notifyLeadsRs)){ ?>
                                        <li>                                        	
                                            <a href="index.php?page=tech_disputes">                                            
                                                <span class="time"><?php echo getFormattedDateHeader($notifyLeadsRec['date']);?></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span>
                                                	Dispute from <?php echo getTechnicianName($notifyLeadsRec['technician_id']); ?>
                                                </span>
                                            </a>
                                        </li>
                                       <?php } ?>
                                       <?php while($notifyCatReqRec = mysqli_fetch_array($notifyCatReqRs)){ ?>
                                        <li>                                        	
                                            <a href="index.php?page=technician_category&tech_id=<?php echo $notifyCatReqRec['technician_id'];?>">                                            
                                                <span class="time"></span>
                                                <span class="details">
                                                <span class="notification-icon circle deepPink-bgcolor"><i class="fa fa-check"></i></span> Category request from <?php echo getTechNameHeader($notifyCatReqRec['technician_id']); ?> </span>
                                            </a>
                                        </li>
                                       <?php } ?>