<?php
$admin_id = $_SESSION['admin_id'];
$licence = $_SESSION['admin_licence'];
if($licence == '1'){
$sql = 'SELECT COUNT(noti_new) AS total FROM tbnoti WHERE noti_new = 0 GROUP BY noti_admin HAVING noti_admin = '.$admin_id.'';
$ntq = $db->findorderdesclimit('tbagency,tbnoti,tbadmin,tbbook,tblogin','tbagency.agency_id = tbnoti.noti_agency && tbbook.book_id = tbnoti.noti_book && tbnoti.noti_agency = tblogin.u_agency && tbadmin.admin_id = tbbook.book_admin && u_license = 0 && noti_admin',$admin_id,'noti_id')->execute();
$sql2 = 'SELECT COUNT(noti_admin) AS total2 FROM tbnoti WHERE noti_admin = '.$admin_id.'';
} else if($licence == '2'){
$sql = 'SELECT COUNT(noti_new) AS total FROM tbnoti WHERE noti_new = 0 GROUP BY noti_status HAVING noti_status = 3';
$ntq = $db->findorderdesclimit('tbagency,tbnoti,tbadmin,tbbook,tblogin','tbagency.agency_id = tbnoti.noti_agency && tbbook.book_id = tbnoti.noti_book && tbnoti.noti_agency = tblogin.u_agency && tbadmin.admin_id = tbbook.book_admin && u_license = 0 && noti_status','3','noti_id')->execute();
$sql2 = 'SELECT COUNT(noti_status) AS total2 FROM tbnoti WHERE noti_status = 3';
} else if($licence == '0'){
$sql = 'SELECT COUNT(noti_new) AS total FROM tbnoti WHERE noti_new = 0 GROUP BY noti_status HAVING noti_status = 4';
$ntq = $db->findorderdesclimit('tbagency,tbnoti,tblogin','tbagency.agency_id = tbnoti.noti_agency && tbnoti.noti_agency = tblogin.u_agency && u_license = 0 && noti_status','4','noti_id')->execute();
$sql2 = 'SELECT COUNT(noti_status) AS total2 FROM tbnoti WHERE noti_status = 4';
}

$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$count = $row['total'];
$result2 = mysql_query($sql2);
$row2 = mysql_fetch_array($result2);
$count2 = $row2['total2'];
echo '<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">';
echo '<i class="icon-bell" style="font-size:30px; margin-top:-24px; margin-right:5px"></i>';
if($count != 0){
	echo '<span style="font-size: 10px;padding: 1px 6px;background-color: red;border-radius: 5px;font-weight: bold;margin-top: -13px;margin-left: -12px;position: absolute;">'.$count.'</span>';
} else {
	echo '<span style="font-size: 10px;padding: 1px 6px;background-color: red;border-radius: 5px;font-weight: bold;margin-top: -13px;margin-left: -12px;position: absolute; display:none;">'.$count.'</span>';
}
echo '</a>';
echo '<ul class="dropdown-menu messages">';
echo '<li class="dropdown-menu-title"><span>Notifications</span>';
echo '</li>';

if($count2 != 0){
while($nt = mysql_fetch_array($ntq)){

if($nt['noti_status'] == '0'){
	$ses = 'booking_list&adid='.$nt['agency_id'].'';
} elseif($nt['noti_status'] == '1'){
	$ses = 'booking_list&adid='.$nt['agency_id'].'';
} elseif($nt['noti_status'] == '2'){
	$ses = 'booking_list&adid='.$nt['agency_id'].'';
} elseif($nt['noti_status'] == '3'){
	$ses = 'finance_edit&id='.$nt['book_id'].'';
} elseif($nt['noti_status'] == '4'){
	$ses = 'agency_edit&id='.$nt['u_id'].'';
} elseif($nt['noti_status'] == '5'){
	$ses = 'booking_list&adid='.$nt['agency_id'].'';
} elseif($nt['noti_status'] == '6'){
	$ses = 'booking_list&adid='.$nt['agency_id'].'';
} elseif($nt['noti_status'] == '7'){
	$ses = 'booking_list&adid='.$nt['agency_id'].'';
} elseif($nt['noti_status'] == '10'){
	$ses = 'finance_list&adid='.$nt['noti_trip'].'';
} else {
	$ses = '';
}

if($nt['noti_new'] == '0'){
	echo '<li style="background: rgba(241, 196, 15,0.6);">';
	$class = 'class="noti_btn"';
} else {
	echo '<li>';
	$class = 'class="noti_btn"';
}
echo '<a href="#" code="'.$ses.'" '.$class.' data-id="'.$nt['noti_id'].'">';
echo '<span class="avatar">
		<table cellpadding="0" border="0" cellspacing="0" style="float:left; margin-right:10px;">
		<tr>
		<td style="background-color:#fff; text-align:center; width:40px; height:40px;">
		<img src="logo/thumbnail/'.$nt['agency_logo'].'" style="max-height:40px !important; max-width:40px !important; height:auto !important; width:auto !important; margin:0 !important; float:none !important;">
		</td>
		</tr>
		</table>
	  </span>';
echo '<span class="header">';
echo '<span class="from">';
echo ''.$nt['agency_name_eng'].'';
echo '</span>';
echo '<span class="time">';
echo ''.time_elapsed_string($nt['noti_time']).'';
echo '</span></span>';
echo '<span class="message">';
if($nt['noti_status'] == '0'){
	echo 'ได้ทำการจองทัวร์เลขที่ BT'.$nt['book_id'].'';
} elseif($nt['noti_status'] == '1'){
	echo 'ได้ทำการจองทัวร์เลขที่ BT'.$nt['book_id'].' **Invoice ส่งไม่ไป**';
} elseif($nt['noti_status'] == '2'){
	echo 'ขอยกเลิกใบจองเลขที่ BT'.$nt['book_id'].'';
} elseif($nt['noti_status'] == '3'){
	echo 'มีการจ่ายเงินของใบจองเลขที่ BT'.$nt['book_id'].' กรุณาเช็คใบ Pay-in';
} elseif($nt['noti_status'] == '4'){
	echo 'สมัครเข้ามาใหม่ กรุณาเช็คและทำการอนุมัติเอเจน';
} elseif($nt['noti_status'] == '5'){
	echo 'ใบจองเลขที่ BT'.$nt['book_id'].' ถูกยกเลิกอัติโนมัติเนื่องจากเกินกำหนดชำระ';
} elseif($nt['noti_status'] == '6'){
	echo 'ใบจองเลขที่ BT'.$nt['book_id'].' ถูกยกเลิกแล้ว';
} elseif($nt['noti_status'] == '7'){
	echo 'ใบจองเลขที่ BT'.$nt['book_id'].' ไม่สามารถยกเลิกได้';
} elseif($nt['noti_status'] == '10'){
	echo 'ใบจองเลขที่ BT'.$nt['book_id'].' มีการปิดปรุ๊ป';	
} else {
}
echo '</span></a></li>';
}
echo '<li><a class="dropdown-menu-sub-footer" href="http://www.bestindochina.com/backend/?url=notification">ดูการแจ้งเตือนทั้งหมด</a></li>';
} else {
	echo '<li><a class="dropdown-menu-sub-footer">ไม่มีการแจ้งเตือน</a></li>';
}
echo '</ul>';

?>