// packageE/others/application_market/change_site/change_site.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    plugin_id: "",
    isNew: null,
    form: {
      name: "",
      domain: "",
      mobile: "",
      code: "",
    },
    domain: "",
    count: 0,
    timer: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkdata();
    this.setData({
      domain: app.data.host.match(/https:\/\/(\S*)\//)[1],
    });
    if (options.isNew) {
      this.setData({
        isNew: true,
        plugin_id: options.plugin_id,
      });
      wx.setNavigationBarTitle({
        title: "新建站点",
      });
    } else {
      this.setData({
        isNew: false,
        plugin_id: options.plugin_id,
      });
      wx.setNavigationBarTitle({
        title: "绑定站点",
      });
    }
  },
  changeNew() {
    this.isNew = !this.isNew;
    this.setData({
      isNew: !this.data.isNew,
    });
  },
  tel(e) {
    this.setData({
      "form.mobile": e.detail,
    });
  },
  domain(e) {
    this.setData({
      "form.domain": e.detail,
    });
  },
  company(e) {
    this.setData({
      "form.name": e.detail,
    });
  },
  auth(e) {
    this.setData({
      "form.code": e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  checkdata() {
    let url = "plugin.product-market.api.client.index";
    if (this.data.plugin_id == 58) {
      url = "plugin.market-sub.api.client.index";
    }
    let urlStr = app.getNetAddresss(url);
    app._getNetWork({
      url: urlStr,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result != 1) {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1,
                });
              }
            },
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  toSave(e) {
    if (this.checkIn()) {
      let url = "";
      if (this.data.isNew) {
        url = "plugin.product-market.api.client.index.add";
        if (this.data.plugin_id == 58) {
          url = "plugin.market-sub.api.client.index.add";
        }
      } else {
        url = "plugin.product-market.api.client.index.bind";
        if (this.data.plugin_id == 58) {
          url = "plugin.market-sub.api.client.index.bind";
        }
      }
      let urlStr = app.getNetAddresss(url);
      app._postNetWork({
        url: urlStr,
        data: this.data.form,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 1000,
              success: function () {
                wx.navigateBack({
                  delta: 1,
                });
              },
            });
          } else {
            app.tips(res.msg, "none", 1500);
          }
        },
        fail: function () {},
      });
    }
  },
  checkDomain() {
    if (this.data.form.domain.indexOf("https://") > -1 || this.data.form.domain.indexOf("http://") > -1) {
      return false;
    }
    return true;
  },
  checkIn() {
    if (this.data.isNew && app._isTextEmpty(this.data.form.name)) {
      app.tips("请填写公司/个人名称", "none", 1000);
      return false;
    }
    if (app._isTextEmpty(this.data.form.domain)) {
      app.tips("请填写域名", "none", 1000);
      return false;
    }
    if (!this.checkDomain()) {
      wx.showModal({
        title: "提示",
        content: "域名格式不正确: 请填写没有https://或http:// 格式的域名",
        showCancel: false,
        success(res) {},
      });
      return false;
    }
    if (app._isTextEmpty(this.data.form.mobile)) {
      app.tips("请填写手机号码", "none", 1000);
      return false;
    }
    if (app._isTextEmpty(this.data.form.code)) {
      app.tips("请输入验证码", "none", 1000);
      return false;
    }

    return true;
  },
  sureIdentify() {
    let that = this;
    let form = this.data.form;
    if (app._isTextEmpty(form.mobile)) {
      app.tips("请先填写手机号", "none", 1000);
      return;
    }
    let json = {
      mobile: that.data.form.mobile,
    };
    const TIME_COUNT = 60;
    // let uid = wx.getStorageSync('uid');
    that.data.count = TIME_COUNT;
    let url = "plugin.product-market.api.client.index.send-code";
    if (this.data.plugin_id == 58) {
      url = "plugin.market-sub.api.client.index.send-code";
    }
    let urlStr = app.getNetAddresss(url);
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          app.tips("已发送", "none", 1500);
          that.data.timer = setInterval(() => {
            if (that.data.count > 0 && that.data.count <= TIME_COUNT) {
              that.setData({
                count: that.data.count - 1,
              });
            } else {
              clearInterval(that.data.timer);
              that.data.timer = null;
            }
          }, 1000);
        } else {
          app.tips(res.msg, "none", 1500);
        }
      },
      fail: function (res) {
        console.log(res);
        wx.hideLoading({
          complete: (res) => {},
        });
      },
    });
  },
});
