const { getProjectNameLang } = require("../common");

// packageH/project_verification/WriteOff/WriteOff.js
const App = getApp();
const GetProjectUrl = App.getNetAddresss("plugin.store-projects.frontend.project-service.get-service-info");
const GetMiniProgramCodeUrl = App.getNetAddresss("plugin.store-projects.frontend.project-order.qr-code-url");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectId: null,
    project: {},
    projectQRCode: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!options.project_order_id) {
      wx.showToast({
        title: getProjectNameLang() + "不存在",
        icon: "none",
      });
      wx.navigateBack({
        delta: 1,
      });
      return;
    }
    this.setData({
      projectId: options.project_order_id,
    });
    this.getProject();
    this.getMiniProgramCode();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getProject();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getProject() {
    wx.showLoading();
    App._getNetWork({
      url: GetProjectUrl,
      data: {
        id: this.data.projectId,
      },
      success: ({ data: { data: response, result, msg } }) => {
        wx.hideLoading();
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        this.setData({
          project: response,
        });
      },
    });
  },
  getMiniProgramCode() {
    App._getNetWork({
      url: GetMiniProgramCodeUrl,
      data: {
        id: this.data.projectId,
      },
      success: ({
        data: {
          data: { miQrCodeUrl },
          result,
          msg,
        },
      }) => {
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        if (typeof miQrCodeUrl === "string") {
          this.setData({
            projectQRCode: miQrCodeUrl,
          });
        } else {
          wx.showToast({
            title: miQrCodeUrl.message,
            icon: "none",
          });
        }
      },
    });
  },
});
