<?php

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

    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 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, eshterak) values (\"$firstname\",\"$family\",\"$username\",\"$chatid\", 'no')";
        $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->eshterak = $getter[0]["eshterak"];
      
      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'];
        }

    }

}


?>