
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  if (request.subject == 'TRACKING') {
      console.log(request.data.name);
      sendResponse("tracking");
      document.getElementById("tracking").value = request.data.name;

  }
  else if (request.subject == 'OTHER_SUBJECT') {
      console.log(request.data.name);
      sendResponse("other response");
  }
});


function doFulfill() {
  
  var select = document.getElementById("Select");
  var tracking = document.getElementById("tracking");
  var formData = JSON.parse(select.value);  
  function doRefresh(responseText) {
    //console.log('???'+responseText);
    select.remove(select.selectedIndex);
    tracking.value="";
    document.getElementById("msg").innerHTML = "";
    //getOrders(responseText);
    //window.location.href="popup.html?x="+Math.random().toString(16).slice(2);
  }
  //console.log("will mount");
  fetch("https://fb.magnolify.com/fulfill.php?o="+formData.id+"&t="+tracking.value)
  .then((response) => response.text())
  .then((responseText) => {
    doRefresh(responseText);
  })
}
function autofillPage() {
  //chrome.tabs.create({ url: chrome.runtime.getURL("pages/send.html") });
  //document.querySelector("#top > div.space-y-4 > section:nth-child(1) > div > div.w-full.md\\:w-1\\/2.space-y-2.mr-24.p-4.shadow.rounded-xl.overflow-y-hidden > div:nth-child(6) > div:nth-child(3) > div > input").
  var select = document.getElementById("Select");
  var formData = JSON.parse(select.value);

  if (formData) {
    //console.log(formData)

    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      chrome.tabs.sendMessage(tabs[0].id, formData, function (response) {
        console.log(response)
      })

    })
  }  
  //alert(select.value);
}
function autofillPage2() {

  //let test = document.Livewire.getByName('firstName');
  //console.log('find: '+test); // returns -> Array [Proxy]
  
  //test[0].set('to',42);
  //document.Livewire.getByName('first_name').set('to','value',live = true);
  //const input = document.evaluate("//*[@id=\"top\"]/div[2]/section[1]/div/div[2]/div[5]/div[1]/div/input", document.getElementById("myFrame"), null, XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue;
  
  //console.log('well?: '+serverMemo.data.to);

  chrome.storage.sync.get("formData", function (data) {
    var formData = data.formData;
    if (formData) {
      console.log(formData)

      chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        chrome.tabs.sendMessage(tabs[0].id, formData, function (response) {
          console.log(response);
          var tracking = document.getElementById("tracking");

        })

      })
    }
  })
}

function updateTracking(){
  var tracking = document.getElementById("tracking").value;

  chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    chrome.tabs.sendMessage(tabs[0].id, tracking, function (response) {  
      alert('got it! '+response);
      console.log(response);
      tracking.value=response;

    })
})
}
//function getComboA(selectObject) {
//  var value = selectObject.value;  
//  console.log(value);
//}
function setTracking() {
  var orderData = document.querySelector("Select").value;
  var json = JSON.parse(orderData);
  trackNo = json.id;

  document.getElementById("msg").innerHTML = "ORDER ID: <a href=https://admin.shopify.com/store/b7c1eb-2/orders/"+trackNo+">"+trackNo+"</a>";
  document.getElementById("shopLink").href = "https://admin.shopify.com/store/b7c1eb-2/orders/"+trackNo;
  console.log("tracking changed: "+trackNo);
}

document.addEventListener("DOMContentLoaded", function () {
  document.querySelector("#doFulfill").addEventListener("click", doFulfill);
});
document.addEventListener("DOMContentLoaded", function () {
  document.querySelector("#Select").addEventListener("change", setTracking);  
});

document.addEventListener("DOMContentLoaded", function () {
  document.querySelector("#autofill").addEventListener("click", autofillPage);  
});

//document.addEventListener("DOMContentLoaded", function () {
//  document.querySelector("Select").addEventListener("change", setTracking());  
//});
