import transactionData from "$popup/popup-detail/components/game/data/transaction";
import Status from "$mixins/status/const/status";
import socket from "$utils/socket";
import {MessageType} from "$utils/socket/const";

export default <LibMixins>{

    data(){
      return {
          transactionData,
          item:{},
          value:0,
          payOrder:['balance'],
      }
    },

    computed:{
      total(){
          return this.item.number * this.item.price || 0;
      }
    },

    methods:{

        installConfig(config){

            this.value = config.value;

            let dataObject = {};
            this.option.gameList.map((item,index)=>{
                dataObject[item.value] = item;
            });

            this.dataObject = dataObject;

            this.item = this.dataObject[this.value];
        },

        changeGameType(pid){

            if (pid !== this.value) {
                this.value = pid;
                if (this.dataObject && this.dataObject[pid]){
                    this.item = this.dataObject[pid];
                }

            }

        },

        // 点击确认支付触发
        payOrderInfo(data){

            return this.$request({
                url:'player/create_player_order',
                token:true,
                data:{
                    pid: this.item.value,
                    buy_num:this.item.number,
                    trade_password:data.password
                },
                loading:'支付中',
                next:({status})=>{
                    this.payStatus = status ? Status.loading : Status.none
                },
                message:true,
                failMessage:true
            }).then((data)=>{
                if (data.isSuccess) {

                    // 优先更新用户积分
                    if (this.user.integral) {
                       let integral =  parseFloat(this.user.integral) -  this.total;
                        integral = integral <= 0 ? 0 : integral;
                        this.user = {
                            integral
                        }
                    }

                    // 调用接口更新 防止出现意外
                    this.$store.dispatch('updateUserMoneyPromise');

                    // 执行发送消息
                    socket.send(this.option.user.uid,{
                        messageType:MessageType.order,
                        content:{
                            message:'下单了-'+this.item.label,
                            order:{
                                item:this.item,
                                buy_num: this.item.number
                            }
                        }
                    });

                    return this.close();
                }
            });

        }

    },

    created() {
        this.installConfig(this.option);
    }

}
