import popup from "$utils/tool/popup";
import {InstructionsMessageType} from "$utils/request";
import {PopupExportComponent} from "$popup/popup-export/const";
import {ExportType} from "$popup/popup-message/const/type";
import verification, {VerificationType} from '$utils/verification/index';
export default <LibMixins>{

    data(){
      return {
          inputPayItem:{
              integral:'其他余额',
              crid:0,
              format:false,
              input:true,
              value:''
          },
          authorization:true,
          paySelect:0,
          payOrder:['weChat','alipay'],
      }
    },

    computed:{

        payList(){
            return [
                ...this.$store.state.pay.payList,
                this.inputPayItem
            ]
        }

    },

    methods:{

        triggerPayNumber(index){

            if (this.paySelect !== index) {
                this.paySelect = index;
            }

        },

        payOrderInfoVerify(callback){

            if (!this.authorization) return popup.$toast('请同意充值服务协议');

            let item = this.payList[this.paySelect] || undefined;
            if (!item) {
               return popup.$toast('未知错误');
            }

            // @ts-ignore
            if (item.crid == 0 && !verification.trigger[VerificationType.empty](item.value)) {
                return popup.$toast('请输入充值余额');
            }

            return this.triggerPayOrder(callback);

        },

        payOrderInfo(obj){

            let item = this.payList[this.paySelect];

            return this.$request({
                url:'hxPay/create_pay_order',
                token:true,
                data:{
                    crid: item.crid,
                    custom_amount: item.value || '',
                    pay_type: obj.data.type
                },
                message:InstructionsMessageType.other,
                failMessage:true
            }).then((data)=>{

                if (data.isSuccess) {
                    this.triggerPayEnd(data.data);
                }

                return obj.none();

            }).catch(obj.none);

        },

        paySuccessCallback(){
          return this.close();
        },

        openRecharge(){
            return popup.$popup && popup.$popup.open(PopupExportComponent.message,{
                type: ExportType.recharge
            });
        }

    },

    created() {
        this.$store.dispatch('initializationPayPromise');
    },

    beforeUnmount() {
        clearInterval(this.pattern);
    }

}