// packageH/orderImport/orderImportApply/orderImportApply.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    mobile:'',
    goods_list:[],
    goods_id:0,
    shopkeeper:0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.shopkeeper && options.shopkeeper==1){
      this.setData({
        shopkeeper:1
      });
      wx.setNavigationBarTitle({
        title: '店主信息'
      });
    }else {
      this.getApplyInfo();
    }
    
  },

  getApplyInfo(){
    let urlStr = app.getNetAddresss('plugin.owner-order-import.frontend.modules.owner.controllers.register.index');
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          goods_list:res.data.goods_list,
          name:res.data.owner.name,
          mobile:res.data.owner.mobile
        });         
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },

  setGoodsId(evt){
    let id = evt.currentTarget.dataset.id;
    this.setData({
      goods_id:id
    });
  },

  sendApply(){
    if (app._isTextEmpty(this.data.name)) return app.tips('请输入申请姓名');
    if (app._isTextEmpty(this.data.mobile)) return app.tips('请输入手机号');
    if (app._isMoblie(this.data.mobile)) return app.tips('请输入正确手机号');
    let urlStr = app.getNetAddresss('plugin.owner-order-import.frontend.modules.owner.controllers.perfect.index');
    if(this.data.shopkeeper !=1){
      if (this.data.goods_id<=0) return app.tips('请选择商品');
      urlStr = app.getNetAddresss('plugin.owner-order-import.frontend.modules.owner.controllers.apply.index');
    }
      
    app._postNetWork({
      url: urlStr,
      data:{
        name:this.data.name,
        mobile:this.data.mobile
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        if(this.data.shopkeeper !=1){
          wx.redirectTo({
            url: '/packageA/detail_v2/detail_v2?id='+this.data.goods_id,
          });
        }else {
          let pages = getCurrentPages();
          let page = pages[pages.length-2];
          page.getListData();
          wx.navigateBack();
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });





  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});