<?php
session_start();
include('../inc/dbcon.php');

if(isset($_POST['audimei'])){

    $response = [];

    $pid=  intval($_POST['audimei']);
    
    $search = mysqli_real_escape_string($con, $_POST['search']);
    $brid   = intval($_SESSION['abrid']);

    $sql = "
        SELECT serial
        FROM tbl_serial
        WHERE pid = $pid
        AND serial LIKE '%$search%'
        AND brid = $brid
        AND rci = 'Y'
        AND status = '0'
        AND serial NOT IN (
            SELECT selimei1 FROM tbl_exchange
            UNION
            SELECT selimei2 FROM tbl_exchange
        )
        ORDER BY serial ASC
        LIMIT 30
    ";

    $result = mysqli_query($con, $sql) or die(mysqli_error($con));

    while($row = mysqli_fetch_assoc($result)){
        $response[] = [
            "label" => $row['serial'],
            "value" => $row['serial']
        ];
    }

    echo json_encode($response);
    exit;
}


if(isset($_POST['getprice'])){

        $id=$_POST['getprice'];
        $sql="SELECT tbl_brstock.unqid,price FROM tbl_brstock LEFT JOIN tbl_stock ON tbl_brstock.unqid=tbl_stock.unqid WHERE tbl_brstock.pid='$id' AND avqty>0 LIMIT 1";    
        $query=mysqli_query($con,$sql)or die(mysqli_error($con));
        $row=mysqli_fetch_array($query);
        $redata=array($row['unqid'],$row['price']);
        echo json_encode($redata);    
        exit;
   
}






