import dateParse from '@/utils/tool/date';
export default {

    data(){
        return {
            couponModal:false,
            couponList:{},
            couponStorageId:{},
            couponId:{}
        }
    },

    methods:{

        cancelCoupon(){
            if (this.couponModal) {
                this.couponModal = false;
            }
        },

        openCoupon(){

            if (this.loadingStatus) return;

            this.couponStorageId = JSON.parse(JSON.stringify(this.couponId));

            if (!this.couponModal) {
                this.couponModal = true;
            }
        },

        confirmCoupon(){

            this.setConfirmCoupon(this.couponStorageId);

            this.cancelCoupon();

        },

        // 设置优惠券
        setConfirmCoupon(coupon={}){

            let keys = Object.keys(coupon);
            let confirmKeys = Object.keys(this.couponId);
            if (keys !== confirmKeys) {
                // 设置选择的优惠券
                this.couponId = coupon;
                // 设置价格
                let price = 0;
                // 计算总价格
                keys.map((item)=>{
                    price += parseFloat(coupon[item].value);
                });

                if (price) {
                    price = price.toFixed(2);
                } else {
                    price = '';
                }

                // 设置优惠券的总价格
                this.$set(this.info,'useCouponPrice',price);
            }

        },

        changeCoupon({item}){
            if (this.couponStorageId[item.coupon_id]) {
                this.couponStorageId = {};
            } else {
                return this.$set(this,'couponStorageId',{
                    [item.coupon_id]: item
                });
            }
        },

        setCoupon(couponList){

            for (let key in couponList) {
                if (couponList.hasOwnProperty(key)) {
                    couponList[key] = (couponList[key] || []).map((item)=>{

                        item.starttime = dateParse(item.createtime,'YYYY.MM.DD');
                        item.endtime = dateParse(item.duetime,'YYYY.MM.DD');

                        return item;
                    })
                }
            }

            this.couponList = couponList;
        }

    }

}