text: var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
      return new (P || (P = Promise))(function (resolve, reject) {
          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
          function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  import { API } from "./api.js";
  import { CreateStockMemoRequestBody, UpdateStockMemoRequestBody, } from "./requestBody.js";
  export class StockMemoForm {
      constructor(backgroundDiv, type, defaultValue) {
          StockMemoForm.backgroundDiv = backgroundDiv;
          StockMemoForm.type = type;
          StockMemoForm.defaultValue = defaultValue;
          StockMemoForm.buildForm();
      }
      static buildForm() {
          StockMemoForm.backgroundDiv.innerHTML = "";
          let formContainer = document.createElement("div");
          formContainer.id = "form-container";
          let formContainerHeader = document.createElement("div");
          formContainerHeader.id = "form-container-header";
          formContainer.appendChild(formContainerHeader);
          formContainer.appendChild(StockMemoForm.buildFormBody());
          formContainer.appendChild(StockMemoForm.buildFormFooter());
          StockMemoForm.backgroundDiv.appendChild(formContainer);
          StockMemoForm.backgroundDiv.classList.add("active");
      }
      static buildFormBody() {
          let formBody = document.createElement("form");
          formBody.classList.add("form-container-body");
          formBody.classList.add("active");
          formBody.id = "stock-memo-form";
          if (StockMemoForm.type === "update") {
              let label0 = StockMemoForm.buildInput("ID", "id", "stock-memo-id", ["memo-form-input", "form-input"], true);
              formBody.appendChild(label0);
          }
          let label1 = StockMemoForm.buildInput("證券代號", "sid", "stock-memo-sid", ["memo-form-input", "form-input"], true);
          let label2 = StockMemoForm.buildInput("主要業務", "main-goods-or-services", "stock-memo-main-goods-or-services", ["memo-form-input", "form-input"], false);
          let label3 = StockMemoForm.buildInput("交易策略", "strategy-used", "stock-memo-strategy-used", ["memo-form-input", "form-input"], false);
          let label4 = StockMemoForm.buildInput("其他備註", "my-note", "stock-memo-my-note", ["memo-form-input", "form-input"], false);
          formBody.appendChild(label1);
          formBody.appendChild(label2);
          formBody.appendChild(label3);
          formBody.appendChild(label4);
          return formBody;
      }
      static buildInput(labelWordShowed, nameToIdentify, inputDivId, inputDivClassList, shouldDisableAndHide) {
          let labelDiv = document.createElement("label");
          let labelName = document.createElement("div");
          labelName.innerHTML = labelWordShowed;
          let inputDiv = document.createElement("textarea");
          inputDiv.name = nameToIdentify;
          inputDiv.id = inputDivId;
          for (let each of inputDivClassList)
              inputDiv.classList.add(each);
          inputDiv.value = StockMemoForm.defaultValue[nameToIdentify];
          inputDiv.disabled = shouldDisableAndHide;
          inputDiv.addEventListener("input", () => {
              inputDiv.style.height = "";
              inputDiv.style.height = `${inputDiv.scrollHeight}px`;
          });
          labelDiv.appendChild(labelName);
          labelDiv.appendChild(inputDiv);
          if (shouldDisableAndHide)
              labelDiv.style.display = "none";
          return labelDiv;
      }
      static buildFormFooter() {
          let formFooter = document.createElement("div");
          formFooter.id = "form-container-footer";
          formFooter.appendChild(StockMemoForm.buildDiscardBtn());
          formFooter.appendChild(StockMemoForm.buildSubmitBtn());
          return formFooter;
      }
      static buildDiscardBtn() {
          let discardBtn = document.createElement("div");
          discardBtn.className = "form-btn";
          discardBtn.id = "discard-btn";
          discardBtn.innerHTML =
              StockMemoForm.type === "create" ? "捨棄" : "取消";
          discardBtn.addEventListener("click", StockMemoForm.discardAndRemoveRecordForm);
          return discardBtn;
      }
      static discardAndRemoveRecordForm() {
          StockMemoForm.backgroundDiv.innerHTML = "";
          StockMemoForm.backgroundDiv.classList.remove("active");
      }
      static buildSubmitBtn() {
          let submitBtn = document.createElement("div");
          submitBtn.className = "form-btn";
          submitBtn.innerHTML = StockMemoForm.type === "create" ? "送出" : "儲存";
          submitBtn.id = "submit-btn";
          submitBtn.addEventListener("click", (e) => {
              StockMemoForm.createPlan(e);
          });
          return submitBtn;
      }
      static createPlan(e) {
          return __awaiter(this, void 0, void 0, function* () {
              let mainGoodsOrServicesInputDiv = document.getElementById("stock-memo-main-goods-or-services");
              let newMainGoodsOrServices = StockMemoForm.defaultValue["main-goods-or-services"];
              let strategyUsedInputDiv = document.getElementById("stock-memo-strategy-used");
              let newStrategyUsed = StockMemoForm.defaultValue["strategy-used"];
              let myNoteDiv = document.getElementById("stock-memo-my-note");
              let newMyNote = StockMemoForm.defaultValue["my-note"];
              if (mainGoodsOrServicesInputDiv instanceof HTMLTextAreaElement &&
                  strategyUsedInputDiv instanceof HTMLTextAreaElement &&
                  myNoteDiv instanceof HTMLTextAreaElement) {
                  newMainGoodsOrServices = mainGoodsOrServicesInputDiv.value;
                  newStrategyUsed = strategyUsedInputDiv.value;
                  newMyNote = myNoteDiv.value;
                  let requestBody;
                  if (StockMemoForm.type === "create") {
                      requestBody = new CreateStockMemoRequestBody(StockMemoForm.defaultValue["sid"], newMainGoodsOrServices, newStrategyUsed, newMyNote);
                  }
                  else {
                      requestBody = new UpdateStockMemoRequestBody(StockMemoForm.defaultValue["id"], newMainGoodsOrServices, newStrategyUsed, newMyNote);
                  }
                  let loading = document.getElementById("loading");
                  if (loading instanceof HTMLElement)
                      loading.style.display = "flex";
                  yield API.memoCRUD(requestBody);
                  location.reload();
              }
          });
      }
  }
  