<?php
class webProduct_functions extends object_class
{
    public $product;
    public $webClass;

    function __construct()
    {
        parent::__construct('3');

        if (isset($GLOBALS['webClass'])) {
            $this->webClass = $GLOBALS['webClass'];
        }

    }


    public function webUserId()
    {
        return webUserId();
    }


    public function AllUsersArray()
    {
        $sql = "SELECT * FROM `accounts_user` WHERE `status` = ?";
        $users = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $users;
        }
        return [];
    }

    public function AllEmployeesAndManagersArray()
    {
        $sql = "SELECT * FROM `accounts_user` WHERE `status` = ? AND `acc_type` = ? OR `acc_type` = ? ORDER BY `acc_id` DESC";
        $users = $this->dbF->getRows($sql, [1, 2, 5]);
        if ($this->dbF->rowCount > 0) {
            return $users;
        }
        return [];
    }

    public function AllUsersArrayExceptClient()
    {
        $sql = "SELECT * FROM `accounts_user` WHERE `status` = ? AND `acc_type` <> ?";
        $users = $this->dbF->getRows($sql, [1, 4]);
        if ($this->dbF->rowCount > 0) {
            return $users;
        }
        return [];
    }

    public function AllDesignationArray()
    {
        $sql = "SELECT * FROM `designations` WHERE `publish` = ? ORDER BY `designation` ASC";
        $designations = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $designations;
        }
        return [];
    }

    public function AllServicesArray()
    {
        $sql = "SELECT * FROM `services` WHERE `publish` = ?";
        $services = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $services;
        }
        return [];
    }

    public function getUserDesignation($userId)
    {
        $sql = "SELECT d.designation 
                FROM staffs s 
                JOIN staff_designations sd ON sd.staff_id = s.id 
                JOIN designations d ON d.id = sd.designation_id 
                WHERE s.userId = ?";

        $designations = $this->dbF->getRows($sql, [$userId]);

        if ($this->dbF->rowCount > 0) {
            $designationNames = array_column($designations, 'designation');
            return implode(', ', $designationNames);
        }

        return '';
    }


    public function AllClientsArray()
    {
        $sql = "SELECT * FROM `accounts_user` WHERE `status` = ? AND `acc_type` = ? ORDER BY `acc_name` ASC";
        $clients = $this->dbF->getRows($sql, [1, 4]);
        if ($this->dbF->rowCount > 0) {
            return $clients;
        }
        return [];
    }

    public function AllEmployeesArray()
    {
        $sql = "SELECT * FROM `accounts_user` WHERE `status` = ? AND `acc_type` = ? ORDER BY `acc_name` ASC";
        $employees = $this->dbF->getRows($sql, [1, 5]);
        if ($this->dbF->rowCount > 0) {
            return $employees;
        }
        return [];
    }

    public function getProjectTypes()
    {
        $sql = "SELECT * FROM `project_types` WHERE `publish` = ?";
        $projectTypes = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $projectTypes;
        }
        return [];
    }

    public function getProjectCategories()
    {
        $sql = "SELECT * FROM `project_categories` WHERE `publish` = ?";
        $projectCategories = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $projectCategories;
        }
        return [];
    }

    public function getProjectStatuses()
    {
        $sql = "SELECT * FROM `project_statuses` WHERE `publish` = ?";
        $projectStatuses = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $projectStatuses;
        }
        return [];
    }

    public function getUserImage()
    {
        $userId = $this->webUserId();

        $sql = "SELECT `acc_image` FROM `accounts_user` WHERE `acc_id` = ?";
        $userImage = $this->dbF->getRow($sql, [$userId]);

        if ($this->dbF->rowCount > 0 && !empty($userImage["acc_image"])) {
            return WEB_URL . '/images/' . $userImage["acc_image"];
        }
        return WEB_URL . '/webImages/avatars/default.png';
    }

    public function getUserById($userId)
    {
        $sql = "SELECT * FROM `accounts_user` WHERE `acc_id` = ?";
        $userData = $this->dbF->getRow($sql, [$userId]);

        if ($this->dbF->rowCount > 0) {
            return $userData;
        }
        return [];
    }

    public function getUserDetailsById($userId)
    {
        $sql = "SELECT * FROM `accounts_user_detail` WHERE `id_user` = ?";
        $userDetails = $this->dbF->getRows($sql, [$userId]);

        if ($this->dbF->rowCount > 0) {
            return $userDetails;
        }
        return [];
    }

    public function getUserImageById($userId)
    {
        $sql = "SELECT `acc_image` FROM `accounts_user` WHERE `acc_id` = ?";
        $userImage = $this->dbF->getRow($sql, [$userId]);

        if ($this->dbF->rowCount > 0 && !empty($userImage["acc_image"])) {
            return $userImage["acc_image"];
        }
        return '';
    }

    public function getStaffArray()
    {
        $sql = "SELECT au.acc_id, au.acc_name FROM staffs s JOIN accounts_user au ON au.acc_id = s.userId ORDER BY s.id DESC";
        $staffs = $this->dbF->getRows($sql);
        if ($this->dbF->rowCount > 0) {
            return $staffs;
        }
        return [];
    }

    public function getStaffById($staffId)
    {
        $sql = "SELECT * FROM staffs WHERE id = ?";
        $staff = $this->dbF->getRow($sql, [$staffId]);

        if (!$this->dbF->rowCount) {
            return false;
        }

        $designationSql = "SELECT designation_id FROM staff_designations WHERE staff_id = ?";
        $designationRows = $this->dbF->getRows($designationSql, [$staffId]);

        $designations = array_column($designationRows, 'designation_id');
        $staff['designation_ids'] = $designations;

        return $staff;
    }

    public function getTicketCategories()
    {
        $sql = "SELECT * FROM `ticket_categories` WHERE `publish` = ? ORDER BY `category` ASC";
        $ticketCategories = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $ticketCategories;
        }
        return [];
    }
    public function getExpenseCategories()
    {
        $sql = "SELECT * FROM `expense_categories` WHERE `publish` = ? ORDER BY `category` ASC";
        $expenseCategories = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $expenseCategories;
        }
        return [];
    }

    public function getInvoiceStatuses()
    {
        $sql = "SELECT * FROM `invoice_statuses` WHERE `publish` = ? ORDER BY `name` ASC";
        $invoiceStatuses = $this->dbF->getRows($sql, [1]);
        if ($this->dbF->rowCount > 0) {
            return $invoiceStatuses;
        }
        return [];
    }

    public function updateLastSeen()
    {
        $sql = "UPDATE `accounts_user` SET `last_seen` = NOW() WHERE `acc_id` = ?";
        $this->dbF->setRow($sql, [$this->webUserId()]);
    }

    public function getUserEmailsById($userId)
    {
        $sql = "SELECT * FROM `user_emails` WHERE `user_id` = ?";
        $userEmails = $this->dbF->getRows($sql, [$userId]);

        if ($this->dbF->rowCount > 0) {
            $emails = [];
            foreach ($userEmails as $row) {
                $emails[] = $row['email'];
            }
            return $emails;
        }

        return [];
    }

    public function getUserPhonesById($userId)
    {
        $sql = "SELECT * FROM `user_phones` WHERE `user_id` = ?";
        $userPhones = $this->dbF->getRows($sql, [$userId]);

        if ($this->dbF->rowCount > 0) {
            $phones = [];
            foreach ($userPhones as $row) {
                $phones[] = $row['phone'];
            }
            return $phones;
        }
        return [];
    }

    public function getAllEventsByClient($userId)
    {
        $sql = "SELECT * FROM `events` WHERE `category` = ? AND `client_id` = ?";
        $events = $this->dbF->getRows($sql, ['Event', $userId]);

        if ($this->dbF->rowCount > 0) {
            return $events;
        }
        return [];
    }

    public function getInvoiceDetailsById($invId)
    {
        $sql = "SELECT * FROM `invoice_vendor_details` WHERE `invoice_id` = ?";
        $invoiceDetails = $this->dbF->getRows($sql, [$invId]);

        if ($this->dbF->rowCount > 0) {
            return $invoiceDetails;
        }
        return [];
    }

    public function invoiceDetailInfoArray($data, $settingName)
    {
        foreach ($data as $val) {
            if ($val['detail_name'] == $settingName) {
                return $val['detail_value'];
            }
        }
        return "";
    }
}
