// components/search-filter/index.js
import api from '../../utils/Api.js'

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    filter_idx: 0,
    selected: {
      order: 'default',
      region_params:{}
    },
    selected_titles: {
      region: '全部地域',
      order: '默认排序'
    },
    orders: [
      {
        id: 1,
        name: '默认排序',
        value: 'default'
      },
      {
        id: 2,
        name: '最新发布',
        value: 'new'        
      }, {
        id: 3,
        name: '价格最低',
        value: 'price'        
      }
    ],

    // 地域选择
    region_selected: {
      level_1: -1,
      level_1_title: '',
      level_2: -1,
      level_2_title: '',
      level_3: -1,
      level_3_title: '',
    },
    region_level1_datas: [],
    region_level2_datas: [],
    region_level3_datas: []
  },
  /**
   * 组件渲染完后的方法
   */
  ready() {
    api.search.filter().then(res => {
      this.setData({
        types: res.data.type,
        catalogs: res.data.catalog_id,
        region_level1_datas: res.data.region_level_1,
        tags: res.data.tags
      })
    })
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 排序筛选
    orderFilter: function (event) {
      let item = event.target.dataset.item
      let _selected = this.data.selected
      _selected.order = item.value
      let _selected_titles = this.data.selected_titles
      _selected_titles.order = item.name
      this.setData({
        filter_idx: 0,
        selected: _selected,
        selected_titles: _selected_titles
      })
      this._submitFilter()
    },

    // 点击筛选项
    filterTap: function(event) {
      let idx = event.target.dataset.idx;
      console.log(idx)
      if (idx == this.data.filter_idx) {
        this.setData({
          filter_idx: 0
        })
      } else {
        this.setData({
          filter_idx: idx
        })
      }
    },
    // 提交筛选数据请求
    _submitFilter() {
      // console.log(this.data.selected)
      let myEventDetail = {
        filters: this.data.selected
      } // detail对象，提供给事件监听函数
      let myEventOption = {} // 触发事件的选项
      this.triggerEvent('filter', myEventDetail, myEventOption)
    },
    // 点击蒙层关闭筛选弹框
    closeFilter: function(event) {
      this.setData({
        filter_idx: 0
      })
    },
    // 阻止子元素冒泡点击事件到付元素
    preventEvent: function(event) {

    },

    // 选择地域
    getLevelDatas: function(level, id) {
      let that = this
      let params = {
        id: id
      }
      api.search.queryRegion(params).then(res => {
        // TODO:success code id 0
        if (res.code == 1) {
          if (level == 1) {
            that.setData({
              region_level2_datas: res.data,
              region_level3_datas: []
            })
          } else if (level == 2) {
            that.setData({
              region_level3_datas: res.data
            })
          }
        }
      })
    },

    /* 选择地域等级 */
    selectRegionLevel: function(event) {
      let region = event.target.dataset.item
      let order = event.target.dataset.order
      // 设置选中的ID
      let _region_selected = this.data.region_selected
      
      if (order == '1') {
        _region_selected.level_1 = parseInt(region.id)
        _region_selected.level_1_title = region.title
      } else if (order == '2') {
        _region_selected.level_2 = parseInt(region.id)
        _region_selected.level_2_title = region.title
      } else if (order == '3') {
        _region_selected.level_3 = parseInt(region.id)
        _region_selected.level_3_title = region.title
      }
      this.setData({
        region_selected: _region_selected
      })
      // 选择区域
      if (order == '1' || order == '2') {
        if (order == '1') {
          _region_selected.level_2 = -1
          _region_selected.level_3 = -1
          this.setData({
            region_selected: _region_selected,
            region_level2_datas: [],
            region_level3_datas: []
          })
        } else {
          _region_selected.level_3 = -1
          this.setData({
            region_selected: _region_selected,
            region_level3_datas: []
          })
        }
        if (region.id == 0) {
          // 搜索上一级数据的全部
          this.submitRegionFilter()
        } else {
          // 请求下一级数据
          this.getLevelDatas(region.level, region.id)
        }
      } else {
        //　搜索当前数据
        this.submitRegionFilter()
      }
    },
    /* 提交地域搜索 */
    submitRegionFilter: function(){
      let region_params = {}
      let region_selected = this.data.region_selected

      let title = '全部地域'
      if (region_selected.level_1 == 0) {
        region_params['key'] = 'level_1'
        region_params['value'] = region_selected.level_1
      }else{
        if (region_selected.level_2 == 0) {
          region_params['key'] = 'level_1'
          region_params['value'] = region_selected.level_1
          title = region_selected.level_1_title
        } else {
          if (region_selected.level_3 == 0) {
            region_params['key'] = 'level_2'
            region_params['value'] = region_selected.level_2
            title = region_selected.level_2_title
          } else {
            region_params['key'] = 'level_3'
            region_params['value'] = region_selected.level_3
            title = region_selected.level_3_title
          }
        }
      }
      // 设置全部地域的标题
      let _selected_titles = this.data.selected_titles
      _selected_titles.region = title
      this.setData({
        selected_titles: _selected_titles
      })
      // 设置地域筛选参数
      let _selected = this.data.selected
      _selected.region_params = region_params
      this.setData({
        selected: _selected,
        filter_idx: 0   // 关闭弹框
      })
      // 提交搜索
      this._submitFilter()
    }
  }
})