import url from '@/utils/tool/url';
import Throttle from '../../../utils/tool/throttle';
export default {

    data(){
        return {
            count:{
                count:0,
                goodCount:0,
                imgCount:0
            },
            opacity:1
        }
    },

    computed:{
      typeItem(){
          return this.tabData[this.active] || {};
      },
      useCount(){
          let count = this.count[this.typeItem.key] || 0;
          return count > 10 ? 10 : count;
      }
    },

    methods:{

        fetch(obj){
            this.useActive = this.active;
            return this.$request({
                url:'product/getCommondList',
                data:{
                    product_id: this.$params.id,
                    type:this.typeItem.type || 0
                },
                page:obj,
                token:true
            }).then((data)=>{

                data = data.data || {};

                let resultData = data.data || [];

                resultData.map((item)=>{
                    if (typeof item.images === 'string'  && item.images) item.images = item.images.split(',');

                    item.images = url.handleImage(item.images || []);
                })

                delete data.data;

                this.count = data;

                return obj.success(resultData);

            }).catch(obj.fail);

        },

        switchActive(index){

            if (this.active !== index) {
                this.active = index;
                this.opacity = 0;
                return this.reload();
            }

        },

        reload(){
            this.opacity = 1;
            if (this.useActive !== this.active) {
                return this.flatReload(1,true);
            }
        }

    },

    onLoad(){
      this.count = this.$params || {};
    },

    created(){

        this.throttle = new Throttle({
            first:false,
            call:this,
            delay:500,
            handle: this.reload
        });

        this.reload = this.throttle.supper;
    },

    beforeDestroy(){
        this.throttle && this.throttle.destroy();
    }

}