import { request } from "../../../utils/request";
import { shopId } from '../../../env';
// components/static/coupon-record/coupon-record.ts
Component({

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

    },

    /**
     * 组件的初始数据
     */
    data: {
        couponList: []
    },
    pageLifetimes: {
        show(this: WechatMiniprogram.Component.TrivialInstance) {
            this.search();
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        async search(this: WechatMiniprogram.Component.TrivialInstance) {
            try {
                const res = await request({
                    options: {
                        url: "/app/mini-app/user-coupon",
                        data: {
                            mbuId: wx.getStorageSync("mbuId"),
                            shopId: shopId
                        }
                    },
                    needLogin: true
                })
                if (res.data.length > 0) res.data.forEach((item: any) => {
                    item.discountPrice = (item.discountPrice / 100).toFixed(2);
                    item.usePrice = item.usePrice / 100;
                    item.validStartTime = item.validStartTime.replace("T", " ");
                    item.validEndTime = item.validEndTime.replace("T", " ");
                });  //金额单位为分
                this.setData({ couponList: res.data })
            } catch (e) {
                console.log(e, 'error');
                wx.showToast({
                    title: "获取优惠券列表失败",
                    icon: "none",
                    duration: 2000
                });
            }
        },
        handleClick(this: WechatMiniprogram.Component.TrivialInstance) {
            wx.switchTab({
                url: "/pages/tab-bar/medicine-box/index"
            })
        }
    }
})