import { observable, action, computed } from 'mobx';

export default class Profile {
  @observable isLoading = false;
  @observable profilleId = '';
  @observable userProfile = {
    firstname : '',
    lastname : '',
    gender: '',
    place_of_birth: '',
    date_of_birth:'',
    phone:'',
    photo : "",
    user: {
      id:'',
      user_type_id: '',
      email:'',
      username: '',
      password:'',
      salt: '',
      full_name:'',
    }
  };

  @observable selectedTab = 'general_information'

  constructor(context) {
    this.http = context.http;
    this.context = context;
  }



  @action
  changeTab(value){
    this.selectedTab = value;
  }

  @action
  getProfile() {
    this.isLoading = true;
    return this.http.get('profile')
      .then(res => {
        this.isLoading = false;
        this.userProfile = res;
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }

  @action
  updateProfile() {
    this.isLoading = true;
    return this.http.put('profile', this.userProfile)
      .then(res => {
        this.isLoading = false;
        this.getProfile()
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
  }

  @action
  updatePhotoProfile() {
    this.isLoading = true;
    return this.http.put('profile', {photo : this.userProfile.photo})
      .then(res => {
        this.isLoading = false;
        this.getProfile()
      })
      .catch(err => {
        this.isLoading = false;
        throw err;
      })
    console.log(this.userProfile);
  }

}
