<?php
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;     
}
$dtnow = date("Y-m-d h:i:s", time());
?>



<?php 
//Serial Search
if(isset($_POST['tyserial'])){
    $serial = $_POST['search'];
    $sql="SELECT * FROM tbl_serial  WHERE serial LIKE '%$serial%' AND rci='Y' AND brid='$brid' AND status='0' LIMIT 20";    
    $result = mysqli_query($con,$sql) or die(mysqli_error($con));
        
    while($row = mysqli_fetch_array($result) ){
    $response[] = array("value"=>$row['id'],"label"=>$row['serial']);
    }
    
    echo json_encode($response);
    exit;    
}

if(isset($_POST['editserial'])){   
?>

<div class="col-md-12 popup_details_div editserial">

            <div class="row">
                <div class="col-md-12">
                    <div class="col-md-1"></div>
                    <div class="col-md-10">    

                            <div class="form-group">  
                                <div class="input-group">
                                    <input type="text" class="form-control" name="search" id="search" placeholder="e.g. Type Product Serial" autocomplete="off">
                                    <span class="input-group-addon"><span class="fa fa-search"></span></span>    
                                </div>
                            </div>    

                        <div class="form-group">
                            <label>Serial</label>
                            <input type="text" value="" name="oldsrl" id="name" class="form-control" placeholder="e.g. AXE-456-123" readonly="">
                            <input type="hidden" maxlength="11" value="" class="form-control" name="srlid" id="srlid" autocomplete="off" readonly="">
                            <input type="hidden" name="serialupdate" readonly="">     
                        </div>    

                        <div class="form-group">
                            <label>Type New Serial</label>
                            <input type="text" maxlength="25" value="" name="upserial" class="form-control" placeholder="e.g. AXE-456-123">
                        </div>    
                        
                    </div>    
                    <div class="col-md-1"></div>    
                </div>    
            </div>
        
    </div>
    <div class="clearfix"></div>    
    <div class="row" style="margin-top: 15px">
        <div class="col-md-8"></div>
        <div class="col-md-4 text-right">
            <input type="button" id="addserial" class="btn btn-flat bg-purple btn-sm " value="Update">&nbsp;&nbsp;
        </div> 
    </div>
    <script type="text/javascript">
        $(document).on('keydown', '#search', function () {
        $('#search' ).autocomplete({
        source: function( request, response ) {
        $.ajax({
        url: 'pur_editserial.php',
        type: 'post',
        dataType: 'json',
        data: {
        search: request.term, tyserial:1
        },
        success: function(data) {
        response(data);
        }
        });
        },
        select: function (event, ui) {    
        var srl = ui.item.label;
        var id = ui.item.value;
            
        $('#name').val(srl);     
        $('#srlid').val(id);    
        }
        });        
        });
            
        $(document).on('click', '#addserial', function() { 
        var serial_data = $('.editserial input');
            
        $.ajax({
        url: "pur_editserial.php",
        data: serial_data,
        type: 'post',
        dataType: 'json',        
        success: function(data){
        if(data.status === "success"){    
        $('#search').val('');
        $('#name').val('');     
        $('#srlid').val('');
        if($('.right-side-add').is(':visible')) { $('.right-side-add').toggle('slide', { direction: 'right' }, 300); }
        toastr.success(data.message);    
        }else{    
        toastr.error(data.message);    
        }         
        }
        })    
            
        });
    </script> 
<?php 
}
if(isset($_POST['serialupdate'])){

   
   
    $id = remove_junk(escape($_POST['search']));
    $serial = remove_junk(escape($_POST['upserial']));
    $inv=get_fild_data('tbl_serial',$id,'purinv');
    if(isset($_POST['upserial'])){
        $ducode = mysqli_query($con,"SELECT * FROM tbl_serial WHERE serial ='$serial'");
        }
        
        if($ducode->num_rows > 0) {
            echo json_encode(array(
                'status' => 'error',
                'message'=> 'Serial already Exits! Plz try another!!'
                ));  
        }else{ 
            $sql="UPDATE tbl_serial SET serial='$serial' WHERE id='$id'";
            $result = mysqli_query($con,$sql) or die(mysqli_error($con)); 
            $efid=mysqli_affected_rows($con);    
            if($efid>0){    
            $act =remove_junk(escape('Purchase Invoice: '.$inv));    
            write_activity($aid,'PUR','Purchase invoice has been Updated',$act);    
            echo json_encode(array(
            'status' => 'success',
            'message'=> 'Data Update Successfully!!'   
            ));
            exit;
            }else{
            echo json_encode(array(
            'status' => 'error',
            'message'=> 'Sorry! Data Fail to Update!!'
            ));    
            }
        }   
    }
?>