<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=ds.ajax.send.presend
 * [END_COT_EXT]
 */

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

if($mtype == 0 && !empty($newpmtext)) {
  $dstate = cot_ds_dstate($chatid);
  if($dstate['lastactiveorder'] > 0) {
    require_once cot_incfile('marketorders', 'plug');

    $mkorder = $db->query("SELECT * FROM $db_market_orders WHERE order_id=".$dstate['lastactiveorder']." AND order_paid>0 AND order_status!='cancel' AND order_status!='done'
        AND (order_userid=".$usr['id']." OR order_seller=".$usr['id'].") LIMIT 1")->fetch();
    if($mkorder['order_id'] > 0) {
      $touid = ($mkorder['order_userid'] == $usr['id'] ? $mkorder['order_seller'] : $mkorder['order_userid']);

      $tid_to = cot_mk_addtrack($mkorder['order_id'], 'market_msg', $usr['id'], $touid, array('msg' => nl2br($newpmtext), 'type' => 'text'));
      $tid = cot_mk_addtrack($mkorder['order_id'], 'market_msg', $usr['id'], $usr['id'], array('msg' => nl2br($newpmtext), 'type' => 'text'), 1, $tid_to);

      echo '<script>location.href="'.cot_url('plug', 'e=marketorders&m=order&id='.$mkorder['order_id']).'";</script>';
      exit;
    }
  }
}