import {
  setShoppingCart,
  getShoppingCart,
  _GLOBAL_UI_DATA_KEY
} from '../../utils/index'
import { loadEnd, loadData } from '../../utils/index'
import { __FAIL_FN } from '../../utils/network'
const baseUrl = require('../../api/index.js').config
const masses = require('../../api/masses.js')
const store = require('../../api/store.js')
const order = require('../../api/order.js')
const jieliu = require('../../utils/throttle.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bannerimg: '',
    bannerid: '',
    foodlist: [],
    uidata: {},
    uiList: {},
    cartList: [],
    scrollHeight: 0,
    // 登录授权
    anflag: false,
    // 多属性
    attributelisting: [],
    // 购物车信息
    showcard: false,
    cartList: [],
    hasList: false, // 列表是否有数据
    totalPrice: 0, // 总价，初始为0
    totalNum: 0, //总数，初始为0
    packageprice: 0, //餐盒费

    //商品详情
    oneimage: [
      'https://ybm-public.oss-cn-beijing.aliyuncs.com/image/nophoto.png'
    ],
    catchflag: true,
    cardflag: false,
    disappear: false,
    fooddetail: [],
    skulist: [],
    foodintroduction: '',
    foodnickname: '',
    foodsalse: '',
    skuindex: 0,
    moreorder: [],
    testlable: 0,
    //满减活动
    reducelist: [],
    reductindex: '',
    reductprice: '',
    reducejian: [],
    manjianflag: false,
    readyflag: true,
    readyindex: '',
    promotionPrice: '',
    promotionflag: false,
    hope: true,
    finaflag: true,
    // 门店信息
    storelist: [],
    inpuVal: '',
    limitDiscount: [],
    astrict: 0,
    copyastrict: 0,
    allowancenum: 0,
    specsnum: 0,
    searchflag: true,
    // 是否请求过
    ifSearched: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    let storeid = wx.getStorageSync('storeid')
    const merchantId = wx.getStorageSync('merchantid')
    that.setData({
      bannerimg: options.img,
      bannerid: options.bannerid,
      uidata: JSON.parse(options.uidata),
      storeCartKey: `cart_${merchantId}_${storeid}`
    })
    that.carryReduction()
    that.carryDeliery()
    that.takeStoredetail()
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.screenHeight - 160
        })
      }
    })
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 授权显示
    var that = this
    that.setData({
      anflag: wx.getStorageSync('anflag')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    const { uidata, storeCartKey } = this.data
    setShoppingCart(storeCartKey, uidata, _GLOBAL_UI_DATA_KEY)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    const { uidata, storeCartKey } = this.data
    setShoppingCart(storeCartKey, uidata, _GLOBAL_UI_DATA_KEY)
  },

  inputValue: function (e) {
    var that = this
    this.setData({
      inputVal: e.detail
    })
  },
  // 搜索菜品
  takeSearchfood: function () {
    var that = this
    const merchantid = wx.getStorageSync('merchantid') || 14092020102252
    const storeid = wx.getStorageSync('storeid')
    masses.getSearchfood(
      baseUrl.waimaiUrl +
        `/client/merchants/${merchantid}/stores/${storeid}/search-food`,
      {
        foodName: that.data.inputVal
      },
      {},
      '加载中',
      function (res) {
        var result = res.results
        for (var j = 0; j < result.storeFoodDtoList.length; j++) {
          if (result.storeFoodDtoList[j].moreSku == false) {
            if (result.storeFoodDtoList[j].storeFoodSkuDtoList[0].stock == 0) {
              result.storeFoodDtoList[j].sellout = false
            } else {
              result.storeFoodDtoList[j].sellout = true
            }
          } else {
            result.storeFoodDtoList[j].sellout = true
          }
        }
        that.setData({
          foodlist: result.storeFoodDtoList,
          categoryIds: result.uiLayer,
          limitDiscount: result.discountFoodActRsp,
          ifSearched: true
        })
        that.onShow()
      },
      function (err) {
        __FAIL_FN({
          res: err
        })
      }
    )
  },
  codingadd: jieliu.throttle(function (e) {
    var that = this
    const { comp } = e.currentTarget.dataset
    if (comp) e = e.detail
    var id = e.currentTarget.dataset.id
    var catgid = e.currentTarget.dataset.catgid
    var uiList = that.data.categoryIds
    var obj2 = that.data.uidata
    var arr = getShoppingCart(this.data.storeCartKey) || []
    var attributerealid = e.currentTarget.dataset.attributerealid
    var attrid = ''
    // 限时购的添加商品判断
    var foodtype = e.currentTarget.dataset.foodtype
    if (arr == '') {
      arr = []
    }
    if (attributerealid == undefined) {
      attrid = id
    } else {
      attrid = attributerealid
    }
    // attrid=id;
    // 判断key是否存在
    // 判断商品是否存在
    if (obj2.hasOwnProperty(catgid)) {
      if (obj2[catgid].hasOwnProperty(attrid)) {
        //判断折扣的总限制数
        if (that.data.limitDiscount == null) {
          if (obj2[catgid][attrid].num < obj2[catgid][attrid].stock) {
            // 未超过折扣的菜品的限制
            if (
              (obj2[catgid][attrid].num < obj2[catgid][attrid].limitBuyNumber ||
                obj2[catgid][attrid].limitBuyNumber == 0) &&
              obj2[catgid][attrid].foodType == 2
            ) {
              that.data.astrict += 1
              obj2[catgid][attrid].num += 1
              obj2[catgid].num += 1
              for (var k = 0; k < arr.length; k++) {
                if (arr[k].attributeid == attrid) {
                  arr[k].num = obj2[catgid][attrid].num
                }
              }
            } else if (
              (obj2[catgid][attrid].num >=
                obj2[catgid][attrid].limitBuyNumber ||
                obj2[catgid][attrid].limitBuyNumber != 0) &&
              obj2[catgid][attrid].foodType == 2
            ) {
              obj2[catgid][attrid].commonNum += 1
              obj2[catgid][attrid].num += 1
              obj2[catgid].num += 1
              for (var k = 0; k < arr.length; k++) {
                if (arr[k].attributeid == attrid) {
                  arr[k].commonNum = obj2[catgid][attrid].commonNum
                  arr[k].num = obj2[catgid][attrid].num
                  arr[k].rebateflag = true
                }
              }
              obj2[catgid][attrid].rebateflag = true
              wx.showToast({
                title: `折扣菜品限购${obj2[catgid][attrid].limitBuyNumber}份，超过${obj2[catgid][attrid].limitBuyNumber}份恢复原价`,
                icon: 'none',
                duration: 2000
              })
            } else if (obj2[catgid][attrid].foodType == 1) {
              obj2[catgid][attrid].num += 1
              obj2[catgid].num += 1
              for (var k = 0; k < arr.length; k++) {
                if (arr[k].attributeid == attrid) {
                  arr[k].num = obj2[catgid][attrid].num
                }
              }
            }
          } else {
            // 库存数
            obj2[catgid][attrid].stockFlag = false
            if (obj2[catgid][attrid].num >= obj2[catgid][attrid].stock) {
              wx.showToast({
                title: '库存不足',
                icon: 'none'
              })
            }
          }
        } else {
          if (
            obj2[catgid][attrid].num < obj2[catgid][attrid].stock &&
            (that.data.limitDiscount.limitNum > that.data.astrict ||
              !that.data.limitDiscount.limitStatus ||
              that.data.limitDiscount == null)
          ) {
            // 未超过折扣的菜品的限制
            if (
              (obj2[catgid][attrid].num < obj2[catgid][attrid].limitBuyNumber ||
                obj2[catgid][attrid].limitBuyNumber == 0) &&
              obj2[catgid][attrid].foodType == 2
            ) {
              that.data.astrict += 1
              obj2[catgid][attrid].num += 1
              obj2[catgid].num += 1
              for (var k = 0; k < arr.length; k++) {
                if (arr[k].attributeid == attrid) {
                  arr[k].num = obj2[catgid][attrid].num
                }
              }
            } else if (
              obj2[catgid][attrid].num >= obj2[catgid][attrid].limitBuyNumber &&
              obj2[catgid][attrid].limitBuyNumber != 0 &&
              obj2[catgid][attrid].foodType == 2
            ) {
              obj2[catgid][attrid].commonNum += 1
              obj2[catgid][attrid].num += 1
              obj2[catgid].num += 1
              for (var k = 0; k < arr.length; k++) {
                if (arr[k].attributeid == attrid) {
                  arr[k].commonNum = obj2[catgid][attrid].commonNum
                  arr[k].num = obj2[catgid][attrid].num
                  arr[k].rebateflag = true
                }
              }
              obj2[catgid][attrid].rebateflag = true
              wx.showToast({
                title: `折扣菜品限购${obj2[catgid][attrid].limitBuyNumber}份，超过${obj2[catgid][attrid].limitBuyNumber}份恢复原价`,
                icon: 'none',
                duration: 2000
              })
            } else if (obj2[catgid][attrid].foodType == 1) {
              obj2[catgid][attrid].num += 1
              obj2[catgid].num += 1
              for (var k = 0; k < arr.length; k++) {
                if (arr[k].attributeid == attrid) {
                  arr[k].num = obj2[catgid][attrid].num
                }
              }
            }
          } else {
            // 总限制数
            obj2[catgid][attrid].stockFlag = false
            if (obj2[catgid][attrid].num >= obj2[catgid][attrid].stock) {
              wx.showToast({
                title: '库存不足',
                icon: 'none'
              })
            } else if (
              that.data.limitDiscount.limitNum <= that.data.astrict &&
              that.data.limitDiscount.limitStatus
            ) {
              obj2[catgid][attrid].commonNum += 1
              obj2[catgid][attrid].num += 1
              obj2[catgid].num += 1
              for (var k = 0; k < arr.length; k++) {
                if (arr[k].attributeid == attrid) {
                  arr[k].num = obj2[catgid][attrid].num
                  arr[k].commonNum = obj2[catgid][attrid].commonNum
                  arr[k].rebateflag = true
                }
              }
              obj2[catgid][attrid].rebateflag = true
              wx.showToast({
                title: `每单的折扣菜品限购${that.data.limitDiscount.limitNum}份，超过${that.data.limitDiscount.limitNum}份恢复原价`,
                icon: 'none',
                duration: 2000
              })
            }
          }
        }
        // 独一次
      } else {
        if (foodtype == 1) {
          attrid = id + '_' + foodtype
          obj2[catgid][attrid] = {}
          obj2[catgid][attrid] = JSON.parse(JSON.stringify(uiList[catgid][id]))
          obj2[catgid][attrid].attributeid = attrid
          obj2[catgid][attrid].num += obj2[catgid][attrid].startNum
          obj2[catgid].num += obj2[catgid][attrid].startNum
          arr.push(obj2[catgid][attrid])
        } else if (foodtype == 2) {
          attrid = id + '_' + foodtype
          obj2[catgid][attrid] = {}
          obj2[catgid][attrid] = JSON.parse(JSON.stringify(uiList[catgid][id]))
          obj2[catgid][attrid].attributeid = attrid
          obj2[catgid][attrid].num += obj2[catgid][attrid].startNum
          obj2[catgid].num += obj2[catgid][attrid].startNum
          that.data.astrict += obj2[catgid][attrid].startNum
          if (
            that.data.limitDiscount.limitNum < that.data.astrict &&
            that.data.limitDiscount.limitStatus
          ) {
            // 普通
            // that.data.astrict -= obj2[catgid][attrid].startNum;
            // obj2[catgid][attrid].commonNum += obj2[catgid][attrid].startNum;
            // 折扣菜品的起购份数
            obj2[catgid][attrid].commonNum =
              that.data.astrict - that.data.limitDiscount.limitNum
            that.data.astrict = that.data.limitDiscount.limitNum
            obj2[catgid][attrid].rebateflag = true
            wx.showToast({
              title: `每单的折扣菜品限购${that.data.limitDiscount.limitNum}份，超过${that.data.limitDiscount.limitNum}份恢复原价`,
              icon: 'none',
              duration: 2000
            })
          } else if (
            obj2[catgid][attrid].num > obj2[catgid][attrid].limitBuyNumber &&
            obj2[catgid][attrid].limitBuyNumber != 0
          ) {
            //TODO折扣菜品
            that.data.astrict -= obj2[catgid][attrid].startNum
            that.data.astrict += obj2[catgid][attrid].limitBuyNumber
            obj2[catgid][attrid].commonNum =
              obj2[catgid][attrid].num - obj2[catgid][attrid].limitBuyNumber
            obj2[catgid][attrid].rebateflag = true
            wx.showToast({
              title: `折扣菜品限购${obj2[catgid][attrid].limitBuyNumber}份，超过${obj2[catgid][attrid].limitBuyNumber}份恢复原价`,
              icon: 'none',
              duration: 2000
            })
          } else {
            obj2[catgid][attrid].rebateflag = false
          }
          arr.push(obj2[catgid][attrid])
        }
      }
    } else {
      if (foodtype == 1) {
        attrid = id + '_' + foodtype
        obj2[catgid] = {}
        obj2[catgid][attrid] = {}
        obj2[catgid][attrid] = JSON.parse(JSON.stringify(uiList[catgid][id]))
        obj2[catgid][attrid].attributeid = attrid
        obj2[catgid][attrid].num += obj2[catgid][attrid].startNum
        obj2[catgid].num = obj2[catgid][attrid].startNum
        arr.push(obj2[catgid][attrid])
      } else if (foodtype == 2) {
        attrid = id + '_' + foodtype
        obj2[catgid] = {}
        obj2[catgid][attrid] = {}
        obj2[catgid][attrid] = JSON.parse(JSON.stringify(uiList[catgid][id]))
        obj2[catgid][attrid].attributeid = attrid
        obj2[catgid][attrid].num += obj2[catgid][attrid].startNum
        obj2[catgid].num = obj2[catgid][attrid].startNum
        that.data.astrict += obj2[catgid][attrid].startNum
        if (
          that.data.limitDiscount.limitNum < that.data.astrict &&
          that.data.limitDiscount.limitStatus
        ) {
          // that.data.astrict -= obj2[catgid][attrid].startNum;
          // obj2[catgid][attrid].commonNum += obj2[catgid][attrid].startNum;
          // 折扣菜品的起购份数
          obj2[catgid][attrid].commonNum =
            that.data.astrict - that.data.limitDiscount.limitNum
          that.data.astrict = that.data.limitDiscount.limitNum
          obj2[catgid][attrid].rebateflag = true
          wx.showToast({
            title: `每单的折扣菜品限购${that.data.limitDiscount.limitNum}份，超过${that.data.limitDiscount.limitNum}份恢复原价`,
            icon: 'none',
            duration: 2000
          })
        } else if (
          obj2[catgid][attrid].num > obj2[catgid][attrid].limitBuyNumber &&
          obj2[catgid][attrid].limitBuyNumber != 0
        ) {
          //TODO折扣菜品
          that.data.astrict -= obj2[catgid][attrid].startNum
          that.data.astrict += obj2[catgid][attrid].limitBuyNumber
          obj2[catgid][attrid].commonNum =
            obj2[catgid][attrid].num - obj2[catgid][attrid].limitBuyNumber
          obj2[catgid][attrid].rebateflag = true
          wx.showToast({
            title: `折扣菜品限购${obj2[catgid][attrid].limitBuyNumber}份，超过${obj2[catgid][attrid].limitBuyNumber}份恢复原价`,
            icon: 'none',
            duration: 2000
          })
        } else {
          obj2[catgid][attrid].rebateflag = false
        }
        arr.push(obj2[catgid][attrid])
      }
    }
    that.setData({
      uidata: obj2,
      cartList: arr,
      cardflag: true,
      astrict: that.data.astrict
    })
    wx.setStorageSync('bannerfoodflag', 'agree')
    setShoppingCart(
      that.data.storeCartKey,
      that.data.uidata,
      _GLOBAL_UI_DATA_KEY
    )
    setShoppingCart(this.data.storeCartKey, arr)
    that.getTotalPrice()
  }, 280),
  // 新修改减
  codingminus: jieliu.throttle(function (e) {
    var that = this
    const { comp } = e.currentTarget.dataset
    if (comp) e = e.detail
    var index = e.currentTarget.dataset.index
    var parentIndex = e.currentTarget.dataset.parentindex
    var id = e.currentTarget.dataset.id
    var flag = e.currentTarget.dataset.dis
    var arr = getShoppingCart(this.data.storeCartKey) || []
    var catgid = e.currentTarget.dataset.catgid
    var attrid = e.currentTarget.dataset.attrid
    var uiList = that.data.categoryIds
    var obj1 = that.data.uidata
    var attributerealid = e.currentTarget.dataset.attributerealid
    // 限时购的添加商品判断
    var foodtype = e.currentTarget.dataset.foodtype
    attrid = attributerealid
    if (obj1[catgid][attrid].num == obj1[catgid][attrid].startNum) {
      obj1[catgid][attrid].num -= obj1[catgid][attrid].startNum
      obj1[catgid].num -= obj1[catgid][attrid].startNum
    } else {
      obj1[catgid][attrid].num -= 1
      obj1[catgid].num -= 1
    }
    // obj1[catgid][attrid].num -= 1;
    // obj1[catgid].num -= 1;
    if (obj1[catgid][attrid].rebateflag) {
      // obj1[catgid][attrid].commonNum = obj1[catgid][attrid].num - obj1[catgid][attrid].limitBuyNumber;
      // TODO折扣菜品的起购数量
      // obj1[catgid][attrid].commonNum -= obj1[catgid][attrid].startNum;
      if (obj1[catgid][attrid].num == 0) {
        obj1[catgid][attrid].commonNum -= obj1[catgid][attrid].startNum
      } else {
        obj1[catgid][attrid].commonNum -= 1
      }
      if (obj1[catgid][attrid].commonNum <= 0) {
        obj1[catgid][attrid].commonNum = 0
      }
    }
    if (
      obj1[catgid][attrid].foodType == 2 &&
      obj1[catgid][attrid].commonNum == 0 &&
      that.data.limitDiscount.limitNum <= that.data.astrict
    ) {
      for (let key in obj1[catgid]) {
        console.log(obj1[catgid][key])
        if (obj1[catgid][key].foodType == 2) {
          if (
            obj1[catgid][key].num - obj1[catgid][key].commonNum <
              obj1[catgid][key].limitBuyNumber &&
            that.data.limitDiscount.limitStatus &&
            obj1[catgid][key].commonNum > 0
          ) {
            // obj1[catgid][key].num+=1;
            // TODO折扣菜品的起购份数的增减
            obj1[catgid][key].commonNum -= obj1[catgid][attrid].startNum
            // that.data.astrict+=1;
            if (obj1[catgid][key].commonNum <= 0) {
              obj1[catgid][key].rebateflag = false
              obj1[catgid][key].commonNum = 0
            }
            break
          }
        }
      }
    }
    for (var i = 0; i < arr.length; i++) {
      if (attributerealid == arr[i].attributeid) {
        if (arr[i].num == arr[i].startNum) {
          // 起购量
          arr[i].num -= arr[i].startNum
        } else {
          arr[i].num -= 1
        }

        // 折扣
        if (
          arr[i].foodType == 2 &&
          that.data.limitDiscount.limitNum >= that.data.astrict &&
          that.data.limitDiscount.limitStatus &&
          arr[i].commonNum == 0
        ) {
          for (var p = 0; p < arr.length; p++) {
            if (
              arr[p].num - arr[p].commonNum < arr[p].limitBuyNumber &&
              arr[p].commonNum > 0
            ) {
              // TODO折扣菜品的起购份数
              arr[p].commonNum -= arr[p].startNum
              // that.data.astrict += 1;
              if (arr[p].commonNum <= 0) {
                arr[p].rebateflag = false
                arr[p].commonNum == 0
              }
              break
            }
          }
        } else if (
          arr[i].num < obj1[catgid][attrid].limitBuyNumber &&
          arr[i].foodType == 2
        ) {
          // TODO折扣菜品
          if (arr[i].num == 0) {
            arr[i].commonNum -= arr[i].startNum
          } else {
            arr[i].commonNum -= 1
          }
          if (
            that.data.limitDiscount.limitNum < that.data.astrict &&
            that.data.limitDiscount.limitStatus
          ) {
            arr[i].rebateflag = false
          }
          if (arr[i].commonNum <= 0) {
            arr[i].commonNum = 0
          }
        } else if (
          arr[i].num >= obj1[catgid][attrid].limitBuyNumber &&
          arr[i].foodType == 2
        ) {
          // arr[i].commonNum = arr[i].num - arr[i].limitBuyNumber;
          //TODO折扣菜品
          if (arr[i].num == 0) {
            arr[i].commonNum -= arr[i].startNum
          } else {
            arr[i].commonNum -= 1
          }
          if (arr[i].commonNum <= 0) {
            arr[i].commonNum = 0
            // 普通折扣
            arr[i].rebateflag = false
          }
        }
      }
    }
    if (
      obj1[catgid][attrid].foodType == 2 &&
      !obj1[catgid][attrid].rebateflag &&
      obj1[catgid][attrid].num >= 0
    ) {
      //TODO折扣菜品的起购份数
      if (obj1[catgid][attrid].num == 0) {
        that.data.astrict -= obj1[catgid][attrid].startNum
      } else {
        that.data.astrict -= 1
      }
    }
    if (obj1[catgid][attrid].num < obj1[catgid][attrid].stock) {
      obj1[catgid][attrid].stockFlag = true
    }
    if (
      obj1[catgid][attrid].num <= obj1[catgid][attrid].limitBuyNumber &&
      obj1[catgid][attrid].num
    ) {
      obj1[catgid][attrid].stockFlag = true
      obj1[catgid][attrid].rebateflag = false
    }
    if (obj1[catgid][attrid].num <= 0) {
      obj1[catgid][attrid].num = 0
      delete obj1[catgid][attrid]
      for (var i = 0; i < arr.length; i++) {
        if (attributerealid == arr[i].attributeid && arr[i].num == 0) {
          arr.splice(i, 1)
        }
        if (arr.length <= 0) {
          that.setData({
            cartList: [],
            totalNum: 0,
            totalPrice: 0,
            showcard: false,
            cardflag: false
          })
        }
      }
    }
    if (obj1[catgid].num <= 0) {
      obj1[catgid].num = 0
      delete obj1[catgid]
    }

    that.setData({
      uidata: obj1,
      cartList: arr,
      astrict: that.data.astrict
    })
    wx.setStorageSync('bannerfoodflag', 'agree')
    setShoppingCart(
      that.data.storeCartKey,
      that.data.uidata,
      _GLOBAL_UI_DATA_KEY
    )
    setShoppingCart(this.data.storeCartKey, arr)
    that.getTotalPrice()
  }, 280),
  // 获取购物车总价、总数
  getTotalPrice: function () {
    var that = this
    var cartList = that.data.cartList // 获取购物车列表.
    var totalP = 0 //总价
    // var specifictotal=0;
    var totalN = 0
    var packagelist = 0
    var ftest = true
    var fpromotion = true
    var shopreadyflag = false
    var shopfinaflag = false
    var shophope = false
    var shopreductindex = 0
    var shoppromotionPrice = 0
    var shopdisappear = false
    var shopreductprice = 0
    var shopreadyindex = 0
    var shoppaidflag = false
    for (var i in cartList) {
      // 循环列表得到每个数据
      // totalP += cartList[i].num * cartList[i].price + cartList[i].packagePrice * cartList[i].num; // 所有价格加起来
      totalN += cartList[i].num
      packagelist += cartList[i].packagePrice * cartList[i].num
      if (cartList[i].foodType == 2) {
        ftest = false
        fpromotion = false
      }
      // 新叠加
      if (cartList[i].foodType == 2 && cartList[i].rebateflag) {
        totalP +=
          (cartList[i].num - cartList[i].commonNum) *
            cartList[i].discountPrice +
          cartList[i].commonNum * cartList[i].price +
          cartList[i].packagePrice * cartList[i].num
        cartList[i].specifictotal = (
          (cartList[i].num - cartList[i].commonNum) *
            cartList[i].discountPrice +
          cartList[i].commonNum * cartList[i].price
        ).toFixed(2)
      } else if (cartList[i].foodType == 2 && !cartList[i].rebateflag) {
        totalP +=
          cartList[i].discountPrice * cartList[i].num +
          cartList[i].packagePrice * cartList[i].num
        cartList[i].specifictotal = (
          cartList[i].num * cartList[i].discountPrice
        ).toFixed(2)
      } else if (cartList[i].foodType == 1) {
        totalP +=
          cartList[i].num * cartList[i].price +
          cartList[i].packagePrice * cartList[i].num
        cartList[i].specifictotal = (
          cartList[i].num * cartList[i].price
        ).toFixed(2)
      }
    }
    if (ftest) {
      if (that.data.reducelist.length > 0) {
        for (var j = 0; j < that.data.reducelist.length; j++) {
          if (totalP >= that.data.reducelist[j].overflow) {
            var proprice = totalP - that.data.reducelist[j].reduction
            if (
              totalP >=
              that.data.reducelist[that.data.reducelist.length - 1].overflow
            ) {
              var propricelist =
                totalP -
                that.data.reducelist[that.data.reducelist.length - 1].reduction
              shopreadyflag = true
              shopfinaflag = false
              shophope = true
              shopreductindex = that.data.reducelist.length - 1
              shoppromotionPrice = propricelist.toFixed(2)
              shopdisappear = true
              break
            }
            shopreductprice = 0
            shopreductindex = j
            shopreadyindex = j
            shopreadyflag = false
            fpromotion = true
            shopdisappear = true
            shopfinaflag = true
            shoppromotionPrice = proprice.toFixed(2)
          } else {
            var price =
              parseFloat(that.data.reducelist[j].overflow) - parseFloat(totalP)
            shopreductindex = j
            shopreductprice = price.toFixed(2)
            shopdisappear = true
            shophope = false
            shopfinaflag = true
            if (that.data.reducelist[0].overflow > totalP) {
              shopreadyflag = true
              fpromotion = false
            }

            break
          }
        }
      } else {
        // 没有满减
        fpromotion = false
      }
    } else {
      shopdisappear = false
    }

    // 起送价判断
    if (that.data.delivery) {
      //外卖
      //是达到满减
      if (fpromotion) {
        if (shoppromotionPrice < that.data.storelist.startSalePrice) {
          shoppaidflag = false
        } else {
          shoppaidflag = true
          wx.setStorageSync('lunchbox', shoppromotionPrice)
        }
      } else {
        // 正常价格
        if (totalP < that.data.storelist.startSalePrice) {
          shoppaidflag = false
        } else {
          shoppaidflag = true
          wx.setStorageSync('lunchbox', totalP)
        }
      }
    } else {
      //自提
      //是达到满减
      if (that.data.fpromotion) {
        if (shoppromotionPrice < that.data.storelist.startOrderPrice) {
          shoppaidflag = false
        } else {
          shoppaidflag = true
          wx.setStorageSync('lunchbox', shoppromotionPrice)
        }
      } else {
        // 正常价格
        if (totalP < that.data.storelist.startOrderPrice) {
          shoppaidflag = false
        } else {
          shoppaidflag = true
          wx.setStorageSync('lunchbox', totalP)
        }
      }
    }
    that.setData({
      // 最后赋值到data中渲染到页面
      cartList: cartList,
      totalNum: totalN,
      totalPrice: totalP.toFixed(2),
      packageprice: packagelist,
      promotionflag: fpromotion,
      readyflag: shopreadyflag,
      finaflag: shopfinaflag,
      hope: shophope,
      reductindex: shopreductindex,
      promotionPrice: shoppromotionPrice,
      disappear: shopdisappear,
      reductprice: shopreductprice,
      readyindex: shopreadyindex,
      paidflag: shoppaidflag
    })
    that.carryDelierylist()
  },
  // 定义根据id删除数组的方法
  removeByValue: function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i].id == val) {
        array.splice(i, 1)
        break
      }
    }
  },
  cleanList: function (e) {
    var that = this
    var cleans = {}
    try {
      setShoppingCart(this.data.storeCartKey, '')
      wx.setStorageSync('orderflag', '')
      wx.setStorageSync('foodflag', '')
    } catch (e) {
      console.error(e)
    }
    that.setData({
      cartList: [],
      cardflag: false,
      totalNum: 0,
      totalPrice: 0,
      showcard: false,
      readyflag: true,
      hope: true,
      finaflag: true,
      promotionflag: false,
      uidata: cleans,
      astrict: 0
    })
    console.log(that.data.cartList)
    // this.cascadeDismiss()
  },
  // 门店详情
  takeStoredetail: function () {
    var that = this
    var merchantid = wx.getStorageSync('merchantid')
    let lat = wx.getStorageSync('latitude')
    let lon = wx.getStorageSync('longitude')
    let storeid = wx.getStorageSync('storeid')
    masses.getFeedbackstore(
      baseUrl.waimaiUrl +
        '/client/merchants/' +
        merchantid +
        '/stores/' +
        storeid,
      {
        latitude: lat,
        longitude: lon
      },
      {},
      '加载中',
      function (res) {
        var result = res.results
        var splitbusiness = result.businessLicenseImgs.split(',')
        var splitmanageLicense = result.manageLicenseImags.split(',')
        that.setData({
          storelist: result,
          businessLicenseImgs: splitbusiness,
          manageLicenseImags: splitmanageLicense
        })
      },
      function (err) {
        __FAIL_FN({
          res: err
        })
      }
    )
  },
  // 满减
  carryReduction: function () {
    var that = this
    var ordertype = ''
    if (that.data.delivery == true) {
      ordertype = 1
    } else {
      ordertype = 2
    }
    var storeid = wx.getStorageSync('storeid')
    store.getReduction(
      baseUrl.waimaiUrl + '/client/' + storeid + '/overflow-reduction',
      {
        orderType: ordertype
      },
      {},
      '加载中',
      function (res) {
        var test = []
        for (var i = 0; i < res.results.length; i++) {
          if (i > 2) {
            break
          }
          test.push(res.results[i])
        }
        that.setData({
          reducejian: test,
          reducelist: res.results
        })
      },
      function (err) {
        __FAIL_FN({
          res: err
        })
      }
    )
  },
  // 限时配送费
  carryDeliery: function () {
    var that = this
    var storeid = wx.getStorageSync('storeid')
    order.takeReductiondelivery(
      baseUrl.waimaiUrl + '/client/' + storeid + '/reduce-delivery',
      {},
      {},
      '',
      function (res) {
        var result = res.results
        if (result) {
          that.setData({
            feelist: result,
            feeflag: true,
            feetrueflag: true
          })
        } else {
          that.setData({
            feelist: result,
            feeflag: false,
            feetrueflag: false
          })
        }
      },
      function (err) {
        __FAIL_FN({
          res: err
        })
      }
    )
  },
  carryDelierylist: function () {
    var that = this
    var result = that.data.feelist
    if (result) {
      // 有无门槛
      if (result.noThresholdFlag) {
        // 无门槛
        // 全免标志位
        if (result.allReduceFlag) {
          //全免
          that.setData({
            feeprice: 0,
            feeflag: true
          })
        } else {
          // 限时减免
          var price = that.data.storelist.deliveryPrice - result.reduceFee
          if (price <= 0) {
            that.setData({
              feeprice: 0,
              feeflag: true
            })
          } else {
            that.setData({
              feeprice: price,
              feeflag: true
            })
          }
        }
      } else {
        // 有门槛
        // 满减
        if (that.data.promotionPrice == '') {
          if (that.data.totalPrice >= result.threshold) {
            // 全免标志位
            if (result.allReduceFlag) {
              //全免
              that.setData({
                feeprice: 0,
                feeflag: true
              })
            } else {
              // 限时减免
              var price = that.data.storelist.deliveryPrice - result.reduceFee
              if (price <= 0) {
                that.setData({
                  feeprice: 0,
                  feeflag: true
                })
              } else {
                that.setData({
                  feeprice: price,
                  feeflag: true
                })
              }
            }
          } else {
            that.setData({
              feeflag: false
            })
          }
        } else {
          if (that.data.promotionPrice >= result.threshold) {
            // 全免标志位
            if (result.allReduceFlag) {
              //全免
              that.setData({
                feeprice: 0,
                feeflag: true
              })
            } else {
              // 限时减免
              var price = that.data.storelist.deliveryPrice - result.reduceFee
              if (price <= 0) {
                that.setData({
                  feeprice: 0,
                  feeflag: true
                })
              } else {
                that.setData({
                  feeprice: price,
                  feeflag: true
                })
              }
            }
          } else {
            that.setData({
              feeflag: false
            })
          }
        }
      }
    } else {
    }
  },
  takePaid: function () {
    var that = this
    let flag = false
    let stflag = false
    let name = ''
    const res = getShoppingCart(this.data.storeCartKey)
    var storeid = wx.getStorageSync('storeid')
    // 友盟埋点
    wx.uma.trackEvent('settlement')
    wx.setStorageSync('trueindex', 'fail')
    wx.setStorageSync('mentionlist', '')
    wx.setStorageSync('startSalePrice', that.data.storelist.startSalePrice)
    wx.setStorageSync('selectCouponlist', '')
    if (that.data.delivery == true) {
      wx.navigateTo({
        url:
          '../good/affirm/affirm?storeid=' +
          storeid +
          '&currentflag=1' +
          '&addressflag=1'
      })
    } else {
      wx.navigateTo({
        url:
          '../good/affirm/affirm?storeid=' +
          storeid +
          '&currentflag=1' +
          '&addressflag=1'
      })
    }
  },
  takeDetail: function () {
    var that = this
    var opencart = !that.data.showcard
    that.setData({
      showcard: opencart
    })
  },
  takeImg: function (e) {
    var that = this
    const { comp } = e.currentTarget.dataset
    if (comp) e = e.detail
    var index = e.currentTarget.dataset.index
    var specsnum = e.currentTarget.dataset.specsnum
    var id = e.currentTarget.dataset.id
    var image = e.currentTarget.dataset.img
    if (image == 'images') {
      var split = that.data.oneimage
    } else {
      var split = image.split(',')
    }
    // wx.hideTabBar();
    let merchantid = wx.getStorageSync('merchantid')
    let storeid = wx.getStorageSync('storeid')
    store.getSku(
      baseUrl.waimaiUrl +
        '/client/merchants/' +
        merchantid +
        '/stores/' +
        storeid +
        '/store-foods/' +
        e.currentTarget.dataset.id,
      {},
      {},
      '加载中',
      function (res) {
        var result = res.results
        let listing = []
        for (var i = 0; i < result.sku.length; i++) {
          result.sku[i].num = result.sku[i].startNum
        }
        for (var j = 0; j < result.attribute.length; j++) {
          for (var k = 0; k < result.attribute[j].value.length; k++) {
            if (k == 0) {
              result.attribute[j].value[k].attributeflag = true
              listing.push(result.attribute[j].value[k].name)
            } else {
              result.attribute[j].value[k].attributeflag = false
            }
          }
        }
        result.sku[0].select = true
        if (specsnum != undefined) {
        } else {
          specsnum = 0
        }
        // 折扣菜品
        if (result.sku[0].foodType == 2) {
          if (
            specsnum >= result.sku[0].limitBuyNumber &&
            result.sku[0].limitBuyNumber != 0
          ) {
            specsnum += result.sku[0].startNum
          }
          if (that.data.limitDiscount != null) {
            if (
              that.data.limitDiscount.limitNum < that.data.astrict &&
              that.data.limitDiscount.limitStatus
            ) {
            } else {
              that.data.astrict += result.sku[0].startNum
              that.data.copyastrict += result.sku[0].startNum
              specsnum += result.sku[0].startNum
            }
          }
        }
        if (result.attribute.length > 0) {
          that.setData({
            haveattribute: true
          })
        } else {
          that.setData({
            haveattribute: false
          })
        }
        that.setData({
          skulist: result.sku,
          attributelist: result.attribute,
          skuindex: 0,
          attributelisting: listing,
          astrict: that.data.astrict,
          copyastrict: that.data.copyastrict,
          specsnum: specsnum
          // allowancenum:that.data.allowancenum,
        })
        that.setData({
          showmodal: true,
          foodintroduction: that.data.foodlist[index].introduction,
          foodnickname: that.data.foodlist[index].name,
          foodsalse: that.data.foodlist[index].saleNum,
          showSaleLimit: that.data.foodlist[index].showSaleLimit,
          imagedata: split
        })
        loadEnd(that)
      },
      function (err) {
        loadEnd(that)
        __FAIL_FN({
          res: err
        })
      }
    )
  },
  // 秒杀菜品
  takeImgseckill: function (e) {
    var that = this
    const { comp } = e.currentTarget.dataset
    if (comp) e = e.detail
    var index = e.currentTarget.dataset.index
    var id = e.currentTarget.dataset.id
    var image = e.currentTarget.dataset.img
    var foodtype = e.currentTarget.dataset.foodtype
    if (foodtype == 2) {
      if (image == 'images') {
        var split = that.data.oneimage
      } else {
        var split = image.split(',')
      }
      // wx.hideTabBar();
      let storeid = wx.getStorageSync('storeid')
      let merchantid = wx.getStorageSync('merchantid')
      store.takeDiscountfoods(
        baseUrl.waimaiUrl +
          '/client/merchants/' +
          merchantid +
          '/stores/' +
          storeid +
          '/discount-foods/' +
          e.currentTarget.dataset.id,
        {},
        {},
        '加载中',
        function (res) {
          var result = res.results
          let listing = []
          for (var i = 0; i < result.sku.length; i++) {
            result.sku[i].num = result.sku[i].startNum
          }
          result.sku[0].select = true
          for (var j = 0; j < result.attribute.length; j++) {
            for (var k = 0; k < result.attribute[j].value.length; k++) {
              if (k == 0) {
                result.attribute[j].value[k].attributeflag = true
                listing.push(result.attribute[j].value[k].name)
              } else {
                result.attribute[j].value[k].attributeflag = false
              }
            }
          }
          if (result.attribute.length > 0) {
            that.setData({
              haveattribute: true
            })
          } else {
            that.setData({
              haveattribute: false
            })
          }
          that.setData({
            skulist: result.sku,
            skuindex: 0,
            attributelist: result.attribute,
            attributelisting: listing
          })
          that.setData({
            showmodal: true,
            foodintroduction: that.data.foodlist[index].introduction,
            foodnickname: that.data.foodlist[index].name,
            foodsalse: that.data.foodlist[index].saleNum,
            showSaleLimit: that.data.foodlist[index].showSaleLimit,
            imagedata: split
          })
          // console.log(result);
        },
        function (err) {
          wx.showToast({
            title: '失败'
          })
        }
      )
    } else {
    }
  },
  // 弹窗
  hidemodal: function () {
    var that = this
    var test = []
    that.data.astrict = that.data.astrict - that.data.copyastrict
    that.setData({
      showmodal: false,
      fooddetail: test,
      astrict: that.data.astrict,
      copyastrict: 0,
      allowancenum: 0,
      specsnum: 0
    })
  },
  // 菜品详情返回
  takeBack: function () {
    this.hidemodal()
  },
  // 商品数据
  plusCount: function (e) {
    var that = this
    for (var i = 0; i < that.data.skulist.length; i++) {
      if (that.data.skulist[i].select == true) {
        // 库存不足
        if (that.data.skulist[i].num >= that.data.skulist[i].stock) {
          // 库存和限购份数
          if (that.data.skulist[i].num >= that.data.skulist[i].stock) {
            wx.showToast({
              title: '库存不足',
              icon: 'none'
            })
          }
        } else if (
          that.data.specsnum >= that.data.skulist[i].limitBuyNumber &&
          that.data.skulist[i].limitBuyNumber != 0 &&
          that.data.skulist[i].foodType == 2
        ) {
          if (that.data.skulist[i].num == 0) {
            that.data.skulist[i].num = that.data.skulist[i].startNum
            that.data.allowancenum += that.data.skulist[i].startNum
            that.data.specsnum += that.data.skulist[i].startNum
          } else {
            that.data.skulist[i].num += 1
            that.data.allowancenum += 1
            that.data.specsnum += 1
          }
          wx.showToast({
            title: `折扣菜品限购${that.data.skulist[i].limitBuyNumber}份，超过${that.data.skulist[i].limitBuyNumber}份恢复原价`,
            icon: 'none',
            duration: 2000
          })
        } else if (that.data.skulist[i].foodType == 2) {
          if (
            that.data.limitDiscount.limitNum <= that.data.astrict &&
            that.data.limitDiscount.limitStatus
          ) {
            if (that.data.skulist[i].num == 0) {
              that.data.skulist[i].num = that.data.skulist[i].startNum
              that.data.astrict += that.data.skulist[i].startNum
              that.data.copyastrict += that.data.skulist[i].startNum
              that.data.specsnum += that.data.skulist[i].startNum
            } else {
              that.data.skulist[i].num += 1
              that.data.astrict += 1
              that.data.copyastrict += 1
              that.data.specsnum += 1
            }
            wx.showToast({
              title: `每单的折扣菜品限购${that.data.limitDiscount.limitNum}份，超过${that.data.limitDiscount.limitNum}份恢复原价`,
              icon: 'none',
              duration: 2000
            })
          }
        } else if (that.data.skulist[i].foodType == 2) {
          if (that.data.skulist[i].num == 0) {
            that.data.skulist[i].num = that.data.skulist[i].startNum
            that.data.astrict += that.data.skulist[i].startNum
            that.data.copyastrict += that.data.skulist[i].startNum
            that.data.specsnum += that.data.skulist[i].startNum
          } else {
            that.data.skulist[i].num += 1
            that.data.astrict += 1
            that.data.copyastrict += 1
            that.data.specsnum += 1
          }
        } else if (that.data.skulist[i].foodType == 1) {
          if (that.data.skulist[i].num == 0) {
            that.data.skulist[i].num = that.data.skulist[i].startNum
          } else {
            that.data.skulist[i].num += 1
          }
        }
      } else {
        that.data.skulist[i].num = that.data.skulist[i].startNum
      }
    }
    console.log(that.data.skuindex)
    that.setData({
      skulist: that.data.skulist,
      astrict: that.data.astrict,
      copyastrict: that.data.copyastrict,
      allowancenum: that.data.allowancenum,
      specsnum: that.data.specsnum
    })
  },
  reduceCount: function () {
    var that = this
    // var id = e.currentTarget.dataset.id;
    for (var i = 0; i < that.data.skulist.length; i++) {
      if (that.data.skulist[i].select == true) {
        if (that.data.skulist[i].num == that.data.skulist[i].startNum) {
          // 起购量
          that.data.skulist[i].num =
            that.data.skulist[i].num - that.data.skulist[i].startNum
        } else {
          that.data.skulist[i].num -= 1
        }
        if (that.data.skulist[i].num < that.data.skulist[i].startNum) {
          that.data.skulist[i].num = 0
        }
        if (that.data.skulist[i].num < that.data.skulist[i].stock) {
          that.data.skulist[i].stockFlag = true
        }
        // TODO折扣菜品
        if (that.data.skulist[i].foodType == 2) {
          if (
            (that.data.skulist[i].num < that.data.skulist[i].limitBuyNumber &&
              that.data.skulist[i].limitBuyNumber != 0) ||
            (that.data.limitDiscount.limitNum > that.data.astrict &&
              that.data.limitDiscount.limitStatus)
          ) {
            if (that.data.skulist[i].num == 0) {
              that.data.astrict -= that.data.skulist[i].startNum
              that.data.copyastrict -= that.data.skulist[i].startNum
              that.data.specsnum -= that.data.skulist[i].startNum
            } else {
              that.data.astrict -= 1
              that.data.copyastrict -= 1
              that.data.specsnum -= 1
            }
            if (that.data.astrict < 0) {
              that.data.astrict = 0
              that.data.copyastrict = 0
            }
          }
        }
        // allowancenum
        if (
          that.data.skulist[i].num >= that.data.skulist[i].limitBuyNumber &&
          that.data.skulist[i].limitBuyNumber != 0
        ) {
          if (that.data.skulist[i].num == 0) {
            that.data.allowancenum -= that.data.skulist[i].startNum
          } else {
            that.data.allowancenum -= 1
          }
        }
      } else {
        that.data.skulist[i].num = that.data.skulist[i].startNum
      }
    }
    that.setData({
      skulist: that.data.skulist,
      astrict: that.data.astrict,
      copyastrict: that.data.copyastrict,
      allowancenum: that.data.allowancenum,
      specsnum: that.data.specsnum
    })
  },
  takebox: function (e) {
    var that = this
    var arr = getShoppingCart(this.data.storeCartKey) || []
    var id = e.currentTarget.dataset.id
    var catgid = e.currentTarget.dataset.catgid
    var uiList = that.data.categoryIds
    var obj3 = that.data.uidata
    var attrid = ''
    var boxflag = true
    // 限时购的添加商品判断
    var foodtype = e.currentTarget.dataset.foodtype
    var attributeid = id + that.data.attributelisting.join('+') + '_' + foodtype
    attrid = attributeid
    for (var i = 0; i < that.data.skulist.length; i++) {
      if (that.data.skulist[i].select == true) {
        id = that.data.skulist[i].id
        if (that.data.skulist[i].num == 0) {
          wx.showToast({
            title: '选择数量不能为0',
            icon: 'none'
          })
          boxflag = false
        }
        if (that.data.skulist[i].num > that.data.skulist[i].stock) {
          wx.showToast({
            title: '库存不足',
            icon: 'none'
          })
          boxflag = false
        }
      }
    }
    if (boxflag) {
      // 多属性插入
      if (obj3.hasOwnProperty(catgid)) {
        if (
          obj3[catgid].hasOwnProperty(attrid) &&
          obj3[catgid][attrid].foodType == foodtype
        ) {
          // 总限制份数
          // TODO折扣菜品的起购份数
          obj3[catgid][attrid].num += that.data.skulist[that.data.skuindex].num
          obj3[catgid].num += that.data.skulist[that.data.skuindex].num
          if (that.data.limitDiscount == null) {
            // 限时购互斥
            if (
              obj3[catgid][attrid].stockFlag == true &&
              obj3[catgid][attrid].num < obj3[catgid][attrid].stock
            ) {
              // 未超过折扣的菜品的限制
              if (
                (obj3[catgid][attrid].num <
                  obj3[catgid][attrid].limitBuyNumber ||
                  obj3[catgid][attrid].limitBuyNumber == 0) &&
                obj3[catgid][attrid].foodType == 2
              ) {
                // that.data.astrict += obj3[catgid][attrid].startNum;
                if (
                  JSON.stringify(obj3[catgid][attrid].arraylisting) ==
                    JSON.stringify(that.data.attributelisting) ||
                  !that.data.haveattribute
                ) {
                  var testattrid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  // obj3[catgid][attrid].num +=that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  for (var k = 0; k < arr.length; k++) {
                    if (arr[k].attributeid == testattrid) {
                      arr[k].num += that.data.skulist[that.data.skuindex].num
                    }
                  }
                } else {
                  obj3[catgid][attrid] = JSON.parse(
                    JSON.stringify(uiList[catgid][attrid])
                  )
                  // TODO多属性
                  obj3[catgid][attrid].attributeid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  obj3[catgid][attrid].arraylisting = []
                  obj3[catgid][attrid].arraylisting = that.data.attributelisting
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  //TODO未超过折扣
                  arr.push(obj3[catgid][attrid])
                }
              } else if (
                (obj3[catgid][attrid].num >=
                  obj3[catgid][attrid].limitBuyNumber ||
                  obj3[catgid][attrid].limitBuyNumber != 0) &&
                obj3[catgid][attrid].foodType == 2
              ) {
                // 多属性
                if (
                  JSON.stringify(obj3[catgid][attrid].arraylisting) ==
                    JSON.stringify(that.data.attributelisting) ||
                  !that.data.haveattribute
                ) {
                  var testattrid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  if (
                    obj3[catgid][attrid].num >=
                      obj3[catgid][attrid].limitBuyNumber &&
                    obj3[catgid][attrid].limitBuyNumber != 0
                  ) {
                    obj3[catgid][attrid].commonNum =
                      obj3[catgid][attrid].num -
                      obj3[catgid][attrid].limitBuyNumber
                    // TODO折扣菜品
                    // that.data.astrict -= that.data.copyastrict
                    // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
                    obj3[catgid][attrid].rebateflag = true
                    wx.showToast({
                      title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                      icon: 'none',
                      duration: 2000
                    })
                    for (var k = 0; k < arr.length; k++) {
                      if (arr[k].attributeid == testattrid) {
                        // arr[k].num = that.data.skulist[that.data.skuindex].num;
                        arr[k].num = obj3[catgid][attrid].num
                        arr[k].commonNum = obj3[catgid][attrid].commonNum
                        arr[k].rebateflag = true
                      }
                    }
                  } else {
                    for (var k = 0; k < arr.length; k++) {
                      if (arr[k].attributeid == testattrid) {
                        arr[k].num += that.data.skulist[that.data.skuindex].num
                      }
                    }
                  }
                } else {
                  obj3[catgid][attrid] = JSON.parse(
                    JSON.stringify(uiList[catgid][attrid])
                  )
                  obj3[catgid][attrid].attributeid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  obj3[catgid][attrid].arraylisting = []
                  obj3[catgid][attrid].arraylisting = that.data.attributelisting
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  if (
                    obj3[catgid][attrid].num >=
                      obj3[catgid][attrid].limitBuyNumber &&
                    obj3[catgid][attrid].limitBuyNumber != 0
                  ) {
                    obj3[catgid][attrid].commonNum =
                      obj3[catgid][attrid].num -
                      obj3[catgid][attrid].limitBuyNumber
                    // TODO折扣菜品
                    // that.data.astrict -= that.data.copyastrict
                    // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
                    obj3[catgid][attrid].rebateflag = true
                    wx.showToast({
                      title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                      icon: 'none',
                      duration: 2000
                    })
                  }
                  arr.push(obj3[catgid][attrid])
                }
                // wx.showToast({
                //   title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                //   icon: "none",
                //   duration: 2000,
                // });
              } else if (obj3[catgid][attrid].foodType == 1) {
                if (
                  JSON.stringify(obj3[catgid][attrid].arraylisting) ==
                    JSON.stringify(that.data.attributelisting) ||
                  !that.data.haveattribute
                ) {
                  var testattrid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  if (
                    obj3[catgid][attrid].num >=
                      obj3[catgid][attrid].limitBuyNumber &&
                    obj3[catgid][attrid].limitBuyNumber != 0
                  ) {
                    obj3[catgid][attrid].commonNum =
                      obj3[catgid][attrid].num -
                      obj3[catgid][attrid].limitBuyNumber
                    // TODO折扣菜品
                    // that.data.astrict -= that.data.copyastrict
                    // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
                    obj3[catgid][attrid].rebateflag = true
                    wx.showToast({
                      title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                      icon: 'none',
                      duration: 2000
                    })
                    for (var k = 0; k < arr.length; k++) {
                      if (arr[k].attributeid == testattrid) {
                        // arr[k].num = that.data.skulist[that.data.skuindex].num;
                        arr[k].num = obj3[catgid][attrid].num
                        arr[k].commonNum = obj3[catgid][attrid].commonNum
                        arr[k].rebateflag = true
                      }
                    }
                  } else {
                    for (var k = 0; k < arr.length; k++) {
                      if (arr[k].attributeid == testattrid) {
                        arr[k].num = obj3[catgid][attrid].num
                        // arr[k].num = that.data.skulist[that.data.skuindex].num;
                      }
                    }
                  }
                } else {
                  obj3[catgid][attrid] = JSON.parse(
                    JSON.stringify(uiList[catgid][attrid])
                  )
                  obj3[catgid][attrid].attributeid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  obj3[catgid][attrid].arraylisting = []
                  obj3[catgid][attrid].arraylisting = that.data.attributelisting
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  if (
                    obj3[catgid][attrid].num >=
                      obj3[catgid][attrid].limitBuyNumber &&
                    obj3[catgid][attrid].limitBuyNumber != 0
                  ) {
                    obj3[catgid][attrid].commonNum =
                      obj3[catgid][attrid].num -
                      obj3[catgid][attrid].limitBuyNumber
                    // TODO折扣菜品
                    // that.data.astrict -= that.data.copyastrict
                    // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
                    obj3[catgid][attrid].rebateflag = true
                    wx.showToast({
                      title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                      icon: 'none',
                      duration: 2000
                    })
                  }
                  arr.push(obj3[catgid][attrid])
                }
              }
            } else {
              // 未达到条件
              obj3[catgid][attrid].stockFlag = false
              if (obj3[catgid][attrid].num >= obj3[catgid][attrid].stock) {
                wx.showToast({
                  title: '库存不足',
                  icon: 'none'
                })
              }
            }
          }
          // 没有折扣菜品
          else {
            // 限时购互斥
            // 限制的初始判断
            // that.data.astrict += that.data.skulist[that.data.skuindex].num;
            // TODO折扣菜品
            if (
              obj3[catgid][attrid].stockFlag == true &&
              obj3[catgid][attrid].num < obj3[catgid][attrid].stock &&
              (that.data.limitDiscount.limitNum > that.data.astrict ||
                !that.data.limitDiscount.limitStatus)
            ) {
              // 未超过折扣的菜品的限制
              if (
                (obj3[catgid][attrid].num <
                  obj3[catgid][attrid].limitBuyNumber ||
                  obj3[catgid][attrid].limitBuyNumber == 0) &&
                obj3[catgid][attrid].foodType == 2
              ) {
                // that.data.astrict += obj3[catgid][attrid].startNum;
                if (
                  JSON.stringify(obj3[catgid][attrid].arraylisting) ==
                    JSON.stringify(that.data.attributelisting) ||
                  !that.data.haveattribute
                ) {
                  var testattrid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  //TODO未超过折扣
                  for (var k = 0; k < arr.length; k++) {
                    if (arr[k].attributeid == testattrid) {
                      // arr[k].num += that.data.skulist[that.data.skuindex].num;
                      arr[k].num = obj3[catgid][attrid].num
                    }
                  }
                } else {
                  obj3[catgid][attrid] = JSON.parse(
                    JSON.stringify(uiList[catgid][attrid])
                  )
                  obj3[catgid][attrid].attributeid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  obj3[catgid][attrid].arraylisting = []
                  obj3[catgid][attrid].arraylisting = that.data.attributelisting
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  arr.push(obj3[catgid][attrid])
                }
              } else if (
                (obj3[catgid][attrid].num >=
                  obj3[catgid][attrid].limitBuyNumber ||
                  obj3[catgid][attrid].limitBuyNumber != 0) &&
                obj3[catgid][attrid].foodType == 2
              ) {
                // 多属性
                if (
                  JSON.stringify(obj3[catgid][attrid].arraylisting) ==
                    JSON.stringify(that.data.attributelisting) ||
                  !that.data.haveattribute
                ) {
                  var testattrid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  if (
                    obj3[catgid][attrid].num >=
                      obj3[catgid][attrid].limitBuyNumber &&
                    obj3[catgid][attrid].limitBuyNumber != 0
                  ) {
                    obj3[catgid][attrid].commonNum =
                      obj3[catgid][attrid].num -
                      obj3[catgid][attrid].limitBuyNumber
                    // TODO折扣菜品
                    // that.data.astrict -= that.data.copyastrict
                    // that.data.astrict += (that.data.copyastrict-obj3[catgid][attrid].commonNum)
                    obj3[catgid][attrid].rebateflag = true
                    wx.showToast({
                      title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                      icon: 'none',
                      duration: 2000
                    })
                    for (var k = 0; k < arr.length; k++) {
                      if (arr[k].attributeid == testattrid) {
                        // arr[k].num += that.data.skulist[that.data.skuindex].num;
                        arr[k].num = obj3[catgid][attrid].num
                        arr[k].commonNum = obj3[catgid][attrid].commonNum
                        arr[k].rebateflag = true
                      }
                    }
                  } else {
                    for (var k = 0; k < arr.length; k++) {
                      if (arr[k].attributeid == testattrid) {
                        // arr[k].num += that.data.skulist[that.data.skuindex].num;
                        arr[k].num = obj3[catgid][attrid].num
                      }
                    }
                  }
                } else {
                  obj3[catgid][attrid] = JSON.parse(
                    JSON.stringify(uiList[catgid][attrid])
                  )
                  obj3[catgid][attrid].attributeid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  obj3[catgid][attrid].arraylisting = []
                  obj3[catgid][attrid].arraylisting = that.data.attributelisting
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  if (
                    obj3[catgid][attrid].num >=
                      obj3[catgid][attrid].limitBuyNumber &&
                    obj3[catgid][attrid].limitBuyNumber != 0
                  ) {
                    obj3[catgid][attrid].commonNum =
                      obj3[catgid][attrid].num -
                      obj3[catgid][attrid].limitBuyNumber
                    // TODO折扣菜品
                    // that.data.astrict -= that.data.copyastrict
                    // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
                    obj3[catgid][attrid].rebateflag = true
                    wx.showToast({
                      title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                      icon: 'none',
                      duration: 2000
                    })
                  }
                  arr.push(obj3[catgid][attrid])
                }
                // wx.showToast({
                //   title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                //   icon: "none",
                //   duration: 2000,
                // });
              } else if (obj3[catgid][attrid].foodType == 1) {
                // 属性插入
                if (
                  JSON.stringify(obj3[catgid][attrid].arraylisting) ==
                    JSON.stringify(that.data.attributelisting) ||
                  !that.data.haveattribute
                ) {
                  var testattrid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  if (
                    obj3[catgid][attrid].num >=
                      obj3[catgid][attrid].limitBuyNumber &&
                    obj3[catgid][attrid].limitBuyNumber != 0
                  ) {
                    obj3[catgid][attrid].commonNum =
                      obj3[catgid][attrid].num -
                      obj3[catgid][attrid].limitBuyNumber
                    // TODO折扣菜品
                    // that.data.astrict -= that.data.copyastrict
                    // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
                    obj3[catgid][attrid].rebateflag = true
                    wx.showToast({
                      title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                      icon: 'none',
                      duration: 2000
                    })
                    for (var k = 0; k < arr.length; k++) {
                      if (arr[k].attributeid == testattrid) {
                        // arr[k].num += that.data.skulist[that.data.skuindex].num;/
                        arr[k].num = obj3[catgid][attrid].num
                        arr[k].commonNum = obj3[catgid][attrid].commonNum
                        arr[k].rebateflag = true
                      }
                    }
                  } else {
                    for (var k = 0; k < arr.length; k++) {
                      if (arr[k].attributeid == testattrid) {
                        // arr[k].num += that.data.skulist[that.data.skuindex].num;
                        arr[k].num = obj3[catgid][attrid].num
                      }
                    }
                  }
                } else {
                  obj3[catgid][attrid] = JSON.parse(
                    JSON.stringify(uiList[catgid][attrid])
                  )
                  obj3[catgid][attrid].attributeid =
                    id + that.data.attributelisting.join('+') + '_' + foodtype
                  obj3[catgid][attrid].arraylisting = []
                  obj3[catgid][attrid].arraylisting = that.data.attributelisting
                  // obj3[catgid][attrid].num +=
                  //   that.data.skulist[that.data.skuindex].num;
                  // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                  if (
                    obj3[catgid][attrid].num >=
                      obj3[catgid][attrid].limitBuyNumber &&
                    obj3[catgid][attrid].limitBuyNumber != 0
                  ) {
                    obj3[catgid][attrid].commonNum =
                      obj3[catgid][attrid].num -
                      obj3[catgid][attrid].limitBuyNumber
                    // TODO折扣菜品
                    // that.data.astrict -= that.data.copyastrict
                    // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
                    obj3[catgid][attrid].rebateflag = true
                    wx.showToast({
                      title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                      icon: 'none',
                      duration: 2000
                    })
                  }
                  arr.push(obj3[catgid][attrid])
                }
              }
            } else {
              // 未达到条件
              obj3[catgid][attrid].stockFlag = false
              if (obj3[catgid][attrid].num >= obj3[catgid][attrid].stock) {
                wx.showToast({
                  title: '库存不足',
                  icon: 'none'
                })
              } else if (
                that.data.limitDiscount.limitNum <= that.data.astrict &&
                that.data.limitDiscount.limitStatus
              ) {
                // 总折扣菜品
                // obj3[catgid][attrid].num +=
                //   that.data.skulist[that.data.skuindex].num;
                // obj3[catgid].num += that.data.skulist[that.data.skuindex].num;
                // TODO每单折扣的commnum
                // obj3[catgid][attrid].commonNum = (obj3[catgid][attrid].num) - that.data.limitDiscount.limitNum;
                obj3[catgid][attrid].commonNum =
                  that.data.astrict +
                  that.data.allowancenum -
                  that.data.limitDiscount.limitNum
                that.data.astrict = that.data.limitDiscount.limitNum
                for (var k = 0; k < arr.length; k++) {
                  if (arr[k].attributeid == attrid) {
                    arr[k].num = obj3[catgid][attrid].num
                    arr[k].commonNum = obj3[catgid][attrid].commonNum
                    arr[k].rebateflag = true
                  }
                }
                obj3[catgid][attrid].rebateflag = true
                wx.showToast({
                  title: `每单的折扣菜品限购${that.data.limitDiscount.limitNum}份，超过${that.data.limitDiscount.limitNum}份恢复原价`,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          }
        } else {
          // 限时购互斥
          if (foodtype == 1) {
            // attrid = id + that.data.attributelisting.join("+") + "_" + foodtype
            obj3[catgid][attrid] = {}
            obj3[catgid][attrid] = JSON.parse(
              JSON.stringify(uiList[catgid][id])
            )
            obj3[catgid][attrid].attributeid =
              id + that.data.attributelisting.join('+') + '_' + foodtype
            obj3[catgid][attrid].arraylisting = []
            obj3[catgid][attrid].arraylisting = that.data.attributelisting
            obj3[catgid][attrid].num +=
              that.data.skulist[that.data.skuindex].num
            obj3[catgid].num += that.data.skulist[that.data.skuindex].num
            arr.push(obj3[catgid][attrid])
          } else if (foodtype == 2) {
            obj3[catgid][attrid] = {}
            obj3[catgid][attrid] = JSON.parse(
              JSON.stringify(uiList[catgid][id])
            )
            obj3[catgid][attrid].attributeid =
              id + that.data.attributelisting.join('+') + '_' + foodtype
            obj3[catgid][attrid].arraylisting = []
            obj3[catgid][attrid].arraylisting = that.data.attributelisting
            obj3[catgid][attrid].num +=
              that.data.skulist[that.data.skuindex].num
            obj3[catgid].num += that.data.skulist[that.data.skuindex].num
            // 总限制份数
            // that.data.astrict += that.data.skulist[that.data.skuindex].num;
            if (
              that.data.limitDiscount.limitNum < that.data.astrict &&
              that.data.limitDiscount.limitStatus
            ) {
              // TODO每单折扣的commonnum
              obj3[catgid][attrid].commonNum =
                that.data.astrict +
                that.data.allowancenum -
                that.data.limitDiscount.limitNum
              that.data.astrict = that.data.limitDiscount.limitNum
              // obj3[catgid][attrid].commonNum +=
              //   obj3[catgid][attrid].num - that.data.limitDiscount.limitNum;
              obj3[catgid][attrid].rebateflag = true
              wx.showToast({
                title: `每单的折扣菜品限购${that.data.limitDiscount.limitNum}份，超过${that.data.limitDiscount.limitNum}份恢复原价`,
                icon: 'none',
                duration: 2000
              })
            } else {
              obj3[catgid][attrid].rebateflag = false
              if (
                (obj3[catgid][attrid].num <=
                  obj3[catgid][attrid].limitBuyNumber ||
                  obj3[catgid][attrid].limitBuyNumber != 0) &&
                obj3[catgid][attrid].foodType == 2
              ) {
                //TODO折扣菜品
                // that.data.astrict -= that.data.skulist[that.data.skuindex].num;
                // that.data.astrict += obj3[catgid][attrid].limitBuyNumber;
                obj3[catgid][attrid].commonNum =
                  obj3[catgid][attrid].num - obj3[catgid][attrid].limitBuyNumber
                // TODO折扣菜品
                // that.data.astrict -= that.data.copyastrict
                // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
                for (var k = 0; k < arr.length; k++) {
                  if (arr[k].attributeid == attrid) {
                    arr[k].commonNum = obj3[catgid][attrid].commonNum
                    arr[k].num = obj3[catgid][attrid].num
                    arr[k].rebateflag = true
                  }
                }
                obj3[catgid][attrid].rebateflag = true
                wx.showToast({
                  title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
            arr.push(obj3[catgid][attrid])
          }
        }
      } else {
        // 限时购详情（限购数量）
        if (foodtype == 1) {
          obj3[catgid] = {}
          obj3[catgid][attrid] = JSON.parse(JSON.stringify(uiList[catgid][id]))
          obj3[catgid][attrid].attributeid =
            id + that.data.attributelisting.join('+') + '_' + foodtype
          obj3[catgid][attrid].arraylisting = []
          obj3[catgid][attrid].arraylisting = that.data.attributelisting
          obj3[catgid][attrid].num += that.data.skulist[that.data.skuindex].num
          obj3[catgid].num = that.data.skulist[that.data.skuindex].num
          arr.push(obj3[catgid][attrid])
        } else if (foodtype == 2) {
          obj3[catgid] = {}
          obj3[catgid][attrid] = {}
          obj3[catgid][attrid] = JSON.parse(JSON.stringify(uiList[catgid][id]))
          obj3[catgid][attrid].attributeid =
            id + that.data.attributelisting.join('+') + '_' + foodtype
          obj3[catgid][attrid].arraylisting = []
          obj3[catgid][attrid].arraylisting = that.data.attributelisting
          obj3[catgid][attrid].num += that.data.skulist[that.data.skuindex].num
          obj3[catgid].num = that.data.skulist[that.data.skuindex].num
          // 总限制份数
          // that.data.astrict += that.data.skulist[that.data.skuindex].num;
          if (
            that.data.limitDiscount.limitNum <= that.data.astrict &&
            that.data.limitDiscount.limitStatus
          ) {
            // TODO每单的折扣的commonnum
            obj3[catgid][attrid].commonNum =
              that.data.astrict +
              that.data.allowancenum -
              that.data.limitDiscount.limitNum
            that.data.astrict = that.data.limitDiscount.limitNum
            // obj3[catgid][attrid].commonNum +=
            //   obj3[catgid][attrid].num - that.data.limitDiscount.limitNum;
            obj3[catgid][attrid].rebateflag = true
            wx.showToast({
              title: `每单的折扣菜品限购${that.data.limitDiscount.limitNum}份，超过${that.data.limitDiscount.limitNum}份恢复原价`,
              icon: 'none',
              duration: 2000
            })
          } else {
            obj3[catgid][attrid].rebateflag = false
            if (
              (obj3[catgid][attrid].num <=
                obj3[catgid][attrid].limitBuyNumber ||
                obj3[catgid][attrid].limitBuyNumber != 0) &&
              obj3[catgid][attrid].foodType == 2
            ) {
              //TODO折扣菜品
              // that.data.astrict -= that.data.skulist[that.data.skuindex].num;
              // that.data.astrict += obj3[catgid][attrid].limitBuyNumber;
              obj3[catgid][attrid].commonNum =
                obj3[catgid][attrid].num - obj3[catgid][attrid].limitBuyNumber
              // TODO折扣菜品
              // that.data.astrict -= that.data.copyastrict
              // that.data.astrict += obj3[catgid][attrid].num - obj3[catgid][attrid].commonNum
              for (var k = 0; k < arr.length; k++) {
                if (arr[k].attributeid == attrid) {
                  arr[k].commonNum = obj3[catgid][attrid].commonNum
                  arr[k].num = obj3[catgid][attrid].num
                  arr[k].rebateflag = true
                }
              }
              obj3[catgid][attrid].rebateflag = true
              wx.showToast({
                title: `折扣菜品限购${obj3[catgid][attrid].limitBuyNumber}份，超过${obj3[catgid][attrid].limitBuyNumber}份恢复原价`,
                icon: 'none',
                duration: 2000
              })
            }
          }
          arr.push(obj3[catgid][attrid])
        }
      }
      that.setData({
        uidata: obj3,
        cartList: arr,
        showmodal: false,
        cardflag: true,
        astrict: that.data.astrict,
        copyastrict: 0,
        allowancenum: 0,
        specsnum: 0
      })
      setShoppingCart(this.data.storeCartKey, arr)
      wx.setStorageSync('bannerfoodflag', 'agree')
      setShoppingCart(
        that.data.storeCartKey,
        that.data.uidata,
        _GLOBAL_UI_DATA_KEY
      )
      wx.showTabBar()
      that.getTotalPrice()
    } else {
    }
  },
  takeicon: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    // 总限制份数
    if (index === that.data.skuindex) {
    } else {
      that.data.astrict -= that.data.copyastrict
    }
    for (var i = 0; i < that.data.skulist.length; i++) {
      if (that.data.skulist[i].id == id) {
        that.data.skulist[i].select = true
      } else {
        that.data.skulist[i].select = false
      }
    }
    that.setData({
      skulist: that.data.skulist,
      skuindex: index,
      astrict: that.data.astrict,
      copyastrict: 0
    })
  },
  // 多属性
  takeAttribute: function (e) {
    var that = this
    var keyindex = e.currentTarget.dataset.parent
    var index = e.currentTarget.dataset.index
    for (var i = 0; i < that.data.attributelist[keyindex].value.length; i++) {
      that.data.attributelist[keyindex].value[i].attributeflag = false
    }
    that.data.attributelist[keyindex].value[index].attributeflag = true
    that.data.attributelisting[keyindex] =
      that.data.attributelist[keyindex].value[index].name
    console.log(that.data.attributelisting)
    that.setData({
      attributelist: that.data.attributelist,
      // skuindex: index,
      attributelisting: that.data.attributelisting
    })
  },
  // 登录授权
  takeAnflag: function () {
    wx.navigateTo({
      url: '/pages/authorization/authorization'
    })
  },
  // 购物车信息展开
  hidecard: function () {
    var that = this
    var cardlist = getShoppingCart(this.data.storeCartKey)
    var fcard = true
    for (var i = 0; i < cardlist.length; i++) {
      if (cardlist[i].foodType == 2) {
        fcard = false
      }
    }
    if (fcard) {
      if (that.data.reducelist.length > 0) {
        that.setData({
          showcard: false,
          disappear: true
        })
      } else {
        that.setData({
          showcard: false,
          disappear: false
        })
      }
    } else {
      that.setData({
        showcard: false,
        disappear: false
      })
    }
  }
})
