import inputData from '../data/input';

import verification from '$utils/verification';
import {InstructionsMessageType} from "$utils/request";

export default <LibMixins>{

    data(){
        return {
            inputData:inputData()
        }
    },

    methods:{

        submitVerify(callback){

            let useInputData = [];

            for (let key in this.inputData) {
                if (this.inputData.hasOwnProperty(key)) {
                    useInputData.push(...this.inputData[key]);
                }
            }

            return verification.verificationPromise(useInputData,true).then((resultData)=>{
                return callback(resultData);
            });

        },

        submit(obj){

            this.verificationCode(obj.data).then((resultData)=>{

                if (resultData.isSuccess) {

                    // 获取所有需要上传的image
                    let upload = [];
                    let uploadItem = [];

                    this.inputData.right.map((item)=>{
                        if (typeof obj.data[item.key] !== 'string' && obj.data[item.key] && this.$refs[item.key]) {
                            upload.push(this.$refs[item.key].triggerUploadPromise(obj.data[item.key]));
                            uploadItem.push(item);
                        }
                    });

                    if (upload.length > 0) {

                        Promise.all(upload).then((data)=>{

                            let failMessage;
                            uploadItem.map((item,index)=>{
                                if (data[index].isSuccess) {
                                    item.value = data[index].data;
                                    obj.data[item.key] = item.value;
                                } else {
                                    failMessage = data[index].msg || '上传失败';
                                }
                            });

                            if (failMessage) {
                                obj.none();
                                return this.$toast(failMessage);
                            } else {
                                return this.authentication(obj);
                            }

                        }).catch(obj.fail);

                    } else {
                        return this.authentication(obj);
                    }

                } else {
                    return obj.none();
                }

            }).catch(obj.fail);

        },

        // 验证验证码是否正确
        verificationCode(resultData){

            return this.$request({
                url:'api/verification_code',
                data:resultData,
                message: InstructionsMessageType.other
            });

        },

        // 实名认证
        authentication(obj){
            return this.$request({
                url:'player/real_name_authentication',
                token:true,
                data: obj.data,
                message: true
            }).then((resultData)=>{
                obj.none();
                if (resultData.isSuccess) {

                    this.user = { is_real:3 };

                    return this.close();
                }

            }).catch(obj.fail);
        }

    }

}