<?php 
    require_once ("../var/res.php");
    if(ISSET($_POST['res'])){
    try {
        
	   $i = 0;
       $Gtot=0;
       $req=$_POST['res'];
       $con=$_POST['con'];
       $asgn=$_POST['asgn'];
       $appoi=$_POST['appoi'];
       ####### TITLE #####
	   $stmt = $db->prepare('SELECT * FROM tbl_boq_title WHERE req_tle="'.$req.'" AND conbtle="'.$con.'" AND sts_btle=8 ORDER BY btle_id DESC LIMIT 1');
	    
	   $stmt->execute(array());
	   $title_count = $stmt->rowCount();

	   if($title_count > 0)
	   {
       	while ($rows = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $btle_id=$rows['btle_id'];
	?>
    <tr>
        <td></td>        
        <td style="font-weight: bold;font-size: 16px;color: #555;"><?php echo $rows['boqtitle_name'];?></td>
        <td></td>  
        <td></td>  
        <td></td>  
        <td></td>          
	</tr>
    <?php 
    ########## ELEMENTS #################
    $eno=0;
    $elemQue=$db->prepare('SELECT *FROM tbl_elements WHERE elemid 
    IN(select elmnt from tbl_boqs where boqtitle="'.$btle_id.'" AND breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4)');
    $elemQue->execute();
    while($getelem = $elemQue->fetch()){
        $elemid = $getelem['elemid'];
        $eno++;
        
        $subTot=0;
    ?>
    <tr>
        <td></td>        
        <td style="font-weight: bold;font-size: 14px;"><?php echo "Element N<sup>o</sup>".$eno.": ".$getelem['elemName'];?></td>      
        <td></td>  
        <td></td>  
        <td></td>  
        <td></td>  
	</tr>


    <?php
    ###### CATEGORY #####
    $ctgQue=$db->prepare('SELECT *FROM tbl_ictgs WHERE ictg_id 
    IN(select bctg from tbl_boqs where elmnt="'.$elemid.'" AND boqtitle="'.$btle_id.'" AND breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4)');
    $ctgQue->execute();
    while($qq=$ctgQue->fetch()){
        $ictg_id=$qq['ictg_id'];
    ?>
    <tr>
        <td></td>        
        <td style="font-weight: bold;font-size: 11px;color: #555;font-style: italic;">
            <?php echo $qq['ctgi_name'];?>
        </td>  
        <td></td>  
        <td></td>  
        <td></td>  
        <td></td>      
	</tr>

    <?php 
    ####### ITEM ########
    
    $itemQue=$db->prepare('SELECT *FROM tbl_boqs b
    INNER JOIN tbl_items i ON i.item_id=b.itmid 
    INNER JOIN tbl_units u ON u.unit_id=i.iUnit
    WHERE bctg="'.$ictg_id.'" AND elmnt="'.$elemid.'" AND boqtitle="'.$btle_id.'" 
    AND breq_id="'.$req.'" AND con_idboq="'.$con.'" AND stsboq=4');
    $itemQue->execute(array());
    while($bit=$itemQue->fetch()){
        $boqty=$bit['boqty'];
        $unit_prix=$bit['unit_prix'];
        $totMont=$boqty*$unit_prix;
        
        ##### SUBTOT ELEM #######
        $subTot=$subTot+$totMont;

        ###### TOTAL ########
        $Gtot= $Gtot+$totMont;
   
    ?>
    <tr>
    <td>
        <div class="custom-control custom-checkbox">
            <input type="checkbox" name="delids[]" value="<?php echo $bit['boq_id'];?>" class="custom-control-input active" id="ckit_<?php echo $bit['boq_id'];?>">
            <label class="custom-control-label" for="ckit_<?php echo $bit['boq_id'];?>"></label>
        </div>
    </td>

    <td><?php echo $bit['item_name'];?></td>
    <td class="text-right"><?php echo $bit['unit_name'];?></td>
    <td class="text-right"><?php echo $boqty;?></td>
    <td class="text-right"><?php echo number_format($unit_prix);?></td>
    <td class="text-right" style="font-weight: bold;"><?php echo number_format(round($totMont, 2));?></td>

    </tr>
    <?php 
        }
    //ITEM END
    ?>

    <?php 
        }
    //CATEGORY END
    ?>
    <tr style="background-color: #f3f3f3;">
        <th></th>
        <th style="font-weight: bold;">SUB. TOTAL</th>
        <th></th>
        <th></th>
        <th></th>        
        <th style="font-weight: bold;" class="text-right"><?php echo number_format(round($subTot, 2));?></th>
    </tr>
    <?php 
        }
    //ELEMENT END
    ?>

	<?php 	
       	}//END. TITLE
       	
	   }//End Count.	
       }
		catch (PDOException $ex) {
		echo $ex->getMessage();
		}		
    }
	?>
    
    <tr>
        <th style="font-weight: bold;" colspan="5">TOTAL</th>
        <th style="font-weight: bold;" class="text-right"><?php echo number_format(round($Gtot, 2));?></th>
    </tr>