// pages/mine/product/coupon/couponlist.js
// 优惠券列表
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        couponList: [],
        currentPage: 1,
        isRefreshing: false,
        isAddPage: false,
        hasMore: true,
        isLoadMore: false,
        shopId: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            shopId: options.shopId
        })
        this.getCouponList()
    },
    getCouponList() {
        var _self = this
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get(app.urls.getCouponList, {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                _self.setData({
                    isRefreshing: false,
                    couponList: res.data
                })
            })
            .catch(err => {
                wx.hideLoading({})
                _self.setData({
                    isRefreshing: false
                })
            })
    },
    //跳转添加页面
    toAddPage() {
        wx.navigateTo({
            url: '/pages/mine/product/coupon/addcoupon?shopId=' + this.data.shopId,
        })
    },
    //跳转编辑页面
    toEditPage(e) {
        var item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: '/pages/mine/product/coupon/editcoupon?id=' + item.couponId + '&shopId=' + this.data.shopId,
        })
    },
    //删除
    doDel(e) {
        var item = e.currentTarget.dataset.item
        var _self = this
        wx.showModal({
            title: '警告',
            content: '确定要删除该优惠券吗?',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '删除中...',
                    })
                    app.http.delete(app.urls.doDelCoupon.format({
                            ids: item.couponId
                        }), {
                            header: {
                                token: app.globalData.token
                            }
                        })
                        .then(res => {
                            wx.hideLoading({})
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success'
                            })
                            _self.dorefresh()
                        })
                        .catch(err => {
                            wx.hideLoading({})
                        })
                }
            }
        })
    },
    dorefresh() {
        this.setData({
            couponList: [],
            isRefreshing: true,
        })
        this.getCouponList()
    },
    onShow(e) {
        if (this.data.isAddPage) {
            this.setData({
                isAddPage: false
            })
            this.dorefresh()
        }
    },
})