<?php
 echo @$_GET['msg'];
  session_start();
  if(isset($_SESSION['userId'])){ 
include 'template/header.php';

?>

 <body  style="background-color: white; margin-top: 0%; ">
 <div  class="container"> 

<?php 
 // echo @$_GET['msg'];
 //  session_start();
	// if(isset($_SESSION['userId'])){
	// if(isset($_GET['accid'])){
		require 'connection.php';
		$dated= $_GET['dated'];
		
		?>
		 <div style="background-color: white; margin-top: 20px;" class="table-responsive"> 
   

    <h1> JV Detail </h1> 

<table style="background-color: white;" class="table  table-stripped col-md-12">    
  
    <tr>
    <th style="min-width: 70px; max-width: 80px; font-size: 12px; min-height: 5px; color: black; border-left: 1px solid; border-left-color: #ECE9E8;">V.No</th>
    
    
     <th colspan="7" style="font-size: 12px; height: 5px; color: black; min-width: 300px;">Description</th>
     <th  style="font-size: 12px; height: 5px; color: black;">Amount</th>
     <th hidden style="font-size: 12px; height: 5px; color: black;">Image</th>
     <th hidden  style="font-size: 12px; height: 5px; color: black;">address</th>


 
    <th colspan="2" style="font-size: 12px; height: 5px; color: black; text-align: right; border-right: 1px solid; border-right-color: #ECE9E8;">Action</th>
</tr>
<?php
$crtotal = 0;
$credit = 0;
$debit = 0;
$qty = 0;
$drtotal = 0;
$qtytotal = 0;
$jvsid = '';
$jvs = 0;
$imgfile = '';
$imgdone = '';
$uploadfile='';
$vimgid='';
			$qry = "SELECT * from jvvoucher where dated = '$dated' order by trid ASC";
        $run_qry = mysqli_query($connect, $qry);
        while ($row=mysqli_fetch_array($run_qry)) {
           $crtotal=$crtotal+$row['amount'];
           $jvns=$row['vno'];
           $imgfile='img'.$jvns;
           $imgdone='done'.$jvns;
           $jvsid='jv'.$jvns;
           $vimgid='vimgid'.$jvns;
           $uploadfile='uploadfile'.$jvns;
           $jvs=$jvns;
?>  

<tr>
<td id="vno" style="border-bottom: 1px solid #ECE9E8; font-size: 12px; max-width: 80px; height: auto; min-width: 70px; color: black; width: auto; border-left: 1px solid; border-left-color: #ECE9E8;"><?php echo $row['vno'];?></td>
 <td colspan="4" style="border-bottom: 1px solid #ECE9E8; font-size: 12px; height: auto; color: black; min-width: 100px;"><?php 

      $qry2 = "SELECT accname from accreg where accid=(select craccid from jvvoucher where vno='$jvns')";
        $run_qry2 = mysqli_query($connect, $qry2);
        $row2=mysqli_fetch_array($run_qry2);
?>
<i style=" font-size: 14px; color: darkgreen;  font-weight: bold;"><?php
 echo $row2['accname'].' (Cr)'; 
 ?></i>

 <?php

$qry2 = "SELECT accname from accreg where accid=(select draccid from jvvoucher where vno='$jvns')";
        $run_qry2 = mysqli_query($connect, $qry2);
        $row2=mysqli_fetch_array($run_qry2);


?>
<br>
<i style="font-size: 14px; color: darkred; font-weight: bold; "><?php echo $row2['accname'].'(Dr)';?></i>
<?php

 ?>

<br>
<?php 
if($row['slipimage']==''){

?>
<img hidden style="height: 20px; width: 20px;"  src="upload/done.png" id="<?php echo $imgdone; ?>">
    <input hidden id="<?php echo $jvsid; ?>" value="<?php echo $jvs; ?>">

   <input type="file" name="<?php echo $imgfile; ?>" id="<?php echo $imgfile; ?>" />
   <br />
   <span id="uploaded_image"></span>
<button class="btn btn-primary btn-sm" id="<?php echo $uploadfile; ?>">Upload</button>

<?php
}
else
{
?>
<img style="height: 20px; width: 20px;"  src="upload/done.png" id="<?php echo $imgdone; ?>">


<button id="viewbtn" class="btn btn-success btn-xs">view</button>
<button type="button" class="btn btn-danger btn-xs dltimgbtn" data-vno="<?php echo $row['vno']; ?>">Delete Image</button>


<?php
}

?>

</td>

 
 <td colspan="3" style="border-bottom: 1px solid #ECE9E8; font-size: 12px; height: auto; color: black; min-width: 100px;"><?php 

 echo $row['description']; 

$qry2 = "SELECT accname from accreg where accid=(select draccid from jvvoucher where vno='$jvns')";
        $run_qry2 = mysqli_query($connect, $qry2);
        $row2=mysqli_fetch_array($run_qry2);


?>
</td>
<td class="d-none d-sm-block" style="border-bottom: 1px solid #ECE9E8; font-size: 12px; height: auto; color: black;"><?php  echo $row['amount']; ?></td> 

<td hidden class="d-none d-sm-block" style="border-bottom: 1px solid #ECE9E8; font-size: 12px; height: auto; color: black;">
    <input hidden type="checkbox" id="chkbox" name="" value="Fetch Date">
</td> 


<td hidden  class="d-none d-sm-block" style="border-bottom: 1px solid #ECE9E8; font-size: 12px; height: auto; color: black;">






</td> 

    <td hidden colspan="2" style="font-size: 12px; height: auto; color: black; border-bottom: 1px solid #ECE9E8; text-align: right; border-right: 1px solid; border-color: #ECE9E8;">
<?php echo $row['slipimage']; ?>
</td>



	<td colspan="2" style="font-size: 12px; height: auto; color: black; border-bottom: 1px solid #ECE9E8; text-align: right; border-right: 1px solid; border-color: #ECE9E8;">

                    
         <button id="edtbtn" class="btn btn-sm btn-info  icon-white "><i class="glyphicon glyphicon-edit icon-white"></i> Edit</button>





    <button id="prntjv" class="btn btn-sm btn-success">Print Voucher</button>
</td>
</tr>

<script>

     $("#<?php echo $uploadfile; ?>").click(function(){

  var name = document.getElementById("<?php echo $imgfile; ?>").files[0].name;
  var form_data = new FormData();
  var ext = name.split('.').pop().toLowerCase();
  if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1) 
  {
   alert("Invalid Image File");
  }
  var oFReader = new FileReader();
  oFReader.readAsDataURL(document.getElementById("<?php echo $imgfile; ?>").files[0]);
  var f = document.getElementById("<?php echo $imgfile; ?>").files[0];
  var fsize = f.size||f.fileSize;
  if(fsize > 2000000)
  {
   alert("Image File Size is very big");
  }
  else
  {
      var file = document.getElementById('<?php echo $imgfile; ?>').files[0];
form_data.append("file", document.getElementById('<?php echo $imgfile; ?>').files[0]);
form_data.append("jvnos", $('#<?php echo $jvsid; ?>').val());

   $.ajax({
    url:"upldimg.php",
    method:"POST",
    data: form_data,
    contentType: false,
    cache: false,
    processData: false,
    // beforeSend:function(){
    //  $('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>");
    // },   
    success:function(data)
    {

        $("#<?php echo $imgfile; ?>").hide();
        $("#<?php echo $uploadfile; ?>").hide();
        $("#<?php echo $imgdone; ?>").show();
     // alert("Uploaded");
      $('#uploaded_image').html("");
    }
   });
  }
 });

</script>




		<?php
        	}
				?>
	
<tr>
 <td colspan="8"></td>
<td style="font-size: 16px; font-weight: bold;"><?php  echo $crtotal; ?></td> 
<td></td>
    
</tr>
</table>
 </div>


<!-- *****************************View image modal start*************************** -->

<div class="modal fade" id="myModal2" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
     <!--    <div style="background-color: darkblue" class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h5 style="color: white" class="modal-title">Slip Detail</h5>
          <label hidden id="accidlbl"></label>
          
        </div> -->
        <div class="modal-body">
      <img style="height: 80%; width: 100%;" id="viewimg"  src="">
        </div>
        <div class="modal-footer">
            <i class="pull-left" style=" color: red; font-size: 26px; font-weight: bold;" id="itmnamelbl"></i>

          <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
</div>
<!-- **** -->

<!-- ******************************view image modal end********************** -->















<!-- *****************************modal start*************************** -->

<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div style="background-color: darkblue" class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h5 style="color: white" class="modal-title">JV Edit / Update</h5>
          <label hidden id="accidlbl"></label>
          
        </div>
        <div class="modal-body">
            <input value="<?php echo $dated; ?>" style=" border: none; border-bottom: 1px solid black; float: right; font-size: 2rem" type="date"  data-date-format="DD MMMM YYYY" id="dated">
            <br>
            <label>Credit Side</label>
            <br>
              <select id="craccid" class="form-control selectpicker"data-live-search="true">
                 <?php
                    require 'connection.php';

              $qry = "SELECT * FROM accreg where activityacc=0";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
?>
        <option  value="<?php echo $row['accid']; ?>"><?php echo $row['accname']; ?></option>
        <?php } ?>
            </select>
          
            <br>
 <label>Description</label>
            <br>
<input class="form-control" type="text" id="descbox" name="">
<br>
 <label>Amount</label>
            <br>
<input class="form-control" type="text" id="amtbox" name="">
<br>
          <label>Debit Side</label>
            <br>
              <select id="draccid" class="form-control selectpicker"data-live-search="true">
                 <?php
                    require 'connection.php';

              $qry = "SELECT * FROM accreg where activityacc=0";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
?>
        <option  value="<?php echo $row['accid']; ?>"><?php echo $row['accname']; ?></option>
        <?php } ?>
            </select>
        </div>
        <div class="modal-footer">
            <i class="pull-left" style=" color: red; font-size: 26px; font-weight: bold;" id="itmnamelbl"></i>

          <button  id="btndlt"  class="btn btn-sm btn-primary pull-left" style="background-color: red">Delete Voucher</button>

          <button id="btnupdt"  class="btn btn-sm btn-warning" style="background-color: red">Update JV</button>

          <button type="button" class="btn btn-sm btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
</div>
<!-- **** -->

<!-- ******************************modal end********************** -->

</div>
<?php
include 'template/footer.php';
?>

<script type="text/javascript">
    $(document).ready(function(e){
        
        $('#printbtn').click(function(){
            $('#printbtn').hide();
            $('#closebtn').hide();

              window.print();
        })

        $('#closebtn').click(function(){
       
              window.close();
        })

    });
</script>

  <!-- Fetch Rate -->
 <script type="text/javascript">
  $('.table').on('click','#prntjv',function(){
    var currow = $(this).closest('tr');
    var vno = currow.find('#vno').html();
    var ftchtype = 0;
    if(currow.find('#chkbox'). prop("checked") == true){
    var ftchtype = 1;
    }
   event.preventDefault();
 
       var win = window.open('printjv.php?vno='+vno+'& ftchtype='+ftchtype+'', '_blank');

 })
</script>


     <script type="text/javascript">
  $('.table tbody').on('click','#viewbtn',function(){
    var currow = $(this).closest('tr');
    var jvno = currow.find('td:eq(0)').text();
    var imageUrl = currow.find('td:eq(6)').html();
// Assuming $vimgid holds the correct ID of the input field
// var imageUrl = 'upload/4734.png';

// Setting the src attribute of the image with ID "viewimg"
$("#viewimg").attr("src", imageUrl);;
  
  $('#myModal2').modal('show');

  });
</script>



     <script type="text/javascript">
  $('.table tbody').on('click','#edtbtn',function(){
    var currow = $(this).closest('tr');
    var jvno = currow.find('td:eq(0)').text();
    var jvamt = currow.find('td:eq(3)').text();
    var Descbx = currow.find('td:eq(2)').text();
    $("#accidlbl").html(jvno);  
    $("#amtbox").val(jvamt);  
    $("#descbox").val(Descbx);  

  // $('#myModal').modal('toggle');
  $('#myModal').modal('show');

  });
</script>



     <script type="text/javascript">
    $("#btnupdt").click(function(){
      var jvno = $('#accidlbl').html();
      var dated = $('#dated').val();
      var craccid = $('#craccid').val();
      var draccid = $('#draccid').val();
      var amtbox = $('#amtbox').val();
      var descbox = $('#descbox').val();

      event.preventDefault();
 
            $.ajax({
        url: 'jvupdt.php',
        method: 'POST',
       data: {jvno: jvno, dated: dated, craccid: craccid, draccid: draccid, amtbox: amtbox, descbox: descbox},
        success: function(data){
            // $('#prate').val(data);
            alert(data);
            location.reload();


        }
      })

    });

</script>

     <script type="text/javascript">
    $("#btndlt").click(function(){
      var jvnodlt = $('#accidlbl').html();
        event.preventDefault();
 
            $.ajax({
        url: 'jvupdt.php',
        method: 'POST',
       data: {jvnodlt: jvnodlt},
        success: function(data){
            // $('#prate').val(data);
            alert(data);
            location.reload();


        }
      })

    });

</script>






<script>
$(document).ready(function(){
  $('.dltimgbtn').click(function(){
    var vno = $(this).data('vno');
    if(confirm("Are you sure you want to delete the image for JV #" + vno + "?")) {
      $.ajax({
        url: "delete_jv_image.php",
        type: "POST",
        data: { vno: vno },
        success: function(response){
          alert(response);
          location.reload(); // Reload the page to update UI
        }
      });
    }
  });
});
</script>





</body>
<?php } ?>