var requestSign = require('../../../../utils/requestData.js');
var api = require('../../../../utils/api.js').open_api;
var util = require('../../../../utils/util.js');
var header = getApp().header;
var time = require('../../../../utils/time.js');
const app = getApp();
var stockTable = getApp().globalData.stockTable;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTopTips: false,
    shopcode: '',
    shopname: '',
    shopsaleprice: null,
    shopchengbenprice: null,
    stocksum: '1',
    goodsskulist: '',
    skuid: '',
    formData: {

    },
    defaultImage: 'https://upload.ishare-go.com/miniprogram/casher/default.jpg',//默认图片
    tempFile: '',//上传的图片
    istiaoxingmaedit: false, //条形码编辑状态锁定
    rules: [{
      name: 'shopcode',
      rules: {
        required: true,
        message: '请输入条形码'
      },
      name: 'shopname',
      rules: {
        required: true,
        message: '请输入商品名称'
      },
      name: 'shopsaleprice',
      rules: {
        required: true,
        message: '请输入销售价'
      },
      name: 'shopchengbenprice',
      rules: {
        required: true,
        message: '请输入成本价'
      },
      name: 'stocksum',
      rules: {
        required: true,
        message: '请输入本次入库数量'
      }
    }]
  },
  formInputChange1(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      shopcode: e.detail.value
    })
  },
  formInputChange2(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      shopname: e.detail.value
    })
  },
  formInputChange3(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      shopsaleprice: e.detail.value
    })
  },
  formInputChange4(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      shopchengbenprice: e.detail.value
    })
  },
  formInputChange5(e) {
    const {
      field
    } = e.currentTarget.dataset
    this.setData({
      stocksum: e.detail.value
    })
  },
  checkInputValue: function() {   //这里函数
    const that = this;
    if (!that.data.shopname) {
      wx.showToast({
        title: '请输入商品名称'
      })
      return false;
    } else if (!that.data.shopsaleprice) {
      wx.showToast({
        title: '请输入销售价'
      })
      return false;
    } else if (!that.data.shopchengbenprice) {
      wx.showToast({
        title: '请输入成本价'
      })
      return false;
    } else if (!that.data.stocksum) {
      wx.showToast({
        title: '请输入入库数量'
      })
      return false;
    }
    return true;
  },
  submitForm(e) {  // form表单的确定按钮
    this.selectComponent('#form').validate((valid, errors) => {
      const that = this;

      if (!that.checkInputValue()) {   //这里判断如果不通过终端 
        return false;
      } else {  // 通过则请求接口
        var postData = {
          shopcode: that.data.shopcode,
          shopname: that.data.shopname,
          shopsaleprice: that.data.shopsaleprice,
          shopchengbenprice: that.data.shopchengbenprice,
          stocksum: that.data.stocksum,
          skuid: that.data.skuid,
          shoppic: that.data.tempFile
        }
        // console.log(stockTable);
        // getApp().globalData.stockTable.push(data);
        // console.log(stockTable)
        let datainfo = requestSign.requestSign(postData);
        header.sign = datainfo
        wx.request({
          url: api.get_addChannelGoods,
          data: postData,
          header: header,
          method: 'POST',
          dataType: 'json',
          responseType: 'text',
          success: (res) => {

            if (res.data.code >= 0) {
              console.log(res)
              wx.showToast({
                title: '提交成功'
              })
              wx.navigateBack({
                delta: 1
              })
            }
          },
          fail: (res) => {},
        })
      }
    })
  },
  takePhoto: function() {
    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        // that.setData({
        //   tempFile: tempFilePaths
        // })

        for (let path of res.tempFilePaths) {
          wx.uploadFile({
            url: api.get_uploadImage,
            filePath: path,
            name: 'file',
            header: {
              'Content-Type': 'multipart/form-data',
              'X-Requested-With': 'XMLHttpRequest',
              'user-token': wx.getStorageSync('user_token'),
            },
            formData: {
              "type": 'customform'
            },
            success: (res) => {
              let image_data = res.data;
              let image_src = JSON.parse(image_data);
              that.setData({
                tempFile: image_src.data.src
              })
            }
          })
        }
      }
    })
  },
  clearShopInfo: function() {
    const that = this;
    that.setData({
      showTopTips: false,
      shopcode: '',
      shopname: '',
      shopsaleprice: '0',
      shopchengbenprice: '0',
      stocksum: '1',
      goodsskulist: '',
      skuid: '',
      formData: {

      }
    })
  },
  //扫码条形码
  sanstock: function() {
    const that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        //console.log(res.result);
        // wx.navigateTo({
        //   url: '/packageThirst/pages/cacher/addstock/index?shopcode=' + res.result
        // })
        that.clearShopInfo();
        that.setData({
          shopcode: res.result
        })
        //根据条形码获取平台的商品数据
        that.getgoodsbycode(res.result)
      }
    })
  },
  //根据条形码获取平台的商品信息
  getgoodsbycode: function(code) {
    wx.showLoading({
      title: '加载中',
    })
    const that = this;
    let postData = {
      'shopcode': code
    }
    let datainfo = requestSign.requestSign(postData);
    header.sign = datainfo
    wx.request({
      url: api.get_goodsbycode,
      data: postData,
      header: header,
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {

        if (res.data.code >= 0) {
          // console.log(res.data.goodslist.goods_name)

          that.setData({
            shopname: res.data.goodslist.goods_name,
            shopsaleprice: res.data.goodslist.price,
            shopchengbenprice: res.data.goodslist.cost_price,
            goodsskulist: res.data.goodsskulist,
            tempFile: res.data.goodslist.localpicurl ? res.data.goodslist.localpicurl : that.data.defaultImage,
            istiaoxingmaedit: 'disabled'
          })
        }
        wx.hideLoading()
      },
      fail: (res) => {},
    })
  },
  radioChange: function(e) {
    const that = this;
    var goodsskulist = this.data.goodsskulist;
    if (goodsskulist) {
      var saleprice = goodsskulist[e.detail.value].price;
      var chengbenprice = goodsskulist[e.detail.value].cost_price;
      var sku_id = goodsskulist[e.detail.value].sku_id;
      that.setData({
        shopsaleprice: saleprice,
        shopchengbenprice: chengbenprice,
        skuid: sku_id
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    stockTable = getApp().globalData.stockTable;
    const that = this;
    console.log(options.shopcode);
    if (options.shopcode) {
      that.setData({
        shopcode: options.shopcode
      })
      that.getgoodsbycode(options.shopcode);
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})