import order from "../../../utils/controls/order";
import config from "../../../config/config";
import modal from "../../../utils/tool/modal";
import request from "../../../utils/request";
import toast from "../../../utils/tool/toast";

import notice from '@/utils/notice/index';

export default {

    data(){
        return {
            buttonGroupData: order.status
        }
    },

    methods:{

        /* 点击按钮触发 */
        pressButton:function (item) {
            if (this.buttonStatus || this.disabled)  return;
            if (this[item]) {
                return this[item](this.buttonGroupData[item]);
            }
        },

        /* 确认收货 */
        confirm(item){
            modal.confirm({
                title:'确认收货',
                content:'是否确认收货?',
                confirm:()=>{
                    return request({
                        url:'order/changeOrderStatus',
                        data:{
                            order_id: this.item.id,
                            status:3
                        },
                        message:this.toast ? true : 2,
                        next:({status})=>{
                            this.buttonStatus = status;
                        },
                        token:true,
                        loading:'取消中',
                        failMessage:true
                    }).then((data)=>{
                        if (data.isSuccess) {
                            return this.emitParentStatus('confirm',item,data.msg);
                        }
                    });
                }
            })
        },

        /* 点击评价 */
        comment:function(){

            // 跳转到评价页面
            return this.$router.navigateTo({
                name:'order-comment',
                params:{
                  id:this.item.id,
                  index: this.index,
                  page:this.page
                },
                memoryParams:{
                    data: this.item.productList,
                    item: this.item
                }
            });
        },

        /* 点击售后触发 */
        aftermarket:function(item){

            // 跳转到评价页面
            return this.$router.navigateTo({
                name:'apply-after',
                params:{
                    id:this.item.id,
                    index: this.index,
                    page:this.page,
                    price: this.item.order_price,
                    status: order.createOrderDefaultStatus(this.item)
                },
                memoryParams:{
                    item: this.item,
                    name: (this.item.productList || []).map((item)=> item.title).join(',')
                }
            });
        },

        /* 点击取消按钮触发 */
        cancel:function (item) {

            modal.confirm({
                title:'取消确认',
                content:'是否取消该订单?',
                confirm:()=>{
                    return request({
                        url:'order/changeOrderStatus',
                        data:{
                            order_id: this.item.id,
                            status:-1
                        },
                        message:this.toast ? true : 2,
                        next:({status})=>{
                            this.buttonStatus = status;
                        },
                        token:true,
                        loading:'取消中',
                        failMessage:true
                    }).then((data)=>{
                        if (data.isSuccess) {
                            notice.trigger('user',{
                                update:true
                            });
                            return this.emitParentStatus('cancel',item,data.msg);
                        }
                    });
                }
            })

        },

        distribution:function (){
            return this.$router.navigateTo({
                name:'distribution',
                params:{
                    order_id: this.item.id
                }
            });
        },

        /* 点击支付触发 */
        pay:function(item){

            modal.confirm({
                title: '支付确认',
                content: '是否支付该订单?',
                confirm:()=>{
                    return request({
                        url: 'order/toPay',
                        data:{
                            order_id: this.item.id
                        },
                        token: true,
                        message:1,
                        failMessage:true,
                        next:({status})=>{
                            this.buttonStatus = status;
                        },
                        loading:'处理中'
                    }).then((data)=>{

                        if (data.isSuccess) {
                            notice.trigger('user',{
                                update:true
                            });
                            return this.emitParentStatus('pay',item,data.msg);
                        } else {
                            return this.$emit('openPayModal',{
                                msg:data.msg
                            });
                        }

                    });
                }
            });


        },

        // 触发响应事件
        emitParentStatus:function (name,item,msg) {

            notice.trigger('order',order.createdUpdateObj(name,this.item,this.index,msg));

            return this.$emit('changeStatus',order.createdUpdateObj(name,this.item,this.index,msg));

        },

    }

}