<?php 
include "config.php";
error_reporting(0);

?>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="th.css">
<script src="datatable/data.js" type="text/javascript"></script>
<script src="datatable/table.js" type="text/javascript"></script>
<script src="datatable/datatable.js"></script>
<link rel="stylesheet" href="datatable/data-boot.css">
<link href="javascript/select2.css" rel="stylesheet" >
<script src="javascript/selectt2.js"></script>

<style>
    
    #navbar
    {
        flex:15%;
        height:100%; 
        flex-direction:row;
        overflow:auto;
        
    }
    #content
    {
        flex:90%;
        overflow:auto;
        padding-left:10px;
        padding-right:10px;
    }
    #fullbody
    {
        display:flex;
        
         height:100%;         
    }
    label
    {
      font-size:20px;
    }
    .edit
    {
        font-size:18px;
        color:green;
        cursor:pointer;
    }
    .dele
    {
        font-size:18px;
        color:red;
        cursor:pointer;
    }
    .edit:hover
    {
        font-size:20px;
        
    }
    .dele:hover
    {
        font-size:20px;
        color:red;
        cursor:pointer;
        
    }
    td
    {
      text-align:center;
    }
    th
    {
      text-align:center;
    }
    .row input
    {
      height:30px;
    }
    .row select
    {
      height:35px;
    }
    .input[type="date"],input[type="text"]
    {
        width:100%;
    }
    .payment-radio
    {
        height:20px;
        width:20px;
    }
    @media (max-width: 800px) {
        #fullbody
    {
        display:flex;
        flex-wrap: wrap;
                 
    }
        #navbar,#content {
    flex: 100%;
  }
}
</style>
<script>
$(document).ready( function () {
    $('#protable').DataTable();
} );
</script>
</head>
<body>
    
    <div id="fullbody"> 
<div id="navbar" class="bg-info"><?php include "navbar.php"?></div>
<div id="content">
<br>
<h3> Manage Debit</h3>
<br>
<?php 
        $userall=$_SESSION['log'];
        $usper = mysqli_query($connect,"select * from userpermission where username = '$userall'  && filename='debitdetails'");
while($per=mysqli_fetch_array($usper))
{
          ?>
          <?php if($per['dataadd'] =='1'){ ?>
<input type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal" style="float:right;margin-right:0px;" value="+ Add"><?php }?>
<br><br>
<!-- table -->
<div>
<table class="table table-bordered table-hover " style="border:1px solid lightgrey" id="protable">
    <thead >
<tr>
  <th>Sno</th>
    <th>Code</th>
    <th>Name</th>
    <th>Method</th>
    <th>Cash</th>
    <th>Bank</th>
    <th>Advance</th>
    <th>Remarks</th>
    <?php if($per['dataedit'] =='1'){ ?><th>Edit</th><?php }?>
    
</tr>

    </thead>
    <tbody>
    <?php 
$advance=mysqli_query($connect,"select *,(select name from customerdetails where customerdetails.code=debitdetails.customer && status!='deleted')as customername from debitdetails where status!='deleted' order by concat(date,time) desc");
$sno=0;
while($advance_dis = mysqli_fetch_array($advance))
{
    $sno+=1;
?>


        <tr>
          <td><?php echo $sno ?></td>
        <td><?php echo $advance_dis['code'] ?></td>
    <td><?php echo $advance_dis['customername'] ?></td>
    <td><?php echo $advance_dis['paymentmethod'] ?></td>
    <td><?php echo $advance_dis['amount'] ?></td>
    <td><?php echo $advance_dis['otheramount'] ?></td>
    <td><?php echo $advance_dis['advance'] ?></td>
    <td><?php echo $advance_dis['remark'] ?></td>
    <?php if($per['dataedit'] =='1'){ ?>  <td><a href="edit_debit.php?code=<?php echo $advance_dis['code'] ?>" ><i class="fas fa-edit edit"></i></a></td><?php }?>
   
        </tr>
        <?php }}?>
    </tbody>



</table>
</div>
<!-- modal -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog modal-dialog-centered modal-xl">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Add Debit</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
            <!--  -->
            <form action="insertdebit.php" method="post">
              <!--  -->
              <?php
              include "financial_year_calculation.php";
            $adv_count = mysqli_query($connect,"select count(code)as code from debitdetails where status !='deleted'");
        while($dis_count = mysqli_fetch_array($adv_count))
        {
            $advance_code = $dis_count['code']+1;
            $year_code = substr($financialYearEnd,2);
		}
        
        ?>
              <div class="row">
                  <div class="col-sm-1"></div>
                  <div class="col-sm-2"><label>Code</label></div>
                  <div class="col-sm-3"><input type="text" class="required" name="code" value="<?php echo 'DBT'.$year_code.'-'.$advance_code ?>" placeholder="Enter Code" readonly></div>
              </div><br>
              <div class="row">
                  <div class="col-sm-1"></div>
                  <div class="col-sm-2"><label>Date</label></div>
                  <div class="col-sm-3"><input type="date" class="required" placeholder="Enter Date" name="date"></div>
                  <div class="col-sm-2"><label>Customer</label></div>
                  <div class="col-sm-3"><select class="form-control selectpro" id="customerAdd" name="customer" required><br>
              <option value="">--Select Customer--</option>
              <?php 
$cus=mysqli_query($connect,"select * from customerdetails where status !='deleted'");
while($cusshow = mysqli_fetch_array($cus))
{
?>
<option value="<?php echo $cusshow['code'] ?>"><?php  echo $cusshow['name'] ?></option>
        <?php }?>          
  
              </select>
</div>
              </div><br>
              <div class="row">
              <div class="col-sm-1"></div>
                  <div class="col-sm-2"><label>Total Amount</label></div>
                  <div class="col-sm-3"><input type="text" class="required" placeholder="Enter Total" name="totalamount" id="totalAmount" readonly></div>
                  <div class="col-sm-2"><label>Pending Amount</label></div>
                  <div class="col-sm-3"><input type="text" class="required" placeholder="Enter pending" name="pendingamount" id="pendingAmount" readonly></div>
              </div><br>
              <div class="row">
                  <div class="col-sm-1"></div>
                  <div class="col-sm-2"><label>Payment Method</label></div>
                  <div class="col-sm-3"><select class="form-control" id="paymentMethod" name="paymentmethod">
                      <option>--select Method--</option>
                      <option value="cash">Cash</option>
                      <option value="bank">Bank</option>
                      <option value="advance">Advance</option>
                      <option value="all">All</option>
                  </select></div>
                  <div class="col-sm-2"><label>Cash</label></div>
                  <div class="col-sm-3"><input type="text" class="required" placeholder="Enter Cash" name="cash" id="cashPay"></div>
                  
              </div><br>
              <div class="row">
                  <div class="col-sm-1"></div>
                  <div class="col-sm-2"><label>Bank Amount</label></div>
                  <div class="col-sm-3"><input type="text" class="required" placeholder="Enter payment" name="othercash" id="bankPay"></div>
                   <div class="col-sm-2"><label>Cheque No</label></div>
                  <div class="col-sm-3"><input type="text" class="required" placeholder="Enter Cheque No" name="cheque" id="chequeNo"></div>
                 
              </div><br>
             <div class="row">
                  <div class="col-sm-1"></div>
                  <div class="col-sm-2"><label>Advance</label></div>
                  <div class="col-sm-3"><input type="text" class="required" placeholder="Enter Advance" name="advanceamount" id="advancePay"><span style="color:green" id="advanceText">Advance:</span><span style="color:green" id="advanceValue">0</span></div> 
                  <div class="col-sm-2"><label>Remark</label></div>
                  <div class="col-sm-3"><textarea class="required" placeholder="Enter Remarks" name="remark"></textarea></div>
              </div><br>
        <!-- Modal footer -->
        <div class="modal-footer">
        <input type="button"  class="btn btn-success" id="ValidationButton" value="Add" >
          <input type="hidden" id="submitButton"  class="btn btn-success" data-toggle="modal" data-target="#addpro" value="Add" >
        </div>
        <!-- modal for add -->
<div class="modal fade" id="addpro">
    <div class="modal-dialog modal-sm">
      <div class="modal-content" style="background-color:rgba(77, 219, 106,0.9)">
      
        <!-- Modal Header -->
        
        
        <!-- Modal body -->
        <div class="modal-body">
          <span style="color:white"><i class="fas fa-check" style="font-size:24px"></i> Debit Have Been Created Successfully</span>
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="submit" type="button" class="btn btn-warning" >Ok</button>
        </div>
        
      </div>
    </div>
  </div>
  
<!-- modal for add -->
      </div>
    </div>
  </div>
        </form>
        
<!-- modal -->


<!-- main-content-div -->
</div>
<!-- main-content-div -->
</div>
<script src="js/debit.js"></script>
<script src="restriction/debit.js"></script>
<script>
    $('.selectpro').select2({ width: '100%',height:'420px;' });
    
</script>
</body>