// packageE/video_question/apply/apply.js
var WxParse = require('../../../wxParse/wxParse.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submit_status:false,
    status:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  toRecord(){
    wx.navigateTo({
      url: '/packageE/video_question/videoRecord/videoRecord',
    });
  },
  tap_agreement(){
    this.setData({
      is_agreement:true
    });
  },
  close_agreement(){
    this.setData({
      is_agreement:false
    });
  },
  tapSubmit_status(){
    this.setData({
      submit_status:false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goHome(){
    wx.navigateTo({
      url: '/packageE/video_question/video_question',
    });
  },
  user(e){
    let name = e.detail.value;
    this.setData({
      username:name
    });
  },
  mobile(e){
    let mobile = e.detail.value;
    this.setData({
      mobile:mobile
    });
  },
  tapAgeSta(){
    this.setData({
      status:!this.data.status
    });
  },
  submit(){
    if(!this.data.status){
      wx.showToast({
        title: '请阅读服务协议并同意',
        icon:'none',
        duration:1000
      });
      return;
    }
    var that = this;
    let urlStr = app.getNetAddresss('plugin.agent-qa.api.application.apply-agent');
    app._postNetWork({
      url: urlStr,
      data: {
        username: that.data.username,
        mobile:that.data.mobile
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            submit_status:true,
          });
          that.getData();
        }else{
          if(res.msg){
            if(res.msg.mobile){
              wx.showToast({
                title: res.msg.mobile[0],
                icon:'none',
                duration:1000
              });
            }else if(res.msg.username){
              wx.showToast({
                title: res.msg.username[0],
                icon:'none',
                duration:1000
              });
            }else{
              wx.showToast({
                title: res.msg,
                icon:'none',
                duration:1000
              });
            }
          }
        }
      },
      fail: (res) => {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
      }
    });
  },
  getData(){
    var that = this;
    let urlStr = app.getNetAddresss('plugin.agent-qa.api.index.index');
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            info:res.data,
            agreement:res.data.agreement,
            application_audit_status:res.data.application_audit_status
          });
          let article = this.data.agreement;
          try {
            if (article.indexOf('&#') <= -1) {
              WxParse.wxParse('article', 'html', article, this);
            }
          } catch (e) {
            //TODO handle the exception
          }
        }
      },
      fail: (res) => {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
      }
    });
  }
});