import logisticsData from '../data/logistics-status';
import costData from '../data/cost';
import orderData from '../data/order';
import order from "../../../utils/controls/order";
import dateParse from '@/utils/tool/date';
import url from '@/utils/tool/url';
import {
    mapState
} from 'vuex';
import modal from "../../../utils/tool/modal";
export default {

    data(){
        return {
            logisticsData,
            costData,
            orderData,
            loadingStatus:true,
            firstLoadingStatus:true,
            payModal:false,
            payModalMessage:'',
            item:{},
            buttonItem:{}
        }
    },

    computed:{
      ...mapState(['config'])
    },

    methods:{
        fetch(obj){
            let useUpdateLoading = this.useUpdateLoading;
            this.useUpdateLoading = false;
            this.loadingStatus = true;
            return this.$request({
                url:'order/getOrderDetail',
                data:{
                    order_id: this.$params.id
                },
                token:true,
                loading: useUpdateLoading ? '加载中' :false,
                message:2
            }).then((data)=>{
                if (data.isSuccess) {
                    this.firstLoadingStatus =false;
                    this.loadingStatus = false;
                    this.initOrderDetail(data.data);
                }
                return obj.success();
            }).catch(obj.fail);
        },

        triggerRouterDetail(){
            if (this.loadingStatus) return ;
          return this.$router.navigateTo({
              name:'distribution',
              params:{
                  order_id: this.$params.id
              }
          });
        },

        initOrderDetail(item = {}){

            this.buttonItem = {
                ...item.orderInfo,
                productList: item.productList
            };

            if (item.orderInfo) {
                item.orderInfo.discount_price = Math.abs(item.orderInfo.discount_price || 0).toFixed(2);
            }

            if (item.orderComment && item.orderComment.length >= 0) {

                item.orderComment.map((item,index)=>{
                    if (typeof item.images==='string' && item.images) {
                        item.images = item.images.split(',');
                    }
                    item.images = url.handleImage(item.images || []);
                });

            }

            this.item = item || {};

            this.setStatus(item.orderInfo || {});

            return this.installPayEnd(item.orderInfo);

        },

        openPayModal(item){
            if (!this.payModal) {
                this.payModal = true;
                this.payModalMessage = item.msg;
            }
        },

        triggerDeleteComment(item,index){

            if (this.deleteCommentStatus) return ;

            return modal.confirm({
                title:'删除评价确认',
                content:'是否删除该商品的评价',
                confirmText:'删除',
                confirmColor:'#FC3538',
                confirm:()=>{
                    return this.$request({
                        url:'product/delCommond',
                        loading:'删除中',
                        token:true,
                        data:{
                            commond_id: item.id
                        },
                        failMessage:true,
                        message:true,
                        next:({status})=> this.deleteCommentStatus = status
                    }).then((data)=>{
                        if (data.isSuccess) {
                            this.item.orderComment.splice(index,1);
                        }
                    });
                }
            });

        }
    },

    onLoad(){

        if (this.$memoryParams.item && this.$memoryParams.item.statusOrder) {
            this.item = JSON.parse(JSON.stringify(this.$memoryParams.item));
            this.setTargetStatus(this.$memoryParams.item.statusOrder.status);
            this.setStatusSpeed(order.createOrderNumberDefaultStatus(this.$memoryParams.item));
        }

    }
}

