// packageC/mycomponent/option_menu/option_menu.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    ok_subs_select:{
      value:null,
      type:Number
    },
    successLocation:{
      value:false,//有定位信息
      type: Boolean
    },
  },
  observers: {
    'successLocation': function(res) {
      this.setData({
        _successLocation: res
      });
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    parentActive: null,
    _successLocation: false,
    
    subs_select: null,
    activeType: "",
    isshowO2oOptionMenu: false, //是否开启组件
    option: [],
    selectOption: [], //二级筛选器选项
    isShowSubContent: false, //二级筛选器选项显隐，

  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      console.log("筛选器===========");
      this.getStoreSearchMenu();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onOptionsItemClick(e) {
      // console.log(e.currentTarget.dataset.item)
      let item = e.currentTarget.dataset.item;
      if (!this.data._successLocation) {
        wx.showToast({
          duration: 3000,
          title: '定位中，请稍等...',
          icon: 'none'
        });
        return;
      }

      if (!item.subs && this.data.parentActive == item.value) {
        //取消筛选
        this.resetFilter();
        return;
      }
      

      if (item.subs) {
        if (this.data.activeType == item.value && this.data.isShowSubContent) {
          this.cloneFilter();
          return;
        }
        this.setData({
          selectOption: item.child,
          isShowSubContent: true,
          subs_select: this.data.ok_subs_select
        });
      } else {
        this.setData({
          isShowSubContent: false,
          subs_select: null
        });
      }
      this.setData({
        activeType: item.value
      });
      if (!item.subs && item.value != "goods_show") {
        this.setData({
          parentActive: this.data.activeType
        });
        // this.getData();
        this.triggerEvent('changeOptiones',{
          tag: false,
          activeType: this.data.activeType,
          subs_select: this.data.subs_select
        });
      } else if (item.value == "goods_show") {
        this.setData({
          parentActive: this.data.activeType
        });
        // this.getData(true);
        this.triggerEvent('changeOptiones',{
          tag: true,
          activeType: this.data.activeType,
          subs_select: this.data.subs_select
        });
      }
    },
    getStoreSearchMenu() {
      var that = this;
      var urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-store-search-praise');
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        success: function (resdata) {
          var response = resdata.data;
          if (response.result == 1) {
            let option = [];
            if (response.data.is_category_ids == 1) {
              option.push({
                text: "门店分类",
                value: "classify",
                subs: true,
                child: response.data.categories
              });
            }
            if (response.data.is_distance == 1) {
              option.push({
                text: "离我最近",
                value: "distance",
                subs: true,
                child: [{
                  name: "1km内",
                  id: 1
                },
                {
                  name: "3km内",
                  id: 3
                },
                {
                  name: "5km内",
                  id: 5
                },
                {
                  name: "10km内",
                  id: 10
                }
                ]
              });
            }
            if (response.data.is_open_praise == 1) {
              option.push({
                text: "好评优先",
                value: "is_open_praise",
                subs: false
              });
            }
            if (response.data.is_open_sales_volume == 1) {
              option.push({
                text: "销量最高",
                value: "is_open_sales_volume",
                subs: false
              });
            }
            if (response.data.goods_show == 1) {
              option.push({
                text: "优惠力度",
                value: "goods_show",
                subs: false
              });
            }
            that.setData({
              option,
              isshowO2oOptionMenu: true
            });
          }
        },
        fail: function (res) {}
      });
    },
    onSubOptionsItemClick(e) {
      let subs_select = this.data.subs_select == e.currentTarget.dataset.items.id ? null : e.currentTarget.dataset.items.id;
      this.setData({
        subs_select
      });
    },
    cloneFilter() {
      this.setData({
        isShowSubContent: false
      });
    },
    resetFilter() {
      this.setData({
        subs_select: null,
        parentActive: null,
        isShowSubContent: false
      });
      this.triggerEvent('resetFilter',{});
    },
    filterData() {
      if (this.data.subs_select) {
        this.setData({
          parentActive: this.data.activeType
        });
      }
      this.triggerEvent('changeOptiones',{
        tag: false,
        activeType: this.data.activeType,
        subs_select: this.data.subs_select
      });
      this.setData({
        isShowSubContent: false
      });
    },
  }
});