<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=market.tags
 * [END_COT_EXT]
 */

defined('COT_CODE') or die('Wrong URL.');

require_once cot_incfile('reviews', 'plug');

$reviews_cnt = array(
  'all' => 0,
  'pos' => $db->query("SELECT COUNT(*) FROM $db_reviews WHERE item_score=1 AND item_area='market' AND item_type='review' AND item_mcode=".$item['item_id'])->fetchColumn(),
  'neg' => $db->query("SELECT COUNT(*) FROM $db_reviews WHERE item_score=0 AND item_area='market' AND item_type='review' AND item_mcode=".$item['item_id'])->fetchColumn()
);
$reviews_cnt['all'] = $reviews_cnt['pos'] + $reviews_cnt['neg'];

$review_index = 1;
$review_limit = 5;

$reviews = $db->query("SELECT * FROM $db_reviews WHERE item_score=1 AND item_area='market' AND item_type='review' AND item_mcode=".$item['item_id'].' LIMIT 30')->fetchAll();
foreach($reviews as $review) {
  $timeago = cot_build_timeago(($sys['now'] - ($sys['now'] - $review['item_date'])));
  $t->assign(cot_generate_usertags($review['item_userid'], 'REVIEW_ROW_OWNER_'));
  $t->assign(array(
  	'REVIEW_ROW_ID' => $review['item_id'],
  	'REVIEW_ROW_TEXT' => $review['item_text'],
  	'REVIEW_ROW_SCORE' => $review['item_score'],
  	'REVIEW_ROW_DATE' => $review['item_date'],
    'REVIEW_ROW_TIMEAGO' => $timeago,
    'REVIEW_ROW_INDEX' => $review_index
  ));

  if($review_limit > 0) {
    $mreview = $db->query("SELECT * FROM $db_reviews WHERE item_area='market' AND item_type='review_answer' AND item_code='".$review['item_code']."' LIMIT 1")->fetch();
    if($mreview['item_id'] > 0) {
      $timeago = cot_build_timeago(($sys['now'] - ($sys['now'] - $mreview['item_date'])));
      $t->assign(array(
      	'REVIEW_ANSW_ID' => $mreview['item_id'],
      	'REVIEW_ANSW_TEXT' => $mreview['item_text'],
      	'REVIEW_ANSW_SCORE' => $mreview['item_score'],
      	'REVIEW_ANSW_DATE' => $mreview['item_date'],
        'REVIEW_ANSW_TIMEAGO' => $timeago
      ));
      $t->parse('MAIN.REVIEWS_ROWS.REV_ANSWER');
    }
  }

  //$t->assign('REVIEW_ROW_FOLIO', '');

  //if($review['item_code'] > 0) {
  //  $t->assign('REVIEW_ROW_FOLIO', $db->query("SELECT order_folio FROM $db_market_orders WHERE order_id=".$review['item_code'].' LIMIT 1')->fetchColumn());
  //}

  $t->parse('MAIN.REVIEWS_SLIDER');
  $t->parse('MAIN.REVIEWS_SLIDER_MOBILE');

  if($review_limit > 0) $t->parse('MAIN.REVIEWS_ROWS');

  $review_limit--;
  $review_index++;
}

//допарсиваем остальные фото
while($review_index < 3) {
  if(!empty($item['item_file_'.($review_index+1)])) {
    $t->assign(array(
    	'REVIEW_ROW_ID' => 0,
      'REVIEW_ROW_INDEX' => $review_index
    ));

    $t->parse('MAIN.REVIEWS_SLIDER');
    $t->parse('MAIN.REVIEWS_SLIDER_MOBILE');

    $review_index++;
  } else {
    break;
  }
}

$t->assign(array(
  'PRD_REVIEWS_CNT_ALL' => $reviews_cnt['all'],
  'PRD_REVIEWS_CNT_POS' => $reviews_cnt['pos'],
  'PRD_REVIEWS_CNT_NEG' => $reviews_cnt['neg'],
));

?>