<?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'];
$turi = $_POST['turi'];
$count=1;

$sql_sddd = "";
if($turi !== null){
    $sql_sddd = "is_dostavshik = $turi AND ";
}


$sql_sorov = "SELECT agent_id, fio, boshagan, telefon, parol, region_idlar, kontr_idlar, gruppa_idlar, can_narx, can_zakaz, can_tolov, can_otchot,naqd,optom,kelishilgan, sklad_id  FROM agentlar WHERE $sql_sddd 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'];
        $naqd=$row['naqd'];
        $optom=$row['optom'];
        $sklad_id=$row['sklad_id'];
        $kelishilgan=$row['kelishilgan'];


        $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['naqd']=$naqd;
        $responce['optom']=$optom;
        $responce['kelishilgan']=$kelishilgan;
        $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();
?>
