<?php
header('Access-Control-Allow-Origin: *');
header("Content-type: application/json; charset=utf-8");

if (isset($_SERVER['HTTP_ORIGIN'])) {
  // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
  // you want to allow, and if so:
  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Credentials: true');
  header('Access-Control-Max-Age: 1000');
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
    // may also be using PUT, PATCH, HEAD etc
    header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
  }

  if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
    header("Access-Control-Allow-Headers: Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization");
  }
  exit(0);
}

include '../../utils/database.php';
include '../../utils/helper.php';

error_reporting(0);

$project = "ai_";

$domain = "https://api.matchadiet.com";

$horizontalComments = array();

$typeFirst = $_POST['type'];
$aidFirst = $_POST['aid'];
$codeFirst = $_POST['code'];
$textFirst = $_POST['textBox'];
if (sizeof($_POST) == 0) {
  $typeFirst = $_GET['type'];
  $aidFirst = $_GET['aid'];
  $codeFirst = $_GET['code'];
  $textFirst = $_GET['textBox'];
}

$customDescription = "";
$customText = "";
$customText2 = "";
$customImageLink = "";
$customTransitionText = "";
$customTitleFade = "";

$chartFirstPointTxt = "";
$chartFirstPointValue = "";

$chartSecondPointTxt = "";
$chartSecondPointValue = "";

$singleChartGoal = "وزن هدف";

$chartStatus = "";

$language = "fa";

$symbol = "";

$fitImportants = array("fastfitg28" . $symbol, "fastfitb28" . $symbol, "fastfitg27" . $symbol, "fastfitb27" . $symbol, "fastfitg13" . $symbol, "fastfitb13" . $symbol, "fastfitg8" . $symbol, "fastfitb8" . $symbol, "fastfitb9" . $symbol, "fastfitg9" . $symbol, "fastfitb10" . $symbol, "fastfitg10" . $symbol, "fastfit1" . $symbol, "fitg9", "fitb9", "gymg9", "gymb9", "incg9", "incb9", "fitg34", "fitb34", "gymg34", "gymb34", "incg34", "incb34", "fitg13", "fitb13", "gymg13", "gymb13", "incg13", "incb13",  "fitg40", "fitg40", "fitb40", "fitg38.4", "fitb38.4", "fit3", "inc3", "gym3", "gym2", "fitb40t", "fitg40t", "fitb42", "fitg42", "fitb38.1", "fitg38.1", "fit.ramadan.b", "fit.ramadan.g", "fitg61", "fitb61", "gym2", "gym2.3.1", "gymg43", "gymb43", "gymg62", "gymb62", "inc40", "incb40", "incg38.4", "incb38.4", "inc3", "incb40t", "incg40t", "incg61", "incb61", "fitg40k", "fitb40k", "fit1", "gym1", "inc1");

$number = $_GET["number"];
$calendar = $_GET["calendar"];
if (isset($_GET["calendar"])) {
  $calendar = convertPersianToEnglish($calendar);
  $yearCalendar = substr($calendar, 0, 4);
  $monthCalendar = substr($calendar, 5, 2);
  $dayCalendar = substr($calendar, 8, 2);
  $calendarTs = shamsiToTs($yearCalendar, $monthCalendar, $dayCalendar);

  if ($calendarTs < (time() - 86400)) {

    $response['success'] = 'false';
    $response['message'] = 'تاریخ انتخاب شده باید مربوط به آینده باشه';
    $json_response = json_encode($response);
    echo $json_response;
    die();
  }
}



$parasiteElements = array();
$parasite = array();
$currentTime = time();

$token = null;
$headers = apache_request_headers();
$language = getLanguageFromHeader($headers);

$preAid = "main";

$allQuestions = 60;
$currentQuestion = 1;

$monthsArrayForChart = array();
$valueArrayForChart = array();

$nextPage = "preCheckout";

$summery = array();


$type = convertPersianToEnglish($typeFirst);

if ($type == "null" or $type == "") {
  $type = "fit";
}


if (($type == "fit") or ($type == "fastfit" . $symbol)) {
  $chartStatus = "down";
} else {
  $chartStatus = "up";
}

if ($type == "fastfit" . $symbol) {
  $allQuestions = 40;
}


//if ($type == "eghdam" or $type == "eghdam_ad" or $type == "desire" or $type == "Preg" or $type == "prevent" or $type == "PageFitness" or $type == "ovary" or $type == "eghdamA"){
//
//}
//
//else{
//
//    header("HTTP/1.0 404 Not Found");
//    die();
//}

$aid = convertPersianToEnglish($aidFirst);
$aidList = $aid;
$aidListString = $aid;

$code = convertPersianToEnglish($codeFirst);
$qid = null;

$persona = getPersonaFromType($type);

$conn = new mysqli($servername, $username, $password, $dbname_process);
mysqli_set_charset($conn, "utf8");
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}


if (isset($headers['Authorization']) and strlen($headers['Authorization']) > 5) {
  $token = $headers['Authorization'];
} else {
  if (isset($_GET['Authorization']) and strlen($_GET['Authorization']) > 5) {
    $token = $_GET['Authorization'];
  } else {
    $token = md5(uniqid(rand(), true)) . md5(uniqid(rand(), true)) . md5(uniqid(rand(), true));
    $redirect = $_GET["redirect"];
    $conn->query("INSERT INTO users(token , persona , redirect , created_at)VALUES('$token' , '$persona' , '$redirect' , '$currentTime')");
  }
}

$resultLink = "https://aipay.matchadiet.com/preCheckout?project=chatfit&token=" . $token . "&type=" . $row['type'] . "&code=" . $code . "&discount=dHJ1ZQ==";


//Weight and Height
if (isset($_GET['height_cm'])) {
  $value = $_GET['height_cm'];
  $value = convertPersianToEnglish($value);
  $unit = "cm";
  $conn->query("UPDATE users SET `height`='$value' , `height_unit`='$unit' WHERE `token`='$token'");
}
if (isset($_GET['height_in']) or isset($_GET['height_ft'])) {
  $value = round(($_GET['height_ft'] * 30.48) + ($_GET['height_in'] * 2.54));
  $value = convertPersianToEnglish($value);
  $unit = "ft";
  $conn->query("UPDATE users SET `height`='$value' , `height_unit`='$unit' WHERE `token`='$token'");
}
if (isset($_GET['weight_current_kg'])) {
  $value = $_GET['weight_current_kg'];
  $value = convertPersianToEnglish($value);
  $unit = "kg";
  $conn->query("UPDATE users SET `weight`='$value' , `weight_unit`='$unit' WHERE `token`='$token'");
}
if (isset($_GET['weight_current_lbs'])) {
  $value = $_GET['weight_current_lbs'];
  $value = convertPersianToEnglish($value);
  $unit = "lbs";
  $conn->query("UPDATE users SET `weight`='$value' , `weight_unit`='$unit' WHERE `token`='$token'");
}
if (isset($_GET['weight_goal_lbs'])) {
  $value = $_GET['weight_goal_lbs'];
  $value = convertPersianToEnglish($value);
  $unit = "lbs";
  $conn->query("UPDATE users SET `weight_goal`='$value' , `weight_unit`='$unit' WHERE `token`='$token'");
}
if (isset($_GET['weight_goal_kg'])) {
  $value = $_GET['weight_goal_kg'];
  $value = convertPersianToEnglish($value);
  $unit = "kg";
  $conn->query("UPDATE users SET `weight_goal`='$value' , `weight_unit`='$unit' WHERE `token`='$token'");
}

if (isset($_GET['phoneNumber'])) {
  $value = $_GET['phoneNumber'];
  $value = convertPersianToEnglish($value);
  $conn->query("UPDATE users SET `phone`='$value' WHERE `token`='$token'");
}

$image = "https://www.mintapp.ir/wp-content/uploads/2020/08/woman-687560_1280.jpg";
$video = "https://file.mintapp.ir/jadde/varzesh.mp4";
$diagramData = array(
  //    array("label"=>"sat" , "open"=>"1201" , "high"=>"1245" , "low"=>"1187" , "close"=>"1235"),
  //    array("label"=>"sun" , "open"=>"1235" , "high"=>"1375" , "low"=>"1135" , "close"=>"1353"),
  //    array("label"=>"mon" , "open"=>"1353" , "high"=>"1381" , "low"=>"1087" , "close"=>"1103"),
  //    array("label"=>"tue" , "open"=>"1103" , "high"=>"1245" , "low"=>"1187" , "close"=>"997"),
  //    array("label"=>"wed" , "open"=>"997" , "high"=>"1045" , "low"=>"956" , "close"=>"993"),
  //    array("label"=>"thu" , "open"=>"993" , "high"=>"1245" , "low"=>"1187" , "close"=>"1135"),
  //    array("label"=>"fri" , "open"=>"1135" , "high"=>"1245" , "low"=>"1087" , "close"=>"1231"),
);
$diagram = array(
  "type" => "line",
  "data" => $diagramData
);


$text = "";
$code = "";

$title = "";

$isResult = "false";

$dialogTitle = "";
$dialogDescription = "";

$questionType = "singleSelect";
$answers = array();

if (isset($_GET["test"]) and $aidListString != "pre0.1") {
  $code = generateCode(16);
  $aid = "pre48.1";
}

if (strlen($codeFirst) > 5) {
  $code = $codeFirst;
}

if (strlen($textFirst) > 5) {
  $text = $textFirst;
}

if ($aid == "fitg12.1.1" or $aid == "fitg12.2.1" or $aid == "fitg12.3.1") {

  $customImageLink = "https://mintdoctor.app/wp-content/uploads/2022/12/1-3.png";
}
if ($aid == "fitb12.1.1" or $aid == "fitb12.2.1" or $aid == "fitb12.3.1") {

  $customImageLink = "https://mintdoctor.app/wp-content/uploads/2022/12/2-3.png";
}

if ($aid == "fastfitg15.1" . $symbol or $aid == "fastfitb15.1" . $symbol or $aid == "fastfitg15.4" . $symbol or $aid == "fastfitb15.4" . $symbol) {
  $conn->query("UPDATE users SET `lifeStyle` = 'q50' WHERE `token`='$token'");
}
if ($aid == "fastfitg15.2" . $symbol or $aid == "fastfitb15.2" . $symbol or $aid == "fastfitg15.3" . $symbol or $aid == "fastfitb15.3" . $symbol) {
  $conn->query("UPDATE users SET `lifeStyle` = 'q51' WHERE `token`='$token'");
}
if ($aid == "fastfitg15.5" . $symbol or $aid == "fastfitb15.5" . $symbol) {
  $conn->query("UPDATE users SET `lifeStyle` = 'q52' WHERE `token`='$token'");
}

if ($aid == "fastfitb8.4" . $symbol or $aid == "fastfitg8.4" . $symbol or $aid == "fastfitb9.4" . $symbol or $aid == "fastfitg9.4" . $symbol or $aid == "fastfitg10.4" . $symbol  or $aid == "fastfitb10.4" . $symbol) {
  $conn->query("UPDATE users SET `mealSkipper` = `mealSkipper`+1 WHERE `token`='$token'");
}

if ($aid == "fastfitg12.2" . $symbol or $aid == "fastfitb12.2" . $symbol) {
  $conn->query("UPDATE users SET `fastingMotivation` = 'q55' WHERE `token`='$token'");
}
if ($aid == "fastfitg12.1" . $symbol or $aid == "fastfitb12.1" . $symbol) {
  $conn->query("UPDATE users SET `fastingMotivation` = 'q54' WHERE `token`='$token'");
}
if ($aid == "fastfitg12.3" . $symbol or $aid == "fastfitb12.3" . $symbol) {
  $conn->query("UPDATE users SET `fastingMotivation` = 'q53' WHERE `token`='$token'");
}


if ($aid == "gymb2.2.1") {

  $customImageLink = "https://blog.matchadiet.com/wp-content/uploads/2023/02/Matcha-Testemonial-B.png";
}

if ($aid == "gymg2.2.1") {

  $customImageLink = "https://blog.matchadiet.com/wp-content/uploads/2023/11/Matcha-TesteG.png";
}

if ($aid == "incg12.1.1" or $aid == "incg12.2.1" or $aid == "incg12.3.1") {

  $customImageLink = "https://blog.matchadiet.com/wp-content/uploads/2023/11/Matcha-Teste.png";
}

if ($aid == "incb12.1.1" or $aid == "incb12.2.1" or $aid == "incb12.3.1") {

  $customImageLink = "https://blog.matchadiet.com/wp-content/uploads/2023/03/Matcha-inc-Testemonial-B.png";
}

if ($aid == "fitb27.1" or $aid == "fitb27.2" or $aid == "incb27.1" or $aid == "incb27.2" or $aid == "gymb18.1" or $aid == "gymb18.2" or $aid == "gymb18.3" or $aid == "gymb18.4") {

  $customImageLink = "https://mintdoctor.app/wp-content/uploads/2022/12/young-coach.png";
}

if ($aid == "fitg27.1" or $aid == "fitg27.2" or $aid == "incg27.1" or $aid == "incg27.2" or $aid == "gymg18.1" or $aid == "gymg18.2" or $aid == "gymg18.3" or $aid == "gymg18.4") {

  $customImageLink = "https://mintdoctor.app/wp-content/uploads/2022/11/45.png";
}



if ($aid == "fitg6.1" or $aid == "fitb6.1") {

  $resultHeight = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resultHeight->num_rows > 0) {
    while ($rowHeight = $resultHeight->fetch_assoc()) {
      $height = (int)$rowHeight["height"];
      $height1 = $height - 95;
      $height2 = $height - 110;

      $customDescription = "بازه وزنی پیشنهادی: " . $height1 . " تا " . $height2;
    }
  }
}

if ($aid == "fastfitg27.2" . $symbol or $aid == "fastfitg27.3" . $symbol or $aid == "fastfitg27.4" . $symbol or $aid == "fastfitg27.5" . $symbol or $aid == "fastfitg27.6" . $symbol or $aid == "fastfitg27.7" . $symbol or $aid == "fastfitb27.2" . $symbol or $aid == "fastfitb27.3" . $symbol or $aid == "fastfitb27.4" . $symbol or $aid == "fastfitb27.5" . $symbol or $aid == "fastfitb27.6" . $symbol or $aid == "fastfitb27.7" . $symbol) {
  $result_celeb = $conn->query("SELECT * FROM " . $project . "answersV2 WHERE `aid`='$aid'");
  if ($result_celeb->num_rows > 0) {
    while ($rowCeleb = $result_celeb->fetch_assoc()) {

      $regimeTypeTxt = $rowCeleb["text"];
      $conn->query("UPDATE users SET `regimeType`='$regimeTypeTxt' WHERE `token`='$token'");
    }
  }
}

if ($aid == "fastfitg38.1" . $symbol or $aid == "fastfitb38.1" . $symbol) {
  $result_horizontal_comment = $conn->query("SELECT * FROM horizontal_comments WHERE MOD(id,2) = 0 AND `type`='text' ORDER BY id DESC LIMIT 10");
  $mainURL = "https://api.matchadiet.com/files/v2/comments/horizontal/main/";
  $profileURL = "https://api.matchadiet.com/files/v2/comments/horizontal/profile/";
  $socialURL = "https://api.matchadiet.com/files/social/";

  if ($result_horizontal_comment->num_rows > 0) {
    while ($row_horizontal_comment = $result_horizontal_comment->fetch_assoc()) {
      $res_horizontal_comment["name"] = $row_horizontal_comment["name"];
      $profileImageInDB = $row_horizontal_comment["profile_image"];
      if (strlen($profileImageInDB) < 6) {
        $res_horizontal_comment["profileImage"] = "https:/api.matchadiet.com/files/v2/profile/ph.png";
      } else {
        $res_horizontal_comment["profileImage"] = $profileURL . $profileImageInDB;
      }
      $socialLogoInDB = $row_horizontal_comment["social_logo"];
      if (strlen($socialLogoInDB) < 4) {
        $res_horizontal_comment["socialLogo"] = "https://api.matchadiet.com/files/social/telegram.svg";
      } else {
        $res_horizontal_comment["socialLogo"] = $socialURL . $row_horizontal_comment["social_logo"] . ".svg";
      }
      $res_horizontal_comment["mainImage"] = $mainURL . $row_horizontal_comment["main_image"];
      $res_horizontal_comment["comment"] = $row_horizontal_comment["comment"];
      $res_horizontal_comment["date"] = $row_horizontal_comment["date"];

      array_push($horizontalComments, $res_horizontal_comment);
    }
  }
}

if ($aid == "fastfitg31.1" . $symbol or $aid == "fastfitb31.1" . $symbol) {

  $resultHeight = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resultHeight->num_rows > 0) {
    while ($rowHeight = $resultHeight->fetch_assoc()) {
      $height = (int)$rowHeight["height"];
      $weightUnit = $rowHeight["weight_unit"];

      $height1 = $height - 95;
      $height2 = $height - 110;
      if ($weightUnit == "lbs") {
        $height1 = convertKgToLbs($height1);
        $height2 = convertKgToLbs($height2);
      }

      $weightUnitTxt = "کیلوگرم";
      if ($weightUnit == "lbs") {
        $weightUnitTxt = "پوند";
      }

      $customDescription = getTextFromId("q2", $language) . $height2 . " " . getTextFromId("q1", $language) . " " . $height1 . " " . $weightUnitTxt;
    }
  }
}

if ($aid == "incg6.1" or $aid == "incb6.1") {

  $resultHeight = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resultHeight->num_rows > 0) {
    while ($rowHeight = $resultHeight->fetch_assoc()) {
      $height = (int)$rowHeight["height"];
      $height1 = $height - 95;
      $height2 = $height - 110;

      $customDescription = "بازه وزنی پیشنهادی: " . $height1 . " تا " . $height2;
    }
  }
}

if ($aid == "gymg6.1" or $aid == "gymb6.1") {

  $resultHeight = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resultHeight->num_rows > 0) {
    while ($rowHeight = $resultHeight->fetch_assoc()) {
      $height = (int)$rowHeight["height"];
      $height1 = $height - 95;
      $height2 = $height - 110;

      $customDescription = "بازه وزنی پیشنهادی: " . $height1 . " تا " . $height2;
    }
  }
}

if ($aid == "prg4.1.1") {

  if ($number < 1 or $number > 40) {

    $response['success'] = 'false';
    $response['message'] = 'هفته بارداری باید بین 1 تا 40 هفته باشد';
    $json_response = json_encode($response);
    echo $json_response;
    die();
  } else {
    $conn->query("UPDATE users SET `number`='$number' WHERE `token`='$token'");
  }
}

if ($aid == "fit3.1" or $aid == "inc3.1") {
  $conn->query("UPDATE users SET `gender`='female' WHERE `token`='$token'");
}
if ($aid == "fit3.2" or $aid == "inc3.2") {
  $conn->query("UPDATE users SET `gender`='male' WHERE `token`='$token'");
}

if ($aid == "gym3.1") {
  $conn->query("UPDATE users SET `gender`='female' WHERE `token`='$token'");
}
if ($aid == "gym3.2") {
  $conn->query("UPDATE users SET `gender`='male' WHERE `token`='$token'");
}

if ($aid == "fastfit1.2" . $symbol) {
  $conn->query("UPDATE users SET `gender`='female' WHERE `token`='$token'");
}
if ($aid == "fastfit1.1" . $symbol) {
  $conn->query("UPDATE users SET `gender`='male' WHERE `token`='$token'");
}

if ($aid == "fitg10.1" or $aid == "fitb10.1" or $aid == "incg10.1" or $aid == "incb10.1" or $aid == "fastfitg34.1" . $symbol or $aid == "fastfitb34.1" . $symbol) {

  $conn->query("UPDATE users SET `calendar`='$calendar' WHERE `token`='$token'");
}


// set meddle age
$midAge = 25;
if ($aid == "fastfitg2.1" . $symbol) {
  $midAge = 24;
}
if ($aid == "fastfitg2.2" . $symbol) {
  $midAge = 35;
}
if ($aid == "fastfitg2.3" . $symbol) {
  $midAge = 45;
}
if ($aid == "fastfitg2.4" . $symbol) {
  $midAge = 55;
}
$conn->query("UPDATE users SET `midAge`='$midAge' WHERE `token`='$token'");
//

if ($aid == "fastfitg34.1" . $symbol or $aid == "fastfitb34.1" . $symbol or $aid == "fastfitg33.8" . $symbol or $aid == "fastfitb33.8" . $symbol) {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightGoalRaw = (int)$rowfitg19["weight_goal"];
      $weightCurrentRaw = (int)$rowfitg19["weight"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $celebInChart = $rowfitg19["celebInChart"];

      $weightUnit = $rowfitg19["weight_unit"];

      if ($weightUnit == "lbs") {
        $weightGoal = convertLbsToKg($weightGoal);
        $weightCurrent = convertLbsToKg($weightCurrent);
      }

      $weightDiff = $weightCurrent - $weightGoal;
      $monthsCount = $weightDiff / 5;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);
      $currentDay = tsToShamsiDay($ts);

      $currentDayRangeStart = $currentDay - 3;
      $currentDayRangeEnd = $currentDay + 3;
      if ($currentDayRangeStart < 1) {
        $currentDayRangeStart = 1;
      }
      if ($currentDayRangeEnd > 30) {
        $currentDayRangeEnd = 30;
      }
      $destinationDay = round(rand($currentDayRangeStart, $currentDayRangeEnd));

      //added in v3
      $chartFirstPointTxt = monthNumberToMonthName($currntMonth);
      $chartFirstPointValue = $weightCurrent;
      //

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 20) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;
      $tsXMonthLaterNumPlusOne = $tsXMonthLaterNum + 1;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }
      if ($tsXMonthLaterNumPlusOne > 12) {
        $tsXMonthLaterNumPlusOne - 12;
      }

      $monthsCountTemp = $monthsCount + 1;
      if ($monthsCountTemp < 1) {
        $monthsCountTemp = 1;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }
      if ($tsXMonthLaterNumPlusOne < 0) {
        $tsXMonthLaterNumPlusOne * -1;
      }

      $conn->query("UPDATE users SET `thirdMonth`='$tsXMonthLaterNum', `programLength`='$monthsCountTemp' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/fit/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

      //added in v3
      $chartSecondPointTxt = $xMonthName;
      $chartSecondPointValue = $weightGoal;
      //

      if ($currentYear != $goalYear) {
        // $xMonthName = $xMonthName . " " . $goalYear;
      }


      if ($weightUnit == "lbs") {
        $chartFirstPointValue = $weightCurrentRaw;
        $chartSecondPointValue = $weightGoalRaw;
        $weightGoal = $weightGoalRaw;
      }
      $customText = "<b><u>" . $weightGoal . " " . $weightUnit . " " . getTextFromId("q4", $language) . " " . $destinationDay . " " . $xMonthName . "</u></b>";
      $customText2 = $celebInChart;

      if (($tsXMonthLaterNum - $currntMonth) == 1) {
        $monthNumber2 = "";
        $monthNumber3 = "";
      } else if (($tsXMonthLaterNum - $currntMonth) == 2) {
        $monthNumber2 = $currntMonth + 1;
        $monthNumber3 = "";
      } else if (($tsXMonthLaterNum - $currntMonth) == 3) {
        $monthNumber2 = $currntMonth + 1;
        $monthNumber3 = $currntMonth + 2;
      } else if (($tsXMonthLaterNum - $currntMonth) == 6) {
        $monthNumber2 = $currntMonth + 2;
        $monthNumber3 = $currntMonth + 4;
      }

      $monthNumber2 = monthNumberToMonthName($monthNumber2, $language, "true");
      $monthNumber3 = monthNumberToMonthName($monthNumber3, $language, "true");
      $monthsArrayForChart = array(monthNumberToMonthName($currntMonth, $language, "true"), $monthNumber2, $monthNumber3, monthNumberToMonthName($tsXMonthLaterNum, $language, "true"), monthNumberToMonthName($tsXMonthLaterNumPlusOne, $language, "true"));
      $valueArrayForChart = array($chartFirstPointValue . " " . $weightUnit, "", "", $chartSecondPointValue . " " . $weightUnit);
    }
  }
}

if ($aid == "fitg18.1" or $aid == "fitb18.1") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightCurrent - $weightGoal;
      $monthsCount = $weightDiff / 3;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `firstMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/fit/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);
      if ($currentYear != $goalYear) {
        // $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 3;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " کم میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }
      $customText = "با توجه به جوابات در " . "</br></br>" . "<b>" . $xMonthName . "</b></br>" . "</br>" . " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if (($aid == "fitg18.1" or $aid == "fitb18.1") and $_GET["version"] == "v3") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightCurrent - $weightGoal;
      $monthsCount = $weightDiff / 3;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);

      //added in v3
      $chartFirstPointTxt = monthNumberToMonthName($currntMonth);
      $chartFirstPointValue = $weightCurrent;
      //

      $currentYear = tsToShamsiYear($ts);

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `firstMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/fit/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

      //added in v3
      $chartSecondPointTxt = $xMonthName;
      $chartSecondPointValue = $weightGoal;
      //

      if ($currentYear != $goalYear) {
        // $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 3;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " کم میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }
      $customText = "با توجه به جوابات در " . "</br></br>" . "<b>" . $xMonthName . "</b></br>" . "</br>" . " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if ($aid == "incg18.1" or $aid == "incb18.1") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightGoal - $weightCurrent;
      if ($weightDiff < 0) {
        $weightDiff = $weightDiff * -1;
      }
      $monthsCount = $weightDiff / 2;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }


      $conn->query("UPDATE users SET `firstMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/inc/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);
      if ($currentYear != $goalYear) {
        // $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 3;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " وزنتو زیاد میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }
      $customText = "با توجه به جوابات در " . "</br></br>" . "<b>" . $xMonthName . "</b></br>" . "</br>" . " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if (($aid == "incg18.1" or $aid == "incb18.1") and $_GET["version"] == "v3") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightGoal - $weightCurrent;
      if ($weightDiff < 0) {
        $weightDiff = $weightDiff * -1;
      }
      $monthsCount = $weightDiff / 2;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      //added in v3
      $chartFirstPointTxt = monthNumberToMonthName($currntMonth);
      $chartFirstPointValue = $weightCurrent;
      //

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }


      $conn->query("UPDATE users SET `firstMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/inc/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

      //added in v3
      $chartSecondPointTxt = $xMonthName;
      $chartSecondPointValue = $weightGoal;
      //

      if ($currentYear != $goalYear) {
        // $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 3;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " وزنتو زیاد میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }
      $customText = "با توجه به جوابات در " . "</br></br>" . "<b>" . $xMonthName . "</b></br>" . "</br>" . " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if ($aid == "fitg35.1" or $aid == "fitb35.1") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightCurrent - $weightGoal;
      $monthsCount = $weightDiff / 4;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];
      $firstMonth = $rowfitg19["firstMonth"];
      $firstMonthName = monthNumberToMonthName($firstMonth);

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `secondMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");

      $customImageLink = "https://api.matchadiet.com/files/v2/charts/fit/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);
      if ($currentYear != $goalYear) {
        //  $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 4;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " کم میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }

      $customTransitionText = $firstMonthName . "," . $xMonthName;
      $customText = "با توجه به جوابات در ";
      $customText2 =  " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if (($aid == "fitg35.1" or $aid == "fitb35.1") and $_GET["version"] == "v3") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightCurrent - $weightGoal;
      $monthsCount = $weightDiff / 4;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];
      $firstMonth = $rowfitg19["firstMonth"];
      $firstMonthName = monthNumberToMonthName($firstMonth);

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      //added in v3
      $chartFirstPointTxt = monthNumberToMonthName($currntMonth);
      $chartFirstPointValue = $weightCurrent;
      //

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `secondMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");

      $customImageLink = "https://api.matchadiet.com/files/v2/charts/fit/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

      //added in v3
      $chartSecondPointTxt = $xMonthName;
      $chartSecondPointValue = $weightGoal;
      //

      if ($currentYear != $goalYear) {
        //  $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 4;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " کم میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }

      $customTransitionText = $firstMonthName . "," . $xMonthName;
      $customText = "با توجه به جوابات در ";
      $customText2 =  " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if ($aid == "incg35.1" or $aid == "incb35.1") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightGoal - $weightCurrent;
      if ($weightDiff < 0) {
        $weightDiff = $weightDiff * -1;
      }
      $monthsCount = $weightDiff / 3;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];
      $firstMonth = $rowfitg19["firstMonth"];
      $firstMonthName = monthNumberToMonthName($firstMonth);

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `secondMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");

      $customImageLink = "https://api.matchadiet.com/files/v2/charts/inc/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);
      if ($currentYear != $goalYear) {
        // $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 4;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " وزنتو زیاد میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }

      $customTransitionText = $firstMonthName . "," . $xMonthName;
      $customText = "با توجه به جوابات در ";
      $customText2 =  " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if (($aid == "incg35.1" or $aid == "incb35.1") and $_GET["version"] == "v3") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightGoal - $weightCurrent;
      if ($weightDiff < 0) {
        $weightDiff = $weightDiff * -1;
      }
      $monthsCount = $weightDiff / 3;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];
      $firstMonth = $rowfitg19["firstMonth"];
      $firstMonthName = monthNumberToMonthName($firstMonth);

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      //added in v3
      $chartFirstPointTxt = monthNumberToMonthName($currntMonth);
      $chartFirstPointValue = $weightCurrent;
      //

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `secondMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");

      $customImageLink = "https://api.matchadiet.com/files/v2/charts/inc/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

      //added in v3
      $chartSecondPointTxt = $xMonthName;
      $chartSecondPointValue = $weightGoal;
      //

      if ($currentYear != $goalYear) {
        // $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 4;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " وزنتو زیاد میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }

      $customTransitionText = $firstMonthName . "," . $xMonthName;
      $customText = "با توجه به جوابات در ";
      $customText2 =  " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if ($aid == "fitg43.1" or $aid == "fitg43.2" or $aid == "fitb43.1" or $aid == "fitb43.2") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightCurrent - $weightGoal;
      $monthsCount = $weightDiff / 5;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];
      $firstMonth = $rowfitg19["secondMonth"];
      $firstMonthName = monthNumberToMonthName($firstMonth);

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      $monthsCountTemp = $monthsCount + 1;
      if ($monthsCountTemp < 1) {
        $monthsCountTemp = 1;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `thirdMonth`='$tsXMonthLaterNum' , `programLength`='$monthsCountTemp' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/fit/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);
      if ($currentYear != $goalYear) {
        //   $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 5;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " کم میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }

      $customTransitionText = $firstMonthName . "," . $xMonthName;
      $customText = "با توجه به جوابات در ";
      $customText2 =  " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if (($aid == "fitg43.1" or $aid == "fitg43.2" or $aid == "fitb43.1" or $aid == "fitb43.2") and $_GET["version"] == "v3") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightCurrent - $weightGoal;
      $monthsCount = $weightDiff / 5;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];
      $firstMonth = $rowfitg19["secondMonth"];
      $firstMonthName = monthNumberToMonthName($firstMonth);

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      //added in v3
      $chartFirstPointTxt = monthNumberToMonthName($currntMonth);
      $chartFirstPointValue = $weightCurrent;
      //

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      $monthsCountTemp = $monthsCount + 1;
      if ($monthsCountTemp < 1) {
        $monthsCountTemp = 1;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `thirdMonth`='$tsXMonthLaterNum' , `programLength`='$monthsCountTemp' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/fit/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

      //added in v3
      $chartSecondPointTxt = $xMonthName;
      $chartSecondPointValue = $weightGoal;
      //

      if ($currentYear != $goalYear) {
        //   $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 5;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " کم میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }

      $customTransitionText = $firstMonthName . "," . $xMonthName;
      $customText = "با توجه به جوابات در ";
      $customText2 =  " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if ($aid == "incg43.1" or $aid == "incg43.2" or $aid == "incb43.1" or $aid == "incb43.2") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightGoal - $weightCurrent;
      if ($weightDiff < 0) {
        $weightDiff = $weightDiff * -1;
      }
      $monthsCount = $weightDiff / 4;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];
      $firstMonth = $rowfitg19["secondMonth"];
      $firstMonthName = monthNumberToMonthName($firstMonth);

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      $monthsCountTemp = $monthsCount + 1;
      if ($monthsCountTemp < 1) {
        $monthsCountTemp = 1;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `thirdMonth`='$tsXMonthLaterNum' , `programLength`='$monthsCountTemp' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/inc/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);
      if ($currentYear != $goalYear) {
        //  $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 5;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " وزنتو زیاد میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }

      $customTransitionText = $firstMonthName . "," . $xMonthName;
      $customText = "با توجه به جوابات در ";
      $customText2 =  " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if (($aid == "incg43.1" or $aid == "incg43.2" or $aid == "incb43.1" or $aid == "incb43.2") and $_GET["version"] == "v3") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $weightGoal = (int)$rowfitg19["weight_goal"];
      $weightCurrent = (int)$rowfitg19["weight"];
      $weightDiff = $weightGoal - $weightCurrent;
      if ($weightDiff < 0) {
        $weightDiff = $weightDiff * -1;
      }
      $monthsCount = $weightDiff / 4;
      $monthsCount = round($monthsCount);
      $celebText = $rowfitg19["celebration"];
      $calendar = $rowfitg19["calendar"];
      $firstMonth = $rowfitg19["secondMonth"];
      $firstMonthName = monthNumberToMonthName($firstMonth);

      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      //added in v3
      $chartFirstPointTxt = monthNumberToMonthName($currntMonth);
      $chartFirstPointValue = $weightCurrent;
      //

      $currentDay = tsToShamsiDay($ts);
      if ($currentDay > 15) {
        $currntMonth = $currntMonth + 1;
        if ($currntMonth > 12) {
          $currntMonth = $currntMonth - 12;
        }
      }
      $monthsCount = $monthsCount - 1;

      $goalTs = time() + ($monthsCount * 30 * 86400);
      $goalYear = tsToShamsiYear($goalTs);

      $tsXMonthLaterNum = $currntMonth + $monthsCount;

      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      $monthsCountTemp = $monthsCount + 1;
      if ($monthsCountTemp < 1) {
        $monthsCountTemp = 1;
      }

      if ($tsXMonthLaterNum < 0) {
        $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
      }

      $conn->query("UPDATE users SET `thirdMonth`='$tsXMonthLaterNum' , `programLength`='$monthsCountTemp' WHERE `token`='$token'");
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/inc/" . $tsXMonthLaterNum . ".png";
      $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

      //added in v3
      $chartSecondPointTxt = $xMonthName;
      $chartSecondPointValue = $weightGoal;
      //

      if ($currentYear != $goalYear) {
        //  $xMonthName = $xMonthName . " " . $goalYear;
      }
      if ($celebText != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 5;
        $celebText = "</br></br>" . $celebWeight . " کیلو تا " . $celebText . " وزنتو زیاد میکنی";
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
      }

      $customTransitionText = $firstMonthName . "," . $xMonthName;
      $customText = "با توجه به جوابات در ";
      $customText2 =  " به وزن " . "<b>" . $weightGoal . " کیلو " . "</b>" . " میرسی" . $celebText;
    }
  }
}

if ($aid == "gymg13.1" or $aid == "gymg13.2" or $aid == "gymg13.3" or $aid == "gymg13.4" or $aid == "gymg13.5" or $aid == "gymb13.1" or $aid == "gymb13.2" or $aid == "gymb13.3" or $aid == "gymb13.4" or $aid == "gymb13.5") {


  $answeredAids = array();
  $gym2Aids = array("gym2.4", "gym2.3.1.1", "gym2.3.1.2");

  $monthsCount = 3;
  $goalTs = time() + ($monthsCount * 30 * 86400);
  $goalYear = tsToShamsiYear($goalTs);
  $tsXMonthLaterNum = $currntMonth + $monthsCount;
  while ($tsXMonthLaterNum > 12) {
    $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
  }
  $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

  $conn->query("UPDATE users SET `firstMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");

  $monthText = $xMonthName;
  $tempText = "";

  $BMI = null;

  $resultAnswered = $conn->query("SELECT * FROM process WHERE `code`='$code'");
  if ($resultAnswered->num_rows > 0) {
    while ($rowAnswered = $resultAnswered->fetch_assoc()) {

      array_push($answeredAids, $rowAnswered["aid"]);
    }
  }

  $findItem = false;

  for ($i = 0; $i < sizeof($gym2Aids); $i++) {
    $gym2Aid = $gym2Aids[$i];
    if (in_array($gym2Aid, $answeredAids)) {

      $findItem = true;

      switch ($gym2Aid) {
        case "gym2.4":
          $tempText = "سلولیتت کاملا برطرف میشه";
          break;
        case "gym2.3.1.1":
          $tempText = "3 سانت سایز باسنت بزرگتر میشه";
          break;
        case "gym2.3.1.2":
          $tempText = "کاهش ۲ سایز دور کمر";
          break;
      }
    }
  }

  if (!$findItem) {
    $resulGym14 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
    if ($resulGym14->num_rows > 0) {
      while ($rowGym = $resulGym14->fetch_assoc()) {
        $weightGoal = (int)$rowGym["weight_goal"];
        $weightCurrent = (int)$rowGym["weight"];
        $height = (int)$rowGym["height"];
        if ($weightCurrent > $weightGoal) {
          $weightDiff = $weightCurrent - $weightGoal;
          $monthsCount = $weightDiff / 4;
        } else {
          $weightDiff = $weightGoal - $weightCurrent;
          $monthsCount = $weightDiff / 3;
        }

        $monthsCount = round($monthsCount);

        $ts = time();
        $currntMonth = tsToShamsiMonth($ts);
        $currentYear = tsToShamsiYear($ts);

        $currentDay = tsToShamsiDay($ts);
        if ($currentDay > 15) {
          $currntMonth = $currntMonth + 1;
          if ($currntMonth > 12) {
            $currntMonth = $currntMonth - 12;
          }
        }
        $monthsCount = $monthsCount - 1;

        $goalTs = time() + ($monthsCount * 30 * 86400);
        $goalYear = tsToShamsiYear($goalTs);

        $tsXMonthLaterNum = $currntMonth + $monthsCount;

        while ($tsXMonthLaterNum > 12) {
          $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
        }

        if ($tsXMonthLaterNum < 0) {
          $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
        }

        $conn->query("UPDATE users SET `firstMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");
        $customImageLink = "https://api.matchadiet.com/files/v2/charts/gym/" . $tsXMonthLaterNum . ".png";
        $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);
        if ($currentYear != $goalYear) {
          //  $xMonthName = $xMonthName . " " . $goalYear;
        }
        $monthText = $xMonthName;
        $BMI = $weightGoal / ($height * $height / 10000);
        $BMI = round($BMI);
        $tempText = " به وزن " . "<b>" . $weightGoal . "</b>" . " کیلو " . " و شاخص توده بدنی " . "<b>" . $BMI . "</b>" . " میرسی";
      }
    }
  }

  $customText = "با توجه به جوابات در " . "</br></br>" . "<b>" . $monthText . "</b></br>" . "</br>" . $tempText;
}

if (($aid == "gymg13.1" or $aid == "gymg13.2" or $aid == "gymg13.3" or $aid == "gymg13.4" or $aid == "gymg13.5" or $aid == "gymb13.1" or $aid == "gymb13.2" or $aid == "gymb13.3" or $aid == "gymb13.4" or $aid == "gymb13.5") and $_GET["version"] == "v3") {


  $answeredAids = array();
  $gym2Aids = array("gym2.4", "gym2.3.1.1", "gym2.3.1.2");

  $monthsCount = 3;
  $goalTs = time() + ($monthsCount * 30 * 86400);
  $goalYear = tsToShamsiYear($goalTs);
  $tsXMonthLaterNum = $currntMonth + $monthsCount;
  while ($tsXMonthLaterNum > 12) {
    $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
  }
  $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

  $conn->query("UPDATE users SET `firstMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");

  $monthText = $xMonthName;
  $tempText = "";

  $BMI = null;

  $resultAnswered = $conn->query("SELECT * FROM process WHERE `code`='$code'");
  if ($resultAnswered->num_rows > 0) {
    while ($rowAnswered = $resultAnswered->fetch_assoc()) {

      array_push($answeredAids, $rowAnswered["aid"]);
    }
  }

  $findItem = false;

  for ($i = 0; $i < sizeof($gym2Aids); $i++) {
    $gym2Aid = $gym2Aids[$i];
    if (in_array($gym2Aid, $answeredAids)) {

      $findItem = true;

      switch ($gym2Aid) {
        case "gym2.4":
          $tempText = "سلولیتت کاملا برطرف میشه";
          break;
        case "gym2.3.1.1":
          $tempText = "3 سانت سایز باسنت بزرگتر میشه";
          break;
        case "gym2.3.1.2":
          $tempText = "کاهش ۲ سایز دور کمر";
          break;
      }
    }
  }

  if (!$findItem) {
    $resulGym14 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
    if ($resulGym14->num_rows > 0) {
      while ($rowGym = $resulGym14->fetch_assoc()) {
        $weightGoal = (int)$rowGym["weight_goal"];
        $weightCurrent = (int)$rowGym["weight"];
        $height = (int)$rowGym["height"];
        if ($weightCurrent > $weightGoal) {
          $weightDiff = $weightCurrent - $weightGoal;
          $monthsCount = $weightDiff / 4;
        } else {
          $weightDiff = $weightGoal - $weightCurrent;
          $monthsCount = $weightDiff / 3;
        }

        $monthsCount = round($monthsCount);

        $ts = time();
        $currntMonth = tsToShamsiMonth($ts);
        $currentYear = tsToShamsiYear($ts);

        $currentDay = tsToShamsiDay($ts);
        if ($currentDay > 15) {
          $currntMonth = $currntMonth + 1;
          if ($currntMonth > 12) {
            $currntMonth = $currntMonth - 12;
          }
        }
        $monthsCount = $monthsCount - 1;

        $goalTs = time() + ($monthsCount * 30 * 86400);
        $goalYear = tsToShamsiYear($goalTs);

        $tsXMonthLaterNum = $currntMonth + $monthsCount;

        while ($tsXMonthLaterNum > 12) {
          $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
        }

        if ($tsXMonthLaterNum < 0) {
          $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
        }

        $conn->query("UPDATE users SET `firstMonth`='$tsXMonthLaterNum' WHERE `token`='$token'");
        $customImageLink = "https://api.matchadiet.com/files/v2/charts/gym/" . $tsXMonthLaterNum . ".png";
        $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

        //  $singleChartGoal = "سطح تناسب اندام شما در " . $xMonthName . " ماه";
        $singleChartGoal = "میزان فیتنس";

        if ($currentYear != $goalYear) {
          //  $xMonthName = $xMonthName . " " . $goalYear;
        }
        $monthText = $xMonthName;
        $BMI = $weightGoal / ($height * $height / 10000);
        $BMI = round($BMI);
        $tempText = " به وزن " . "<b>" . $weightGoal . "</b>" . " کیلو " . " و شاخص توده بدنی " . "<b>" . $BMI . "</b>" . " میرسی";
      }
    }
  }

  $customText = "با توجه به جوابات در " . "</br></br>" . "<b>" . $monthText . "</b></br>" . "</br>" . $tempText;
}

if ($aid == "gymg28.2.1" or $aid == "gymb28.2.1") {


  $answeredAids = array();
  $gym2Aids = array("gym2.4", "gym2.3.1.1", "gym2.3.1.2");

  $monthsCount = 2;
  $goalTs = time() + ($monthsCount * 30 * 86400);
  $goalYear = tsToShamsiYear($goalTs);
  $tsXMonthLaterNum = $currntMonth + $monthsCount;
  while ($tsXMonthLaterNum > 12) {
    $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
  }

  $firstMonthName = monthNumberToMonthName(3);
  $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

  $monthsCountTemp = $monthsCount + 1;
  if ($monthsCountTemp < 1) {
    $monthsCountTemp = 1;
  }

  $conn->query("UPDATE users SET `secondMonth`='$tsXMonthLaterNum' , `programLength`='$monthsCountTemp' WHERE `token`='$token'");

  $monthText = $xMonthName;
  $tempText = "";

  $resultAnswered = $conn->query("SELECT * FROM process WHERE `code`='$code'");
  if ($resultAnswered->num_rows > 0) {
    while ($rowAnswered = $resultAnswered->fetch_assoc()) {

      array_push($answeredAids, $rowAnswered["aid"]);
    }
  }

  $findItem = false;

  for ($i = 0; $i < sizeof($gym2Aids); $i++) {
    $gym2Aid = $gym2Aids[$i];
    if (in_array($gym2Aid, $answeredAids)) {
      $findItem = true;
      switch ($gym2Aid) {
        case "gym2.4":
          $tempText = "سلولیتت کاملا برطرف میشه";
          break;
        case "gym2.3.1.1":
          $tempText = "6 سانت سایز باسنت بزرگتر میشه";
          break;
        case "gym2.3.1.2":
          $tempText = "کاهش ۴ سایز دور کمر";
          break;
      }
    }
  }

  if (!$findItem) {
    $resulGym14 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
    if ($resulGym14->num_rows > 0) {
      while ($rowGym = $resulGym14->fetch_assoc()) {
        $weightGoal = (int)$rowGym["weight_goal"];
        $weightCurrent = (int)$rowGym["weight"];
        $height = (int) $rowGym["height"];
        $firstMonth = $rowGym["firstMonth"];
        $firstMonthName = monthNumberToMonthName($firstMonth);
        if ($weightCurrent > $weightGoal) {
          $weightDiff = $weightCurrent - $weightGoal;
          $monthsCount = $weightDiff / 5;
        } else {
          $weightDiff = $weightGoal - $weightCurrent;
          $monthsCount = $weightDiff / 4;
        }

        $monthsCount = round($monthsCount);

        $ts = time();
        $currntMonth = tsToShamsiMonth($ts);
        $currentYear = tsToShamsiYear($ts);

        $currentDay = tsToShamsiDay($ts);
        if ($currentDay > 15) {
          $currntMonth = $currntMonth + 1;
          if ($currntMonth > 12) {
            $currntMonth = $currntMonth - 12;
          }
        }
        $monthsCount = $monthsCount - 1;

        $goalTs = time() + ($monthsCount * 30 * 86400);
        $goalYear = tsToShamsiYear($goalTs);

        $tsXMonthLaterNum = $currntMonth + $monthsCount;

        while ($tsXMonthLaterNum > 12) {
          $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
        }

        $monthsCountTemp = $monthsCount + 1;
        if ($monthsCountTemp < 1) {
          $monthsCountTemp = 1;
        }

        if ($tsXMonthLaterNum < 0) {
          $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
        }

        $conn->query("UPDATE users SET `secondMonth`='$tsXMonthLaterNum' , `programLength`='$monthsCountTemp' WHERE `token`='$token'");
        $customImageLink = "https://api.matchadiet.com/files/v2/charts/gym/" . $tsXMonthLaterNum . ".png";
        $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);
        if ($currentYear != $goalYear) {
          //  $xMonthName = $xMonthName . " " . $goalYear;
        }
        $monthText = $xMonthName;
        $BMI = $weightGoal / ($height * $height / 10000);
        $BMI = round($BMI);
        $tempText = " به وزن " . "<b>" . $weightGoal . "</b>" . " کیلو " . " و شاخص توده بدنی " . "<b>" . $BMI . "</b>" . " میرسی";
      }
    }
  }
  $customTransitionText = $firstMonthName . "," . $xMonthName;
  $customText = "با توجه به جوابات در ";
  $customText2 =  "</br>" . "</br>" . $tempText;
}

if (($aid == "gymg28.2.1" or $aid == "gymb28.2.1") and $_GET["version"] == "v3") {


  $answeredAids = array();
  $gym2Aids = array("gym2.4", "gym2.3.1.1", "gym2.3.1.2");

  $monthsCount = 2;
  $goalTs = time() + ($monthsCount * 30 * 86400);
  $goalYear = tsToShamsiYear($goalTs);
  $tsXMonthLaterNum = $currntMonth + $monthsCount;
  while ($tsXMonthLaterNum > 12) {
    $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
  }

  $firstMonthName = monthNumberToMonthName(3);
  $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

  $monthsCountTemp = $monthsCount + 1;
  if ($monthsCountTemp < 1) {
    $monthsCountTemp = 1;
  }

  $conn->query("UPDATE users SET `secondMonth`='$tsXMonthLaterNum' , `programLength`='$monthsCountTemp' WHERE `token`='$token'");

  $monthText = $xMonthName;
  $tempText = "";

  $resultAnswered = $conn->query("SELECT * FROM process WHERE `code`='$code'");
  if ($resultAnswered->num_rows > 0) {
    while ($rowAnswered = $resultAnswered->fetch_assoc()) {

      array_push($answeredAids, $rowAnswered["aid"]);
    }
  }

  $findItem = false;

  for ($i = 0; $i < sizeof($gym2Aids); $i++) {
    $gym2Aid = $gym2Aids[$i];
    if (in_array($gym2Aid, $answeredAids)) {
      $findItem = true;
      switch ($gym2Aid) {
        case "gym2.4":
          $tempText = "سلولیتت کاملا برطرف میشه";
          break;
        case "gym2.3.1.1":
          $tempText = "6 سانت سایز باسنت بزرگتر میشه";
          break;
        case "gym2.3.1.2":
          $tempText = "کاهش ۴ سایز دور کمر";
          break;
      }
    }
  }

  if (!$findItem) {
    $resulGym14 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
    if ($resulGym14->num_rows > 0) {
      while ($rowGym = $resulGym14->fetch_assoc()) {
        $weightGoal = (int)$rowGym["weight_goal"];
        $weightCurrent = (int)$rowGym["weight"];
        $height = (int) $rowGym["height"];
        $firstMonth = $rowGym["firstMonth"];
        $firstMonthName = monthNumberToMonthName($firstMonth);
        if ($weightCurrent > $weightGoal) {
          $weightDiff = $weightCurrent - $weightGoal;
          $monthsCount = $weightDiff / 5;
        } else {
          $weightDiff = $weightGoal - $weightCurrent;
          $monthsCount = $weightDiff / 4;
        }

        $monthsCount = round($monthsCount);

        $ts = time();
        $currntMonth = tsToShamsiMonth($ts);
        $currentYear = tsToShamsiYear($ts);

        $currentDay = tsToShamsiDay($ts);
        if ($currentDay > 15) {
          $currntMonth = $currntMonth + 1;
          if ($currntMonth > 12) {
            $currntMonth = $currntMonth - 12;
          }
        }
        $monthsCount = $monthsCount - 1;

        $goalTs = time() + ($monthsCount * 30 * 86400);
        $goalYear = tsToShamsiYear($goalTs);

        $tsXMonthLaterNum = $currntMonth + $monthsCount;

        while ($tsXMonthLaterNum > 12) {
          $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
        }

        $monthsCountTemp = $monthsCount + 1;
        if ($monthsCountTemp < 1) {
          $monthsCountTemp = 1;
        }

        if ($tsXMonthLaterNum < 0) {
          $tsXMonthLaterNum = $tsXMonthLaterNum * -1;
        }

        $conn->query("UPDATE users SET `secondMonth`='$tsXMonthLaterNum' , `programLength`='$monthsCountTemp' WHERE `token`='$token'");
        $customImageLink = "https://api.matchadiet.com/files/v2/charts/gym/" . $tsXMonthLaterNum . ".png";
        $xMonthName = monthNumberToMonthName($tsXMonthLaterNum);

        // $singleChartGoal = "سطح تناسب اندام شما در " . $xMonthName . " ماه";

        $singleChartGoal = "میزان فیتنس";

        if ($currentYear != $goalYear) {
          //  $xMonthName = $xMonthName . " " . $goalYear;
        }
        $monthText = $xMonthName;
        $BMI = $weightGoal / ($height * $height / 10000);
        $BMI = round($BMI);
        $tempText = " به وزن " . "<b>" . $weightGoal . "</b>" . " کیلو " . " و شاخص توده بدنی " . "<b>" . $BMI . "</b>" . " میرسی";
      }
    }
  }
  $customTransitionText = $firstMonthName . "," . $xMonthName;
  $customText = "با توجه به جوابات در ";
  $customText2 =  "</br>" . "</br>" . $tempText;
}

if ($aid == "fitg59.1" or $aid == "fitb59.1") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $thirdMonth =  $rowfitg19["thirdMonth"];
      $thirdMonth = monthNumberToMonthName($thirdMonth);
      $weightGoal = $rowfitg19["weight_goal"];
      $customTitleFade = "به ماچا خوش اومدی...
            ,
            بر اساس پاسخ هایی که دادی...
            ,
            شرایط رو کاملا تحلیل کردیم و یه برنامه مخصوص برات طراحی کردیم...
            ,
            حداکثر تا " . "آخر ماه " . $thirdMonth . " به  " . $weightGoal . " کیلوگرم میرسی";
    }
  }
}

if ($aid == "fastfitg35.1" . $symbol or $aid == "fastfitb35.1" . $symbol) {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $celebInFade = $rowfitg19["celebInFade"];
      $thirdMonth =  $rowfitg19["thirdMonth"];
      $thirdMonth = monthNumberToMonthName($thirdMonth, $language);
      $weightGoal = $rowfitg19["weight_goal"];
      $weightCurrent = $rowfitg19["weight"];
      $weightUnit = $rowfitg19["weight_unit"];
      $calendar = $rowfitg19["calendar"];
      $weightDiff = $weightCurrent - $weightGoal;
      $ts = time();
      $currntMonth = tsToShamsiMonth($ts);
      $currentYear = tsToShamsiYear($ts);

      $monthsCount = 3;
      $tsXMonthLaterNum = $currntMonth + $monthsCount;
      while ($tsXMonthLaterNum > 12) {
        $tsXMonthLaterNum = $tsXMonthLaterNum - 12;
      }

      if ($celebInFade != "") {
        $yearCeleb = substr($calendar, 0, 4);
        $monthCeleb = substr($calendar, 5, 2);
        $dayCeleb = substr($calendar, 8, 2);
        $celebTs = shamsiToTs($yearCeleb, $monthCeleb, $dayCeleb);
        $tsCelebMonthNum = tsToShamsiMonth($celebTs);
        $celebDiffMonth = ($celebTs - $ts) / (86400) / 30;
        $celebDiffMonth = round($celebDiffMonth * 2) / 2;
        $celebWeight = $celebDiffMonth * 5;
        if ($weightUnit == "lbs") {
          $celebWeight = convertKgToLbs($celebWeight);
        }
        $celebText = "</br>" . getTextFromId("q7", $language) . $celebWeight . " " . $weightUnit  . " " . getTextFromId("q19", $language) . " " . $celebText;
        if ($celebTs > $goalTs) {
          $celebText = "";
        }
        if ($celebWeight == 0) {
          $celebText = "";
        }
        $customTitleFade = getTextFromId("q26", $language) . "," . getTextFromId("q20", $language) . "," . getTextFromId("q21", $language) . "," . getTextFromId("q22", $language) . " " . $weightGoal . " " . $weightUnit . "," . getTextFromId("q59", $language) . $celebWeight . " " . $weightUnit . " " . $celebInFade . "," . getTextFromId("q25", $language);
      } else {
        $customTitleFade = getTextFromId("q26", $language) . "," . getTextFromId("q20", $language) . "," . getTextFromId("q21", $language) . "," . getTextFromId("q22", $language) . " " . $weightGoal . " " . $weightUnit . "," . getTextFromId("q25", $language);
      }

      if ($language == "fa") {

        $weightUnitTxt = "کیلوگرم";
        if ($weightUnit == "lbs") {
          $weightUnitTxt = "پوند";
        }

        $customTitleFade =
          "به ماچا خوش اومدی...
            ,
            بر اساس پاسخ هایی که دادی...
            ,
            شرایط رو کاملا تحلیل کردیم و یه برنامه مخصوص برات طراحی کردیم...
            ,
            حداکثر تا " . "آخر ماه " . $thirdMonth . " به  " . $weightGoal . " " . $weightUnitTxt . " میرسی";
      }
    }
  }
}

if ($aid == "incg59.1" or $aid == "incb59.1") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $thirdMonth =  $rowfitg19["thirdMonth"];
      $thirdMonth = monthNumberToMonthName($thirdMonth);
      $weightGoal = $rowfitg19["weight_goal"];
      $customTitleFade = "به ماچا خوش اومدی...
            ,
            بر اساس پاسخ هایی که دادی...
            ,
            شرایط رو کاملا تحلیل کردیم و یه برنامه مخصوص برات طراحی کردیم...
            ,
            حداکثر تا " . "آخر ماه " . $thirdMonth . " به  " . $weightGoal . " کیلوگرم میرسی";
    }
  }
}

if ($aid == "gymg40.1" or $aid == "gymb40.1") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $thirdMonth =  $rowfitg19["secondMonth"];
      $thirdMonth = monthNumberToMonthName($thirdMonth);
      $weightGoal = $rowfitg19["weight_goal"];
      $customTitleFade = "به ماچا خوش اومدی...
            ,
            بر اساس پاسخ هایی که دادی...
            ,
            شرایط رو کاملا تحلیل کردیم و یه برنامه مخصوص برات طراحی کردیم...
            ,
            حداکثر تا " . "آخر ماه " . $thirdMonth . " به  " . "هدفت" . " میرسی";
    }
  }
}


if ($aid == "fitg34.1.1" or $aid == "fitg34.1.2" or $aid == "fitb34.1.1" or $aid == "fitb34.1.2") {

  $resulFitg19 = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resulFitg19->num_rows > 0) {
    while ($rowfitg19 = $resulFitg19->fetch_assoc()) {
      $firstMonth =  $rowfitg19["firstMonth"];
      $customImageLink = "https://api.matchadiet.com/files/v2/charts/fitYoYo/" . $firstMonth . ".png";
    }
  }
}

if ($aid == "incg34.1.1" or $aid == "incg34.1.2" or $aid == "incb34.1.1" or $aid == "incb34.1.2") {

  $customImageLink = "https://blog.matchadiet.com/wp-content/uploads/2023/03/YoYo-Matcha-Chart-New.png";
}

if ($aid == "fastfitb32.1" . $symbol or $aid == "fastfitg32.1" . $symbol) {

  $resultHeight = $conn->query("SELECT * FROM users WHERE `token`='$token'");
  if ($resultHeight->num_rows > 0) {
    while ($rowUser = $resultHeight->fetch_assoc()) {
      $summeryImage = "";
      $gender = $rowUser["gender"];
      $weightCurrent = $rowUser["weight"];
      $weightGoal = $rowUser["weight_goal"];
      $height = $rowUser["height"];
      $weightUnit = $rowUser["weight_unit"];
      $heightUnit = $rowUser["height_unit"];
      $lifeStyle = $rowUser["lifeStyle"];
      $mealSkipper = $rowUser["mealSkipper"];
      $fastingMotivation = $rowUser["fastingMotivation"];
      if ($weightUnit == "lbs") {
        $weightCurrent = convertLbsToKg($weightCurrent);
      }
      //if ($heightUnit == "ft") {
      //    $height = convertFootToCm($height);
      //}
      $beforeBMI = $weightCurrent / ($height * $height / 10000);
      $beforeBMI = round($beforeBMI, 2);
      if ($gender == "male") {
        if ($beforeBMI >= 30) {
          $summeryImage = $domain . "/files/v2/precheckout_ba/fit-man-upper30-before.png";
        } else {
          $summeryImage = $domain . "/files/v2/precheckout_ba/fit-man-under30-before.png";
        }
      } else {
        if ($beforeBMI >= 30) {
          $summeryImage = $domain . "/files/v2/precheckout_ba/fit-woman-upper30-before.png";
        } else {
          $summeryImage = $domain . "/files/v2/precheckout_ba/fit-woman-under30-before.png";
        }
      }

      $section3Emoji = "";
      $section3Title = "";
      $section3Description = "";
      if ($beforeBMI >= 25) {
        $section3Emoji = "🤔";
        $section3Title = getTextFromId("qV2_BMI_over_title", $language);
        $section3Description = getTextFromId("qV2_BMI_over_des", $language);
      } else {
        $section3Emoji = "👌";
        $section3Title = getTextFromId("qV2_BMI_ok_title", $language);
        $section3Description = getTextFromId("qV2_BMI_ok_des", $language);
      }

      if ($beforeBMI >= 35) {
        $beforeBMI = 35;
      }
      if ($beforeBMI <= 20) {
        $beforeBMI = 20;
      }

      $typeOfEater = getTextFromId("q36", $language);
      if ($mealSkipper >= 2) {
        $typeOfEater = getTextFromId("q49", $language);
      }

      $summery = array(
        "title" => getTextFromId("q27", $language),
        "section1" => array(
          "title" => getTextFromId("q28", $language),
          "hint" => getTextFromId("q29", $language),
          "labels" => array(getTextFromId("q30", $language), getTextFromId("q31", $language), getTextFromId("q32", $language),),
          "minRange" => 20,
          "maxRange" => 35,
          "statusText" => "شما " . $beforeBMI,
          "statusValue" => $beforeBMI,
        ),
        "section2" => array(
          "image" => $summeryImage,
          "items" => array(
            array(
              "icon" => $domain . "/files/v2/lifestyle_" . $lifeStyle . ".svg",
              "label" => getTextFromId("q33", $language),
              "value" => getTextFromId($rowUser["lifeStyle"], $language),
            ),
            array(
              "icon" => $domain . "/files/v2/eat.svg",
              "label" => getTextFromId("q35", $language),
              "value" => $typeOfEater,
            ),
            array(
              "icon" => $domain . "/files/v2/motivation.svg",
              "label" => getTextFromId("q37", $language),
              "value" => getTextFromId($rowUser["fastingMotivation"], $language),
            ),
          ),
        ),
        "section3" => array(
          "emoji" => $section3Emoji,
          "title" => $section3Title,
          "description" => $section3Description,
        ),
        "button" => array(
          "text" => getTextFromId("q41", $language),
          "textColor" => "",
          "bgColor" => "",
        ),

      );
    }
  }
}

$query = null;
if ($aid == "start") {

  $code = generateCode(16);

  $query = "SELECT * FROM " . $project . "questionsV2 WHERE `type`='$type' AND `questionNumber`='1' LIMIT 1";
  $result = $conn->query($query);
  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      $qid = $row["qid"];
    }
  }
} else {

  // $answeredQids = array();
  // $resultPreAid = $conn->query("SELECT * FROM process WHERE `code`='$code'");
  // if ($resultPreAid->num_rows > 0) {
  //     while($rowPreAid = $resultPreAid->fetch_assoc()) {
  //         $preAid = $rowPreAid["aid"];
  //     }
  // }

  if (strpos($aid, ',')) {
    $aidList = explode(",", $aid);
    $aid = $aidList[0];
  }
  //before test: $query_a = "SELECT * FROM answersV2 WHERE `aid`='$aid' AND `type`='$type'";
  $query_a = "SELECT * FROM " . $project . "answersV2 WHERE `aid`='$aid'";
  $resultAnswer = $conn->query($query_a);
  if ($resultAnswer->num_rows > 0) {
    while ($rowAnswer = $resultAnswer->fetch_assoc()) {

      $qid_next = $rowAnswer["next_question"];
      $preAid = $rowAnswer["prev_question"];
      $qid = $rowAnswer["qid"];

      if ($aid == "chatsup12.1" . $symbol) {

        if (checkTransactionStatus($_GET['phoneNumber'], "matcha") == "vip") {
          $qid_next = "chatsup14";
        } else if (checkTransactionStatus($_GET['phoneNumber'], "matcha") == "404") {
          $qid_next = "chatsup2";
        } else {
          $qid_next = "chatsup20";
        }
      }

      //before test: $query = "SELECT * FROM questionsV2 WHERE `type`='$type' AND `qid`='$qid_next'";
      $query = "SELECT * FROM " . $project . "questionsV2 WHERE `qid`='$qid_next'";
    }
  }
}
if ($type == "inc") {
  $nextPage = "checkout";
}
if ($type == "fastfit") {
  $nextPage = "/preCheckout/?discount=dHJ1ZQ==";
}

// $conn->query("DELETE FROM process WHERE `qid`='$qid' AND `code`='$code' AND `token`='$token'");

if (in_array($qid, $fitImportants)) {
  $conn->query("INSERT INTO process(`token` , `code` , `type` , `qid` , `aid` , `text_box` , `created_at`) VALUES ('$token' , '$code' , '$type' , '$qid' , '$aidListString' , '$text' , '$currentTime')");
}
$res = null;


$result = $conn->query($query);
if ($result->num_rows > 0) {
  while ($row = $result->fetch_assoc()) {
    $questionType = $row["questionType"];
    $currentQuestion = $row["questionNumber"];
    if ($questionType == "parasite") {
      $pid = $row["pid"];

      // start:  for develop
      if (($row["qid"] == "fitg18" or $row["qid"] == "fitb18") and $_GET['version'] == "v3") {
        $pid = "fitg18_v3";
      }
      if (($row["qid"] == "incg18" or $row["qid"] == "incb18") and $_GET['version'] == "v3") {
        $pid = "incg18_v3";
      }
      if (($row["qid"] == "fitg44" or $row["qid"] == "fitb44") and $_GET['version'] == "v3") {
        $pid = "fitg44_v3";
      }
      if (($row["qid"] == "incg44" or $row["qid"] == "incb44") and $_GET['version'] == "v3") {
        $pid = "incg44_v3";
      }
      if (($row["qid"] == "incg59" or $row["qid"] == "incb59") and $_GET['version'] == "v3") {
        $pid = "incg59_v3";
      }
      if (($row["qid"] == "gymg40" or $row["qid"] == "gymb40") and $_GET['version'] == "v3") {
        $pid = "gymg40_v3";
      }
      if (($row["qid"] == "fitg19" or $row["qid"] == "fitb19") and $_GET['version'] == "v3") {
        $pid = "fitg19_v3";
      }
      if (($row["qid"] == "incg19" or $row["qid"] == "incb19") and $_GET['version'] == "v3") {
        $pid = "incg19_v3";
      }
      if (($row["qid"] == "fitg36" or $row["qid"] == "fitb36") and $_GET['version'] == "v3") {
        $pid = "fitg36_v3";
      }
      if (($row["qid"] == "incg36" or $row["qid"] == "incb36") and $_GET['version'] == "v3") {
        $pid = "incg36_v3";
      }
      if (($row["qid"] == "gymg29" or $row["qid"] == "gymb29") and $_GET['version'] == "v3") {
        $pid = "gymg29_v3";
      }
      if (($row["qid"] == "gymg30" or $row["qid"] == "gymb30") and $_GET['version'] == "v3") {
        $pid = "gymg30_v3";
        $singleChartGoal = "میزان فیتنس";
      }
      if (($row["qid"] == "gymg14" or $row["qid"] == "gymb14") and $_GET['version'] == "v3") {
        $pid = "gymg14_v3";
      }

      if (($row["qid"] == "fitg59" or $row["qid"] == "fitb59") and $_GET['version'] == "v3") {
        $pid = "fitg59_v3";
      }
      if (($row["qid"] == "incg59" or $row["qid"] == "incb59") and $_GET['version'] == "v3") {
        $pid = "incg59_v3";
      }
      if (($row["qid"] == "gymg40" or $row["qid"] == "gymb40") and $_GET['version'] == "v3") {
        $pid = "gymg40_v3";
      }

      // end: for develop

      $res_parasiteOut["id"] = $row["pid"];
      $res_parasiteOut["aid"] = $row["parasiteAid"];
      $res_parasiteOut["bgColor"] = $row["bgColor"];
      $res_parasiteOut["textColor"] = $row["textColor"];

      $result_parasite = $conn->query("SELECT * FROM " . $project . "parasite WHERE `pid`='$pid' ORDER BY priority");
      if ($result_parasite->num_rows > 0) {
        while ($row_parasite = $result_parasite->fetch_assoc()) {

          $hasCode = $row_parasite["hasCode"];
          $hasCode2 = $row_parasite["hasCode2"];

          $res_parasite = null;
          $res_parasite["id"] = $row_parasite["pType"];
          if ($row_parasite["pType"] == "title") {
            $res_parasite["inputs"] = array(
              "text" => $row_parasite["titleText"],
              "textAlign" => $row_parasite["textAlign"],
              "textSize" => $row_parasite["textSize"],
            );
          }

          if ($row_parasite["pType"] == "summery") {
            $res_parasite["inputs"] = array(
              "items" => $summery,
            );
          }

          if ($row_parasite["pType"] == "comments") {
            $res_parasite["inputs"] = array(
              "sectionTitle" => "",
              "items" => $horizontalComments
            );
          }

          if ($row_parasite["pType"] == "description") {
            if ($hasCode == "true") {
              $res_parasite["inputs"] = array(
                "text" => $customText,
                "textAlign" => $row_parasite["textAlign"],
                "textSize" => $row_parasite["textSize"],
              );
            } else if ($hasCode2) {
              $res_parasite["inputs"] = array(
                "text" => $customText2,
                "textAlign" => $row_parasite["textAlign"],
                "textSize" => $row_parasite["textSize"],
              );
            } else {
              $res_parasite["inputs"] = array(
                "text" => $row_parasite["descriptionText"],
                "textAlign" => $row_parasite["textAlign"],
                "textSize" => $row_parasite["textSize"],
              );
            }
          }
          if ($row_parasite["pType"] == "titleFade") {
            if ($hasCode == "true") {
              $temp = explode(",", $customTitleFade);
              $res_parasite["inputs"] = array(
                "texts" => $temp
              );
            } else {
              $temp = explode(",", $row_parasite["titleFadeTexts"]);
              $res_parasite["inputs"] = array(
                "texts" => $temp
              );
            }
          }
          if ($row_parasite["pType"] == "transitionText") {
            if ($hasCode == "true") {
              $temp = explode(",", $customTransitionText);
              $res_parasite["inputs"] = array(
                "texts" => $temp
              );
            } else {
              $temp = explode(",", $row_parasite["transitionTexts"]);
              $res_parasite["inputs"] = array(
                "texts" => $temp
              );
            }
          }
          if ($row_parasite["pType"] == "image") {
            if ($hasCode == "true") {
              $res_parasite["inputs"] = array(
                "link" => $customImageLink,
                "textAlign" => "right"
              );
            } else {
              $res_parasite["inputs"] = array(
                "link" => $row_parasite["imageLink"],
                "textAlign" => "right"
              );
            }
          }
          if ($row_parasite["pType"] == "icon") {
            $res_parasite["inputs"] = array(
              "link" => $row_parasite["imageLink"],
              "textAlign" => "right"
            );
          }
          if ($row_parasite["pType"] == "video") {
            $res_parasite["inputs"] = array(
              "link" => $row_parasite["videoLink"],
            );
          }
          if ($row_parasite["pType"] == "loading") {
            $temp = "";
            if (strlen($row_parasite["loadingTexts"]) > 5) {
              $temp = explode(",", $row_parasite["loadingTexts"]);
            }
            $res_parasite["inputs"] = array(
              "type" => $row_parasite["loadingType"],
              "color" => $row_parasite["loadingColor"],
              "time" => $row_parasite["loadingTime"],
              "maxValue" => $row_parasite["loadingMaxValue"],
              "text" => $row_parasite["loadingText"],
              "texts" => $temp,
              "hasButton" => $row_parasite["loadingHasButton"],
              "delay" => $row_parasite["loadingDelay"],
            );
          }
          if ($row_parasite["pType"] == "multiLoading") {
            $temp = "";
            $tempInput = array();
            $loadingParts = $row_parasite["loadingParts"];
            for ($z = 0; $z < $loadingParts; $z++) {
              $res_input["type"] = "circle";
              $res_input["color"] = explode(",", $row_parasite["loadingColor"])[$z];
              $res_input["time"] = explode(",", $row_parasite["loadingTime"])[$z];
              $res_input["maxValue"] = explode(",", $row_parasite["loadingMaxValue"])[$z];
              $res_input["text"] = explode(",", $row_parasite["loadingText"])[$z];
              $res_input["texts"] = explode(",", $row_parasite["loadingTexts"])[$z];
              $res_input["hasButton"] = "false";
              $res_input["delay"] = explode(",", $row_parasite["loadingDelay"])[$z];

              array_push($tempInput, $res_input);
            }
            $res_parasite["inputs"] = $tempInput;
          }
          if ($row_parasite["pType"] == "diagram") {

            $res_parasite["type"] = $row_parasite["diagramType"];
            $diagramId = $row_parasite["diagramId"];
            $labels = array();
            $datasets = array();

            $result_diagram = $conn->query("SELECT * FROM diagram WHERE `did`='$diagramId' ORDER BY id");
            if ($result_diagram->num_rows > 0) {
              while ($row_diagram = $result_diagram->fetch_assoc()) {
                $label = $row_diagram["label"];
                $dataset = $row_diagram["dataset"];
                array_push($labels, $label);
                array_push($datasets, $dataset);
              }
            }
            $res_parasite["inputs"] = array(
              "color" => $row_parasite["diagramColor"],
              "label" => $row_parasite["diagramLabel"],
              "labels" => $labels,
              "datasets" => $datasets
            );
          }
          if ($row_parasite["pType"] == "button") {
            $res_parasite["inputs"] = array(
              "text" => $row_parasite["buttonText"],
              "bgColor" => $row_parasite["buttonBgColor"],
              "textColor" => $row_parasite["buttonTextColor"]
            );
          }
          //start: for v3
          if ($row_parasite["pType"] == "singleChart") {
            $res_parasite["inputs"] = array(
              "status" => $chartStatus,
              "goal" => $singleChartGoal,
              "unit" => $weightUnit,
              "inputs" => array(
                "status" => $chartStatus,
                "weekNames" => array_reverse(array("هفته ۱", "هفته ۲", "هفته ۳", "هفته ۴")),
                "tooltipCurrent" => getTextFromId("q57", $language),
                "tooltipGoal" => getTextFromId("q58", $language)
              ),
            );
          }
          if ($row_parasite["pType"] == "chart") {
            $res_parasite["inputs"] = array(
              "status" => $chartStatus,
              "current" => $chartFirstPointTxt . " : " . $chartFirstPointValue,
              "goal" => $chartSecondPointTxt . " : " . $chartSecondPointValue,
              "unit" => $weightUnit,
              "inputs" => array(
                "data" => $valueArrayForChart,
                "months" => array_reverse($monthsArrayForChart),
                "lastTooltipText" => getTextFromId("q60", $language),
              ),
            );
          }
          //end: for v3
          array_push($parasiteElements, $res_parasite);
        }
        $res_parasiteOut["elements"] = $parasiteElements;
        $parasite = $res_parasiteOut;
      }
    }
    $qid = $row["qid"];
    //before test: $query_a = "SELECT * FROM answersV2 WHERE `qid`='$qid' AND `type`='$type' ORDER BY priority";
    $query_a = "SELECT * FROM " . $project . "answersV2 WHERE `qid`='$qid' ORDER BY priority";
    $result_a = $conn->query($query_a);
    if ($result_a->num_rows > 0) {
      while ($row_a = $result_a->fetch_assoc()) {
        $res_in["aid"] = $row_a["aid"];
        $res_in["text"] = $row_a['text'];
        $res_in["image"] = $row_a['image'];
        $res_in["description"] = $row_a['description'];
        $res_in["externalLink"] = $row_a['link'];
        $res_in["hint"] = array(
          "icon" => $row_a['hintIcon'],
          "title" => $row_a['hintTitle'],
          "description" => $row_a['hintDescription'],
        );
        $isResult = $row_a["is_result"];
        array_push($answers, $res_in);
      }
    }

    $res['token'] = $token;
    $res['code'] = $code;
    $res['consent'] = "true";
    $res['redirect'] = $_GET["redirect"];
    $res['hasHeader'] = $row['hasHeader'];
    $res['qid'] = $row['qid'];
    $res['preAid'] = $preAid;
    $res['nextPage'] = $nextPage;
    $res['allQuestions'] = $allQuestions;
    $res['currentQuestion'] = $currentQuestion;
    $res['type'] = $row['type'];
    $res['questionType'] = $row['questionType']; // singleSelect , multiSelect , inputQuestion , parasite
    $res['inputQuestion'] = $row['inputQuestion'];
    $res['video'] = $row['video'];
    $res['image'] = "";
    $res['borderImage'] = $row['image'];
    $res['diagram'] = $diagram;
    $res['parasite'] = $parasite;
    $res['text'] = $row['text'];
    $res['description'] = $row['description'];

    if ($aid == "fastfitg31.1" . $symbol or $aid == "fastfitb31.1" . $symbol or $aid == "fitg6.1" or $aid == "fitb6.1" or $aid == "incg6.1" or $aid == "incb6.1" or $aid == "gymg6.1" or $aid == "gymb6.1") {
      $res['description'] = $customDescription;
    }

    if ($aid == "fitg9.5" or $aid == "fitb9.5" or $aid == "incg9.5" or $aid == "incb9.5") {
      $currentYear = tsToShamsiYear(time());
      $nextYear = $currentYear + 1;
      $nextYear = $nextYear . "/01/01";
      $conn->query("UPDATE users SET `calendar`='$nextYear' , `celebration`='نوروز' WHERE `token`='$token'");
    }
    if ($aid == "fitg9.4" or $aid == "fitb9.4" or $aid == "incg9.4" or $aid == "incb9.4") {
      $currentYear = tsToShamsiYear(time());
      $currentYaldaTs = shamsiToTs($currentYear, "09", "30");
      $yalda = "";
      if (time() > $currentYaldaTs) {
        $yalda = ($currentYear + 1) . "/09/30";
      } else {
        $yalda = $currentYear . "/09/30";
      }
      $conn->query("UPDATE users SET `calendar`='$yalda' , `celebration`='یلدا' WHERE `token`='$token'");
    }


    if ($aid == "fastfitb33.1" or $aid == "fastfitb33.2" or $aid == "fastfitb33.3" or $aid == "fastfitb33.4" or $aid == "fastfitb33.5" or $aid == "fastfitb33.6" or $aid == "fastfitb33.7" or $aid == "fastfitg33.1" or $aid == "fastfitg33.2" or $aid == "fastfitg33.3" or $aid == "fastfitg33.4" or $aid == "fastfitg33.5" or $aid == "fastfitg33.6" or $aid == "fastfitg33.7" or $aid == "incb9.5" or $aid == "incg9.5" or $aid == "fitb9.5" or $aid == "fitg9.5" or $aid == "fitg9.1" or $aid == "fitg9.2" or $aid == "fitg9.6" or $aid == "fitg9.7" or $aid == "fitg9.8" or $aid == "fitb9.1" or $aid == "fitb9.2" or $aid == "fitb9.6" or $aid == "fitb9.7" or $aid == "fitb9.8" or $aid == "incg9.1" or $aid == "incg9.2" or $aid == "incg9.3" or $aid == "incg9.6" or $aid == "incg9.7" or $aid == "incg9.8" or $aid == "incb9.1" or $aid == "incb9.2" or $aid == "incb9.3" or $aid == "incb9.5" or $aid == "incb9.6" or $aid == "incb9.7" or $aid == "incb9.8") {
      $result_celeb = $conn->query("SELECT * FROM " . $project . "answersV2 WHERE `aid`='$aid'");
      if ($result_celeb->num_rows > 0) {
        while ($rowCeleb = $result_celeb->fetch_assoc()) {
          $celebText = $rowCeleb["text"];
          $conn->query("UPDATE users SET `celebration`='$celebText' WHERE `token`='$token'");
          $res['text'] = "تاریخ " . $celebText . " کیه؟!";
        }
      }
    }

    if ($aid == "fastfitg33.1" . $symbol or $aid == "fastfitg33.2" . $symbol or $aid == "fastfitg33.3" . $symbol or $aid == "fastfitg33.4" . $symbol or $aid == "fastfitg33.5" . $symbol or $aid == "fastfitg33.6" . $symbol or $aid == "fastfitg33.7" . $symbol or $aid == "fastfitg33.7" . $symbol or $aid == "fastfitb33.1" . $symbol or $aid == "fastfitb33.2" . $symbol or $aid == "fastfitb33.3" . $symbol or $aid == "fastfitb33.4" . $symbol or $aid == "fastfitb33.5" . $symbol or $aid == "fastfitb33.6" . $symbol or $aid == "fastfitb33.7" . $symbol) {
      $result_celeb = $conn->query("SELECT * FROM " . $project . "answersV2 WHERE `aid`='$aid'");
      if ($result_celeb->num_rows > 0) {
        while ($rowCeleb = $result_celeb->fetch_assoc()) {
          $addedWords = "";
          $celebInChart = "";
          $celebInFade = "";
          if ($aid == "fastfitg33.1_it" or $aid == "fastfitb33.1_it") {
            $celebInFade = "per la tua vacanza";
            $celebInChart = "Preparati a stupire tutti durante la vacanza";
          }
          if ($aid == "fastfitg33.2_it" or $aid == "fastfitb33.2_it") {
            $celebInFade = "per il tuo matrimonio";
            $celebInChart = "Preparati a stupire tutti al matrimonio";
          }
          if ($aid == "fastfitg33.3_it" or $aid == "fastfitb33.3_it") {
            $celebInFade = "per il tuo evento sportivo";
            $celebInChart = "Preparati a stupire tutto all'evento sportivo";
          }
          if ($aid == "fastfitg33.4_it" or $aid == "fastfitb33.4_it") {
            $celebInFade = "per la tua gita in spiaggia";
            $celebInChart = "Preparati a stupire tutti sulla spiaggia";
          }
          if ($aid == "fastfitg33.5_it" or $aid == "fastfitb33.5_it") {
            $celebInFade = "per la tua rimpatriata";
            $celebInChart = "Preparati a stupire tutti alla rimpatriata";
          }
          if ($aid == "fastfitg33.6_it" or $aid == "fastfitb33.6_it") {
            $celebInFade = "per la tua riunione di famiglia";
            $celebInChart = "Preparati a stupire tutti alla riunione di famiglia";
          }
          if ($aid == "fastfitg33.7_it" or $aid == "fastfitb33.7_it") {
            $celebInFade = "per il tuo giorno speciale";
            $celebInChart = "Preparati a stupire tutto nel tuo giorno speciale";
          }

          $celebText = $addedWords . $rowCeleb["text"];

          if ($aid == "fastfitg33.8_it" or $aid == "fastfitb33.8_it") {
            $celebInFade = "";
            $celebInChart = "";
            $celebText = "";
          }

          $celebText = $addedWords . $rowCeleb["text"];
          $conn->query("UPDATE users SET `celebInChart`='$celebInChart' , `celebInFade`='$celebInFade' , `celebration`='$celebText' WHERE `token`='$token'");
          //$res['text'] = getTextFromId("q8", $language) . $celebText . "?";
        }
      }
    }

    if ($aid == "fitg44.1" or $aid == "fitb44.1" or $aid == "incg44.1" or $aid == "incb44.1") {
      $result_thirdMonth = $conn->query("SELECT * FROM users WHERE `token`='$token'");
      if ($result_thirdMonth->num_rows > 0) {
        while ($rowCeleb = $result_thirdMonth->fetch_assoc()) {
          $thirdMonth = $rowCeleb["thirdMonth"];
          $thirdMonth = monthNumberToMonthName($thirdMonth);
          $res['text'] = "چقدر مطمئنی که در ماه " . $thirdMonth . " به هیکل ایده‌آلی که میخوای میرسی؟";
        }
      }
    }

    if ($aid == "gymg30.1" or $aid == "gymb30.1") {
      $result_thirdMonth = $conn->query("SELECT * FROM users WHERE `token`='$token'");
      if ($result_thirdMonth->num_rows > 0) {
        while ($rowCeleb = $result_thirdMonth->fetch_assoc()) {
          $thirdMonth = $rowCeleb["secondMonth"];
          $thirdMonth = monthNumberToMonthName($thirdMonth);
          $res['text'] = "چقدر مطمئنی که در ماه " . $thirdMonth . " به هیکل ایده‌آلی که میخوای میرسی؟";
        }
      }
    }

    $hint = $row['hint'];
    if (strlen($hint) < 5) {
      $hint = "";
    }
    $res['hint'] = array(
      "title" => "",
      "caption" => $hint
    );

    $res["isResult"] = (string)$isResult;
    $res["resultLink"] = $resultLink;

    $res['answers'] = $answers;
  }
}

$response['success'] = 'true';
$response['message'] = '200';
$response['result'] = $res;
$response['data'] = $res;
$json_response = json_encode($response);
echo $json_response;
die();

// else {
//     $response['success'] = 'false';
//     $response['message'] = 'مشکل در ذخیره سازی اطلاعات';
//     $json_response = json_encode($response);
//     echo $json_response;
//     die();
// }
