import Errors from './errors';

class Form {
   
    constructor(data) {
        this.originalData = data;

        for (let field in data) {
            this[field] = data[field];
        }

        this.errors = new Errors();
    }

    data() {
        let data = {};

        for (let property in this.originalData) {
            data[property] = this[property];
        }

        return data;
    }

    reset() {
        for (let field in this.originalData) {
            this[field] = '';
        }

        this.errors.clear();
    }

    get(url, header) {
        return this.submit('get', url, header);
    }

    post(url, header) {
        return this.submit('post', url, header);
    }
  
    put(url, header) {
        return this.submit('put', url, header);
    }

    patch(url, header) {
        return this.submit('patch', url, header);
    }

    delete(url, header) {
        return this.submit('delete', url, header);
    }

    submit(method, url, header) {
        let vm = this;
        let params = vm.data();

        let set = {};

        if (method == 'get') {
            set = {
                method: method,
                url: url,
                params: params,
                headers: header
            }
        }
        else {
            set = {
                method: method,
                url: url,
                data: params,
                headers: header
            }
        }

        return new Promise((resolve, reject) => {
            axios(set)
            .then(response => {
                vm.onSuccess(response.data);
                resolve(response.data);
            })
            .catch(error => {
                if (error.response.data.errors) {
                    let error_return = error.response.data.errors;
                    Object.keys(error_return).forEach(function (key) {
                        error.response.data.errors[key] = vm.errorCodeConvert(error_return[key], key);
                    });

                    vm.onFail(error.response.data.errors)
                    return reject(error.response.data.errors)
                }
                return reject(error)
            });
        });
    }

    onSuccess(data) {
        // this.reset();
    }

    onFail(errors) {
        this.errors.record(errors);
    }

    errorCodeConvert(code = [], key) {
        let value = {
            att: General.lang(key)
        }
        let return_message = [];
        for (var i = 0; i < code.length; i++) {
            if(code[i] == 'LOGIN_ERROR') {
                return_message.push(General.lang('incorrect_account_or_password'));                
            }
            else if (code[i] == 'REQUIRED') {
                return_message.push(Sprintf(General.lang('this_field_required'), value));                
            }
            else if(code[i] == 'EMAIL_ERROR') {
                return_message.push(General.lang('incorrect_email_format'));                
            }
            else if(code[i] == 'UNIQUE_ERRO') {
                return_message.push(Sprintf(General.lang('the_content_has_already_been_taken'), value));                
            }
            else if(code[i] == 'CONFIRM_ERROR') {
                return_message.push(Sprintf(General.lang('the_content_confirmation_does_not_match'), value));                
            }
            else if(code[i] == 'IMG_ERROR') {
                return_message.push(Sprintf(General.lang('incorrect_image_format'), value));                
            }
            else if(code[i] == 'EXCEL_ERROR') {
                return_message.push(Sprintf(General.lang('incorrect_excel_format'), value));                
            }
            else if(code[i] == 'NUMERIC_ERROR') {
                return_message.push(Sprintf(General.lang('the_content_must_be_a_number'), value));                
            }
            else if(code[i] == 'GREATER_ERROR_0') {
                return_message.push(Sprintf(General.lang('the_content_must_be_greater_than_0'), value));                
            }
            else if(code[i] == 'GREATER_ERROR_-1') {
                return_message.push(Sprintf(General.lang('the_content_must_be_greater_than_0'), value));                
            }
            else if(code[i] == 'LESS_ERROR_100') {
                return_message.push(Sprintf(General.lang('the_content_must_be_less_than_100'), value));                
            }
            else if(code[i] == 'MIN_ERROR_10') {
                return_message.push(Sprintf(General.lang('the_content_must_be_at_least_10_characters'), value));                
            }
            else if(code[i] == 'EXIST_ERROR') {
                return_message.push(Sprintf(General.lang('the_content_not_exist'), value));                
            }
            else if(code[i] == 'STATUS_ERROR') {
                return_message.push(Sprintf(General.lang('status_error'), value));                
            }
            else if(code[i] == 'CURRENT_PASSWORD_NOT_MATCH') {
                return_message.push(Sprintf(General.lang('current_password_not_match'), value));                
            }
            else {
                return_message.push(code[i]);
            }
        }
        return return_message;
    }
}

export default Form; 