/**
 * 定价工具视图层（全局变量版本）
 */
window.PricingToolView = {
  elements: null,

  createModal() {
    fetch(chrome.runtime.getURL("model/pricing-tool.html"))
      .then((res) => res.text())
      .then((html) => {
        document.body.insertAdjacentHTML("beforeend", html);
        this.elements = this.getElements();
      });
    return document.getElementById("pricingToolModal");
  },

  getElements() {
    return {
      overlay: document.getElementById("pricingToolModal"),//定价工具弹窗容器
      closeBtn: document.getElementById("pricingModalClose"),//关闭按钮 
      fillBtn: document.getElementById("pricingFillBtn"),//填充售价
      costInput: document.getElementById("pricing-cost"),//产品成本 
      discountInput: document.getElementById("pricing-discount"),//店铺优惠
      // shopDiscountInput: document.getElementById("pricing-shopDiscount"),//店铺折扣
      platformCommissionInput: document.getElementById("pricing-platformCommission"),//平台佣金
      allianceCommissionInput: document.getElementById("pricing-allianceCommission"),//联盟佣金
      transactionFeeInput: document.getElementById("pricing-transactionFee"),//交易服务费
      coinMarketingInput: document.getElementById("pricing-coinMarketing"),//金币营销
      activityDiscountInput: document.getElementById("pricing-activityDiscount"),//活动折扣
      profitRatioInput: document.getElementById("pricing-profitRatio"),//利润成本比
      recommendedPriceInput: document.getElementById("pricing-recommendedPrice"),//推荐售价
      currencyRadios: document.querySelectorAll('input[name="pricingCurrency"]'),//汇率：
      exchangeRateText: document.getElementById("pricingExchangeRate")
    };
  },

  renderForm(pricingData) {
    if (!this.elements) return;
    const {
      cost,
      discount,
      // shopDiscount,
      platformCommission,
      allianceCommission,
      transactionFee,
      coinMarketing,
      activityDiscount,
      profitRatio,
      recommendedPrice,
      currency,
      exchangeRate
    } = pricingData;

    this.elements.costInput.value = cost.toFixed(2);
    this.elements.discountInput.value = discount.toFixed(2);
    // this.elements.shopDiscountInput.value = shopDiscount.toFixed(2);
    this.elements.platformCommissionInput.value = platformCommission.toFixed(2);
    this.elements.allianceCommissionInput.value = allianceCommission.toFixed(2);
    this.elements.transactionFeeInput.value = transactionFee.toFixed(2);
    this.elements.coinMarketingInput.value = coinMarketing.toFixed(2);
    this.elements.activityDiscountInput.value = activityDiscount.toFixed(2);
    this.elements.profitRatioInput.value = profitRatio.toFixed(2);
    this.elements.recommendedPriceInput.value = recommendedPrice.toFixed(2);

    this.elements.currencyRadios.forEach((radio) => {
      radio.checked = radio.value === currency;
    });

    this.updateExchangeRate(currency, exchangeRate);
  },

  updateExchangeRate(currency, exchangeRate) {
    if (!this.elements) return;
    const rate = parseFloat(exchangeRate.toFixed(2));
    if (currency === "CNY") {
      
      this.elements.exchangeRateText.textContent = `1 美元 = ${rate} 人民币`;
    } else {
      this.elements.exchangeRateText.textContent = `1 人民币 = ${(1 / rate).toFixed(2)} 美元`;
    }
  },

  showModal() {
    if (this.elements) this.elements.overlay.style.display = "flex";
  },

  hideModal() {
    if (this.elements) this.elements.overlay.style.display = "none";
  }
};