import {OrderButton, OrderPayStatus} from "$utils/control/order/const/order";

import popup, {PopupExportComponent} from "$utils/tool/popup";

export default <LibMixins>{

    inject:['updateOrderStatus'],

    methods:{

        triggerPayButton(button:Record<string, any>){

            return this.orderTrigger[button.status] && this[this.orderTrigger[button.status]](button);

        },

        // 点击确认完成触发
        triggerCompleteStatus(){

            return popup.$confirm({
               title:'确认该订单已完成?',
               confirm:()=>{
                   return new Promise((resolve, reject)=>{
                       this.$request({
                           url:'player/confirm_palyer_order',
                           data:{
                               oid: this.item.oid
                           },
                           token:true,
                           message:true,
                           failMessage:true,
                           loading:'确认中'
                       }).then((data)=>{
                           resolve(false);
                           if (data.isSuccess) {
                               // 设置订单已完成
                               return this.updateOrderStatus(this.item,OrderPayStatus.completed);
                           }
                       }).catch(reject);
                   });

               }
            });

        },

        triggerCancelOrder(){
            return popup.$confirm({
                title:'是否取消该订单?',
                confirm:()=>{

                    return new Promise((resolve, reject)=>{
                        this.$request({
                            url:'player/cancel_order',
                            data:{
                                oid: this.item.oid
                            },
                            token:true,
                            message:true,
                            failMessage:true
                        }).then((data)=>{
                            resolve(false);
                            if (data.isSuccess) {
                                // 优先更新用户积分
                                if (this.user.integral) {
                                    let integral =  parseFloat(this.user.integral) + this.item.total_amount;
                                    integral = integral <= 0 ? 0 : integral;
                                    this.user = {
                                        integral
                                    }
                                }
                                // 调用接口更新 防止出现意外
                                this.$store.dispatch('updateUserMoneyPromise');

                                // 设置订单已取消
                                return this.updateOrderStatus(this.item,OrderPayStatus.cancel);

                            }
                        }).catch(reject);
                    });

                }
            });
        },

        triggerContact(){
            return popup.$open(PopupExportComponent.chat,{
                user:{
                    uid:this.item.player_uid,
                    nick_name:this.item.player_nick_name,
                    head_pic:this.item.player_head_pic
                }
            });
        },

        // 点击评价触发
        triggerComment(){

            let cateData = (this.$store.state.cate.gameCate || [])[0];

            cateData = cateData && cateData.children || [];

            let game:Record<string, any> = {
                gid: this.item.gid,
                price: this.item.price,
                game_name: this.item.game_name
            };


            for (let i=0,count=cateData.length;i<count;i++) {
                if (cateData[i].game_name === game.game_name) {
                    game.game_ico = cateData[i].game_ico;
                    game.id = cateData[i].gid;
                    break;
                }
            }
            return popup.$open(PopupExportComponent.comment,{
                game,
                oid: this.item.oid,
                order_sn: this.item.order_sn,
                uid: this.item.player_uid,
                gid: this.item.gid,
                pid: this.item.pid
            });
        }

    },

    created(){
        this.orderTrigger = {
            [OrderButton.complete]:'triggerCompleteStatus',
            [OrderButton.cancel]:'triggerCancelOrder',
            [OrderButton.contact]:'triggerContact',
            [OrderButton.comment]:'triggerComment'
        }
    }

}
