/**
 * Save feedback;
 */
function saveFeedback()
{
    $userID = buckys_is_logged_in();
    if (!$userID) {
        //You should be logged in
        return;
    } else {
        $feedbackIns = new BuckysFeedback();
        $tradeID = get_secure_integer($_REQUEST['tradeID']);
        $score = get_secure_string($_REQUEST['score']);
        $feedback = get_secure_string($_REQUEST['feedback']);
        $feedbackID = $feedbackIns->addFeedback($userID, $score, $feedback, $tradeID, BuckysFeedback::ACTIVITY_TYPE_TRADE);
        if (!$feedbackID) {
            echo json_encode(['success' => 0, 'msg' => "You do not have permission."]);
        } else {
            echo json_encode(['success' => 1, 'msg' => "You have left feedback successfully."]);
        }
    }
}
/**
 * Save feedback;
 */
function saveFeedback()
{
    $userID = buckys_is_logged_in();
    if (!$userID) {
        //You should be logged in
        return;
    } else {
        $feedbackIns = new BuckysFeedback();
        $orderIns = new BuckysShopOrder();
        $orderID = get_secure_integer($_REQUEST['orderID']);
        $score = get_secure_string($_REQUEST['score']);
        $feedback = get_secure_string($_REQUEST['feedback']);
        $orderData = $orderIns->getOrderByID($orderID);
        $feedbackID = null;
        if ($orderData['buyerID'] == $userID) {
            $feedbackID = $feedbackIns->addFeedback($userID, $score, $feedback, $orderID, BuckysFeedback::ACTIVITY_TYPE_SHOP);
        }
        if (!$feedbackID) {
            echo json_encode(['success' => 0, 'msg' => "You do not have permission."]);
        } else {
            echo json_encode(['success' => 1, 'msg' => "You have left feedback successfully."]);
        }
    }
}
//Calc base URL
$baseURLParts = [];
if ($paramType == 'given') {
    $baseURLParts[] = "type=" . $paramType;
} else {
    $paramType = 'received';
}
if ($userID != buckys_is_logged_in()) {
    $baseURLParts[] = "user="******"'s Feedback Given - " . TNB_SITE_NAME;
    } else {
        $TNB_GLOBALS['title'] = trim($userData['firstName'] . ' ' . $userData['lastName']) . "'s Feedback Received- " . TNB_SITE_NAME;
        //Mark the activity (offer received) as read
        $tradeNotificationIns = new BuckysTradeNotification();
        $tradeNotificationIns->markAsRead($userID, BuckysTradeNotification::ACTION_TYPE_FEEDBACK);
    }
 /**
  * Get trades completed by this user
  *
  * @param integer $userID
  * @param string  $type : one of the following 'history', 'completed'
  * @return Indexed
  */
 public function getTradesByUserID($userID, $type = 'completed')
 {
     global $db;
     if (!is_numeric($userID)) {
         return;
     }
     $query = sprintf('
                 SELECT t.tradeID, t.sellerID, t.buyerID, t.sellerItemID, t.buyerItemID, t.sellerTrackingNo, t.buyerTrackingNo, t.createdDate AS tradeCreatedDate, 
                     sItem.title AS sellerItemTitle, sItem.subtitle AS sellerItemSubtitle, sItem.images AS sellerItemImages, 
                     bItem.title AS buyerItemTitle, bItem.subtitle AS buyerItemSubtitle, bItem.images AS buyerItemImages, 
                     CONCAT(sUserDetail.firstname, " ", sUserDetail.lastName) AS sellerShFullName, sShipInfo.address AS sellerShAddress, sShipInfo.address2 AS sellerShAddress2, sShipInfo.city AS sellerShCity, sShipInfo.state AS sellerShState, sShipInfo.zip AS sellerShZip, sShipInfo.countryID AS sellerShCountryID,
                     CONCAT(bUserDetail.firstname, " ", bUserDetail.lastName) AS buyerShFullName, bShipInfo.address AS buyerShAddress, bShipInfo.address2 AS buyerShAddress2, bShipInfo.city AS buyerShCity, bShipInfo.state AS buyerShState, bShipInfo.zip AS buyerShZip, bShipInfo.countryID AS buyerShCountryID,
                     sUser.totalRating AS sellerTotalRating, sUser.positiveRating AS sellerPositiveRating,
                     bUser.totalRating AS buyerTotalRating, bUser.positiveRating AS buyerPositiveRating
                 FROM %s AS t 
                     LEFT JOIN %s AS sItem ON sItem.itemID = t.sellerItemID 
                     LEFT JOIN %s AS bItem ON bItem.itemID = t.buyerItemID 
                     LEFT JOIN %s AS sShipInfo ON sShipInfo.shippingID = t.sellerShippingID 
                     LEFT JOIN %s AS bShipInfo ON bShipInfo.shippingID = t.buyerShippingID 
                     LEFT JOIN %s AS sUser ON sUser.userID = t.sellerID 
                     LEFT JOIN %s AS bUser ON bUser.userID = t.buyerID                         
                     LEFT JOIN %s AS sUserDetail ON sUserDetail.userID = t.sellerID 
                     LEFT JOIN %s AS bUserDetail ON bUserDetail.userID = t.buyerID                         
         ', TABLE_TRADE, TABLE_TRADE_ITEMS, TABLE_TRADE_ITEMS, TABLE_TRADE_SHIPPING_INFO, TABLE_TRADE_SHIPPING_INFO, TABLE_USERS_RATING, TABLE_USERS_RATING, TABLE_USERS, TABLE_USERS);
     switch ($type) {
         case 'history':
             $query = $db->prepare($query . ' WHERE (t.sellerID=%d OR t.buyerID=%d) AND t.status=%d', $userID, $userID, BuckysTrade::TRADE_TRADED);
             break;
         default:
             $query = sprintf($query . ' WHERE (t.sellerID=%d OR t.buyerID=%d) AND t.status=%d AND t.tradeID NOT IN (SELECT tFeedback.activityID FROM %s AS tFeedback WHERE tFeedback.activityType=%d AND tFeedback.writerID=%d)', $userID, $userID, BuckysTrade::TRADE_TRADED, TABLE_FEEDBACK, BuckysFeedback::ACTIVITY_TYPE_TRADE, $userID, $userID);
             $query = $db->prepare($query);
             break;
     }
     //Order by Trade ID
     $query .= " ORDER BY t.createdDate DESC";
     $tradeList = $db->getResultsArray($query);
     if ($tradeList) {
         //We have to add feedback info to display them
         $tradeIDList = [];
         foreach ($tradeList as $data) {
             $tradeIDList[] = $data['tradeID'];
         }
         $feedbackIns = new BuckysFeedback();
         foreach ($tradeList as &$tradeData) {
             $feedbackData = $feedbackIns->getTradeFeedback($tradeData['tradeID']);
             if ($feedbackData) {
                 foreach ($feedbackData as $fData) {
                     if ($fData['writerID'] == $tradeData['sellerID']) {
                         $tradeData['buyerFeedbackScore'] = $fData['score'];
                     } else {
                         $tradeData['sellerFeedbackScore'] = $fData['score'];
                     }
                 }
             }
         }
     }
     return $tradeList;
 }