<?php
defined('COT_CODE') or die('Wrong URL');

/* foreach as $mkt */
  $mkt['t_code_parse'] = $mkt['t_code'];
  $mktdata = json_decode($mkt['t_json'], 1);
  /*
  if(!is_array($mktdata)) {
    $mktdata = json_decode($mktdata, 1);

    $db->update($db_market_track, array('t_json' => json_encode($mktdata)), 't_id='.$mkt['t_id']);
  }
  */

  if(in_array($mkt['t_code'], array('market_msg', 'seller_accept_order', 'payer_addextras', 'seller_addextras', 'seller_check_order', 'payer_reorder', 'payer_to_arbitrage', 'seller_to_arbitrage'))) {
    if(!empty($mktdata['msg'])) {
      $mktdata['msg'] = cot_mk_msg_html($mktdata['msg']);
    }
  }
  elseif(in_array($mkt['t_code'], array_keys($marketorders_status_codes))) {
    $mkt['t_code_parse'] = 'cancel';
    if(!empty($mktdata['msg'])) {
      $mktdata['msg'] = cot_mk_msg_html($mktdata['msg']);
    }
  }
  elseif($mkt['t_code'] == 'arbitrage_result') {
    $mkt['t_code_parse'] = 'arbitrage_result';
    $mkt['t_code'] .= '_'.$mktdata['status'];

    if(!empty($mktdata['msg'])) {
      $mktdata['msg'] = cot_mk_msg_html($mktdata['msg']);
    }
  } elseif($mkt['t_code'] == 'report_phases') {
    if(!empty($mktdata['msg'])) {
      $mktdata['msg'] = cot_mk_msg_html($mktdata['msg']);
    }

    $phases_cur = (!empty($marketorder['order_steps']) ? json_decode($marketorder['order_steps'],1) : array());
    if(count($phases_cur) > 0 && count($mktdata['changes']) > 0) {
       foreach($mktdata['changes'] as $i => $phase) {
         if(is_array($phases_cur[$phase['step']])) {
           $mktdata['changes'][$i]['title'] = (!empty($phases_cur[$phase['step']]) ? $phases_cur[$phase['step']]['title'] : 'Этап №'.($phase['step']+1));
           $mktdata['changes'][$i]['changes_main'] = $phases_cur[$phase['step']]['changes'];
         }
       }
    }
  }

  $mkt['mkt_status'] = (($mkt['t_subid'] > 0 && $mkt['t_fruser'] == $usr['id']) ? $db->query("SELECT t_status FROM $db_market_track WHERE t_id=".$mkt['t_subid'].' LIMIT 1')->fetchColumn() : $mkt['t_status']);

  $t->assign(array(
    "MKT_ID" => $mkt['t_id'],
    "MKT_JJ" => $jj,
    "MKT_JJ_SHOW" => ((isset($mktrack_limit) && isset($mktrack_cnt) && $mktrack_limit > 0) ? ($mktrack_limit >= ($mktrack_cnt - $jj)) : 1),
    "MKT_FRUSER" => $mkt['t_fruser'],
    "MKT_TOUSER" => $mkt['t_touser'],
    "MKT_STATUS" => $mkt['mkt_status'],
    "MKT_MSG_EDITABLE" => (($is_admin || (!$mkt['mkt_status'] && $mkt['t_fruser'] == $usr['id'] && !$marketorder['order_claim'] && !$marketorder['order_done'])) ? 1 : 0),
    "MKT_TITLE" => ($mkt['t_code_parse'] == 'cancel' ? $marketorders_status_codes[$mkt['t_code']]['track_state'] : $marketorders_status_hist[$mkt['t_code']]['track_state']),
    "MKT_TEXT" => ($mkt['t_code_parse'] == 'cancel' ? $marketorders_status_codes[$mkt['t_code']]['track_'.($marketorder['order_seller'] == $usr['id'] ? 'perf' : 'cust')] : $marketorders_status_hist[$mkt['t_code']]['track_'.($marketorder['order_seller'] == $usr['id'] ? 'perf' : 'cust')]),
    "MKT_CODE" => $mkt['t_code_parse'],
    "MKT_CODE_ORIG" => $mkt['t_code'],
    "MKT_CODE_ORIG_IN_SELLERCANCEL_CODES" => (in_array($mkt['t_code'], array('seller_no_reason', 'seller_payer_cannot_execute_correct', 'seller_do_not_like_this_worker', 'seller_no_communication_with_worker', 'seller_no_time', 'seller_force_cancel')) ? 1 : 0),
  	"MKT_DATE" => cot_date('d M, H:i', $mkt['t_date']),
    "MKT_JSON" => $mktdata
  ));

  /* === Hook === */
  if(is_array($extpl1)) foreach ($extpl1 as $pl)
  {
  	include $pl;
  }
  /* ===== */

  $t->parse('MAIN.MKT_ROW_MAIN');
  if($mkt['t_fruser'] == 0) $t->parse('MAIN.MKT_ROW_RIGHT');

  if($mkt['t_code'] == 'market_msg' && $mktdata['type'] == 'instruction' && !$fmsg && $mkt['t_fruser'] == $marketorder['order_userid']) {
    $t->assign(array(
      "MKT_ID" => 0,
      "MKT_TITLE" => "Предоставлены данные",
      "MKT_TEXT" => $queuemsg,
      "MKT_CODE" => 'market_fmsg',
    	"MKT_JSON" => array(
        'type' => $marketorder['order_status'],
        'queue' => $queuecnt
      ),
    ));
    $t->parse('MAIN.MKT_ROW_MAIN');
    $t->parse('MAIN.MKT_ROW_RIGHT');
    $fmsg = true;
  }
/* end foreach */