import productShare from '../data/product-share';
import productData from "../data/product";
import productControl from "../data/product-control";
import url from '@/utils/tool/url';

export default {

    data(){
        return {
            productShare,
            // 分享模块是否打开
            productShareModal:false,
            // 是否处于加载中
            loadingStatus:true,
            // 是否加载出现错误
            failMessage:'',
            productData,
            productControl,
            item:{},
            comment:[{}],
            service:{
                service:'收货后结算·快递发货',
                freight:''
            }
        }
    },

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

    methods:{
        openShareModal(){
            if (this.loadingStatus) return;
            if (!this.productShareModal) {
                this.productShareModal = true;
            }
        },
        triggerProduct(item){
            if (item.trigger) {
                this.routerLogin(()=>{
                   this[item.trigger] && this[item.trigger]();
                });
            }
        },

        fetch(obj){

            return this.$request({
                url:'product/getProductDetail',
                data:{
                    product_id:this.$params.id
                },
                cache:true
            }).then((data)=>{

                if (!data.isSuccess) {
                    this.failMessage = data.msg;
                }

                // 设置加载完成
                this.loadingStatus = false;

                // 处理图片列表
                this.setShopDetail(data.data,data.isSuccess);

                return obj.success();

            }).catch(obj.fail);
        },

        // 设置商品详情
        setShopDetail(data,isSuccess){

            data = data || {};

            // 处理图片列表
            if (typeof data.images === 'string') data.images = data.images.split(',') || [];

            data.images = url.handleImage(data.images || []);

            // 热榜
            try {
                let rankTexts = [];
                data.rank_txt.split(data.rank).map((item)=>{
                    rankTexts.push({
                        label:item
                    });
                    if (data.rank) {
                        rankTexts.push({
                            label:data.rank,
                            active:true
                        });
                        delete data.rank;
                    }
                });
                data.rankTexts = rankTexts;
            } catch (e) {

            }

            // 设置运费介绍
            this.service.freight = (data.freight || {}).msg;

            if (isSuccess) {

                data.evaluate = data.evaluate || {};
                // 处理评论列表
                if (data.evaluate.list && !(data.evaluate.list instanceof Array)) {
                    data.evaluate.list = [data.evaluate.list];
                }

                this.comment = (data.evaluate.list || []).map((item)=>{

                    item = item || {};

                    if (typeof item.images==='string' && item.images) item.images = item.images.split(',');

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

                    return item;

                });
            }


            this.item = data;

            // 设置规格
            this.setRule(data.spaces || []);


        },

        // 查看图片
        previewImage(index){

            if (this.loadingStatus) {
                return uni.previewImage({
                    current:this.item.images[index],
                    urls:this.item.images,
                    indicator:"number",
                    loop: true
                })
            }

        },

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

        // 跳转首页
        goHome(){
            return this.$router.switchTab('index');
        },

        // 跳转购物车
        goCar(){
            return this.$router.switchTab('cart');
        },

        // 跳转到排行榜
        routerRanking(){
            return this.$router.navigateTo('ranking');
        },

        // 跳转评价列表
        routerRankingList(){
            if (!this.loadingStatus) {
                return this.routerLogin(()=>{
                    return this.$router.navigateTo({
                        name:'comment',
                        params:{
                            id: this.item.id,
                            ...(this.item.evaluate || {})
                        }
                    });
                });
            }

        }
    },

    onShareAppMessage(){

        return {
            title:this.item.title || this.$params.title,
            imageUrl: this.item.image || this.$params.image || undefined
        }
    }

}