<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=markettags.main
 * [END_COT_EXT]
 */

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

if(is_array($item_data)){
  require_once cot_incfile('marketorders', 'plug');
  global $db_market_orders;

  $temp_array['MK_INQUEUE'] = cot_mk_queue_cnt(array(), $item_data['item_userid']);
  $temp_array['MK_ORDCNT'] = $db->query("SELECT COUNT(*) FROM $db_market_orders WHERE order_done>0 AND order_pid=".$item_data['item_id'])->fetchColumn();
  if(!$temp_array['MK_ORDCNT']) $temp_array['MK_ORDCNT'] = 0;

  $temp_array['MK_POPULAR'] = ($temp_array['MK_ORDCNT'] > 20 || $item_data['item_mk_popular']) ? 1 : 0;


  $temp_array['MK_ISORDERYET'] = '';
  $temp_array['MK_ISORDERYET_ICON'] = '';
  global $usr;
  if($usr['id'] > 0) {
    $lastpaidorder = $db->query("SELECT * FROM $db_market_orders WHERE order_userid=".$usr['id']." AND order_paid>0 AND order_pid=".$item_data['item_id'].' ORDER BY order_date DESC LIMIT 1')->fetch();
    if($lastpaidorder['order_id'] > 0) {
      $lastpaidorder['order_disorderyet_icon'] = 'green';
      $lastpaidorder['order_disorderyet'] = 'сейчас в работе';

      if($lastpaidorder['order_close'] > 0) {
        $lastpaidorder['order_disorderyet'] = 'был отменен';
        $lastpaidorder['order_disorderyet_icon'] = 'grey';
      } elseif($lastpaidorder['order_done'] > 0) {
        $lastpaidorder['order_disorderyet'] = 'был выполнен';
      } elseif(!$lastpaidorder['order_accept']) {
        $lastpaidorder['order_disorderyet'] = 'на принятии';
      }

      $temp_array['MK_ISORDERYET'] = 'Ранее вы заказывали '.cot_sitename('phrases', 'ety').'. Последний заказ '.$lastpaidorder['order_disorderyet'];
      $temp_array['MK_ISORDERYET_ICON'] = $lastpaidorder['order_disorderyet_icon'];
    }
  }
}