<?php 
require("../connection.php");
include("../user.php");
$user_id=$_SESSION['user'];
$username=$_SESSION['user_name'];
$branch_id=$_SESSION['branch_id'];
$ho_access=$_SESSION['ha'];


echo $catgryid=$_REQUEST['catgryid'];

//echo $catgryid;
?>
<script>
$(document).ready(function() {
		$('#example').DataTable();
		} );
</script>

<?php  
$cat_name=mysqli_query($con,"select category  from category_master where id='$catgryid'");
$catname=mysqli_fetch_array($cat_name)
   ?>
   <h4 align="center">CATEGORY-<b style="color:blue;text-transform:uppercase;"><?php echo  $catname['category']; ?></b></h4>

<table id="example" class="table table-striped table-bordered" >
        <thead>
			<tr>
               <th>s.no</th>
               <th>Branch</th>
               <th>asset-id</th>
               <th>po no.</th>
               <th>Issue date</th>
               <th>employee code</th>
               <th>employee name</th>
               <th>department</th>
               <th>location</th>
               <th>warranty</th>
               <th>status</th>
			</tr>
		</thead>
		<?php 
		if($ho_access=="yes")
			{
				$catgry=mysqli_query($con,"SELECT ism.id as id_no, ismt.item_code,ismt.category,ism.issue_date,em.employee_name,em.emp_no,ism.emp_dept,lm.location,ism.expire_date,im.po_no,sm.name as issue,bm.branch_name,ism.issue_date,case when ism.expire_date=CURDATE() THEN 'TODAY EXPIRED' when ism.expire_date>CURDATE() THEN 'PROCESSING' else 'EXPIRED' end as ex_date  FROM `issue_master` ism  join `issue_master_table` ismt on ism.id=ismt.issue_master_id join item_master im on ismt.item_code=im.asset_id join category_master cm on cm.id=ismt.category join employee_master em on em.emp_no=ism.emp_code join location_master lm on lm.id=ism.location join status_master sm on im.status=sm.status_no join branch_master bm on im.branch_id=bm.id where ismt.category='$catgryid'");
			}
			else
			{
				$catgry=mysqli_query($con,"SELECT  ism.id as id_no,ismt.item_code,ismt.category,ism.issue_date,em.employee_name,em.emp_no,ism.emp_dept,lm.location,ism.expire_date,im.po_no,sm.name as issue,bm.branch_name,ism.issue_date,case when ism.expire_date=CURDATE() THEN 'TODAY EXPIRED' when ism.expire_date>CURDATE() THEN 'PROCESSING' else 'EXPIRED' end as ex_date  FROM `issue_master` ism  join `issue_master_table` ismt on ism.id=ismt.issue_master_id join item_master im on ismt.item_code=im.asset_id join category_master cm on cm.id=ismt.category join employee_master em on em.emp_no=ism.emp_code join location_master lm on lm.id=ism.location join status_master sm on im.status=sm.status_no join branch_master bm on im.branch_id=bm.id where ismt.category='$catgryid' and im.branch_id='$branch_id'");					
			}		
	 $s=1;
		while($type=mysqli_fetch_array($catgry))
				{ ?>
		<tbody>
		<tr>
           <td><?php echo $s++;?></td>
           <td><?php echo $type['branch_name'];?></td>
           <td><?php echo $type['item_code'];?></td>
           <td><?php echo $type['po_no'];?></td>
           <td><?php echo date("d-m-Y",strtotime($type['issue_date']));?></td>
           <td><?php echo $type['emp_no'];?></td>
           <td><?php echo $type['employee_name'];?></td>
           <td><?php echo $type['emp_dept'];?></td>
           <td><?php echo $type['location'];?></td>
          <?php if($type['ex_date']=='TODAY EXPIRED') { ?>
            <td style="color:Green;font-weight:400;"><?php echo $type['ex_date']; ?></td>
			<?php } elseif($type['ex_date']=='PROCESSING') { ?>
			<td style="color:Orange;font-weight:400;"><?php echo $type['ex_date']; ?></td>
			<?php } else { ?>
			<td  style="color:Red;font-weight:400;"><?php echo $type['ex_date']; ?></td>
			<?php } ?>
		   <?php
		   $sts=$type['issue'];
		   if($sts=="available")
		   {
		   ?>
		   <td style="color:green;"><?php echo $type['issue'];?></td>
		    <?php
		   }
		   if($sts=="issue")
			    {
		   ?>
		   <td style="color:blue;"><?php echo $type['issue'];?></td>
		    <?php
		   }
		    if($sts=="repair")
			    {
		   ?>
		   <td style="color:red;"><?php echo $type['issue'];?></td>
		    <?php
		   }
		    if($sts=="scrap")
			    {
		   ?>
		   <td style="color:black;"><?php echo $type['issue'];?></td>
		    <?php
		   }
		    if($sts=="in-movement")
			    {
		   ?>
		   <td style="color:orange;"><?php echo $type['issue'];?></td>
		    <?php
		   }
		   ?>
	     </tr>	
		</tbody>
		<?php } ?>
	</table>
		<table class="table table-hover table-bordered" style="font-family:'Times New Roman', Times, serif">
		<?php $rs=$type['id_no']; 
				$cus1=mysqli_query($con,"SELECT im.created_by,cm.category,imt.item_code,itm.value FROM issue_master im left join issue_master_table imt on im.id=imt.issue_master_id left join category_master cm on cm.id=imt.category left join item_master itm on itm.asset_id=imt.item_code  where im.id='$rs' and imt.category='$catgryid' order by im.id desc");
			echo "";
			 $s=1;
			while($item2=mysqli_fetch_array($cus1))
			{
				$creae=$item2['created_by'];
				
			$get_user=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM `user_master` where user_id='$creae'"));
			$crea=$get_user['user_name'];
				$Value=$item2['value'];
			$ssr = explode(",",$Value);
		
			$cntr=count($ssr);
			$rs1=$item2['item_code'];
		?>
			<thead style="color:#0033FF">
			<tr id="dark">	
			<th rowspan="1" id="dark">#</th>	
			<th rowspan="1" id="dark">Category Name</th>
			<th rowspan="1" id="dark">Item Code</th>
			<?php $ravi1=mysqli_fetch_array(mysqli_query($con,"SELECT header FROM item_master where asset_id='$rs1'"));
			
			
			 $name1=$ravi1['header'];
			 $sss = explode(",",$name1);
			
			$cntt=count($sss);
			for($r=0;$r<$cntt;$r++)
			{
			$header_query=mysqli_fetch_array(mysqli_query($con,"select configuration from configuration_master where id=$sss[$r]"));
			
			?>
			<th rowspan="1" id="dark"><?php echo $header_query['configuration']; ?></th>
			<?php }   ?>
			</tr>
			</thead>
			<tr>
			<td><?php echo $s; ?></td>
			<td><?php echo $item2['category']; ?></td>
			<td><?php echo $item2['item_code']; ?></td>
			<?php for($a=0;$a<$cntr;$a++)
			{
			?>
			<td><?php echo $ssr[$a]; ?></td>
			<?php }   ?>
			</tr>


			<?php  $s++;
			
			?>
        <?php 
		}?>
			</table>