// pages/more/apply_shop/apply_shop.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      '../../../images/ruzhu.jpg',
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    agree: false,
    hideContract: true,
    applyData: {},
    submitDisabled: false,//防止重复提交
    showBtnLoading: false,
    hasApplyed: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  //查看协议
  seeContract: function(){
    this.setData({
      hideContract: false,
    })
  },
  // 同意协议
  agreeContract: function(){
    this.setData({
      hideContract: true,
      agree: true,
    })
  },
  //获取输入的内容
  getKey: function(e){
    var value = e.detail.value;
    var kind = e.currentTarget.dataset.kind;
    var applyData = this.data.applyData;
    applyData[kind] = value;
    this.setData({
      applyData: applyData
    })
  },
  // 立即申请
  applyNow: function(){
    var that = this;
    var submitDisabled = that.data.submitDisabled;
    if ( submitDisabled ){
      wx.showModal({
        title: '温馨提示',
        content: '请不要重复提交',
        showCancel: false,
      })
      return false;
    }
    var applyData = that.data.applyData;
    var title = applyData.title;
    var project = applyData.project;
    var content = applyData.content;
    var linkman = applyData.linkman;
    var phone = applyData.phone;
    var reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var phoneValid = reg.test(phone);
    var agree = that.data.agree;
    var warnTip;
    if ( title == undefined || title == '' ){
      warnTip = '请输入商户名称';
    } else if (project == undefined || project == '' ){
      warnTip = '请输入主营项目';
    } else if (content == undefined || content == '') {
      warnTip = '请输入简单介绍';
    } else if (linkman == undefined || linkman == '') {
      warnTip = '请输入联系人';
    } else if (phone == undefined || phone == '') {
      warnTip = '请输入电话号码';
    } else if (!phoneValid) {
      warnTip = '请输入正确的电话号码';
    }else if (!agree) {
      warnTip = "未选择同意【入驻申请合同】";
    }
    if ( warnTip != undefined ){
      wx.showModal({
        title: '温馨提示',
        content: warnTip,
        showCancel: false
      })
      return false;
    }
    that.setData({
      submitDisabled: true,
      showBtnLoading: true,
    })
    wx.request({
      url: app.globalData.hostname + 'shop_apply',
      data: {
        title: title,
        project: project,
        content: content,
        linkman: linkman,
        phone: phone,
        open_id: wx.getStorageSync('realOpenid')
      },
      header: { 'content-type': 'application/json' },
      method:'GET',
      dataType: 'json',
      success: function(res) {
        var resTitle;
        if(res.data.code == 200){
          resTitle = '提交申请成功';
          that.setData({
            hasApplyed: true,
          })
        } else if (res.data.code == 404){
          resTitle = '该手机号码已申请';
        }else{
          resTitle = '提交申请失败'
        }
        // resTitle = res.data.code ? res.data.code : resTitle
        wx.showToast({
          title: resTitle,
          duration: 2000,
        })
        that.setData({
          showBtnLoading: false,
        })
        // console.log(res);
      },
      fail: function(res) {},
      complete: function(res) {
        setTimeout(function(){
          that.setData({
            submitDisabled: false,
          })
        },1000)
      },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

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

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

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

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

})