<?php
include('db_con.php');
include('function.php');
if (isset($_SESSION['user_id'])) {
    $user_type = $_SESSION['user_type'];
    $user_id = $_SESSION['user_id'];
    $user_name = $_SESSION['user_name'];
    $branch_id = $_SESSION['branch_id'];
    $branch_name = $_SESSION['branch_name'];
    $store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
  header('Location: login');
  die();
}
stock_update($connect);
$sql = "SELECT IFNULL(doc_status,'OFF') FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'cost'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $cost = $statement->fetchColumn();
            
?>
<!DOCTYPE html>
<html>
<head>
<title><?php  if(basename(__FILE__, '.php'=='inventory_update.php')){echo 'Price update';}?></title>
  <?php include('include/head.php'); ?>
  
</head>
<body>
  <?php include('include/header.php'); ?>
  <?php include('include/sidebar.php'); ?>
<div class="main-container">
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark"></h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Inventory</a></li>
              <li class="breadcrumb-item"><a href="items">Items</a></li>
              <li class="breadcrumb-item active">Items Quick update</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- Small boxes (Stat box) -->
     <span id="alert_action"></span>
    <div class="row">
        <div class="col-12">
          <div class="row">
          <div class="col-6">
            <b>Select Type For Items</b>
             <select name="type_id" id="type_id" class="form-control input-sm type_id select2" />
                           <?php echo fill_itemtype($connect);?>
                        </select>
                        <br />
                  
                      
                   
          </div>

          <div class="col-6">
            <b>Select Item </b>
             <select name="item_name" id="item_name" class="form-control input-sm item_name select2" />
                           <?php echo fill_itemname($connect);?>
                        </select>
                        <br />
                  
                      
                   
          </div>
         
          </div>         
                          
                        <br />
          <div class="card">
            <div class="card-header">
              <div class="row">
                
                          <div class="col-lg-10 col-md-10 col-sm-8 col-xs-6">
                              <h4 class="panel-title">Click on Column For Update</h4>
                            </div>
              
                </div>
            </div>
            <!-- /.card-header -->
            <div class="card-body">
              <div class="table-responsive">  
                        <span id="result"></span>
                        <div id="live_data"></div> 
                         </div>
              </div>
              </div>
          </div>
      </div>
             
        
        
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
 
  <!-- /.content-wrapper -->
 <?php include('include/footer.php'); ?>
    </div>
    </div>
  <?php include('include/script.php'); ?>
  
</body>
</html>
        
    
<script>
$(document).ready(function(){
  $(document).on('select2:select','.select2', function(e){
  var brand_id = $(this).val();
  
  if(brand_id != '')
  {
  fetch_data();
  }
  else
  {
  fetch_data();
  }
 });
  
  function fetch_data()
 {
  
  var type_id = $('#type_id').val();
  var item_name = $('#item_name').val();
  $.ajax({  
            url:"inventory_update_fetch.php",  
            method:"POST", 
            data:{type_id:type_id, item_name:item_name}, 
            success:function(data){  
        $('#live_data').html(data);  
            }  
        }); 
 }
 function edit_data(id, text, column_name)  
    {  
        $.ajax({  
            url:"inventory_update_edit.php",  
            method:"POST",  
            data:{id:id, text:text, column_name:column_name},  
            dataType:"text",  
            success:function(data){  
                //alert(data);
        $('#result').html("<div class='alert alert-success'>"+data+"</div>");
            }  
        });  
    }  
    $(document).on('blur', '.retail_price', function(){  
        var id = $(this).data("id6");  
        var retail_price = $(this).text();  
        edit_data(id, retail_price, "retail_price");  
    });
    $(document).on('blur', '.lp_rate', function(){  
        var id = $(this).data("id5");  
        var lp_rate = $(this).text();  
        edit_data(id, lp_rate, "lp_rate");  
    });
    $(document).on('blur', '.item_cost', function(){  
        var id = $(this).data("id4");  
        var item_cost = $(this).text();  
        edit_data(id, item_cost, "item_cost");  
    });
      
$('.select2').select2();  
});
    
</script>