<?php
 echo @$_GET['msg'];
  session_start();
  if(isset($_SESSION['userId'])){ 
include 'template/header.php';  
  require 'connection.php';
 $maxitemqry= "SELECT max(itemid) as itemid from itemreg";
 $maxitemrun= mysqli_query($connect, $maxitemqry) or die(mysqli_error($connect));
$maxitemrow = mysqli_fetch_array($maxitemrun);

?>
 <link rel="stylesheet" href="css/jquery-ui.css">
  <!-- <link rel="stylesheet" href="/resources/demos/style.css"> -->
  <script src="js/jquery-1.12.4.js"></script>
  <script src="js/jquery-ui.js"></script>
    <link rel="stylesheet" href="css/bootsrap_my.css" />


  <link rel="stylesheet" href="css/jquery-ui.css">
        <link rel="stylesheet" href="bootstrap.min.css" />
    <script src="js/jquery-1.12.4.js"></script>
     <script src="js/jquery-ui.js"></script>
<div class="ch-container">
    <div class="row">
        
<!-- SIDEBAR HERE-->
<?php

include 'template/sidebar.php';

?>
  <style>
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}
</style>  

        <div id="content" class="col-lg-10 col-sm-10">
            <!-- content starts -->
      <div>
    <ul class="breadcrumb">
        <li>
            <a href="#">Home</a>
        </li>
        <li>
            <a href="#">Medicine Purchase</a>
        </li>
    </ul>
</div>

<div class="row">
    <div class="box col-md-12">
        <div class="box-inner">

          
         <?php
                    require 'connection.php';

                 $qryvno = "SELECT max(prid) as vno FROM purchase";
             $runvno = mysqli_query($connect, $qryvno) or die(mysqli_error($connect));
            $rowvno = mysqli_fetch_array($runvno);
?>
            <div class="box-header well">
                <h2><i class="glyphicon glyphicon-info-sign"></i> Medicine Purchase No. </h2>
                <h2 id="vno" style="margin-left: 5px"><?php echo $rowvno['vno']+1; ?></h2>

    <!--   <p>Date: <input type="text" id="datepicker" data-role="calendarpicker" data-input-format="%d-%m-%y"></p> -->


                <div class="box-icon">
                    <a href="#" class="btn btn-setting btn-round btn-default"><i
                            class="glyphicon glyphicon-cog"></i></a>
                    <a href="#" class="btn btn-minimize btn-round btn-default"><i
                            class="glyphicon glyphicon-chevron-up"></i></a>
                    <a href="#" class="btn btn-close btn-round btn-default"><i
                            class="glyphicon glyphicon-remove"></i></a>
                </div>
            </div>
            <div class="box-content row">

                <!-- Account -->
              
                <div class="col-lg-5 col-md-12" >

                 <select id="selaccid2" class="form-control selectpicker"data-live-search="true">
                 <?php
                    require 'connection.php';

              // $qry = "SELECT * FROM accreg where groupid=10";
               $qry = "SELECT * FROM accreg  where activityacc=0";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
?>
        <option id="dd" value="<?php echo $row['accid']; ?>"><?php echo $row['accname']; ?></option>
        <?php } ?>
            </select>
         
                </div>

                   <div class="col-lg-6 col-md-12" >
                   <div style="margin-left: 20PX; margin-right: -30px;" class="col-lg-2 col-md-12" >

                 
  <label style=" float: left; color: red; margin-top: 5px; font-size: 16px;"><input  type="checkbox" value="" id="checkid"> Tr.ID</label>
                  </div>
                   <div class="col-lg-3 col-md-12" >
                    
                 <select id="vid" class="form-control selectpicker"data-live-search="true">
                 <?php
                    require 'connection.php';

              $qry = "SELECT * FROM purchase order by prid asc";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
?>
        <option id="dd" value="<?php echo $row['prid']; ?>"><?php echo $row['prid']; ?></option>
        <?php } ?>
            </select>
            </div>
                </div>
                <!-- dated -->
            <div class="col-lg-1 col-md-12" style="margin-bottom: 30px">
               <input style=" border: none; border-bottom: 1px solid black; float: right; font-size: 2rem" type="date"  data-date-format="DD MMMM YYYY" id="dated">
                </div>
<!-- *********************************** -->
<div style="margin-bottom: 100px;"></div>
      <div class="col-lg-12 col-md-12">
      
      <div class="col-lg-3 col-md-12"> 
        <label>Select Item</label>
             <select  name="itemid" id="itemid" class="form-control selectpicker itmbox"data-live-search="true">
                 <?php
                    require 'connection.php';

              $qry = "SELECT * FROM itemreg where itemid>2";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
?>
        <option value="<?php echo $row['itemid']; ?>"><?php echo $row['itemname']; ?></option>
        <?php } ?>
            </select>
        <span id="erro_itemid" class="text-danger"></span>
      </div>

      <div class="col-lg-2 col-md-12">
        <label>Rate</label>
        <input autocomplete="off"  style="margin-left: -10px; " type="number" name="rate" id="rate" class="form-control" />
        <span id="error_rate" class="text-danger"></span>
      </div>

        <div class="col-lg-1 col-md-12">
        <label>Qty</label>
        <input autocomplete="off" style="margin-left: -10px; " type="number" name="qty" id="qty" class="form-control" />
        <span id="error_qty" class="text-danger"></span>
      </div>

        <div class="col-lg-2 col-md-12">
        <label>Amount</label>
        <input disabled style="margin-left: -10px; " type="text" name="amt" id="amt" class="form-control" />
        <span id="error_amt" class="text-danger"></span>

      </div>

      </div>

      <div class="form-group" align="center">
        <input type="hidden" name="row_id" id="hidden_row_id" />
        <!-- <button type="button" name="save" id="save" class="btn btn-info">Save</button> -->
        <!-- <button type="button" name="post" id="post" class="btn btn-info">POST</button> -->
      </div>

    <div id="action_alert" title="Action">

    </div>



      <form method="post" id="user_form">
        <div class="col-lg-12">
          <label class="glyphicon glyphicon-shopping-cart" style="color: maroon; font-size: 30px; margin-top: 3px; margin-left: 15px;"></label>
          <label class="" style="color: maroon; font-size: 20px;">Add to Cart</label>
          <!-- <table class="table table-striped table-bordered" id="user_data"> -->
                  <table class="table responsive" id="user_data">

            <tr>
                   <th hidden>ID</th>
                   <th hidden>Vno</th>
                   <th hidden>Date</th>
                    <th>Sr.No.</th>
                    <th>Description</th>
                    <th>Rate</th>
                    <th>Qty</th>
                    <th>Details</th>
                    <th>Remove</th>
                  </tr>
             
           
          </table>

        </div>
        <div align="center">
          <input style="margin-bottom: 20px;" type="submit" name="insert" id="insert" class="btn btn-info" value="Insert into Database" />
 <img style=" display: none; height: 30px; width: 30px; margin-bottom: 20px;" id="load" src="ajaxresult.gif">
 <i id="refresh" style="color: red; float: right;" class="glyphicon glyphicon-refresh btn btn-lg"></i>


        </div>
      </form>

         <div class="col-lg-12">
     <!--      <button style="margin-bottom: 20px;"  id="postinvoice" class="btn btn-danger">Save Invoice into Database </button> -->



        </div>
    </div>




<!--  <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal">
    Item Register
  </button> -->
<!-- ************************************** -->
            </div>
<!-- *************************NEW ITEM**************************************** -->
 

<div class="row">
    <div class="box col-md-12">
        <div class="box-inner">

            <div class="box-header well">
                <h2></i>Detail Section</h2>
                <div class="box-icon">
                   
                    <a href="index.php" class="btn btn-minimize btn-round btn-default"><i
                            class="glyphicon glyphicon-chevron-up"></i></a>
                    <a href="#" class="btn btn-close btn-round btn-default"><i
                            class="glyphicon glyphicon-remove"></i></a>
                </div>
            </div>
            <div class="box-content row">
              <div class="col-lg-12">
                
                 <table class="table responsive" id="table">
                   
                                     <tbody id="itemdetail">
                                     </tbody>
                                   </table>
                                   <div class="col-lg-9 col-md-12">
                           <input disabled style="float: right; width: 100px;" type="text" id="tamt" name="">
                           </div>

                                 <div class="col-lg-12 col-md-12">
                                        <?php

   if($_SESSION['userId']==2532){
     ?>  
                          <button id="deltebtn" class="btn btn-danger">Delete Invoice</button>
                              <?php

   }
     ?>  
                           </div>
                </div>

            </div>
        </div>
    </div>
</div>

<!-- ********************************************************************** -->


        </div>


    </div>






<!-- ********************************************************************** -->


    <!-- content ends -->
    </div><!--/#content.col-md-0-->
</div><!--/fluid-row-->


<!-- *********Modal Start******************** -->



    <hr>



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



<script>  
$(document).ready(function(){ 
  
  var count = 0;
})


  $( function() {
    $( "#datepicker" ).datepicker();
  } );
  </script>

<script type="text/javascript">
 var today = new Date();

document.getElementById("dated").value = today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2);


document.getElementById("dated2").value = today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)).slice(-2) + '-' + ('0' + today.getDate()).slice(-2);
  </script>



  <!-- Fetch Rate -->
 <script type="text/javascript">
  $('#postinvoice').click(function(){

      var acid= 1 ;
            $.ajax({
        url: 'postmedpr.php',
        method: 'POST',
       data: {acid: acid},
        success: function(data){
          alert(data);
        }
      })


 })
</script>


  <!-- show btn -->
 <script type="text/javascript">
  $('.table').on('keyup','#qty',function(){
    var currow = $(this).closest('tr');
   
    var itid = currow.find('#qty').val();
 if(itid>0){
  currow.find('#sub').show();
 }
else{
  currow.find('#sub').hide();
 }
 })
</script>


  <!-- Fetch item detail-->
 <script type="text/javascript">
  $('#refresh').click(function(){
if($('#checkid'). prop("checked") == true){
    var vnopr= $("#vid").val(); 
}
else{
    var vnopr= $("#vno").text();

}

            $.ajax({
        url: 'slitem.php',
        method: 'POST',
       data: {vnopr: vnopr},
        success: function(data){
          $("#itemdetail").html(data);
        }
      })
    

 })
</script>



 <script type="text/javascript">
  $('#rate').keydown(function(e){
  if(e.keyCode == 13)
    {
       if($('#rate').val()!==""){
     $('#qty').focus();
      }
    }
   var rate = $('#rate').val();
   var qty = $('#qty').val();
   var amt = parseInt(rate*qty)

  $('#amt').val(amt);

 })
</script>




 <script type="text/javascript">
  $('#itemid').change(function(){
   var itid2 = $('#itemid').val();
   var acid = $('#selaccid2').val();
  // kashif
         $.ajax({
        url: 'ratmed.php',
        method: 'POST',
       data: {itid2: itid2, acid: acid},
        success: function(data){
        $("#rate").val(data);
        $("#rate").focus();

        }
      })

 })
</script>



 <script type="text/javascript">
  $('#qty').keyup(function(){
   var rate = $('#rate').val();
   var qty = $('#qty').val();
   var amt = parseInt(rate*qty)
  $('#amt').val(amt);

 })
</script>

 <!-- Delete invoice-->
 <script type="text/javascript">
  $('#deltebtn').click(function(){
if($('#checkid'). prop("checked") == true){
    var vnopr= $("#vid").val(); 
}
else{
    var vnopr= $("#vno").text();

}
  var delet= confirm("Do you realy want to delete invoice# " + vnopr);
    if(delet){
         $.ajax({
        url: 'meddlt.php',
        method: 'POST',
       data: {vnopr: vnopr},
        success: function(data){
        alert(data);
           // location.reload(true);

        }
      })    
      }
 })
</script>

  <script type="text/javascript">
 $('#table').on('click','#dltbtn',function(){
   var currow = $(this).closest('tr');

if($('#checkid'). prop("checked") == true){
    var prvno= $("#vid").val(); 
}
else{
    var prvno= $("#vno").text();

}

  var srno = currow.find('td:eq(0)').text();
  var itemid = currow.find('td:eq(1)').text();
            $.ajax({
        url: 'slitemdlt.php',
        method: 'POST',
       data: {prvno: prvno, itemid: itemid},
        success: function(data){
          $("#itemdetail").html(data);
        }
      })
})

  </script>

   <script type="text/javascript">
 $('#selaccid2').change(function(){
if($("#newivoice").prop("checked") == true){

  var vno = $("#vno").text();
    $("#vno").text(parseInt(vno)+1);
  }
})

  </script>

    <script type="text/javascript">
 $('#vid').change(function(){
if($('#checkid'). prop("checked") == true){
    var vnopr= $("#vid").val(); 
}
else{
    var vnopr= $("#vno").text();

}

            $.ajax({
        url: 'slitem.php',
        method: 'POST',
       data: {vnopr: vnopr},
        success: function(data){
          $("#itemdetail").html(data);
        }
      })
    

 })
  </script>


  <script type="text/javascript">
  $("#tamt").val(<?php echo $tamt; ?>)

  </script>



<script>  
$(document).ready(function(){ 
  
  var count = 0;

  $('#user_dialog').dialog({
    autoOpen:false,
    width:400
  });

  $('#add').click(function(){
    $('#user_dialog').dialog('option', 'title', 'Add Data');
    $('#first_name').val('');
    $('#last_name').val('');
    $('#ids').val('');
    $('#error_first_name').text('');
    $('#error_last_name').text('');
    $('#ids').text('');
    $('#first_name').css('border-color', '');
    $('#last_name').css('border-color', '');
    $('#last_name1').css('border-color', '');
    $('#save').text('Save');
    $('#user_dialog').dialog('open');
  });


  $('#qty').keydown(function(e){
if(e.keyCode==13)
{

  var selectedText = $("#itemid option:selected").html();
    var itemid =  $('#itemid').val();
    var accid =  $('#selaccid2').val();
    var vno =  $('#vno').text();
    var dated =  $('#dated').val();
    var rate =  $('#rate').val();
    var qty =  $('#qty').val();
    count = count + 1;
    var srno =  count;

        output = '<tr id="row_'+count+'">';
   output += '<td>'+srno+' <input type="hidden" name="hidden_srno[]" id="srno'+count+'" class="srno" value="'+srno+'" /></td>';

        output += '<td hidden>'+accid+' <input type="hidden" name="hidden_accid[]" id="accid'+count+'" class="accid" value="'+accid+'" /></td>';
        
        output += '<td hidden>'+vno+' <input type="hidden" name="hidden_vno[]" id="vno'+count+'" value="'+vno+'" /></td>';

      output += '<td hidden>'+dated+' <input type="hidden" name="hidden_dated[]" id="dated'+count+'" value="'+dated+'" /></td>';

   output += '<td hidden>'+itemid+' <input type="hidden" name="hidden_itemid[]" id="itemid'+count+'" value="'+itemid+'" /></td>';
 output += '<td>'+selectedText+' <input type="hidden" name="hidden_itemidss[]" id="selectedText'+count+'" value="'+selectedText+'" /></td>';
   output += '<td>'+rate+' <input type="hidden" name="hidden_rate[]" id="rate'+count+'" value="'+rate+'" /></td>';
   output += '<td>'+qty+' <input type="hidden" name="hidden_qty[]" id="qty'+count+'" value="'+qty+'" /></td>';

        output += '<td><button type="button" name="view_details" class="btn btn-warning btn-xs view_details" id="'+count+'">View</button></td>';

        output += '<td><button type="button" name="remove_details" class="btn btn-danger btn-xs remove_details" id="'+count+'">Remove</button></td>';
        output += '</tr>';
        $('#user_data').append(output);
        // $('#user_dialog').dialog('close');
        $("#qty").val("");
        $("#rate").val("");
        $("#amt").val("");
        $("#newitemname").val("");
        $("#newitemsrate").val("");
        $("#newitemprate").val("");
        setTimeout(function(){ $('#itemid').focus(); }, 100)

}
});



  $('#save').click(function(){
    var error_first_name = '';
    var error_last_name = '';
    var first_name = '';
    var last_name = '';
    if($('#first_name').val() == '')
    {
      error_first_name = 'First Name is required';
      $('#error_first_name').text(error_first_name);
      $('#first_name').css('border-color', '#cc0000');
      first_name = '';
    }
    else
    {
      error_first_name = '';
      $('#error_first_name').text(error_first_name);
      $('#first_name').css('border-color', '');
      first_name = $('#first_name').val();
    } 
    if($('#last_name').val() == '')
    {
      error_last_name = 'Last Name is required';
      $('#error_last_name').text(error_last_name);
      $('#last_name').css('border-color', '#cc0000');
      last_name = '';
    }
    else
    {
      error_last_name = '';
      $('#error_last_name').text(error_last_name);
      $('#last_name').css('border-color', '');
      last_name = $('#last_name').val();
    }

      if($('#last_name1').val() == '')
    {
      error_last_name1 = 'Last Name1 is required';
      $('#error_last_name1').text(error_last_name1);
      $('#last_name1').css('border-color', '#cc0000');
      last_name1 = '';
    }
    else
    {
      error_last_name1 = '';
      $('#error_last_name1').text(error_last_name1);
      $('#last_name1').css('border-color', '');
      last_name1 = $('#last_name1').val();
    }

    if(error_first_name != '' || error_last_name != '' || error_last_name1 != '')
    {
      return false;
    }
    else
    {
      if($('#save').text() == 'Save')
      {
        count = count + 1;
        output = '<tr id="row_'+count+'">';
        output += '<td>'+first_name+' <input type="hidden" name="hidden_first_name[]" id="first_name'+count+'" class="first_name" value="'+first_name+'" /></td>';
        
        output += '<td>'+last_name+' <input type="hidden" name="hidden_last_name[]" id="last_name'+count+'" value="'+last_name+'" /></td>';

      output += '<td>'+last_name1+' <input type="hidden" name="hidden_last_name1[]" id="last_name1'+count+'" value="'+last_name1+'" /></td>';

        output += '<td><button type="button" name="view_details" class="btn btn-warning btn-xs view_details" id="'+count+'">View</button></td>';

        output += '<td><button type="button" name="remove_details" class="btn btn-danger btn-xs remove_details" id="'+count+'">Remove</button></td>';
        output += '</tr>';
        $('#user_data').append(output);

      }
      else
      {
        var row_id = $('#hidden_row_id').val();
        output = '<td>'+first_name+' <input type="hidden" name="hidden_first_name[]" id="first_name'+row_id+'" class="first_name" value="'+first_name+'" /></td>';
      
          output += '<td>'+last_name1+' <input type="hidden" name="hidden_last_name[]" id="last_name'+row_id+'" value="'+last_name+'" /></td>';

        output += '<td>'+last_name1+' <input type="hidden" name="hidden_last_name1[]" id="last_name1'+row_id+'" value="'+last_name1+'" /></td>';
          
            output += '<td>'+last_name+' <input type="hidden" name="hidden_last_name[]" id="last_name'+row_id+'" value="'+last_name+'" /></td>';

        output += '<td><button type="button" name="view_details" class="btn btn-warning btn-xs view_details" id="'+row_id+'">View</button></td>';
        output += '<td><button type="button" name="remove_details" class="btn btn-danger btn-xs remove_details" id="'+row_id+'">Remove</button></td>';
        $('#row_'+row_id+'').html(output);
      }
      $("#first_name").focus();
       $('#user_dialog').dialog('close');
    }
  });


  $(document).on('click', '.view_details', function(){
    var row_id = $(this).attr("id");
    var srno = $('#srno'+row_id+'').val();
    var accid = $('#accid'+row_id+'').val();
    var itemid = $('#itemid'+row_id+'').val();
    var rate = $('#rate'+row_id+'').val();
    var qty = $('#qty'+row_id+'').val();
    $('#srno').val(srno);
    $('#accid').val(accid);
    $('#itemid').val(itemid);
    $('#rate').val(rate);
    $('#qty').val(qty);
    $('#save').text('Edit');
    $('#hidden_row_id').val(row_id);
    $('#user_dialog').dialog('option', 'title', 'Edit Data');
    $('#user_dialog').dialog('open');
  });

  $(document).on('click', '.remove_details', function(){
    var row_id = $(this).attr("id");
    if(confirm("Are you sure you want to remove this row data?"))
    {
      $('#row_'+row_id+'').remove();
    }
    else
    {
      return false;
    }
  });

  $('#action_alert').dialog({
    autoOpen:false
  });

  $('#user_form').on('submit', function(event){
    event.preventDefault();
     var save= confirm("Do you realy want to save this invoice?");
    if(save){
      $("#load").show();
    var count_data = 0;
    $('.srno').each(function(){
      count_data = count_data + 1;
    });
    if(count_data > 0)
    {
      var form_data = $(this).serialize();
      $.ajax({
        url:"addmedpr.php",
        method:"POST",
        data:form_data,
        success:function(data)
        {
           $('#user_data').find("tr:gt(0)").remove();
        
          alert(data);
      $("#load").hide();

           location.reload(true);
        }
      })
    }
    else
    {
      $('#action_alert').html('<p>Please Add atleast one data</p>');
      $('#action_alert').dialog('open');
    }
  }
  });
  
});  
</script>


</body>
</html>
<?php } ?>