<?php
/**
 * Countries manages required data for countries
 */
class Countries {

    /**
     * get currency by package
     * @param  INT $idPackage id for the package
     * @return String         curency
     */
    public function getCurrencyForPackage($idPackage){
        global $database;

        $sql = "SELECT c.currency
                FROM ".TABLES['countries']." c
                INNER JOIN ".TABLES['packages']." p
                ON p.idCountry=c.id
                WHERE p.id=$idPackage";
        $row = $database->fetchResultArray($sql);

        return !empty($row) ? $row[0] : '';
    }

    public function getCurrencyForOrder($idOrder){
        global $database;
        $data = [];

        $sql = "SELECT
                    c.currency
                FROM ".TABLES['countries']." c
                INNER JOIN ".TABLES['packages']." p
                    ON p.idCountry=c.id
                INNER JOIN ".TABLES['rel_order_packages']." rop
                    ON rop.idPackage=p.id
                WHERE rop.idOrder=$idOrder LIMIT 1";
        $row = $database->fetchResultArray($sql);

        return !empty($row) ? $row[0]['currency'] : '';
    }

    /**
     * get all countries in the system
     * @return Array list of countries
     */
    public function getCountries(){
        global $database;
        $sql = "SELECT c.id AS idCountry,
                       c.name AS countryName
                FROM ".TABLES['countries']." c";

        return $database->fetchResultArray($sql);
    }

}
