const requestFunc = async(url, method = "GET", data = null, token = null) => {
    apihost = 'http://schedule.tomtit.tomsk.ru:8888/api'
    method = method.toLocaleUpperCase()
    let fullurl = `${apihost}${url}`;
    let options = {
    method: method,
    headers: {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${token}`,
    },
};

switch(method) {
    case "PUT":
    delete options.headers["Content-Type"];
    options.body = data;
    break;
    case "POST": case "PATCH": case "DELETE":
    options.body = JSON.stringify(data);
    break;
}

const res = await fetch(fullurl, options);
return await res.json();
};

var selectedFile;
document
    .getElementById("fileUpload")
    .addEventListener("click", function(event){
        selectedFile = event.target.files[0];
    });
document
    .getElementById("uploadExcel")
    .addEventListener("click", function(){
        if (selectedFile) {
          console.log("hi");
          var fileReader = new FileReader();
          fileReader.onload = function(event) {
            var data = event.target.result;

            var workbook = XLSX.read(data, {
              type: "binary"
            });
            workbook.SheetNames.forEach(sheet => {
              let rowObject = XLSX.utils.sheet_to_row_object_array(
                workbook.Sheets[sheet]
              );
              var myHeaders = new Headers();
                myHeaders.append("Content-Type", "application/json");
                var raw = JSON.stringify(rowObject);
                requestFunc(url="/plan/", method="POST", data=raw)
                console.log(jsonObject);
              // let jsonObject = JSON.stringify(rowObject);
              // document.getElementById("jsonData").innerHTML = jsonObject;
              // console.log(jsonObject);
            });
          };
          fileReader.readAsBinaryString(selectedFile);
        }
      });
    







































// // <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/xlsx.full.min.js"></script>



// // <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/jszip.js"></script>
// // <script type="text/javascript">
//     function Upload() {
//         //Reference the FileUpload element.
//         var fileUpload = document.getElementById("fileUpload");
 
//         //Validate whether File is valid Excel file.
//         var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xls|.xlsx)$/;
//         if (regex.test(fileUpload.value.toLowerCase())) {
//             if (typeof (FileReader) != "undefined") {
//                 var reader = new FileReader();
 
//                 //For Browsers other than IE.
//                 if (reader.readAsBinaryString) {
//                     reader.onload = function (e) {
//                         ProcessExcel(e.target.result);
//                     };
//                     reader.readAsBinaryString(fileUpload.files[0]);
//                 } else {
//                     //For IE Browser.
//                     reader.onload = function (e) {
//                         var data = "";
//                         var bytes = new Uint8Array(e.target.result);
//                         for (var i = 0; i < bytes.byteLength; i++) {
//                             data += String.fromCharCode(bytes[i]);
//                         }
//                         ProcessExcel(data);
//                     };
//                     reader.readAsArrayBuffer(fileUpload.files[0]);
//                 }
//             } else {
//                 alert("This browser does not support HTML5.");
//             }
//         } else {
//             alert("Please upload a valid Excel file.");
//         }
//         alert("Загружено")
//     };
// // </script>