<?php
include "ChatConfig.php";
include "ChatEita.php";
include_once "jdf.php";
///////////////////////////////////
if($_POST['CMD'] == ''){
$sql =
    "INSERT INTO group_" .
    $_POST["GRP"] .
    ' (id, username, message, time)
  VALUES (NULL, "' .
    '0' .
    '", "' .
    $_POST["MSG"] .
    '", "' .
    jdate("H:i:s, j F Y") .
    '")';
$query = $connection->prepare($sql);
$query->execute();
$json = array('status'=>'ok');
print_r( json_encode($json));
}
///////////////////////////////////
if($_POST['CMD'] == 'clear'){
    $sql1 = "TRUNCATE TABLE group_$_POST[GRP]";
    $query1 = $connection->prepare($sql1);
$query1->execute();
$sql =
    "INSERT INTO group_" .
    $_POST["GRP"] .
    ' (id, username, message, time)
  VALUES (NULL, "' .
    '0' .
    '", "' .
    $_POST["MSG"] .
    '", "' .
    jdate("H:i:s, j F Y") .
    '")';
$query = $connection->prepare($sql);
$query->execute();
$json = array('status'=>'ok');
print_r( json_encode($json));
}
///////////////////////////////////
if($_POST['CMD'] == 'sendForAll'){
$query3 = $connection->prepare("SELECT * FROM group_$_POST[GRP] WHERE id=:id");
$query3->bindParam("id", $_POST["REP"], PDO::PARAM_STR);
$query3->execute();
$result_answer = $query3->fetch(PDO::FETCH_ASSOC);
$sqiol = 'SELECT * FROM groups';
  $userjks       = $connection->query($sqiol);
foreach ($userjks as $roojw) {
    if($_POST['GRP'] !== $roojw["number"]){
$sql =
    "INSERT INTO group_" .
    $roojw["number"] .
    ' (id, username, message, path, type, replay, time)
  VALUES (NULL, "' .
    $result_answer["username"] .
    '", "' .
    $result_answer["message"] .
    '", "' .
    $result_answer["path"] .
    '", "' .
    $result_answer["type"] .
    '", "' .
    '' .
    '", "' .
    jdate("H:i:s, j F Y") .
    '")';
$query = $connection->prepare($sql);
$query->execute();
}
}
send($result_answer["message"]);
$json = array('status'=>'ok');
print_r( json_encode($json));
}
///////////////////////////////////
if($_POST['CMD'] == 'updateTick'){
			    switch ($_POST['MDL']) {
  case " ادمین ":
    $medal_what = 1;
    break;
  case " برنامه نویس ":
    $medal_what = 2;
    break;
  case " طلا ":
    $medal_what = 11;
    break;
    case " نقره ":
   $medal_what = 12;
    break;
    case " برنز ":
    $medal_what = 13;
    break;
    case " اخطار ":
    $medal_what = 14;
    break;
    case " محدودیت ":
    $medal_what = 15;
    break;
  default:
    $medal_what = 0;
}
$count=$connection->prepare("update users set tick='$medal_what' where name='$_POST[WHO]'");

$count->execute();
$sql =
    "INSERT INTO group_" .
    $_POST["GRP"] .
    ' (id, username, message, time)
  VALUES (NULL, "' .
    '0' .
    '", "' .
    $_POST["MSG"] .
    '", "' .
    jdate("H:i:s, j F Y") .
    '")';
$query = $connection->prepare($sql);
$query->execute();
$json = array('status'=>'ok');
print_r( json_encode($json));
}
///////////////////////////////////
///////////////////////////////////
if($_POST['CMD'] == 'editProfileGroup'){
$count=$connection->prepare("update groups set profile='$_POST[PRO]' where number='$_POST[GRP]'");

$count->execute();
$sql =
    "INSERT INTO group_" .
    $_POST["GRP"] .
    ' (id, username, message, time)
  VALUES (NULL, "' .
    '0' .
    '", "' .
    $_POST["MSG"] .
    '", "' .
    jdate("H:i:s, j F Y") .
    '")';
$query = $connection->prepare($sql);
$query->execute();
$json = array('status'=>'ok');
print_r( json_encode($json));
}
///////////////////////////////////
if($_POST['CMD'] == 'editNameGroup'){
$count=$connection->prepare("update groups set name='$_POST[NAM]' where number='$_POST[GRP]'");

$count->execute();
$sql =
    "INSERT INTO group_" .
    $_POST["GRP"] .
    ' (id, username, message, time)
  VALUES (NULL, "' .
    '0' .
    '", "' .
    $_POST["MSG"] .
    '", "' .
    jdate("H:i:s, j F Y") .
    '")';
$query = $connection->prepare($sql);
$query->execute();
$json = array('status'=>'ok');
print_r( json_encode($json));
}
///////////////////////////////////
if($_POST['CMD'] == 'lock'){

    $count=$connection->prepare("update groups set status='0' where number='$_POST[GRP]'");

$count->execute();
$sql =
    "INSERT INTO group_" .
    $_POST["GRP"] .
    ' (id, username, message, time)
  VALUES (NULL, "' .
    '0' .
    '", "' .
    $_POST["MSG"] .
    '", "' .
    jdate("H:i:s, j F Y") .
    '")';
$query = $connection->prepare($sql);
$query->execute();
$json = array('status'=>'ok');
print_r( json_encode($json));
}
///////////////////////////////////
if($_POST['CMD'] == 'unlock'){

    $count=$connection->prepare("update groups set status='1' where number='$_POST[GRP]'");

$count->execute();
$sql =
    "INSERT INTO group_" .
    $_POST["GRP"] .
    ' (id, username, message, time)
  VALUES (NULL, "' .
    '0' .
    '", "' .
    $_POST["MSG"] .
    '", "' .
    jdate("H:i:s, j F Y") .
    '")';
$query = $connection->prepare($sql);
$query->execute();
$json = array('status'=>'ok');
print_r( json_encode($json));
}
///////////////////////////////////
?>