
<?php 
// Delete Item....
if(isset($_REQUEST['del-mat'])){
    $DeMat = $_REQUEST['del-mat'];
    $check_boq = $conn->prepare("SELECT * FROM `tbl_boqs` WHERE `itmid`='$DeMat' ");
    $check_boq->execute();
    if($check_boq->rowCount()==0){
    $d=$conn->prepare("DELETE from `tbl_items` WHERE `item_id`='".$_REQUEST['del-mat']."' ");
    $d->execute();
      $msge="Data Removed";
    }else{
      $msg="Do not delete this data";  
    }
    
}

 if(isset($_REQUEST['btnMat'])){
    $matname = $_REQUEST['matname'];
    $iDesc = $_REQUEST['iDesc'];
    $iunit = $_REQUEST['iunit'];
    $itype = 0;
    $iCateg = $_REQUEST['iCateg'];
    ###### INSERT DATA ############
    
    $iStmt = "INSERT INTO tbl_items (item_name,itype,dec_item,icateg_id,iUnit,i_stts,splzi) VALUES (?,?,?,?,?,?,?)";
    $iQuery = $db->prepare($iStmt);
    $iQuery->execute(array($matname,$itype,$iDesc,$iCateg,$iunit,1,2));   
    $msg="Maerial info. saved successfully";
 }
 
 
if(ISSET($_POST['btnUpdte'])){
    $mat_name_upd = $_REQUEST['mat_name_upd'];
    $mat_id_upd = $_REQUEST['mat_id_upd'];
    $desc = $_REQUEST['desc'];
    $unt = $_REQUEST['unt'];
    $ictg = $_REQUEST['ictg'];
    $upd = $conn->prepare("UPDATE `tbl_items` SET `item_name`='".$mat_name_upd."',`icateg_id`='".$ictg."',`dec_item`='".$desc."',`iUnit`='".$unt."' WHERE `item_id`='".$mat_id_upd."' ");
    $upd->execute();
    
    $msg="Updated successfully";
}

// for activate Item   	
if(isset($_REQUEST['acid']))
{  
  try{
        $a = 1;
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = "UPDATE `tbl_items` SET `i_stts` = '".$a."' WHERE `item_id` = '".$_REQUEST['acid']."'";
        $conn->exec($stmt);
        $msg="Activated successfully";
    }catch(PDOException $e){
        echo $e->getMessage();
    }
 }

// for Deactivate Subject
else if(isset($_REQUEST['did']))
{
try{
        $a = 2;
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = "UPDATE `tbl_items` SET `i_stts` = '".$a."' WHERE `item_id` = '".$_REQUEST['did']."'";
        $conn->exec($stmt);
        $msge="Deactivated successfully";
    }catch(PDOException $e){
        echo $e->getMessage();
    }
      
  }
 ?>

<div class="subheader">
    <h1 class="subheader-title" style="font-weight: bold;">
        <i class='subheader-icon fa fa-bars text-primary'></i><?php echo $title;?> 
    </h1>
    <?php 
     if(empty($_REQUEST['itemid'])){
    ?>
    <div class="d-flex mr-4">
        <li class="position-absolute pos-top pos-right d-none d-sm-block">
            <button class="btn btn-primary" id="add-btn"><i class="fa fa-plus-circle"></i> Add</button>
            <button class="btn btn-default" id="list-btn"><i class="fa fa-eye"></i> View List</button> 
        </li>
    </div>
    <?php }?>
</div>

<?php if(isset($msg)){?>
    <div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <strong>Well Done!</strong> <?php echo htmlentities($msg); ?>
      </div>
    <?php } 
     else if(isset($msge)){?>
     
     <div class="alert alert-danger alert-dismissable">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
      <strong>Oooh Snap!</strong> <?php echo htmlentities($msge); ?>
  </div>
 <?php }
 if(isset($_REQUEST['itemid'])){
 include 'edit_item2.php';
 }else{
?>
<div id="table-result">
    <table id="dt-basic-example" class="table table-hover w-100" style="font-size: 11px;">
        <thead class="header bg-primary" style="color: #fff;">
            <tr>
                <th style="font-weight: bold;width: 5px;"></th>            
                <th style="font-weight: bold;">ITEM NAME</th>
                <th style="font-weight: bold;">CATEGORY</th>
                <th class="text-center" style="font-weight: bold;">UNIT</th>
                <th class="text-right" style="font-weight: bold;">PRICE</th>
                <th class="text-center" style="font-weight: bold;">STATUS</th>
                <th class="text-right" style="font-weight: bold;">ACTION</th>
            </tr>
        </thead>

        <tbody>
        <?php
            $n=0;
            $sql = $conn->prepare("SELECT * FROM tbl_items i 
            INNER JOIN tbl_ictgs c ON c.ictg_id=i.icateg_id
            INNER JOIN tbl_units u ON u.unit_id=i.iUnit
            WHERE splzi=2 ORDER BY item_name DESC");
            $sql->execute();
            while($it = $sql->fetch()){
            $item_id = $it['item_id'];
            $i_stts = $it['i_stts'];
            $n++;
            
            
            $Gtot=0;
            ####### ITEM PRICE #######
            $getPrix = $db->prepare("SELECT * FROM tbl_boqcon b
            INNER JOIN tbl_items i ON i.item_id=b.matid 
            WHERE itmid ='". $item_id."' AND sttscon=4 ORDER BY boqcon_id ASC");                
            $getPrix->execute(array());
            while ($rrprix = $getPrix->fetch(PDO::FETCH_ASSOC)){
                $iPrice = $rrprix['iPrice'];
                $qtyboq = $rrprix['qtyboq'];
                $totMont = $iPrice*$qtyboq;
                $Gtot= $Gtot+$totMont;
            }
        ?>
            <tr class="b-tr">
                <td>
                    <a href="gate?b=assignBoQ&it=<?php echo $item_id;?>" class="btn btn-default btn-sm" title=""Assign BOQ>
                        <i class="fa fa-info-circle" aria-hidden="true"></i>
                    </a>
                </td>                
                <td><?php echo $it['item_name']." ".$it['dec_item'];?></td>     
                <td><?php echo $it['ctgi_name'];?></td>           
                <td class="text-center"><?php echo $it['unit_name'];?></td>
                <td class="text-right"><?php echo number_format($Gtot);?></td>
                <td class="text-center">
                <?php
                if($i_stts == 2 )
                    { 
                ?>
                    <a href="gate?b=itm&acid=<?php echo $item_id;?>" class="btn btn-danger btn-xs" onclick="if(!confirm('Do you really want to Activate this Item?'))return false;else return true;">Inactive</a>
                    <?php }elseif($i_stts == 1){?>
                    <a href="gate?b=itm&did=<?php echo $item_id;?>" class="btn btn-success btn-xs" onclick="if(!confirm('Do you really want to Disactivate this Item?'))return false;else return true;">Active</a>
                <?php }?>
                </td>

                <td class="text-right" width="15%">
                <a class="text-primary" href="gate?b=itm&itemid=<?php echo $item_id;?>" style="font-weight:bold;"><i class="fa fa-edit"></i> Edit</a>
                |
                <a class="text-danger" href="#" data-toggle="modal" data-target="#delete_item<?php echo $item_id;?>" style="font-weight:bold;"><i class="fa fa-trash-o m-r-5"></i> Delete</a>
                </td>
            </tr>
            
            <?php
                include 'delete_item.php';
                }
            ?>
        </tbody>                           
    </table>
</div>

<div id="form-display" style="display: none;">
    <?php include 'itemForm.php';?>
</div>
<?php }?>
<script>
    $("#list-btn").hide();

    $(document).ready(function(){
    $("#add-btn").click(function(){
       $("#form-display").show();
       $("#table-result").hide();
       $("#list-btn").show();
       $("#add-btn").hide();
    });
    
    // SHOW PROJECT DETAILS    
    $("#list-btn").click(function(){
    $("#form-display").hide();
    $("#table-result").show();
    $("#list-btn").hide();
    $("#add-btn").show();
    });

    });
   </script>
