import controlData from '../data/control';
import tabData from '../data/tab';
export default {

    data(){
        return {
            controlData,
            tabData,
            messageValue:'',
            loadingStatus:true,
            firstLoadingStatus:true,
            info:{},
            skeletonData:[]
        }
    },

    computed:{
      total(){
          // 总价格
          let amount = Number(this.info.amount || 0);
          // 获取优惠价格
          let coupon = Number(this.info.useCouponPrice || 0);
          // 需支付价格
          let price = amount - coupon;
          // 兼容支付价格
          price = price < 0 ? 0 : price;

          return price.toFixed(2);
      },
      count(){
          let count = 0;
          (this.info.productInfo || []).map((item)=>{
              count += (parseInt(item.number || 0) || 0)
          });
          return count;
      }
    },

    methods:{

        triggerControl(item){
            return this[item.trigger] && this[item.trigger]();
        },

        fetch(obj){

            this.loadingStatus = true;
            return this.$request({
                url:'product/getConfirmOrderInfo',
                data:{
                    is_cart:this.$params.is_cart,
                    product_id: this.$params.id || '',
                    cycle:this.$params.cycle || '',
                    per_month:this.$params.per_month || '',
                    number: this.$params.number || 1,
                    address_id: this.address && this.address.id || '',
                    choose_ids: JSON.stringify((this.$params.choose_ids || '').split(','))
                },
                loading:this.uploadOrder ? '加载中':undefined,
                token:true,
            }).then((data)=>{
                this.setInfoDataList(data.data);
                return obj.success();
            }).catch(obj.fail);

        },

        setInfoDataList(data){
            this.loadingStatus = false;
            this.firstLoadingStatus = false;
            this.setAddress(data.addressInfo);
            this.setCoupon(data.couponList);
            if (this.is_distribution){
                this.setDeliver(data);
            }
            this.info = data;
        },

        routerDistribution(){
            if (this.loadingStatus) return null;

            return this.$router.navigateTo({
               name:'distribution',
               params:{
                   count:this.info.deliver_count,
                   start:this.info.deliver_time_first,
                   date:this.info.deliver_time
               }
            });

        }

    },

    onLoad(){
        let count = this.$params.is_cart === '1' ? (this.$params.choose_ids || '').split(',').length: 1;
        let skeletonData = [];
        for (let i=0;i<count;i++) {
            skeletonData.push({
                number:1,
                sales_price:'0.00'
            });
        }
        this.skeletonData = skeletonData;
    }
}