<?php

class db
{
    public $conn;
    public $req1;
    public $req2;
    public $req3;

    function __construct()
    {
        include("asl.php");
        $conn = new PDO("mysql:host=$servername;dbname=$mydb", $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"));
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->conn = $conn;
    }
  
  
   function getCategories()
    {
        $sql = "select * from categories";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        return $getter;
    }


    function getBaskets($chatid)
    {
        $sql = "select * from basket where chatid='$chatid' ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        return $getter;
    }


    function getProducts($product)
    {
        $sql = "select * from products where title='$product'";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        return $getter;
    }


    function getProductDetail($product, $category)
    {
        $sql = "select * from products where category='$category' and title='$product'";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        return $getter;
    }

    function getCategoryDetail($category)
    {
        $sql = "select * from categories where title='$category'";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        return $getter;
    }
  

    function setGpUserStatus($bot)
    {
        $sql = "select * from gp_users where gp_chatid='" . $bot->chatid . "' and  chatid='" . $bot->gpSecondMemberChatid . "' ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        $numrowfusers = $stmt->rowCount();
        if ($numrowfusers == 0) {
            $sql = "insert into gp_users (type,gp_title,gp_chatid,chatid,firstname,lastname,username,functor_chatid,functor_firstname,functor_lastname,functor_username,time) 
            values ('" . $bot->gpActionType . "','" . $bot->gpChatTitle . "','" . $bot->chatid . "','" . $bot->gpSecondMemberChatid . "','" . $bot->gpSecondMemberFirstname . "','" . $bot->gpSecondMemberLastname . "','" . $bot->gpSecondMemberUsername . "','" . $bot->gpFromChatId . "','" . $bot->gpFromFirstname . "','" . $bot->gpFromLastname . "','" . $bot->gpFromUsername . "','" . time() . "')";
            $this->conn->exec($sql);
        } else {
            $sql = "update gp_users set type='" . $bot->gpActionType . "',time='" . time() . "'  where gp_chatid='" . $bot->chatid . "' and  chatid='" . $bot->gpSecondMemberChatid . "'";
            $this->conn->exec($sql);
        }
    }
    function ifnewuserinsert($chatid, $username, $firstname, $family)
    {
        $sql = "insert into users (telname,telfamily,username,chatid) values (\"$firstname\",\"$family\",\"$username\",\"$chatid\")";
        $this->conn->exec($sql);
    }


    function getinfo($chatid, $username, $firstname, $family)
    {
        $sql = "select * from users where chatid='" . $chatid . "'  ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        $numrowfusers = $stmt->rowCount();
        if ($numrowfusers == 0) {
            $this->ifnewuserinsert($chatid, $username, $firstname, $family);
        }
        $this->req1 = $getter[0]["req1"];
        $this->req2 = $getter[0]["req2"];
        $this->req3 = $getter[0]["req3"];
      
      return $getter[0];

    }

    function getCountAddUser($chatid)
    {
        $sql = "select * from adduser where mainchatid='" . $chatid . "'  ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        $numrow = $stmt->rowCount();
        return $numrow;
    }


    function getHead($chatid)
    {
        $sql = "select * from adduser where chatid='" . $chatid . "'  ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        $numrow = $stmt->rowCount();
        if ($numrow == 0) {
            return 'a';
        } else {
            return $getter[0]['head'];
        }

    }

}


?>