// pages/components/blockItem/blockItem.js
const getTypeSn    = require('../../../utils/util.js').getTypeSn;
const parseAppjump = require('../../../utils/util.js').parseAppjump;
const buyVGoods = require('../../../config.js').buyVGoods;
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item_data: {
      type: Object
    },
    summery: {
      type: Array
    },
    special_datas: {
      type: Object,
      value: {}
    },
    block_type: {
      type: String,
      value: ""
    }
  },

  ready() {
    if (!this.properties.special_datas || !(this.properties.special_datas).hasOwnProperty('fcodes')) {
      return;
    }
    const fcodes = this.properties.special_datas.fcodes;
    const goods_id = this.properties.item_data.data;
    const summery = this.properties.summery;
    let fcode = {};
    for (let item of summery) {
      if (item.goods_id == goods_id) {
        this.setData({
          goods: item
        })
        break;
      }
    }
    for (let item of fcodes) {
      if (item.goods_id == goods_id) {
        fcode = item;
        break;
      }
    }
    fcode.usable_time = (function (time) {
      let date = new Date(time * 1000);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      return `${year}.${month}.${day}`;
    })(fcode.usable_time);

    fcode.state = (function (state) {
      let bgColor = '';
      let title = '';
      switch (state) {
        case 0:
          bgColor = "#ffa82d";
          title = "可使用";
          break;
        case 1:
          bgColor = "#bfbfbf";
          title = "已用完";
          break;
        case 2:
          bgColor = "#bfbfbf";
          title = "已过期";
          break;
        case 3:
          bgColor = "#7e7e7e";
          title = '已锁定';
          break;
      }
      return {
        bgColor: bgColor,
        title: title
      }
    })(fcode.state);
    this.setData({
      fcode: fcode
    })
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgShow: false,
    goods: {},
    fcode: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    navigator(e) {
      const type = e.target.dataset.type || e.currentTarget.dataset.type;
      const target_data = e.target.dataset.target || e.currentTarget.dataset.target;
      const title = e.target.dataset.title || e.currentTarget.dataset.title;
      if (!type) return;
      switch (type) 
      {
        case "url": {
          let attr = encodeURIComponent(`${target_data}`);
          let type_sn = getTypeSn(target_data)
          if (type_sn) {
            app.navigateto(`/pages/shareBonus/shareBonus?type_sn=${type_sn}`)
            break;
          }
          app.navigateto('/pages/webView/webView?url=' + attr)
          break;
        }
        case "special": {
          app.navigateto(`/pages/special/special?special_id=${target_data}&title=${title}`)
          break;
        }
        case "goods": {
          let page = `/pages/details/details?goods_id=${target_data}&title=商品详情`
          app.navigateto(page)
          break;
        }
        case "brand": {
          app.navigateto(`/pages/brand/brand?brand_id=${target_data}&title=${title}`)
          break;
        }
        case "keyword": {
          app.navigateto(`/pages/brand/brand?keyword=${target_data}&title=${title}`)
          break;
        }
        case "appjump": 
        {
          //let url = 'xyzshop://www.xyzshops.com/DirectBuyVGoods?goods_id=6217&num=1';
          let url = target_data;
          const [prefix, params] = parseAppjump(url);

          if(prefix == 'xyzshop://www.xyzshops.com/DirectBuyVGoods') {
            let goods_id = params['goods_id'];
            let num = params['num'];
            buyVGoods(app,goods_id,num);
          }
          else if(prefix == 'xyzshop://www.xyzshops.com/NavigatePage')
            switch(params.page){
              case "oil" : 
                app.switchtab('/pages/postage/postage')
                break;
              case "phone" : 
                app.navigateto('/pages/phoneCharges/phoneCharges') 
                break;
              default: 
              console.log(params.page + 'page找不到');
            }
          break;
        }
        default: {
          console.log(type + '类型找不到');
        }
      }
    },
    load(e) {
      this.setData({
        imgShow: true
      });
    }
  }
})
