<?php

    $limit = @$_REQUEST['limit'] ? @$_REQUEST['limit'] : 10;
    $offset = @$_REQUEST['limit'] ? @$_REQUEST['offset'] : 0;

    $checkins = mysql_select("feed", "`section` = 'check_ins'  ORDER BY `action_date` DESC LIMIT $limit OFFSET $offset");
?>
<?php

    foreach($checkins as $checkin):
        $checkinData     = json_decode($checkin['json'], true);    
        $activityName = $checkin['section'];
    
        // get listings photo
        if( array_key_exists('listingsNum', $checkinData) ){

            $checkinData['listings'] = mysql_get('listings', $checkinData['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
            $checkinData['listings']['recent_photos'] = mysql_select('photo_uploads', "`listingsNum` = ".intval($checkinData['listingsNum'])." AND `for_photo` = 'photo' AND `flagged` = 0 ORDER BY `createdDate` DESC LIMIT 0,1");
            foreach($checkinData['listings']['recent_photos'] as $photoKey => $photo){ 
              $checkinData['listings']['recent_photos'][$photoKey]['photos'] = getUploadRecords('photo_uploads', 'photos' , $photo['num']);
            }
            // add listing close time
            // $checkinData['listings'] = api_addOpenCloseDetailsToListing($checkinData['listings']);
        }

        if(array_key_exists('membersNum', $checkinData)){
            $checkinData['members'] = mysql_get('members', $checkinData['membersNum'], null, "`facebook_picture`, `facebook_first_name`, `facebook_last_name`, `username`, `num`, `isAdmin`, `points`, `check_ins`, `following`, `followers`, `reviews`");
        }
        
        $userName = $checkinData['members']['facebook_first_name']." ".$checkinData['members']['facebook_last_name'][0];
?>
        <div class="feed_section stats_section clearfix">
            <div class="user_info_wrap clearfix" style="margin: 5px 0;">
                <div class="user_img">
                    <img width="30" height="30" src="<?php echo @$checkinData['members']['facebook_picture']; ?>">
                </div>
                <div class="comment_info">
                    <div class="user_name">
                        <span><?php echo $userName; ?>.</span>
                    </div>
                    <div class="user_action">
                        <span style="font-size: 12px;">Checks in</span>
                        <span id="time" style="color: #9b9b9b;font-size: 12px;"><?php echo shortPrettyDate($checkin['action_date']); ?></span>
                    </div>
                </div>
                <div class="points" style="float: right;">
                    <span class="check_in_icon" style="margin-right: 5px;">
                        <img style="vertical-align: middle;" src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/checkin-small.png">
                    </span>
                    <span style="color: #5BBC2E;font-size: 12px;">10 PTS</span>
                </div>
            </div>
            <div class="sponser_image_row clearfix" style="text-align: left;">
                <div style="border-radius:5px;width:50px;height:50px;float: left;">
                    <img style="border-radius: 5px;width:100%;max-width:100%;max-height:100%;" src="<?php echo empty($checkinData['listings']['recent_photos'][0]['photos']) ? '' : $checkinData['listings']['recent_photos'][0]['photos'][0]['thumbUrlPath2']; ?>" />
                </div>

                <div class="activity_info" style="float: left;padding-top: 5px;">
                    <div class="listing_name"><?php echo $checkinData['listings']['name']; ?></div>
                    <div class="activity_images rating_images">
                        <?php $x = 1; for($x=1;$x<=$checkinData['listings']['average_rating'];$x++){ ?>
                        <img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-yellow.png"/>
                    <?php } while($x<=5) { ?>
                        <img src="https://s3-us-west-1.amazonaws.com/urban-halal/email-newsletter/images/rating-small-grey.png"/>
                    <?php $x++; } ?>
                    </div>
                </div>
            </div>
            <div class="comment_wrap" style="margin-top: 10px;">
                <?php
                    $dotColor = "#5BBC2E";
                    // $listing = mysql_get('listings', $checkinData['listingsNum']);
                    // if($listing){
                    //     $listing = api_addOpenCloseDetailsToListing($listing);
                    //     $status = $listing['restaurant_status'];
                    //     $dotColor = ($status == 'open' ? "#5BBC2E" : ($status == 'closed' ? '#D93549' : '#F39628'));
                    // }
                ?>
                <div class="map_top_content">
                    <span class="dot" style="color:<?php echo $dotColor; ?>; padding: 0 15px;">●</span> <?php echo $checkinData['listings']['address_1']; ?>
                </div>
                <div class="checked_in_map" id="map_<?php echo $checkinData['num']; ?>"></div>
                <?php if($checkinData['listings']['latitude']): ?>
                <script type="text/javascript">
                    function initialize() {
                       var latlng = new google.maps.LatLng(<?php echo $checkinData['listings']['latitude']; ?>, <?php echo $checkinData['listings']['longitude']; ?>);
                        var map = new google.maps.Map(document.getElementById('map_<?php echo $checkinData['num']; ?>'), {
                          center: latlng,
                          zoom: 12
                        });
                        var marker = new google.maps.Marker({
                          map: map,
                          position: latlng,
                          draggable: false,
                          anchorPoint: new google.maps.Point(0, -29)
                       });
                    }
                    initialize();
                </script>
                <?php endif; ?>
            </div>
        </div>
        <hr style="margin:0;">
<?php
    endforeach;
?>        