<?php 
if(isset($_REQUEST['sendCompose'])){
    $receipto = $_REQUEST['receipto'];
    $subj = $_REQUEST['subj'];
    $msgecontent = $_REQUEST['msgecontent'];
    $userSent = $_REQUEST['userSent'];
    $sentAppoint = $_REQUEST['sentAppoint'];
    $DeptSent = $_REQUEST['DeptSent'];
    $document_type = $_REQUEST['document_type'];

    $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');

    $client = 1;

    $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,info_notes, reg_due, acc_sder, appointsnt, reg_dept, prio, created_reg_trm,doctype, stts_reg)
    VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
    $statement->execute(array($uniqueNo,$client,$subj,$msgecontent,$dueDate,$userSent,$sentAppoint,$DeptSent,0,$today,$document_type,1));
    $infoLastId = $db->lastInsertId();
    
    ######## CON ########
    foreach($receipto as $value)
    {
        if ($value<>0)
        {

            $stmt = $db->prepare("INSERT INTO tbl_con_infos (reg_info_id, dSent, appntsent, iSd,info_sts,conTrm,dRcv,recvappnt,iRcv,dueCon) VALUES(?,?,?,?,?,?,?,?,?,?)");
            $stmt->execute( array($infoLastId, $DeptSent, $sentAppoint,$userSent,1,$today,0,$value,0, $dueDate));
        }
    }

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

                $totalFileUploaded++;
          }
     }

}


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

}
?>