<!DOCTYPE html><html><head><meta charset="utf-8"><title>Add License</title><style>body{font-family:Segoe UI,system-ui,sans-serif;font-size:14px;color:#242424;margin:0;padding:20px 24px}h1{font-size:18px;font-weight:600;margin:0 0 6px}p{margin:0 0 14px;color:#424242}.pick{display:inline-block;padding:6px 14px;border:1px solid #8a8886;border-radius:4px;cursor:pointer;background:#fff}.pick:hover{background:#f3f2f1}input[type=file]{display:none}table{border-collapse:collapse;margin:14px 0}td{padding:3px 14px 3px 0;vertical-align:top}td:first-child{color:#616161}.msg{margin:12px 0;padding:8px 10px;border-radius:4px;display:none}.msg.err{display:block;background:#fde7e9;color:#a4262c}.msg.ok{display:block;background:#dff6dd;color:#107c10}.buttons{margin-top:18px;display:flex;gap:8px}button{font:inherit;padding:6px 18px;border-radius:4px;border:1px solid #8a8886;background:#fff;cursor:pointer}button.primary{background:#0f6cbd;border-color:#0f6cbd;color:#fff}button:disabled{opacity:.5;cursor:default}</style></head><body><h1>Add a license</h1><p>Choose the license file (.lic) you were sent. If there's already a license for that app, this one replaces it. No license yet? Use Request License.</p><label class="pick">Choose license file…<input type="file" id="file" accept=".lic,application/json"></label><span id="fname"></span><table id="details" hidden><tr><td>Application</td><td id="d-app"></td></tr><tr><td>Licensed to</td><td id="d-customer"></td></tr><tr><td>Edition</td><td id="d-edition"></td></tr><tr><td>Expires</td><td id="d-expires"></td></tr></table><div class="msg" id="msg"></div><div class="buttons"><button class="primary" id="save" disabled>Save</button><button id="close">Close</button></div><script>
(function () {
  var API = "/api/data/v9.2/";
  var lic = null;
  var $ = function (id) { return document.getElementById(id); };
  var base = (function () {  // the org URL, also when the page runs in a dialog under /WebResources/
    var x = window.parent && window.parent.Xrm;
    try { if (x && x.Utility) return x.Utility.getGlobalContext().getClientUrl().replace(/\/$/, ""); } catch (e) { }
    return location.origin;
  })();
  function call(method, path, body) {
    return fetch(base + API + path, {
      method: method, credentials: "same-origin",
      headers: { "Accept": "application/json", "Content-Type": "application/json", "OData-MaxVersion": "4.0", "OData-Version": "4.0" },
      body: body ? JSON.stringify(body) : undefined
    }).then(function (r) {
      if (r.ok) return r.status === 204 ? null : r.json();
      return r.json().then(function (j) { throw new Error((j.error && j.error.message) || ("HTTP " + r.status)); },
                           function () { throw new Error("HTTP " + r.status); });
    });
  }
  function show(text, ok) { var m = $("msg"); m.textContent = text; m.className = "msg " + (ok ? "ok" : "err"); }
  function hideMsg() { $("msg").className = "msg"; }

  $("file").addEventListener("change", function () {
    var f = this.files && this.files[0];
    lic = null; $("save").disabled = true; $("details").hidden = true; hideMsg();
    if (!f) return;
    $("fname").textContent = " " + f.name;
    var reader = new FileReader();
    reader.onload = function () {
      var j;
      try { j = JSON.parse(String(reader.result).replace(/^﻿/, "")); } catch (e) { j = null; }
      var key = j && typeof j.licenseKey === "string" ? j.licenseKey.trim() : "";
      if (!key) { show("That file isn't a license file: it has no license key in it."); return; }
      lic = { application: j.application || "", key: key };
      $("d-app").textContent = j.application || "(not shown in the file)";
      $("d-customer").textContent = j.customer || "";
      $("d-edition").textContent = (j.edition || "") + (j.trial ? " (trial)" : "");
      $("d-expires").textContent = j.expires || "Never";
      $("details").hidden = false;
      $("save").disabled = false;
    };
    reader.onerror = function () { show("Couldn't read that file."); };
    reader.readAsText(f);
  });

  $("save").addEventListener("click", function () {
    if (!lic) return;
    $("save").disabled = true; hideMsg();
    // the row for this app, if there is one: a renewal replaces its key (the plugin checks the key either way)
    var find = lic.application
      ? call("GET", "m3_deployedlicenses?$select=m3_deployedlicenseid&$filter=m3_applicationname eq '" + lic.application.replace(/'/g, "''") + "'")
      : Promise.resolve({ value: [] });
    find.then(function (r) {
      var row = r.value && r.value[0];
      return row ? call("PATCH", "m3_deployedlicenses(" + row.m3_deployedlicenseid + ")", { m3_licensekey: lic.key })
                 : call("POST", "m3_deployedlicenses", { m3_licensekey: lic.key });
    }).then(function () {
      show("License saved for " + (lic.application || "the app") + ". It takes effect within a minute.", true);
      lic = null;
    }, function (e) {
      show(e.message);
      $("save").disabled = false;
    });
  });

  $("close").addEventListener("click", function () {
    // a web resource dialog has no close API of its own: use the dialog's close button, else the window's
    var p = window.parent && window.parent.document;
    var x = p && (p.querySelector('[data-id="dialogCloseIconButton"]') || p.querySelector('button[aria-label="Close"]'));
    if (x) x.click(); else window.close();
  });
})();
</script></body></html>