
<?php 
if(isset($_REQUEST['btnReq'])){
    $userRec = $_REQUEST['userRec'];
    $recAppoint = $_REQUEST['recAppoint'];
    $DeptRec = $_REQUEST['DeptRec'];

    $clntId = $_REQUEST['clntId'];
    $subj_matt = $_REQUEST['subj_matt'];
    $reqdate = date('Y-m-d', strtotime($_REQUEST['reqdate'])); 
    $reqType = $_REQUEST['reqType'];
    $office = $_REQUEST['office'];
    
    $dueDate = date('Y-m-d');
    $today = date('Y-m-d H:i:s');
    $year= date('Y');

    ###### GENERATE CODE ######
    $this_Yr =  substr($year,2,2);
    $mon = date('m');
    $dy = date('d');
    $msgecontent = NULL;

    $stms_last = $db->prepare("SELECT regst_id FROM tbl_info_register ORDER BY regst_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);
		$reg = $rows['regst_id']+1;
	}else{
		$reg = 1;
	}

	}
	catch (PDOException $ex) {
		echo $ex->getMessage();
	}
    
    ####### INFO #####
    $uniqueNo = $this_Yr.$mon.$dy.$reg; 
    $statement = $db->prepare("INSERT INTO tbl_info_register(track_id, clnt_id,sub_matt, reg_due, acc_sder, appointsnt, reg_dept, prio, created_reg_trm,doctype, requestDate, reqtype, stts_reg)
    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
    $statement->execute(array($uniqueNo,$clntId,$subj_matt,$dueDate,$userRec,$recAppoint,$DeptRec,0,$today,3,$reqdate,$reqType,1));
    $infoLastId = $db->lastInsertId();
    
   ######CONN FILE ####
   $stmt = $db->prepare("INSERT INTO tbl_con_infos (reg_info_id, dSent, appntsent, iSd,info_sts,conTrm,dRcv,recvappnt,iRcv,dueCon,con_doctype) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
   $stmt->execute(array($infoLastId, $DeptRec, $recAppoint,$userRec,2,$today,$office,0,0,$dueDate,3));

   ####### FILES ##########
   $attch=1;
   $stt = 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,attach_type) 
         VALUES(:rec_dpt,:registID,:usID,:docAppoint,:docName,:uploaded_on,:stts_doc,:attach_type)");
         $stmt2->execute(array(':rec_dpt'=>$DeptRec,':registID' => $infoLastId, ':usID' => $userRec, ':docAppoint' => $recAppoint, 
         ':docName' => $filename, ':uploaded_on' => $today,':stts_doc'=>$stt,':attach_type'=>$attch));
         // end to insert tbl_doc

            $totalFileUploaded++;
          }
     }

}

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

echo("<meta http-equiv='refresh' content='3'>");
}

######## NEW CLIENT #########
if(isset($_REQUEST['btnClient'])){
    $clieCode = $_REQUEST['clieCode'];
    $ClieName = $_REQUEST['ClieName'];
    $phoneNo = $_REQUEST['phoneNo'];
    $duedt = date('Y-m-d');

    ##### CHECK Client ###### 
    $checkClie = $db->prepare('SELECT *FROM tbl_clients where ct_phone="'.$phoneNo.'"');
    $checkClie->execute();
    if($checkClie->rowCount()==0){
    $statement = $db->prepare("INSERT INTO tbl_clients(code_clnt, ct_names,ct_phone, stsclnt, due_ct, ct_pvg)
    VALUES(?,?,?,?,?,?)");
    $statement->execute(array($clieCode,$ClieName,$phoneNo,1,$duedt,2));

    echo'<script type="text/javascript">
        jQuery(function validation(){
        swal("Well Done!", "New client saved successfully!", "success", {
        button: "Continue",
        });
        });
    </script>';
    }else{
        echo'<script type="text/javascript">
        jQuery(function validation(){
        swal("Oooh Snap", "Client already exist!", "error", {
        button: "Continue",
        });
        });
    </script>'; 
    }

}
?>

<form id="" action="" method="POST" enctype="multipart/form-data" autocomplete="off">
    <br><br> 
    <div class="row">
    <div class="col-md-1"></div>
    <div class="col-md-10" style="background-image: url(../img/placeholders/inner-texture-bg.png); border-radius: 8px;">
      <div style='border-radius: 8px;border: 2px solid #7E9350;width:100%;margin-top:1%;margin-bottom:1%;'>
        <br/>
        <center>
           <b style="font-family: century gothic;font-size: 23px;"><i class="fa fa-info-circle"></i> Request info.</b>
        </center>
        <br/>

        <input type="hidden" name="userRec" value="<?php echo $userid;?>" required style="width: 90%;">
        <input type="hidden" name="recAppoint" value="<?php echo $tle_id;?>" required style="width: 90%;">
        <input type="hidden" name="DeptRec" value="<?php echo $role_id;?>" required style="width: 90%;">
                
            <div class="form-group row">                
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Client Name<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                    <select class="select2 form-control w-100" name="clntId" id="clntId" data-placeholder="-select client-" required>
                        <option></option>
                            <?php
                                $sth = $db->query('SELECT * FROM tbl_clients WHERE stsclnt=1 AND ct_pvg=2 ORDER BY ct_id DESC');
                                try
                                {
                                    while ($rr2 = $sth->fetch(PDO::FETCH_ASSOC)) {
                                    ?>
                                        <option value="<?php echo $rr2['ct_id']; ?>"><?php echo $rr2['ct_names']; ?></option>
                                    <?php
                                    }
                                    }
                                    catch (PDOException $ex) {
                                    //Something went wrong rollback!
                                    echo $ex->getMessage();
                                }
                            ?>	
                    </select>
                </div>

                <div class="col-sm-1">
                    <button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#addNewClient" style="border-radius: 4px;">
                    <i class="fa fa-plus-circle"></i></button>
                </div>

                <label class="col-form-label col-1 col-lg-1 form-label text-lg-right">Subject Matter<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                   <textarea type="text" name="subj_matt" rows="2" class="form-control" placeholder="Write here..." required></textarea>
                </div>
            </div>
            
            <div class="form-group row">              
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Due Date<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">                  
                    <input type="date" class="form-control" name="reqdate" placeholder="Request date" required>
                </div>

                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Request Type<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">                  
                    <select class="select2 form-control w-100" name="reqType" id="reqType" data-placeholder="-select type-" required>
                      <option></option>
                      <?php
                        $stmt = $db->query('SELECT * FROM tbl_reqs_type WHERE ststype=1 ORDER BY reqtype_id ASC');
                        try
                        {
                            while ($rrr = $stmt->fetch(PDO::FETCH_ASSOC)) {
                            ?>
                                <option value="<?php echo $rrr['reqtype_id']; ?>"><?php echo $rrr['reqtype_name']; ?></option>
                            <?php
                            }
                            }
                            catch (PDOException $ex) {
                            //Something went wrong rollback!
                            echo $ex->getMessage();
                        }
                    ?>	
                    </select>
                </div>
            </div>

            <div class="form-group row">              
            <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Document Files<span class="required" style="color:red">*</span></label>
            <div class="col-3 col-lg-3">
                <input type="file" name="file[]" id="file" class="form-control-file" multiple required>   
            </div>

            <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Office<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                    <select class="select2" name="office" id="office" data-placeholder="-choose office-" required>
                    <option></option>
                    <?php
                    $stmt = $db->query('select * from tbl_role');

                        try
                        {
                            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                                ?>
                                    <option value="<?php echo $row['role_id']; ?>"><?php echo $row['role_name']; ?></option>
                                <?php
                                }
                            }
                            catch (PDOException $ex) {
                            //Something went wrong rollback!
                            echo $ex->getMessage();
                        }
                    ?>
                    </select>	
                </div> 
            </div>
             
        <hr>
        <center>
            <button type="reset" class="btn btn-default btn-sm" style="border-radius: 4px;">Reset</button>
            <button class="btn btn-primary btn-sm" name="btnReq" style="border-radius: 4px;" type="submit" id="">Save Changes</button>
        </center>
         <br>
    </div> <!--border -->
    </div>
  </div>
</form>

