<?php

class db
{
    public $conn;
    public $req1;
    public $req2;
    public $phone;
    public $freeMembership;
    public $expireTime;
    public $type;

    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)
    {
       try {
        $sql = "insert into users (telname,telfamily,username,chatid) values (\"$firstname\",\"$family\",\"$username\",\"$chatid\")";
        $this->conn->exec($sql);

       }
        catch(Exception $e) {
            $sql = "insert into users (username,chatid) values (\"$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->phone = $getter[0]["phone"];
        $this->freeMembership = $getter[0]["free_sub"];
        $this->expireTime = $getter[0]["expire_time"];
        $this->type = $getter[0]["type"];

    }

    function getExpired()
    {
        $now = time();
        $sql = "select * from users where status='active' and expire_time < " . $now . "  ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        $numrowfusers = $stmt->rowCount();
        if ($numrowfusers == 0) {
            return 'nobody';
        }


        return $getter;
    }

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

    }

    function getExpiredUsers()
    {

        $now = time();
        $sql = "select * from users   status=\"active\" ";
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        $numrowfusers = $stmt->rowCount();

        if ($numrowfusers == 0) {
            return $numrowfusers;
        }

        return $getter;

    }


    function getUidExistance($uid)
    {
        $sql = "select uid from users where uid='$uid' ";
        // return $sql;
        $stmt = $this->conn->prepare($sql);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $getter = $stmt->fetchAll();
        $numrowfusers = $stmt->rowCount();

        return $numrowfusers;

    }

}
