<?php

class SupplierBids{

    public function getSuppliers(){
        global $database;

        $sql = "SELECT
                    s.id AS idSupplier,
                    s.name AS supplier
                FROM ".TABLES['suppliers']." s
                ORDER BY s.name";

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

    private function validateBidData($bid){
        $messages = [];
        $requiredFields = ['idProduct', 'bidNumber'];

        foreach ($requiredFields as $field) {
            if(!property_exists($bid , $field)){
                $messages[] = [
                    'code' => 'error',
                    'message' => $field.'_REQUIRED'
                ];

                return $messages;
            }
        }

        return $messages;
    }

    public function getProducts($idSupplier){
        global $database;

        $sql = "SELECT
                    p.idProduct,
                    p.productName
                FROM ".TABLES['suppliers_countries_products']." p
                WHERE p.idSupplier=$idSupplier
                ORDER BY p.productName";

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

    public function addSupplierBid($supplierBid){
        global $database;

        $supplierBid = json_decode($supplierBid);

        $checkData = $this->validateBidData($supplierBid);

        if(!empty($checkData)){
            $data['messages'] = $checkData;

            return $data;
        }

        $sql = "INSERT INTO ".TABLES['supplier_bids']."
                (idProduct, bidNumber)
                VALUES(".$supplierBid->idProduct.", '".$supplierBid->bidNumber."')";

        $query = $database->query($sql);

        if($database->affectedRows() !== 1){
            $data['messages'][] =[
                'code' => 'error',
                'message' => 'SERVER_ERROR'
            ];

            return $data;
        }

        $data['messages'][] = [
            'code' => 'success',
            'message' => 'SUPPLIER_BID_ADDED'
        ];

        return $data;
    }

    public function getUnlinkedSupplierBids(){
        global $database;
        $sql = "SELECT
                    sb.id AS idSupplierBid,
                    sb.bidNumber,
                    sb.idProduct,
                    p.productName,
                    s.name AS supplier
                FROM ".TABLES['supplier_bids']." sb
                INNER JOIN ".TABLES['suppliers_countries_products']." p
                    ON p.idProduct=sb.idProduct
                INNER JOIN ".TABLES['suppliers']." s
                    ON s.id=p.idSupplier
                LEFT OUTER JOIN ".TABLES['rel_bid_supplier_bids']." rbsb
                    ON rbsb.idSupplierBid=sb.id
                WHERE rbsb.idSupplierBid IS NULL";

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

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

        $sql = "SELECT
                    sb.id AS idSupplierBid,
                    sb.bidNumber,
                    sb.idProduct,
                    p.productName,
                    s.name AS supplier,
                    rbsb.idBid
                FROM ".TABLES['supplier_bids']." sb
                INNER JOIN ".TABLES['suppliers_countries_products']." p
                    ON p.idProduct=sb.idProduct
                INNER JOIN ".TABLES['suppliers']." s
                    ON s.id=p.idSupplier
                INNER JOIN ".TABLES['rel_bid_supplier_bids']." rbsb
                    ON rbsb.idSupplierBid=sb.id";
        $query = $database->query($sql);
        while($row = $database->fetchArray($query)){
            $data[$row['idBid']][] = $row;
        }

        return $data;
    }

    public function getOrderSupplierBids($idOrder){
        global $database;
        $data = [];
        $idOrder = $database->escapeValue($idOrder);

        $sql = "SELECT
                    sb.id AS idSupplierBid,
                    sb.bidNumber,
                    sb.idProduct,
                    p.productName,
                    s.id AS idSupplier,
                    s.name AS supplier,
                    rbsb.idBid
                FROM ".TABLES['supplier_bids']." sb
                INNER JOIN ".TABLES['suppliers_countries_products']." p
                    ON p.idProduct=sb.idProduct
                INNER JOIN ".TABLES['suppliers']." s
                    ON s.id=p.idSupplier
                INNER JOIN ".TABLES['rel_bid_supplier_bids']." rbsb
                    ON rbsb.idSupplierBid=sb.id
                INNER JOIN ".TABLES['rel_order_packages']." rop
                    ON rbsb.idBid=rop.idBid
                WHERE rop.idOrder=$idOrder
                ";
        $query = $database->query($sql);
        while($row = $database->fetchArray($query)){
            $data[$row['idSupplier']][] = $row;
        }

        return $data;
    }
}
