<?php

/**
 * Dialog System
 * @version 2.2
 * @package DS
 * @copyright (c) Alexeev Vlad
 */

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

require_once cot_incfile('forms');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('ds', 'a');
cot_block($usr['auth_read']);

$chatid = cot_import('id','G','INT');
$mid = cot_import('mid','G','INT');

if($chatid > 0 && $usr['id'] > 0) {
  cot_check_xg();

  $dsdialog = $db->query("SELECT * FROM $db_ds_dialog WHERE id = $chatid LIMIT 1")->fetch();
  if ((($dsdialog['fromid'] == $usr['id']) || ($dsdialog['toid'] == $usr['id'])))
  {
    $act_f = ($dsdialog['fromid'] == $usr['id']) ? 'act_fid' : 'act_tid';

    /* === Hook === */
    foreach (cot_getextplugins('ds.delete.first') as $pl)
    {
    	include $pl;
    }
    /* ===== */

    $dialupd = array($act_f => 0, 'status' => 0);

    $dstate_update = array();
    $dstate_needupdate = false;

    $dstate = cot_ds_dstate(0, $dsdialog);
    if(is_array($dstate['waitanswer24hour'])) foreach($dstate['waitanswer24hour'] as $mfastansw) {
      if(!$mfastansw['lowrtg'] && $mfastansw['uid'] == $usr['id']) {
        if($mfastansw['date'] > ($sys['now'] - $cfg['ds']['waitanswer24hour_time']['time'])) {
          $dstate_needupdate = 1;
        } else {
          $dstate_update[] = $mfastansw;
        }
      } else {
        $dstate_update[] = $mfastansw;
      }
    }
    if($dstate_needupdate) {
      $dstate['waitanswer24hour'] = $dstate_update;
      $dialupd['dstate'] = json_encode($dstate);
    }

    $db->update($db_ds_msg, $dialupd, 'dialog='.$dsdialog['id']);

    /* === Hook === */
    foreach (cot_getextplugins('ds.delete.done') as $pl)
    {
    	include $pl;
    }
    /* ===== */
  }
} elseif($mid > 0 && $usr['id'] > 0) {
  cot_sendheaders();
  $status = 'Сообщение не найдено';

	$x = cot_import('x', 'G', 'ALP');
	if ($x != $sys['xk'] && (empty($sys['xk_prev']) || $x != $sys['xk_prev'])) {
    $status = 'Произошла ошибка, перезагрузите страницу.';
	} else {
    $dsmsg = $db->query("SELECT * FROM $db_ds_msg WHERE id=".$mid.' LIMIT 1')->fetch();
    if($dsmsg['id'] > 0) {
      $dsdialog = $db->query("SELECT * FROM $db_ds_dialog WHERE id=".$dsmsg['dialog']." LIMIT 1")->fetch();
      if ((($dsdialog['fromid'] == $usr['id']) || ($dsdialog['toid'] == $usr['id'])))
      {
        if($dsmsg['status'] == 1) {
          $status = 'Сообщение удалено';
          $db->update($db_ds_msg, array('act_fid' => 0, 'act_tid' => 0, 'status' => 0), 'id='.$dsmsg['id']);
        } else {
          $status = 'Сообщение уже прочитанно и удалено только у вас';

          $act_f = ($dsdialog['fromid'] == $usr['id']) ? 'act_fid' : 'act_tid';
          $db->update($db_ds_msg, array($act_f => 0, 'status' => 0), 'id='.$dsmsg['id']);
        }

        $dstate_update = array();
        $dstate_needupdate = false;

        $waitfastanswer = 0;
        $dstate = cot_ds_dstate(0, $dsdialog);
        if(is_array($dstate['waitanswer24hour'])) foreach($dstate['waitanswer24hour'] as $mfastansw) {
          if(!$mfastansw['lowrtg'] && $mfastansw['mid'] == $dsmsg['id']) {
            $dstate_needupdate = 1;
          }
          $dstate_update[] = $mfastansw;
        }
        if($dstate_needupdate) {
          $dstate['waitanswer24hour'] = $dstate_update;
          $db->update($db_ds_dialog, array('dstate' => json_encode($dstate)), "id = ".(int)$dsdialog['id']."");
        }
      }
    }
  }

  echo '<i>'.$status.'</i>';
}

if(!COT_AJAX) cot_redirect(cot_url('ds'));


