<?php 
if(isset($_REQUEST['btnProj'])){

    $prjname = $_REQUEST['prjname'];
    $clnt = $_REQUEST['clnt'];
    $agreeno = $_REQUEST['agreeno'];
    $signdate = date('Y-m-d', strtotime($_REQUEST['signdate'])); 
    $start_date = date('Y-m-d', strtotime($_REQUEST['start_date'])); 
    $withtax = $_REQUEST['withtax'];
    $pcode = $_REQUEST['pcode'];
    $durat = $_REQUEST['durat'];
    $agree_mont = str_replace(',', '', $_REQUEST['agree_mont']);

    $userRec = $_REQUEST['userRec'];
    $recAppoint = $_REQUEST['recAppoint'];
    $Deptrec = $_REQUEST['Deptrec'];

    $todate = date('Y-m-d H:i:s');

    $statement = $db->prepare("INSERT INTO tbl_projects(codeproj, aggreno, projname,wt, clnt, agreeMont, signdate, startDue, durat, created_trm, prjsts)
    VALUES(?,?,?,?,?,?,?,?,?,?,?)");
    $statement->execute(array($pcode,$agreeno,$prjname,$withtax,$clnt,$agree_mont,$signdate,$start_date,$durat,$todate,1));
    $ProjLastId = $db->lastInsertId();

    $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,prjID,usID,docAppoint,docName,uploaded_on,stts_doc) VALUES(:rec_dpt,:prjID,:usID,:docAppoint,:docName,:uploaded_on,:stts_doc)");
            $stmt2->execute(array(':rec_dpt'=>$Deptrec,':prjID' => $ProjLastId, ':usID' => $userRec, ':docAppoint' => $recAppoint, ':docName' => $filename, ':uploaded_on' => $todate,':stts_doc'=>$stt));
            // end to insert tbl_doc

                    $totalFileUploaded++;
            }
        }

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

}
?>