// components/couponWrap/index.js
import { parseTime } from '../../utils/util';
import activity from '../../api/activity'
import {getColor} from '../../utils/user'

Component({
    externalClasses: ['class', 'coupon-circule-ex-class', 'coupon-item-ex-class'],
    /**
     * 组件的属性列表
     */
    properties: {
        couponList: {
            type: Array,
            default: []
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        flodMap: {
        }
    },

    lifetimes: {
        attached: function() {
            // this.prepareData()
        }
    },
    observers: {
        'couponList'(dataA) { 
           this.prepareData()
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
      async prepareData() {
            if (!this.data.couponList || this.data.couponList.length == 0) {
                this.data.list = [];
                this.data.flodMap = {};
            }
            this.data.couponList.forEach((v, i) => {
                v.validTimesStr = this.validTimes(v)
                if (typeof (v.type || v.couponType) == "string") {
                    v.type = { value: (v.type || v.couponType) }
                } else {
                    v.type = v.type || v.couponType
                }
                v.title = v.title || v.couponName || v.couponTitle
                if (typeof v.couponState == "undefined") {
                    if (v.payStatus == 4) {
                        v.stateStr = "已取消"
                    } else {
                        v.stateStr = "未支付"
                    }
                } else if (v.couponState == 1) {
                    v.stateStr = "已使用"
                } else if (v.couponState == 2) {
                    v.stateStr = "已过期"
                } else if (v.couponState == 3) {
                    v.stateStr = "已取消"
                } else if (v.couponState == 4) {
                    v.stateStr = "已停用"
                } else if (v.couponState == 5) {
                    v.stateStr = "转增中"
                } else {
                    v.stateStr = "不可用"
                }
                if (this.data.list) {
                    let node = this.data.list[i];
                    if (node) {
                        v.description = node.description || '';
                        v.bindShopType = node.bindShopType || 1;
                        v.bindGoodsType = node.bindGoodsType || 1;
                        v.isLoadCouponDetail = node.isLoadCouponDetail || false;
                    }
                }
            })
            this.setData({
                list: this.data.couponList
            })
            let info =  await getColor();
            this.setData({
              config:info.config
            })
        },
        exFoldPanel(e) {
            const index = e.currentTarget.dataset.index
            if (this.data.flodMap[index]) {
                this.data.flodMap[index] = false
                this.setData({
                    flodMap: this.data.flodMap
                })
            } else {
                this.loadCouponData(index)
            }

        },
        loadCouponData(index) {
            if (this.data.list[index].isLoadCouponDetail) {
                this.data.flodMap[index] = true
                this.setData({
                    flodMap: this.data.flodMap
                })
                return
            }
            activity.getCouponDetailData({ couponId: this.data.list[index].couponId }).then( res => {
                if (res.code == 200) {
                    const data = res.data || {}
                    Object.assign(this.data.list[index], {
                        description: data.description || '',
                        bindShopType: data.bindShopType || 1,
                        bindGoodsType: data.bindGoodsType || 1,
                        isLoadCouponDetail: true
                    })
                    this.data.flodMap[index] = true
                    this.setData({
                        flodMap: this.data.flodMap,
                        list: this.data.list
                    })
                }
            }).catch( _ => { console.log(_)})
        },
        validTimes(item) {
            if (item.isSn) {
                if (item.couponStartTime && item.couponEndTime) {
                    return parseTime(item.couponStartTime, "{y}.{m}.{d}") + " - " + parseTime(item.couponEndTime, "{y}.{m}.{d}")
                } else {
                    return ""
                }
            }
            const timeCondition = item.timeCondition || {}
            if (timeCondition.type == "DATE_TYPE_FIXED_TERM") {
                return "领券当日起" + timeCondition.fixedTerm + "天内有效"
            }
            if (timeCondition.type == "DATE_TYPE_FIXED_TIME_RANGE") {
                return parseTime(timeCondition.beginTimestamp, "{y}.{m}.{d}") + " - " + parseTime(timeCondition.endTimestamp, "{y}.{m}.{d}")
            }
            return ""
        },
        toFixStorePage(e) {
            const couponId = this.data.list[e.currentTarget.dataset.index].couponId;
            wx.navigateTo({
              url: '/pages/welfareMall/couponFitStore/index?couponId=' + couponId,
            })

        },
        toFixFoodPage(e) {
            const couponId = this.data.list[e.currentTarget.dataset.index].couponId;
            const title = this.data.list[e.currentTarget.dataset.index].title;
            wx.navigateTo({
              url: '/pages/welfareMall/couponFitFood/index?couponId=' + couponId + "&couponName=" + title + "&bindGoodsType=" + 1,
            })
        },
        toFixFoodPageNo(e) {
          const couponId = this.data.list[e.currentTarget.dataset.index].couponId;
          const title = this.data.list[e.currentTarget.dataset.index].title;
          wx.navigateTo({
            url: '/pages/welfareMall/couponFitFood/index?couponId=' + couponId + "&couponName=" + title + "&bindGoodsType=" + 3,
          })
      }
    }
})
