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

    $pymtdate = date('Y-m-d', strtotime($_REQUEST['pymtdate'])); 
    $clnt = $_REQUEST['clnt'];
    $prjc = $_REQUEST['prjc'];
    $PayAmount = str_replace(',', '', $_REQUEST['PayAmount']);
    $bankAcc = $_REQUEST['bankAcc'];
    $payNo = $_REQUEST['payNo'];
    $pcmnt = $_REQUEST['pcmnt'];

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

    $statement = $db->prepare("INSERT INTO tbl_payments(projPay, pmtClnt, bankid, txidRef, payMont, pymtDate, trmPay, payDept, appointPay, userPay, pmtSts,cmtpy)
    VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
    $statement->execute(array($prjc,$clnt,$bankAcc,$payNo,$PayAmount,$pymtdate,$todate,$Deptrec,$recAppoint,$userRec,1,$pcmnt));
    
    $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' => $prjc, ':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("Payment Recorded", "Successfully", "success", {
        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%;'>
          <center>
          <b style="font-family: century gothic;font-size: 23px;"><i class="fa fa-circle-o-notch"></i> Payment Record</b>
          </center>
          <br /><br />

            <div class="form-group row">
                <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%;">

                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Date<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                <input type="text" class="form-control" name="pymtdate" id="datepicker-2" placeholder="choose date" required>
                </div>

                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Account<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                    <select class="select2 form-control w-100" name="bankAcc" id="bankAcc" data-placeholder="-choose account-" required>
                            <option></option>
                                <?php
                                    $sth = $db->query('SELECT * FROM tbl_bank_accounts ORDER BY bank_id DESC');
                                    try
                                    {
                                        while ($rr2 = $sth->fetch(PDO::FETCH_ASSOC)) {
                                        ?>
                                            <option value="<?php echo $rr2['bank_id']; ?>"><?php echo $rr2['bank_name']." | ".$rr2['bank_account']; ?></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">Amount<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                <input type="text" name="PayAmount" id="PayAmount" class="form-control" required placeholder="0.0" 
                 oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">                  
                </div>

                
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Client<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">                   
                    <select class="select2 form-control w-100" name="clnt" id="clnt" data-placeholder="-select client-" required>
                        <option></option>
                        <?php
                            $sth = $db->query('SELECT * FROM tbl_clients WHERE stsclnt=1 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>

            <div class="form-group row">
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Project<span class="required" style="color:red">*</span></label>
                <div class="col-3 col-lg-3">
                    <select class="select2 form-control w-100" name="prjc" id="prjc" data-placeholder="-select project-" required>
                        <option></option>
                            <?php
                                $sth = $db->query('SELECT * FROM tbl_projects WHERE prjsts != 10 ORDER BY projid DESC');
                                try
                                {
                                    while ($rr2 = $sth->fetch(PDO::FETCH_ASSOC)) {
                                    ?>
                                        <option value="<?php echo $rr2['projid']; ?>"><?php echo $rr2['codeproj']; ?></option>
                                    <?php
                                    }
                                    }
                                    catch (PDOException $ex) {
                                    //Something went wrong rollback!
                                    echo $ex->getMessage();
                                }
                            ?>	
                    </select>                  
                </div>
                
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">REF N<sup>o</sup>/Txid</label>
                <div class="col-3 col-lg-3">
                  <input type="text" class="form-control" name="payNo" id="payNo" placeholder="eg: 000.." required>
                </div>
            </div>

            <div class="form-group row">
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Attach Files</label>
                <div class="col-3 col-lg-3"> 
                    <input type="file" name="file[]" id="file" class="form-control-file" multiple>
                </div>
                
                <label class="col-form-label col-2 col-lg-2 form-label text-lg-right">Comment</label>
                <div class="col-3 col-lg-3">                
                    <textarea type="text" name="pcmnt" rows="2" class="form-control" placeholder="Write Here..." required></textarea>
                </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="btnPay" style="border-radius: 4px;" type="submit" id="">Record Payment</button>
            </center>
         <br>
    </div> <!--border -->
    </div>
  </div>
</form>

<script>
 $(document).ready(function(){
    //apply on typing and focus
  $('#PayAmount').on('blur',function(){
       $(this).manageCommas();
    });
  $('#PayAmount').on('focus',function(){
        $(this).santizeCommas();
    });
  });

  String.prototype.addComma = function() {
    return this.replace(/(.)(?=(.{3})+$)/g,"$1,").replace(',.', '.');
  }
  //Jquery global extension method
  $.fn.manageCommas = function () {
      return this.each(function () {
          $(this).val($(this).val().replace(/(,|)/g,'').addComma());
      });
  }

  $.fn.santizeCommas = function() {
    return $(this).val($(this).val().replace(/(,| )/g,''));
  }   
</script>