<?php

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

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

require_once cot_incfile('market', 'module');
require_once cot_incfile('marketorders', 'plug');

global $db_market, $db_market_orders, $db_market_cart, $db_market_citems;

$usr['marketorders_cntordersales'] = 0;
$usr['marketorders_cartid'] = 0;
$usr['marketorders_cartinpay'] = 0;
$usr['marketorders_cartcnt'] = 0;
if($usr['id'] > 0) {
  $usr['marketorders_cntordersales'] = $db->query("SELECT COUNT(*) FROM $db_market_orders AS o
	         WHERE o.order_status!='paid' AND o.order_seller=" . $usr['id'])->fetchColumn();

  $mcart = $db->query("SELECT * FROM $db_market_cart WHERE cart_userid=".$usr['id'].' AND cart_paid=0 AND (cart_status="incart" OR cart_status="inpay") ORDER BY cart_id DESC LIMIT 1')->fetch();
  if($mcart['cart_id'] > 0) {
    $usr['marketorders_cartid'] = $mcart['cart_id'];

    $citems = $db->query("SELECT * FROM $db_market_citems WHERE ci_userid=".$usr['id'].' AND ci_cid='.$mcart['cart_id'].' ORDER BY ci_id DESC')->fetchAll();
    foreach($citems as $ci) {
      $item = $db->query("SELECT * FROM $db_market WHERE item_id=".$ci['ci_pid'].' AND item_state=0 LIMIT 1')->fetch();
      if($item['item_id'] > 0) {
        $item['photo_first'] = 'http://via.placeholder.com/90x60';
        $item['photo_first_crop'] = 'http://via.placeholder.com/90x60';
        foreach(array(1,2,3) as $i) {
          if(!empty($item['item_file_'.$i]) && file_exists($market_imgdir . $item['item_file_'.$i])) {
            $item['photo_first'] = $market_imgdir . $item['item_file_'.$i];

            $item['photo_first_crop'] = $market_imgdir_crop . $item['item_file_'.$i];
            if(file_exists($item['photo_first_crop'])) {
              $item['photo_first_crop'] = $item['photo_first_crop'];
            } else {
              $item['photo_first_crop'] = $item['photo_first'];
            }
            break;
          }
        }
        $ci['extrascnt'] = 0;

        $ci['ci_catcostplans'] = (!empty($ci['ci_catcostplans']) ? json_decode($ci['ci_catcostplans'],1) : array());
        if($ci['ci_catcostplans']['package'] == 'single') {
          $ci['extrascnt'] = 0;
          foreach($ci['ci_catcostplans']['extras']['selected'] as $exts) {
            if($exts['count'] > 0) $ci['extrascnt']++;
          }
        }
				$urlparams = empty($item['item_alias']) ?
					array('c' => $item['item_cat'], 'id' => $item['item_id']) :
					array('c' => $item['item_cat'], 'al' => $item['item_alias']);

        $t->assign('CART', array(
          'id' => $ci['ci_id'],
          'title' => $item['item_title'],
          'item_url' => cot_url('market', $urlparams, '', true),
          'photo_first' => $item['photo_first'],
          'photo_first_crop' => $item['photo_first_crop'],
          'cnt_item' => $ci['ci_count'],
          'cnt_extras' => $ci['extrascnt'],
          'cost' => round($ci['ci_cost'], 0),
          'cost_format' => number_format(round($ci['ci_cost'], 0), 0, ' ', '')
        ));
        $t->parse('HEADER.CART_ROW');
        $usr['marketorders_cartcnt']++;
      }
    }
    if($mcart['cart_status'] == 'inpay') {
      $usr['marketorders_cartinpay'] = $db->query("SELECT pay_id FROM $db_payments WHERE pay_area='market_paycart' AND pay_code=".$mcart['cart_id'].' LIMIT 1')->fetchColumn();
    }
  }

  $usr['marketorders_cnt'] = array(
    'seller_new' => $db->query("SELECT COUNT(*) FROM $db_market_orders
                        WHERE order_status!='new' AND order_status!='done' AND order_status!='cancel' AND order_seller=" . $usr['id'])->fetchColumn(),
    'payer_new' => $db->query("SELECT COUNT(*) FROM $db_market_orders
                        WHERE order_status!='new' AND order_status!='done' AND order_status!='cancel' AND order_userid=" . $usr['id'])->fetchColumn(),
  );

  if($usr['maingrp'] == 5 || $usr['maingrp'] == 9) {
    $usr['marketorders_cnt']['admin_claim'] = $db->query("SELECT COUNT(*) FROM $db_market_orders WHERE order_status='claim'")->fetchColumn();
  }

  foreach($usr['marketorders_cnt'] as $key => $val) {
    if(!$val) $usr['marketorders_cnt'][$key] = 0;
  }
}

$out['market_lastord'] = $db->query("SELECT order_paid FROM $db_market_orders WHERE order_paid>0 ORDER BY order_paid DESC LIMIT 1")->fetchColumn();
$out['market_lastord'] = cot_build_timeago($out['market_lastord']);