<?php

class BidMargin{
    public function addBidMargin($idBid, $bidMargin){
        global $database;
        $data = [];
        $bidMargin = json_decode($bidMargin);

        if($idBid === 0){
            $data['messages'][] =[
                'code' => 'error',
                'message' => 'BID_ID_REQUIRED'
            ];

            return $data;
        }

        if(!property_exists($bidMargin, 'fixedExtra')
        || !property_exists($bidMargin, 'recurrentExtra')
        || !property_exists($bidMargin, 'servicesExtra')){
            $data['messages'][] =[
                'code' => 'error',
                'message' => 'BID_MARGINS_REQUIRED'
            ];

            return $data;
        }

        $sql = "UPDATE ".TABLES['packages_bids']."
                SET fixedExtra=".$database->escapeValue($bidMargin->fixedExtra).",
                recurrentExtra=".$database->escapeValue($bidMargin->recurrentExtra).",
                servicesExtra=".$database->escapeValue($bidMargin->servicesExtra)."
                WHERE id=".$database->escapeValue($idBid);
        $query = $database->query($sql);

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

            return $data;
        }

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

        return $data;
    }
}
