<?php 
$req=$_REQUEST['req'];
$conreq=$_REQUEST['conreq'];
$asgnid=$_REQUEST['asgnid'];
$appoint=$_REQUEST['appoint'];

###### GET REQUEST ######
$stmtReq = $db->prepare('SELECT *FROM tbl_info_register i
INNER JOIN tbl_con_infos cn ON cn.reg_info_id=i.regst_id 
INNER JOIN tbl_clients c ON c.ct_id=i.clnt_id 
INNER JOIN tbl_reqs_type r ON r.reqtype_id=i.reqtype
WHERE cn.con_info_id="'.$conreq.'"');
$stmtReq->execute();
while($ffReq = $stmtReq->fetch()){
   $sub_matt=$ffReq['sub_matt'];

}
###### END GET REQ ######

#####GET ASSIGNED APPOINTMENT ####
$asgnQue=$db->prepare('SELECT *FROM tbl_assign_reqs a
INNER JOIN tbl_title t ON t.title_id=a.asgn_appoint 
WHERE assreq_id ="'.$asgnid.'"');
$asgnQue->execute();

$rr=$asgnQue->fetch();
$titlename=$rr['title_name'];
#####---END ASSIGNED APPOINTMENT ####

##### NEW TITLE #####
if(isset($_REQUEST['btnBoqtitle'])){

$req_tle = $_REQUEST['req_tle'];
$conbtle = $_REQUEST['conbtle'];
$appointle = $_REQUEST['appointle'];
$dept_tle = $_REQUEST['dept_tle'];
$usrec_ttle = $_REQUEST['usrec_ttle'];
$boqtitle = $_REQUEST['boqtitle'];

$duedtrm = date('Y-m-d H:i:s');
$statement = $db->prepare("INSERT INTO tbl_boq_title(req_tle, conbtle, boqtitle_name,trmtle, appointle, dept_tle, usrec_ttle,sts_btle)
VALUES(?,?,?,?,?,?,?,?)");
$statement->execute(array($req_tle,$conbtle,$boqtitle,$duedtrm,$appointle,$dept_tle,$usrec_ttle,1));

echo'<script type="text/javascript">
    jQuery(function validation(){
    swal("Successfully!", "New BOQ title saved.", "success", {
    button: "Continue",
    });
    });
</script>';

}

##### NEW TITLE #####
if(isset($_REQUEST['datasavecomp'])){

    $req_comp = $_REQUEST['req_comp'];
    $conbcomp = $_REQUEST['conbcomp'];
    $appoincomp = $_REQUEST['appoincomp'];
    $dept_comp = $_REQUEST['dept_comp'];
    $usrec_tcomp = $_REQUEST['usrec_tcomp'];
    $boqtitle_id = $_REQUEST['boqtitle_id'];
    $boqcomp = $_REQUEST['boqcomp'];
    
    $duedtrm = date('Y-m-d H:i:s');
    $statement = $db->prepare("INSERT INTO tbl_boq_compnt(boqtle_id, req_cpnt, boqcpnt_name, conbcpnt,trmComp, appoicpnt, dept_cpnt, usrec_cpnt,sts_bcpnt)
    VALUES(?,?,?,?,?,?,?,?,?)");
    $statement->execute(array($boqtitle_id,$req_comp,$boqcomp,$conbcomp,$duedtrm,$appoincomp,$dept_comp,$usrec_tcomp,1));
    
    echo'<script type="text/javascript">
        jQuery(function validation(){
        swal("Successfully!", "New BOQ Components saved.", "success", {
        button: "Continue",
        });
        });
    </script>';
    
    }

 ####### UPDATE ######
 if(isset($_POST['updBoqtitle'])){
    try{
        $boqtitle = $_POST['boqtitle'];
        $btileid = $_POST['btileid'];
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        $sql=$db->query("UPDATE `tbl_boq_title` SET `boqtitle_name` = '$boqtitle' WHERE `btle_id` = '$btileid'");
        $msg="Updated Successfully";
        
    }catch(PDOException $e){
        echo $e->getMessage();
    }  
}

$sth1 = $db->query('SELECT * FROM tbl_boq_title WHERE req_tle="'.$req.'" AND appointle="'.$appoint.'" 
AND sts_btle=1 ORDER BY btle_id DESC');
$nubtle=$sth1->rowCount();
while($rr1 = $sth1->fetch(PDO::FETCH_ASSOC)) {
    $btle_id=$rr1['btle_id'];
    $boqtitlename=$rr1['boqtitle_name'];
}

// Delete Item....
if(isset($_REQUEST['delcpnt'])){
    $delcpnt = $_REQUEST['delcpnt'];
    $check = $conn->prepare("SELECT * FROM `tbl_boqs` WHERE `copnt_id`='$delcpnt' ");
    $check->execute();
    if($check->rowCount()==0){
    $d=$conn->prepare("DELETE from `tbl_boq_compnt` WHERE `bcpnt_id`='".$_REQUEST['delcpnt']."' ");
    $d->execute();
      $msge="Data Removed";
    }else{
      $msg="Do not delete this data";  
    }
    
 }

 if(isset($_REQUEST['desgnSubmit'])){
    $reqid = $_REQUEST['reqid'];
    $conid = $_REQUEST['conid'];
    $appoin = $_REQUEST['appoin'];
    $dept = $_REQUEST['dept'];
    $usrec = $_REQUEST['usrec'];

    $dueDate = date('Y-m-d');
    $today = date('Y-m-d H:i:s');
    $year= date('Y');
    
    $upd = $db->prepare("UPDATE `tbl_con_infos` SET `info_sts`=3,cpte_date='".$dueDate."' WHERE `con_info_id`='".$conid."' ");
    $upd->execute();

    ##### DOC ######
    $stt = 1;
    $isdesign=1;
    ######## FILES UPLOAD #############
    $countfiles = count($_FILES['file']['name']);
    
    $totalFileUploaded=0;

    for($i=0;$i<$countfiles;$i++){
     $stms_last = $db->prepare("SELECT doc_id FROM tbl_docs  ORDER BY doc_id DESC LIMIT 1");
        try {
            $stms_last->execute(array());
            $row_count_last = $stms_last->rowCount();
            if ($row_count_last > 0) {
                $rows = $stms_last->fetch(PDO::FETCH_ASSOC);
                    $doc_id = $rows['doc_id']+1;
             }else{
                $doc_id = 1;
             }
        }
        catch (PDOException $ex) {
         echo $ex->getMessage();
        } 
    
          
         $filename = $doc_id."_".$_FILES['file']['name'][$i];
    
         ## Location
         $location = $path_upload.$filename;
         $extension = pathinfo($location,PATHINFO_EXTENSION);
         $extension = strtolower($extension);
    
         ## File upload allowed extensions
         $valid_extensions = array("jpg","jpeg","png","pdf","doc","docx","xls","xlsx","ppt","pptx","zip","tar"."gz");
    
         $response = 0;
         ## Check file extension
         if(in_array(strtolower($extension), $valid_extensions)) {
              ## Upload file
              if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$location)){
    
                 //    echo "file name : ".$filename."<br/>";
             $stmt2 = $db->prepare("INSERT INTO tbl_docs(rec_dpt,registID,usID,docAppoint,docName,uploaded_on,stts_doc,is_dsgn,attach_type) VALUES(:rec_dpt,:registID,:usID,:docAppoint,:docName,:uploaded_on,:stts_doc,:is_dsgn,:attach_type)");
             $stmt2->execute(array(':rec_dpt'=>$dept,':registID' => $reqid, ':usID' => $usrec, ':docAppoint' => $appoin, ':docName' => $filename, ':uploaded_on' => $today,':stts_doc'=>$stt,':is_dsgn'=>$isdesign,':attach_type'=>2));
             // end to insert tbl_doc
    
                $totalFileUploaded++;
              }
         }
    
    }

    echo'<script type="text/javascript">
        jQuery(function validation(){
        swal("Successfully!", "Design Uploaded.", "success", {
        button: "Continue",
        });
        });
    </script>';    

}

if(isset($_REQUEST['desgnsaveData'])){
    ######## UPDATE DATE NOTE ####
    $updNotes = $db->prepare("UPDATE `tbl_con_infos` SET info_sts=8 WHERE `con_info_id`='".$_REQUEST['conid']."'");
    $updNotes->execute();
    ######## END UPDATE DATE NOTE ####

    ######## UPDATE INFO ####
    $updinfo = $db->prepare("UPDATE `tbl_info_register` SET stts_reg=8 WHERE `regst_id`='".$_REQUEST['reqid']."'");
    $updinfo->execute();
    ######## END INFO ####

    ######## UPDATE BOQ TITLE ####
    $updinfo = $db->prepare("UPDATE `tbl_boq_title` SET sts_btle=8 WHERE `req_tle`='".$_REQUEST['reqid']."'");
    $updinfo->execute();
    ######## END INFO ####

    ######## UPDATE COMPONENT ####
    $updinfo = $db->prepare("UPDATE `tbl_boq_compnt` SET sts_bcpnt=8 WHERE `req_cpnt`='".$_REQUEST['reqid']."'");
    $updinfo->execute();
    ######## END INFO ####

    echo'<script type="text/javascript">
    jQuery(function validation(){
    swal("Successfully!", "Data saved.", "success", {
    button: "Continue",
    });
    });
</script>'; 
}


?>


?>
<div class="subheader">
    <h1 class="subheader-title" style="font-weight: bold;">
        <i class='subheader-icon fa fa-bars text-primary'></i><?php echo $title." by ".$titlename;?> 
    </h1>
</div>

<div class="card mb-g border shadow-0">    

    <div class="card-header bg-white p-0 header">
        <div class="p-3 d-flex flex-row">
            <div class="d-block ml-2">
                <span class="h6 font-weight-bold text-uppercase d-block m-0">
                   Sub: <?php echo $sub_matt;?>
                </span>
                
                <div class="d-flex mr-4">                    
                   <li class="position-absolute pos-top pos-right d-none d-sm-block"  style="padding: 10px;">
                    <?php 
                    if($nubtle>0){
                    ?>
                    <button class="btn btn-default btn-sm" data-toggle="modal" data-target="#updBtitle"><i class="fa fa-edit"></i>Update BOQ Title</button>
                    <?php
                    }else{
                    ?>
                    <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#addNewTitle"><i class="fa fa-edit"></i> Create BOQ Title</button>
                   <?php }?> 
                </li>
                </div>
                <div class="clearfix"></div>                  
            </div>
        </div>
    </div>

    <div class="card-body">
    <br>

    <div class="row">
        <div class="col-md-8">
            <?php 
            if($nubtle==0){

            }else{
            ?>
            <h3><?php echo $boqtitlename;?></h3>
            <hr>
            
            <?php 
            include 'add_boq_comp.php';

            }?>
        </div>
        
        <div class="col-md-4">
        <a href="#" data-toggle="modal" data-target="#DesgnBoq" class="btn btn-success btn-lg pull-right" style="border-radius: 4px;" title="Submit Design">
            <i class="fa fa-upload" aria-hidden="true"></i>Upload Design Structure
        </a>
        </div>

    </div>
<br>
    <div class="row">
        <div class="col-md-8">        
        <ul class="list-group">
            
            <?php 
            if($nubtle>0){
            $viewCompnents = $db->query('SELECT *FROM tbl_boq_compnt WHERE boqtle_id="'.$btle_id.'" AND sts_bcpnt!=7');
            while($rrrr = $viewCompnents->fetch()){
            ?>
            <li class="list-group-item header"><?php echo $rrrr['boqcpnt_name'];?> 
            <a href="gate?b=genDesgn&req=<?php echo $req;?>&conreq=<?php echo $conreq;?>&asgnid=<?php echo $asgnid;?>&appoint=<?php echo $appoint;?>&delcpnt=<?php echo $rrrr['bcpnt_id'];?>" class="btn btn-default btn-xs pull-right"><i class="fa fa-trash-o"></i></a></li>
            <?php } }?>
        </ul>
        </div>
        
        <div class="col-md-4">
        <?php 
            include 'desgn_str2.php'; 
        ?>
        </div>
    </div>

   <br>
    <div class="card-footer">
        <div class="d-flex align-items-center">
            <?php ?>
            <span class="text-sm text-muted font-italic">
                <a href="#" data-toggle="modal" data-target="#saveDesgnBoq" class="btn btn-success btn-sm" style="border-radius: 4px;" title="Submit BOQ">
                    <i class="fa fa-check" aria-hidden="true"></i>Submit Data
                </a>                
            </span>
            
            <?php ?>
            <a href="gate?b=ontsk" class="flex-shrink-0 ml-auto" style="font-weight: bold;">Go back <i class="fa fa-mail-reply ml-2"></i> </a>
        </div>
    </div>

</div>  
</form>

<?php 
    include 'boq_title_modal.php';
    include 'modal_designstrc_boq.php';
    include 'updateBoqTitle.php';

    include 'desgnSubmit.php';
?>
 
