<?php
 if(ISSET($_POST['update_invoice'])){
     try{
         $invoice_id = $_POST['invoice_id'];
         $amount = $_POST['invoice_amount'];
         $invdate = $_POST['invceDate'];
         $invComnt = $_POST['invComnt'];
         
         
        $sql = $db->prepare("UPDATE `tbl_invoices` SET `invceMont` = '$amount', `invceDate` = '$invdate', `invComnt` = '$invComnt' WHERE `invceid` = '$invoice_id'");
        $sql->execute();
        
        $msg = "Updated Successfully";
        echo'<meta http-equiv="refresh"'.'content="2;URL=gate?b=inv">';
         
     }catch(PDOException $e){
         echo $e->getMessage();
     }
 }
?>   
     
 <?php
     if(isset($_GET['del_invoice_id'])){
     $invoice_id = $_GET['del_invoice_id'];
     $sql = $db->prepare("UPDATE tbl_invoices SET stsinvce=7 WHERE `invceid`='$invoice_id' ");
     $sql->execute();
     
     $msge = "Data Deleted!";
     echo'<meta http-equiv="refresh"'.'content="2;URL=gate?b=inv">';
     }
 ?>
<?php
    if(!empty($msg)) {?>
<div class="alert alert-success">
	<button type="button" class="close" data-dismiss="alert">×</button>
	<strong>Well done!</strong> <?php echo $msg; ?> 
</div>
<?php
  }
   else if(!empty($msge)) {
?>
<div class="alert alert-error">
	<button type="button" class="close" data-dismiss="alert">×</button>
	<strong>Oh snap!</strong>  <?php echo $msge; ?> 
</div>
<?php
   }
?>
<div class="table-responsive">
	 <table id="dt-basic-example" class="table table-hover table-striped w-100" style="font-size: 11px;">
		<thead class="header">
			<tr>
			    <th style="font-weight: bold;"></th>
                <th style="font-weight: bold;">CLIENT</th>
                <th style="font-weight: bold;">INVOICE DATE</th>
                <th style="font-weight: bold;">PROJECT</th>
                <th style="font-weight: bold;">COMMENT</th>
                <th style="font-weight: bold;">INVOICE TITLE</th>
                <th style="font-weight: bold;">REF</th>
                <th class="text-right" style="font-weight: bold;">AMOUNT</th>
                <th style="font-weight: bold;" width="15%"></th>
			</tr>
		</thead>
		<tbody>
		 <?php
		    $i=0;
            $tot_invoice=0;
            $sth = $db->prepare("SELECT * FROM tbl_invoices i
            INNER JOIN tbl_clients c ON c.ct_id =i.clntInvce 
            INNER JOIN tbl_projects p ON p.projid=i.projInv
            WHERE stsinvce=1 AND invtpe=1 ORDER BY invceid DESC");
            $sth->execute();
            while($ff = $sth->fetch()){
            $tot_invoice=$tot_invoice+$ff['invceMont'];
            $i++;
        ?>
		<tr class="btr">
		    <td><?php echo $i;?></td>
            <td><?php echo $ff['ct_names']; ?></td>
            <td><?php echo date('d/m/Y',strtotime($ff['invcedate']));?></td>            
            <td><?php echo $ff['projname']; ?></td>
            <td><?php echo $ff['invComnt']; ?></td>
            <td><?php echo $ff['invoiceTitle']; ?></td>
            <td><?php echo $ff['refinceno']; ?></td>
            <td class="text-right"><?php echo number_format($ff['invceMont']).".00"; ?></td>
            <td class="cell-icon text-center">
                <a href="#" data-toggle="modal" data-target="#update<?php echo $ff['invceid'];?>" class="btn btn-default btn-xs"><i class="fa fa-edit"></i>Edit</a>|
                <a href="gate?b=inv&del_invoice_id=<?php echo $ff['invceid'];?>" class="btn btn-default btn-xs" onclick="if(!confirm('Do you really want to Delete This invoice?'))return false;else return true;">
                <i class="fa fa-times-circle"></i> Cancel</span></a>
            </td>
       </tr>
       
        <div class="modal fade bs-example-modal-sm" id="update<?php echo $ff['invceid']; ?>" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
               <div class="modal-dialog modal-sm">
                  <div class="modal-content">
                   <div class="modal-header header">
                    <h5 class="modal-title" id="exampleModalLabel"><b>Change invoice for <?php echo $ff['ct_names']; ?></b></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                   
                       <div class="modal-body">
                          <form id="form_validation" method="POST" action="">
                              <input type="hidden" name="invoice_id" value="<?php echo $ff['invceid'];?>"/>
                             
                             <div class="form-group form-float">
                                 <label class="form-label">Amount</label>
                                 <div class="form-line">
                                    <input type="text" class="form-control" value="<?php echo $ff['invceMont'];?>" name="invoice_amount" required style="width: 100%;">
                                 </div>
                             </div>

                             <div class="form-group form-float">
                                 <label class="form-label">Ref.</label>
                                 <div class="form-line">
                                    <input type="text" class="form-control" value="<?php echo $ff['refinceno']; ?>" name="refno" required style="width: 100%;">
                                 </div>
                             </div>

                             <div class="form-group form-float">
                                 <label class="form-label">Title.</label>
                                 <div class="form-line">
                                    <input type="text" class="form-control" value="<?php echo $ff['invoiceTitle']; ?>" name="invtitle" required style="width: 100%;">
                                 </div>
                             </div>
                             
                             <div class="form-group form-float">
                                 <label class="form-label">Invoice Date</label>
                                 <div class="form-line">
                                    <input type="date" class="form-control" value="<?php echo $ff['invcedate']?>" name="invceDate" required style="width: 100%;">
                                 </div>
                             </div>

                             <div class="form-group form-float">
                                 <label class="form-label"> Comment </label>
                                 <div class="form-line">
                                    <textarea type="text" class="form-control" name="invComnt" required style="width: 100%;"><?php echo $ff['invComnt']?></textarea>
                                 </div>
                             </div>
                         </div>
                       <div class="modal-footer">
                     <button type="submit" name="update_invoice" class="btn btn-primary">Submit</button>
                   </div>
                 </form>
                 
                <?php
                    }
                ?>
              </div>
           </div>
         </div>
         
		 </tbody>
		 
	  <tfoot>
        <tr>  
          <td style="font-weight: bold;">Tot.</td>
          <td colspan="5" ></td>
          <td></td>
          <td style="font-weight: bold;" align="right"><?php echo number_format($tot_invoice).".00";?></td>
          <td></td>
        </tr>
      </tfoot>
	</table>
 </div>