<template>
  <el-card style="margin: 2px 1px;">
    <div slot="header" class="clearfix">
      <span>{{ $t('profile.personalInformation') }}</span>
    </div>

    <el-form v-loading="loading">
      <el-form-item :label="$t('shop.country')">
        <el-input v-model="countryName" size="medium" prefix-icon="el-icon-user-solid" readonly />
      </el-form-item>
      <el-form-item :label="$t('shop.memberCode')">
        <el-input v-model="user.name" size="medium" prefix-icon="el-icon-user-solid" readonly />
      </el-form-item>
      <el-form-item :label="$t('shop.memberName')">
        <el-input v-model="user.realName" size="medium" prefix-icon="el-icon-user" readonly />
      </el-form-item>
      <el-form-item :label="$t('shop.phoneNumber')">
        <el-input v-model.trim="user.mobile" size="medium" prefix-icon="el-icon-phone-outline" readonly />
      </el-form-item>
      <el-form-item :label="$t('user.email')">
        <el-input v-model.trim="user.email" size="medium" prefix-icon="el-icon-s-comment" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="submit">{{ $t('common.save') }}</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>

import { updateInfo } from '@/api/user'
import usersInfo from '@/utils/usersInfo'
export default {
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          mobile: '',
          realName: ''
        }
      }
    }
  },
  data() {
    return {
      loading: false,
      countryName:''
    }
  },
  mounted() {
    this.getCountries()
  },
  watch:{
    countriesList: {
      handler(newValue, old) {
        if (newValue) {
          this.getCountryId()
        }
      },
    },
  },
  computed:{
    countriesList() {
      return this.$store.getters.getCountriesList
    },
  },
  methods: {
    submit() {
      const data = {
        bankNo: '1',
        bankAddress: '1',
        email: this.user.email,
        mobile: this.user.mobile,
        nation: '',
        openBank: '',
        realName: this.user.realName
      }

      this.loading = true
      updateInfo(data).then(() => {
        setTimeout(() => {
          this.loading = false
        }, 1.5 * 1000)
        this.$message({
          message: 'User information has been updated successfully.',
          type: 'success',
          duration: 5 * 1000
        })
        // 覆盖localStorage
        usersInfo.userEmail(this.user.email)
        // 页面重载
        location.reload()
      }).catch((err) => {
        this.$message({
          message: err,
          type: 'error',
          duration: 5 * 1000
        })
        this.loading = false
      })
    },
    //国家列表
    getCountries() {
      this.$store.dispatch('settings/getCountries')
    },
    getCountryId(){
      let countryId = JSON.parse(window.localStorage.getItem('baseData')).COUNTRY_ID
      let country =  this.countriesList.find(item => item.ID === countryId)
      console.log(country)
      this.countryName = country.NAME
    }
  }
}
</script>
