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

if(cot_plugin_active('marketorders')) {
  require_once cot_incfile('marketorders', 'plug');

  $itemIds = cot_import('itemIds', 'P', 'ARR');

  $orderId = cot_import('orderId', 'P', 'INT');
  if($orderId > 0 && $usr['id'] > 0) {
    $tracks = array();
    foreach($itemIds as $trk) {
      if($trk > 0) $tracks[] = $trk;
    }
    if(count($tracks) > 0) {
       $db->update($db_market_track, array('t_status' => 1), 't_oid='.$orderId.' AND (t_id='.implode(' OR t_id=', $tracks).') AND t_touser='.$usr['id'].' AND t_status=0');
    }
  }

  $dialogId = cot_import('dialogId', 'P', 'INT');
  if($dialogId > 0 && $usr['id'] > 0) {
    $maxtrack = 0;
    $tracks = array();
    foreach($itemIds as $trk) {
      if($trk > 0) $tracks[] = $trk;
      if($trk > 0 && $trk > $maxtrack) $maxtrack = $trk;
    }
    if(count($tracks) > 0) {
      $dialog = $db->query("SELECT * FROM $db_ds_dialog WHERE (toid='".$usr['id']."' OR fromid='".$usr['id']."'".(in_array($usr['maingrp'], $cfg['ds']['supportinfo']['modergroups']) ? ' OR toid=0 OR fromid=0' : '').") LIMIT 1")->fetch();
      if($dialog['id'] > 0) {
        //$db->update($db_ds_msg, array('status' => '0'), 'dialog='.$dialogId.' AND (id='.implode(' OR id=', $tracks).') AND (touser='.$usr['id'].(in_array($usr['maingrp'], $cfg['ds']['supportinfo']['modergroups']) ? ' OR touser=0' : '').') AND status=1');
        $db->update($db_ds_msg, array('status' => '0'), 'dialog='.$dialogId.' AND status=1 AND id<='.$maxtrack.' AND (touser='.$usr['id'].(in_array($usr['maingrp'], $cfg['ds']['supportinfo']['modergroups']) ? ' OR touser=0' : '').') AND status=1');
        if($db->query("SELECT COUNT(*) FROM $db_ds_msg WHERE dialog=".$dialogId." AND (touser=".$usr['id'].(in_array($usr['maingrp'], $cfg['ds']['supportinfo']['modergroups']) ? ' OR touser=0' : '').') AND status=1')->fetchColumn() == 0) {
          $newpm = ($dialog['fromid'] == $usr['id'] || (in_array($usr['maingrp'], $cfg['ds']['supportinfo']['modergroups']) && $dialog['fromid'] == 0)) ? 'fromstatus' : 'tostatus';
          $db->update($db_ds_dialog, array($newpm => 0), "id = ".(int)$chatid."");
        }
      }
    }
  }

  $return['success'] = true;
}

?>