<?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 selectShops()
    {
        $sql = "select * from shops";
        $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 checkUserExistence($chatid)
    {
        $sql = "select * from users where chatid='" . $chatid . "'  ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $numrow = $stmt->rowCount();
        if ($numrow == 0) {
            return 'ok';
        }
        return 'no';
    }


    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]["lastreq1"];
        $this->req2 = $getter[0]["lastreq2"];
        $this->req3 = $getter[0]["lastreq3"];
       
      
      return $getter[0];

    }

    function checkHash($hash)
    {
        $sql = "select * from hash where hash='" . $hash . "'  ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        $numrow = $stmt->rowCount();
        if ($numrow == 0) {
            return 'ok';
        } else {
            return 'no';
        }
    }

    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'];
        }

    }

}


?>