// pages/operation/index.js
const config = require('../../config/config.js')
const Http = require('../../utils/HttpBasics.js')
const util = require('../../utils/util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDetailIf: true,
    receiptUrl:null,
    type:null,
    paramData:'',
    array: ['美国', '中国', '巴西', '日本'],
    merchentList:[],
    spendStr:null,
    creditNum:0,
    submitAble:true,
    shoesList:[],
    clothList:[],
    business:'',
    selectTag:null,
    selectTag1:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,7777)
    console.log(options.business)
    this.setData({
      businessId: options.businessId ? options.businessId :'' ,
      type: options.type,
      isAdmin: options.isAdmin ? options.isAdmin:0,
      paramData:options,
      flag: "cloth",
    })
    this.getMerchent();
    this.getTag()
  },
  getTag() {
    let that = this;
    Http.get({
      url: config.api.getTag
    })
      .then(res => {
        console.log(res.data.data)
        that.setData({
          shoesList: res.data.data['28'],
          clothList: res.data.data['27'],
        })
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  //切换衣服尺码
  getCloth() {
    this.setData({
      flag: "cloth",
    })
  },
  //切换鞋子尺码
  getShoes() {
    this.setData({
      flag: "shoes",
    })
  },
  //点击选中衣服鞋子尺码
  checkTags(e) {
    console.log(e)
    let id = e.target.id;
    if (this.data.flag == "cloth"){
      this.data.clothList.map((item, index) => {
        if (id == item.id) {
          this.setData({
            selectTag: item.id,
          })
        }
      })
    }else{
      this.data.shoesList.map((item, index) => {
        if (id == item.id) {
          this.setData({
            selectTag1: item.id,
          })
        }
      })
    }
  },
  
  /**
   * 跳转门店选择页面
   */
  checkMerchant(){
    wx.navigateTo({
      url: `/pages/getMerchant/index?address=${this.data.paramData.address}&avatarUrl=${this.data.paramData.avatarUrl}&id=${this.data.paramData.id}&levelName=${this.data.paramData.levelName}&name=${this.data.paramData.name}&nickName=${this.data.paramData.nickName}&phone=${this.data.paramData.phone}&score=${this.data.paramData.score}&credit=${this.data.paramData.credit}&type=1`,
    })
  },
  /**
   * 保存积分操作数据
   */
  submitForm(){
    let _this=this;
    if (_this.data.receiptUrl == null || !_this.data.receiptUrl){
      wx.showToast({
        title: '请先上传小票照片！',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    const reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
    if (!reg.test(_this.data.spendStr) || Number(_this.data.spendStr) > 9999999.99 || Number(_this.data.spendStr)<=0) {
      wx.showToast({
        title: '消费金额限制为大于0且小于9999999.99的保留两位小数的数字！',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    /**
     * 判断服务台操作选择店铺
     */
    if (_this.data.paramData.isAdmin=='1'&&!_this.data.paramData.merchantType && _this.data.paramData.isAdmin == 1) {
      wx.showToast({
        title: '请选择消费店铺！',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    if (_this.data.creditNum <= 0 || !_this.data.creditNum){
      wx.showToast({
        title: '本业态消费无积分！',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    _this.setData({
      submitAble: false
    })
    Http.post({
      url: config.api.addCredit,
      data: {
        spendStr: _this.data.spendStr,
        creditType: 10,//新增积分
        cuserId: _this.data.paramData.id,
        operatorType: 3,
        receiptUrl: _this.data.receiptUrl,
        merchantId: _this.data.paramData.merchant_id,
        tags: [_this.data.selectTag, _this.data.selectTag1]
      }
    })
    .then(res => {
      this.setData({
        submitAble:false
      })
      wx.showToast({
        title: '保存成功！',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      wx.navigateTo({
        url: '/pages/editSuccess/index?type=1&creditNum=' + res.data.creditNum + '&creditAmount='+res.data.creditAmount,
      })
      _this.setData({
        merchentList: res.data,
        submitAble: true
      })
    })
    .catch(err => {
      _this.setData({
        submitAble: true
      })
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    });
  },
  getPrice(e){
    let _this=this;
    this.setData({
      spendStr:e.detail.value
    })
    const reg = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
    if ((!reg.test(e.detail.value) && e.detail.value != "")||Number(_this.data.spendStr) > 9999999.99 || Number(_this.data.spendStr) <= 0) {
      wx.showToast({
        title: "消费金额限制为大于0且小于9999999.99的保留两位小数的数字！",
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    Http.get({
      url: config.api.getCreditNum,
      data:{
        spendStr: e.detail.value,
        merchantId: _this.data.paramData.merchant_id,
        userId:_this.data.paramData.id
      }
    })
    .then(res => {
      _this.setData({
        creditNum: res.data.credit
      })
    })
    .catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    });
  },
  getMerchent(){
    let _this=this;
    Http.get({
      url: config.api.getMerchentList
    })
    .then(res => {
      _this.setData({
        merchentList: res.data
      })
    })
    .catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    });
  },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
    })
  },
  uploadImg(){ 
    let that=this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: config.api.imgUpload, // 仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'token': app.globalData.token
          },
          success(res) {
            const data = res.data
            that.setData({
              receiptUrl:JSON.parse(res.data).data.url
            })
            // do something
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({
    //   receiptUrl: null
    // })
    this.setData({
      submitAble: true
    })
  },

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

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

  },

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

  },

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

  }
})