var token;
var schoolcode;
var grade;
var url;
var courseName;
var subject;
var stdClassInfo;
var refernce_ID;
var shortName;
var chapter;
const urlSearchparams = window.top.location.search.substring(1);
const getValues = new URLSearchParams(urlSearchparams);
console.log(getValues);
grade = getValues.get("grade");
courseName = getValues.get("category");
subject = getValues.get("subject");
shortName = getValues.get("shortName")
console.log(subject, shortName)
if (shortName) {
  chapter = 2
}
if (subject === 'Environmental Studies') {
  subject = 'EVS'
}
else if (subject === 'Social Studies') {
  subject = 'SST'
}
url = getValues.get("url");
token = getValues.get("token");
console.log(
  grade,
  courseName,
  subject,
  url,
  token
);
if (courseName === "Calyx") {
  courseName = calyx;
} else if (courseName === "peaks level up") {
  courseName = "PeaksLevelUp";
}
console.log(courseName, "courseName");

const calyx = {
  1: ["English", "Maths", "EVS"],
  2: ["English", "Maths", "EVS"],
  3: ["English", "Maths", "Science", "SST"],
  4: ["English", "Maths", "Science", "SST"],
  5: ["English", "Maths", "Science", "SST"],
};

// const PeaksLevelUp={
//   1: ["English", "Maths", "EVS","Hindi","Computer","GK"],
//   2: ["English", "Maths", "EVS","Hindi","Computer","GK"],
//   3: ["English", "Maths", "Science", "Social Studies","Hindi","Computer","GK"],
//   4: ["English", "Maths", "Science", "Social Studies","Hindi","Computer","GK"],
//   5: ["English", "Maths", "Science", "Social Studies","Hindi","Computer","GK"],
// }


var index = courseName[grade - 1].indexOf(subject);
console.log(index, "index")
if (index === -1) {
  console.log(`Subject "${subject}" is at position ${index + 2} in grade ${grade}.`);
  stdClassInfo = `Book ${grade}`
  console.log(stdClassInfo, "stdClassInfo");

} else {
  alert(`Subject "${subject}" not found in grade ${grade}.`)
}
var online_offline;
var tgTitle = courseName.split(" ")[0]
var global_var = "test";
var bookInfo = {
  bookname: tgTitle,
  semester: '',
  class: stdClassInfo,
  subject: subject,
  schoolcode: "",
  chapters: chapter
};


var treeStructure = ["class", "chapter", "neworbook", "topic", "type"];
var questions;
const readUrl = `${url}/customwebservice/tg/tgcrud.php?wsfunction=questionread&wstoken=${token}`;
axios
  .post(readUrl, bookInfo)
  .then((response) => {
    online_offline = true;
    global_var = response.data;

    refernce_ID = response.data.book_refrence.id
    console.log("Response:", response.data, refernce_ID);
    questions = response.data.questions;
    online_offline = true;
    $(".loader-wrap").remove();
    $("#startBtn").removeClass("disabled");
    setAllEvents();
  })
  .catch((error) => {
    questions = offlineData();
    console.log(questions);
    // online_offline = false;
    console.log(online_offline, "online_offline");
    $(".loader-wrap").remove();
    $("#startBtn").removeClass("disabled");
    setAllEvents();
  })

