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

  /**
	 * 页面的初始数据
	 */
  data: {
    showBox: "main", //控制主页，和不同弹出框的隐现
    shopName: "",
    userName: "",
    signName: "",
    imageUrl1: "", //用来存储微店头像
    imageUrl2: "", //用来存储用户头像
    bannerImg: "",
    bgImg: "",
    action: 'upload.uploadPic',
    shopModle: {
      shop_background: "",
      signature: ""
    },
    showInfo: true, //控制左右的切换
    changeType: "", //用来标示修改的是哪个属性
    shop_id: '',
    shoplang: ''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.shop_id) {
      this.setData({
        shop_id: options.shop_id
      });
    }
    this.getJson();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  getJson() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.set-shop');
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let temp = res.data.micro_shop;
          that.setData({
            shopName: temp.shop_name,
            userName: temp.has_one_member.nickname,
            signName: temp.signature,
            imageUrl1: temp.shop_avatar,
            imageUrl2: temp.has_one_member.avatar,
            postUrl: temp.api ? temp.api : '',
            bannerImg: res.data.select_goods_thumb,
            bgImg: temp.shop_background == 0 ? '' : temp.shop_background,
            // action: res.data.api,
            'shopModle.shop_background': res.data.micro_shop.shop_background,
            'shopModle.signature': res.data.micro_shop.signature
          });
          if (res.data.lang) {
            that.setData({
              shoplang: res.data.lang
            });
            wx.setNavigationBarTitle({
              title: that.data.shoplang + '设置'
            });
          }
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //控制基本信息和自选商品的切换
  changeActive(e) {
    let val = e.currentTarget.dataset.bol;
    let type = e.currentTarget.dataset.num;
    if (type == 1) {
      //提交数据
      let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.set-shop.edit');
      if (app._isTextEmpty(this.data.shopModle.shop_background)) {
        wx.showToast({
          icon: 'none',
          title: '微店背景不能为空',
          duration: 1500
        });
        return;
      }

      if (app._isTextEmpty(this.data.shopModle.signature)) {
        wx.showToast({
          icon: 'none',
          title: '微店个性签名不能为空',
          duration: 1500
        });
        return;
      }
      urlStr += '&shop_set=' + JSON.stringify(this.data.shopModle);
      app._getNetWork({
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            wx.navigateBack({
              delta: 1
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 2500
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    } else {
      this.setData({
        showInfo: val
      });
    }
  },
  manageShop() {
    wx.navigateTo({
      url: '/packageC/microshop/manage_shop/manage_shop'
    });
  },
  //修改信息时候 用变量来标示修改的是哪个属性
  changeShowBox(e) {
    let val = e.currentTarget.dataset.text;
    this.setData({
      changeType: val
    });
    if (val == "店招背景") {
      this.setData({
        showBox: 'upLoadBox'
      });
    } else if (val == "个性签名" || val == "微店名称" || val == "店主昵称") {
      this.setData({
        showBox: 'inputBox'
      });
    } else {
      this.setData({
        showBox: 'main'
      });
    }
  },
  //选择图片
  onRead() {
    let that = this;
    let urlStr = app.getNetAddresss(this.data.action);
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths[0]);
        wx.compressImage({
          src: tempFilePaths[0], // 图片路径
          quality: 60, // 压缩质量
          success(e) {
            let photourl = e.tempFilePath;
            wx.uploadFile({
              url: urlStr,
              filePath: photourl,
              name: 'file',
              success(resdata) {
                var res = JSON.parse(resdata.data);
                if (res.result == 1) {
                  that.setData({
                    bgImg: res.data.img_url,
                    'shopModle.shop_background': res.data.img_url
                  });
                } else {
                  wx.showToast({
                    icon: 'none',
                    title: res.msg,
                    duration: 1500
                  });
                }
              }
            });
          },
          fail(e) {

          },
          complete(e) {

          }
        });
      }
    });
  },
  signatureinp(e) {
    let val = e.detail.value;
    this.setData({
      'shopModle.signature': val
    });
  },
  gomicroshopaddcategory() {
    wx.navigateTo({
      url: '/packageC/microshop/micro_shop_add_category/micro_shop_add_category?shop_id=' + this.data.shop_id
    });
  },
  closePopInput() {
    //隐藏输入弹框
    this.setData({
      showBox: 'main',
      changeVal: ''
    });
  }
});
