<?php

// Created by Ilhomjon on 06.03.2021
header('Content-type: text/html; charset=UTF-8');
require "connect.php";


$umum_soni = 0;

$tel = $_POST['tel'];
$parol = $_POST['parol'];
$count=1;

$sql_sorov = "SELECT agent_id, fio, boshagan, telefon, parol, region_idlar, kontr_idlar, gruppa_idlar, can_narx, can_zakaz, can_tolov, can_otchot,can_ostatka,naqd,optom,kelishilgan,narx4,can_kelish FROM agentlar WHERE telefon LIKE '%$tel' AND parol = '$parol'";

 mysqli_set_charset($conn,"utf8");
$javob = mysqli_query($conn,"SET NAMES utf8");

$javob = mysqli_query($conn, $sql_sorov);

if ($javob !== false && $javob->num_rows > 0) {
    while($row=mysqli_fetch_array($javob)){

        $Id=$row['agent_id'];
        $fio=$row['fio'];
        $fio=str_replace('"', '|', $fio);
        $boshagan=$row['boshagan'];
        $telefon=$row['telefon'];
        $parol=$row['parol'];
        $region_idlar=$row['region_idlar'];
        $kontr_idlar=$row['kontr_idlar'];
        $gruppa_idlar=$row['gruppa_idlar']; 
        $can_narx=$row['can_narx'];
        $can_zakaz=$row['can_zakaz'];
        $can_tolov=$row['can_tolov'];
        $can_otchot=$row['can_otchot'];
        $can_ostatka=$row['can_ostatka'];
        $naqd=$row['naqd'];
        $optom=$row['optom'];
        $kelishilgan=$row['kelishilgan'];
        $narx4=$row['narx4'];
        $can_kelish=$row['can_kelish'];


        $responce['agent_id']=$Id;
        $responce['fio']=$fio;
        $responce['boshagan']=$boshagan;
        $responce['telefon']=$telefon;
        $responce['parol']=$parol;
        $responce['region_idlar']=$region_idlar;
        $responce['kontr_idlar']=$kontr_idlar;
        $responce['gruppa_idlar']=$gruppa_idlar; 
        $responce['can_narx']=$can_narx;
        $responce['can_zakaz']=$can_zakaz;
        $responce['can_tolov']=$can_tolov;
        $responce['can_otchot']=$can_otchot;
        $responce['can_ostatka']=$can_ostatka;
        $responce['naqd']=$naqd;
        $responce['optom']=$optom;
        $responce['kelishilgan']=$kelishilgan;
        $responce['narx4']=$narx4;
        $responce['can_kelish']=$can_kelish;
        $responce['turi']="1";


    }
} else {
    
    $sql_sorov = "SELECT region_id, kontr_id, kontr_nomi, telefon, parol FROM kontragentlar WHERE telefon LIKE '%$tel' AND parol = '$parol'";

     mysqli_set_charset($conn,"utf8");
    $javob = mysqli_query($conn,"SET NAMES utf8");
    
    $javob = mysqli_query($conn, $sql_sorov);
    
    if ($javob !== false && $javob->num_rows > 0) {
        while($row=mysqli_fetch_array($javob)){
    
            $Id=$row['kontr_id'];
            $fio=$row['kontr_nomi'];
            $fio=str_replace('"', '|', $fio);
            $boshagan=$row['boshagan'];
            $telefon=$row['telefon'];
            $parol=$row['parol'];
            $region_idlar=$row['region_id'];
            $kontr_idlar=$row['kontr_idlar'];
            $gruppa_idlar=$row['gruppa_idlar']; 
            $can_narx=$row['can_narx'];
            $can_zakaz=$row['can_zakaz'];
            $can_tolov=$row['can_tolov'];
            $can_otchot=$row['can_otchot'];
    
    
            $responce['agent_id']=$Id;
            $responce['fio']=$fio;
            $responce['boshagan']=$boshagan;
            $responce['telefon']=$telefon;
            $responce['parol']=$parol;
            $responce['region_idlar']=$region_idlar;
            $responce['kontr_idlar']="";
            $responce['gruppa_idlar']=""; 
            $responce['can_narx']="";
            $responce['can_zakaz']="";
            $responce['can_tolov']="";
            $responce['can_otchot']="";
            $responce['turi']="2";
    
        }
    } 
    
}


echo json_encode($responce);
$conn->close();
?>
