<?php

// Created by Ibrohimjon Ahmadjonov Ilhomjon o'g'li on 07.02.2020
header('Content-type: text/html; charset=UTF-8');
require "connect.php";


$umum_soni = 0;

$tel = $_GET['tel'];
$parol = $_GET['parol'];
$mac = $_GET['mac'];
$count=1;

$sql_sorov = "SELECT user_id, fio, telefon, parol,huquqi,region_idlar,kontr_idlar, naqd, perech, optom, nasiya, brend_idlar FROM users WHERE telefon LIKE '%$tel' AND parol = '$parol' AND boshagan = '0'";

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['user_id'];
        $fio=$row['fio'];
    	$fio=str_replace('"', '|', $fio);
		$telefon=$row['telefon'];
		$parol=$row['parol'];
		$huquqi=$row['huquqi'];
		$region_idlar=$row['region_idlar'];
		$kontr_idlar=$row['kontr_idlar'];
    	$brend_idlar=$row['brend_idlar'];
     	$naqd=$row['naqd'];
    	$perech=$row['perech'];
    	$optom=$row['optom'];
    	$nasiya=$row['nasiya'];

        mysqli_set_charset($conn,"utf8");

        $sql_gen = "SELECT Id FROM generatsiya WHERE user_id = '$Id'";

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

        $result3 = mysqli_query($conn, $sql_gen);

        $gen_id = "";
        if ($result3 !== false && $result3->num_rows > 0) {
            while($row2=mysqli_fetch_array($result3)){
                $gen_id = $row2['Id'];
            }
        } else{
             $sql_insert = "INSERT INTO generatsiya (user_id,fio,telefon,parol,mac_id) VALUES ('$Id','$fio','$telefon','$parol','$mac')";
             if($conn->query($sql_insert) == TRUE){
                $gen_id = $conn->insert_id;
             }
        }
        if($kontr_idlar == ""){
		    $kontr_idlar = "0";
		}

		if($region_idlar == ""){
		    $region_idlar = "0";
		}

    	if($brend_idlar == ""){
		    $brend_idlar = "0";
		}
        echo ''.$Id.'^'.$fio.'^'.$telefon.'^'.$parol.'^'.$huquqi.'^'.$gen_id.'^'.$region_idlar.'^'.$kontr_idlar.'^'.$naqd.'^'.$perech.'^'.$optom.'^'.$nasiya.'^'.$brend_idlar.'~';
        $count++;

    	$conn->close();
    	return;
    }
}
if($count == 1){

	echo "no_c";
	$conn->close();
	return;
} else{

	echo "no_c";
	$conn->close();
	return;
}


$conn->close();
?>
