<?php
    $feedHtml = '';
    $GOOGLE_MAP_API = $GLOBALS['SETTINGS']['googleApiKey'];
    $assetsLink = $GLOBALS['SETTINGS']['s3AssetsLink'];
    $listingPhoto = $assetsLink."uh-default-new.png"; 
    foreach($feeds as $feed){
    $feedData     = json_decode($feed['json'], true);    
    $activityName = $feed['section'];
    
    // get listings photo
    if( array_key_exists('listingsNum', $feedData) ){

        $feedData['listings'] = mysql_get('listings', $feedData['listingsNum'], null, "`timezone`,`monday_open`,`monday_close`,`tuesday_open`,`tuesday_close`,`wednesday_open`,`wednesday_close`,`thursday_open`,`thursday_close`,`friday_open`,`friday_close`,`saturday_open`,`saturday_close`,`sunday_open`,`sunday_close`, `latitude`, `longitude`, `address_1`, `name`, `city`, `average_rating`, `checkins`, `cuisine`, `coupons`, `reviews`, `followers`, `num`");
        // add listings recent photos
        $feedData['listings']['recent_photos'] = mysql_select('photo_uploads', "`listingsNum` = ".intval($feedData['listingsNum'])." AND `for_photo` = 'photo' AND `flagged` = 0 ORDER BY `createdDate` DESC LIMIT 0,1");
        foreach($feedData['listings']['recent_photos'] as $photoKey => $photo){ 
          $feedData['listings']['recent_photos'][$photoKey]['photos'] = getUploadRecords('photo_uploads', 'photos' , $photo['num']);
        }
        // add listing close time
        // $feedData['listings'] = api_addOpenCloseDetailsToListing($feedData['listings']);
    }

    if(array_key_exists('membersNum', $feedData)){
        $feedData['members'] = mysql_get('members', $feedData['membersNum'], null, "`facebook_picture`, `facebook_first_name`, `facebook_last_name`, `username`, `num`, `isAdmin`, `points`, `check_ins`, `following`, `followers`, `reviews`");
    }
    $userName = $feedData['members']['facebook_first_name']." ".@$feedData['members']['facebook_last_name'][0];

    if($activityName == "reviews"){
        
    ?>
    <?php
    $feedHtml .= '<tr>
                          <td align="center" style="padding: 10px 0 0">
                            <table cellspacing="0" cellpadding="0" style="" width="300">
                              <tbody>
                                <tr>
                                  <td style="padding: 0 5px 0 0;" width="35">
                                    <img width="30" height="30" src="'.$feedData['members']['facebook_picture'].'" style="border-radius: 50%;" />
                                  </td>
                                  <td style="font-size:14px;" width="230">
                                    <p style="margin:0;line-height: 12px;"><b>'.$userName.'.</b></p>
                                    <p style="font-size: 11px;margin:0;">Leaves a Tip</p>
                                  </td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;" width="25">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/tips-small.png" />
                                  </td>
                                  <td width="10">20</td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 20px 0;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td style="border-radius: 5px; width: 50px; height: 50px; float: left;"><img style="border-radius: 5px;" width="50" height="50" src="';
                                  $listingPhoto = empty($feedData['listings']['recent_photos'][0]['photos']) ? $listingPhoto : $feedData['listings']['recent_photos'][0]['photos'][0]['thumbUrlPath2'];
                                  $feedHtml .= $listingPhoto.'" />
                                  </td>
                                  <td style="float: left; padding-top: 5px;">
                                    <div style="color: #d93549; font-size: 16px; margin-left: 5px; font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.substr($feedData['listings']['name'], 0, 20).'...</div>
                                    <div style="margin-top: 5px; margin-left: 5px;">';
                                    $x = 1;
                    for($x=1;$x<=$feedData['listings']['average_rating'];$x++){
                        $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-yellow.png"/>';
                    }
                    while($x<=5) {
                        $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-grey.png"/>';
                        $x++; 
                    }
                $feedHtml .= '</div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding:0 0px 20px 0px;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td>
                                    <div style="text-align: left; font-size: 13px; color: #666;">'.
                                                $feedData['tip_content']
                                    .'</div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>';

    }

    if($activityName == 'user_badges'){

        $feedData['badges'] = mysql_get('badges', $feedData['badgesNum'], null, "`badge_name`, `badge_description`, `points`, `badge_type`");

        $feedData['badges']['completed_image'] = getUploadRecords('badges', 'completed_image', $feedData['badgesNum']);
        $feedData['badges']['gif_badge'] = getUploadRecords('badges', 'gif_badge', $feedData['badgesNum']);
 
    $feedHtml .= '<tr>
                          <td align="center" style="padding: 10px 0 0">
                            <table cellspacing="0" cellpadding="0" style="" width="300">
                              <tbody>
                                <tr>
                                  <td style="padding: 0 5px 0 0;" width="35">
                                    <img width="30" height="30" src="'.$feedData['members']['facebook_picture'].'" style="border-radius: 50%;" />
                                  </td>
                                  <td style="font-size:14px;" width="230">
                                    <p style="margin:0;line-height: 12px;"><b>'.$userName.'</b></p>
                                    <p style="font-size: 11px;margin:0;">Unlocks a achievement</p>
                                  </td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;" width="25">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/badge-small.png" />
                                  </td>
                                  <td width="10">100</td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 20px 0;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td style="border-radius: 5px; width: 50px; height: 50px; float: left;"><img style="border-radius: 5px;" width="50" height="50" src="';
                                  $badgeImage = count($feedData['badges']['completed_image']) ? $feedData['badges']['completed_image'][0]['urlPath'] : '';
                                  $feedHtml .= $badgeImage.'" />
                                                        </td>
                                  <td style="float: left; padding-top: 5px;">
                                    <div style="color: #d93549; font-size: 16px; margin-left: 5px; font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.$feedData['badges']['badge_name'].'</div>
                                    <div style="margin-top: 5px; margin-left: 5px;">'.
                                            $feedData['badges']['badge_description']
                                        .'</div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>';
    
    }

    if($activityName == 'ratings'){
        
    $feedHtml .= '<tr>
                          <td align="center" style="padding: 10px 0 0">
                            <table cellspacing="0" cellpadding="0" style="" width="300">
                              <tbody>
                                <tr>
                                  <td style="padding: 0 5px 0 0;" width="35">
                                    <img width="30" height="30" src="'.$feedData['members']['facebook_picture'].'" style="border-radius: 50%;" />
                                  </td>
                                  <td style="font-size:14px;" width="230">
                                    <p style="margin:0;line-height: 12px;"><b>'.$userName.'.</b></p>
                                    <p style="font-size: 11px;margin:0;">Rates</p>
                                  </td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;" width="25">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-grey.png" />
                                  </td>
                                  <td width="10">15</td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 20px 0;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td style="border-radius: 5px; width: 50px; height: 50px; float: left;"><img style="border-radius: 5px;" width="50" height="50" src="';
                                  $listingImage = empty($feedData['listings']['recent_photos'][0]['photos']) ? $listingPhoto : $feedData['listings']['recent_photos'][0]['photos'][0]['thumbUrlPath2'];
                                  $feedHtml .= $listingImage.'" />
                                  </td>
                                  <td style="float: left; padding-top: 5px;">
                                    <div style="color: #d93549; font-size: 16px; margin-left: 5px; font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.$feedData['listings']['name'].'</div>
                                    <div style="margin-top: 5px; margin-left: 5px;">';
                                    $x = 1;
                    for($x=1;$x<=$feedData['rating'];$x++){
                        $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-yellow.png"/>';
                    }
                    while($x<=5) {
                        $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-grey.png"/>';
                        $x++;
                    }
                               
                               $feedHtml .= '</div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>';
    }

    if($activityName == 'foodie_experience'){
        
        $feedHtml .= '<tr>
                          <td align="center" style="padding: 10px 0 0">
                            <table cellspacing="0" cellpadding="0" style="" width="300">
                              <tbody>
                                <tr>
                                  <td style="padding: 0 5px 0 0;" width="35">
                                    <img width="30" height="30" src="'.$feedData['members']['facebook_picture'].'" style="border-radius: 50%;" />
                                  </td>
                                  <td style="font-size:14px;" width="230">
                                    <p style="margin:0;line-height: 12px;"><b>'.$userName.'</b></p>
                                    <p style="font-size: 11px;margin:0;">Takes a survey</p>
                                  </td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;" width="25">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/emoji-happy-small.png" />
                                  </td>
                                  <td width="10">20</td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 20px 0;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td style="border-radius: 5px; width: 50px; height: 50px; float: left;"><img style="border-radius: 5px;" width="50" height="50" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/FoodieExp3x.png" /></td>
                                  <td style="float: left; padding-top: 5px;">
                                    <div style="color: #d93549; font-size: 16px; margin-left: 5px; font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">Foodie Experience</div>
                                    <div style="margin-top: 5px; margin-left: 5px;overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.$feedData['listings']['name'].' food and dining</div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>';
    }

    if($activityName == 'user_follows'){
        
        $feedHtml .= '<tr>
                          <td align="center" style="padding: 10px 0 0">
                            <table cellspacing="0" cellpadding="0" style="" width="300">
                              <tbody>
                                <tr>
                                  <td style="padding: 0 5px 0 0;" width="35">
                                    <img width="30" height="30" src="'.$feedData['members']['facebook_picture'].'" style="border-radius: 50%;" />
                                  </td>
                                  <td style="font-size:14px;" width="230">
                                    <p style="margin:0;line-height: 12px;"><b>'.$userName.'</b></p>
                                    <p style="font-size: 11px;margin:0;">Follows</p>
                                  </td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;" width="25">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/follow-user-small.png" />
                                  </td>
                                  
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 20px 0;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td style="border-radius: 5px; width: 50px; height: 50px; float: left;"><img style="border-radius: 5px;" width="50" height="50" src="'.$feedData['user_following.facebook_picture'].'" /></td>';
                                  $followingUserName = $feedData['user_following.facebook_first_name']." ".$feedData['user_following.facebook_last_name'][0];
                         $feedHtml .=  '<td style="float: left; padding-top: 5px;">
                                    <div style="color: #d93549; font-size: 16px; margin-left: 5px; font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.$followingUserName.'</div>
                                    <div style="margin-top: 5px; margin-left: 5px;overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"><table border="0" cellpadding="0" cellspacing="0">
                              <tbody>
                                <tr>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;padding:0 0 0 5px;" width="20">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/follow-user-small.png" />
                                  </td>
                                  <td style="padding:0 0 0 3px;" width="10">'.$feedData['user_following.followers'].'</td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;padding:0 0 0 5px;" width="20">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/tips-small.png" />
                                  </td>
                                  <td style="padding:0 0 0 3px;" width="10">'.$feedData['user_following.reviews'].'</td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;padding:0 0 0 5px;" width="20">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/checkin-small.png" />
                                  </td>
                                  <td style="padding:0 0 0 3px;" width="10">'.$feedData['user_following.check_ins'].'</td>
                                </tr>
                              </tbody>
                            </table></div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>';
    }










if($activityName == 'check_ins'){
        
        $feedHtml .= '<tr>
                          <td align="center" style="padding: 10px 0 0">
                            <table cellspacing="0" cellpadding="0" style="" width="300">
                              <tbody>
                                <tr>
                                  <td style="padding: 0 5px 0 0;" width="35">
                                    <img width="30" height="30" src="'.$feedData['members']['facebook_picture'].'" style="border-radius: 50%;" />
                                  </td>
                                  <td style="font-size:14px;" width="230">
                                    <p style="margin:0;line-height: 12px;"><b>'.$userName.'</b></p>
                                    <p style="font-size: 11px;margin:0;">Checks in</p>
                                  </td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;" width="25">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/checkin-small.png" />
                                  </td>
                                  <td width="10">20</td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 20px 0;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td style="border-radius: 5px; width: 50px; height: 50px; float: left;"><img style="border-radius: 5px;" width="50" height="50" src="';
                                  $listingPhoto = empty($feedData['listings']['recent_photos'][0]['photos']) ? $listingPhoto : $feedData['listings']['recent_photos'][0]['photos'][0]['thumbUrlPath2'];
                                  $feedHtml .= $listingPhoto.'" />
                                  </td>
                                  <td style="float: left; padding-top: 5px;">
                                    <div style="color: #d93549; font-size: 16px; margin-left: 5px; font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.$feedData['listings']['name'].'</div>
                                    <div style="margin-top: 5px; margin-left: 5px;">';
                                    $x = 1;
                            for($x=1;$x<=$feedData['listings']['average_rating'];$x++){
                                $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-yellow.png"/>';
                            }
                            while($x<=5) {
                                $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-grey.png"/>';
                                $x++;
                            }

                        $feedHtml .='</div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 0 10px 30px 10px;">
                            <table width="100%" cellspacing="0" cellpadding="0" style="margin: 0; padding: 0; border: 1px solid #EEEEEE; border-radius: 5px; overflow: hidden;">
                              <tbody>
                                <tr>
                                  <td  style="padding: 0;" align="center">
                                    <table style="text-align: left; width: 100%;">
                                      <tbody>
                                        <tr>
                                          <td style="padding-top: 5px;border-radius: 5px; width: 8px; height: 8px; float: left;"><img style="border-radius: 5px;" width="8" height="8" src="https://s3-us-west-1.amazonaws.com/cannvus-prod/static_assets/landing_page/green_dot.png" /></td>
                                          <td align="left" style="padding-top: 5px;">
                                            <div style="color: #4a4a4a;  font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.$feedData['listings']['address_1'].'</div>
                                          </td>
                                        </tr>
                                      </tbody>
                                    </table>
                                  </td>
                                </tr>
                                <tr>
                                  <td>
                                    <img src="https://maps.google.com/maps/api/staticmap?center=' . $feedData['listings']['latitude'] . ',' . $feedData['listings']['longitude'] . '&zoom=16&size=280x80&scale=1&key=' . $GLOBALS['SETTINGS']['googleApiKey'] . '" alt="" height="60" width="100%" />
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>';
    }




if($activityName == 'listings_follows'){
        
        $feedHtml .= '<tr>
                          <td align="center" style="padding: 10px 0 0">
                            <table cellspacing="0" cellpadding="0" style="" width="300">
                              <tbody>
                                <tr>
                                  <td style="padding: 0 5px 0 0;" width="35">
                                    <img width="30" height="30" src="'.$feedData['members']['facebook_picture'].'" style="border-radius: 50%;" />
                                  </td>
                                  <td style="font-size:14px;" width="230">
                                    <p style="margin:0;line-height: 12px;"><b>'.$userName.'.</b></p>
                                    <p style="font-size: 11px;margin:0;">Follows</p>
                                  </td>
                                  <td align="left" style="color: #5bbc2e; font-size: 12px;" width="25">
                                    <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/follow-user-small.png" />
                                  </td>
                                  <td width="10">15</td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 20px 0;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td style="border-radius: 5px; width: 50px; height: 50px; float: left;"><img style="border-radius: 5px;" width="50" height="50" src="';
                                  $listingPhoto = empty($feedData['listings']['recent_photos'][0]['photos']) ? $listingPhoto : $feedData['listings']['recent_photos'][0]['photos'][0]['thumbUrlPath2'];
                                  $feedHtml .= $listingPhoto.'" />
                                  </td>
                                  <td style="float: left; padding-top: 5px;">
                                    <div style="color: #d93549; font-size: 16px; margin-left: 5px; font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.$feedData['listings']['name'].'</div>
                                    <div style="margin-top: 5px; margin-left: 5px;">';
                                     $x = 1; for($x=1;$x<=$feedData['listings']['average_rating'];$x++){ 
                       $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-yellow.png"/>';
                         } while($x<=5) { 
                       $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-grey.png"/>';
                        $x++; } 
                  $feedHtml .='  </div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>';
    }


    if($activityName == 'orders'){
        
    $feedHtml .= '<tr>
                          <td align="center" style="padding: 10px 10px 0">
                            <table cellspacing="0" cellpadding="0" style="" width="300">
                              <tbody>
                                <tr>
                                  <td style="padding: 0 5px 0 0;" width="35">
                                    <img width="30" height="30" src="'.$feedData['members']['facebook_picture'].'" style="border-radius: 50%;" />
                                  </td>
                                  <td style="font-size:14px;" width="230">
                                    <p style="margin:0;line-height: 12px;"><b>'.$userName.'.</b></p>
                                    <p style="font-size: 11px;margin:0;">Orders'.ucfirst($feedData['order_delivery_type']).'</p>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>
                        <tr>
                          <td style="padding: 10px;">
                            <table style="text-align: left; width: 100%;">
                              <tbody>
                                <tr>
                                  <td style="border-radius: 5px; width: 50px; height: 50px; float: left;"><img style="border-radius: 5px;" width="50" height="50" src="';
                                  $listingImage = empty($feedData['listings']['recent_photos'][0]['photos']) ? $listingPhoto : $feedData['listings']['recent_photos'][0]['photos'][0]['thumbUrlPath2'];
                                  $feedHtml .= $listingImage.'" />
                                  </td>
                                  <td style="float: left; padding-top: 5px;">
                                    <div style="color: #d93549; font-size: 16px; margin-left: 5px; font-weight: bold; max-width: 215px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">'.$feedData['listings']['name'].'</div>
                                    <div style="margin-top: 5px; margin-left: 5px;">';
                                    $x = 1;
                    for($x=1;$x<=$feedData['listings']['average_rating'];$x++){
                        $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-yellow.png"/>';
                    }
                    while($x<=5) {
                        $feedHtml .= '<img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-grey.png"/>';
                        $x++;
                    }
                               
                               $feedHtml .= '</div>
                                  </td>
                                </tr>
                              </tbody>
                            </table>
                          </td>
                        </tr>';
    }

}