<template>
  <el-dialog
    title="修改用户默认语言"
    :visible.sync="visible"
    :append-to-body="true">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="账号">
        <span>{{ userName }}</span>
      </el-form-item>
      <el-form-item label="当前语言" prop="newPassword">
        <span>{{language}}</span>
      </el-form-item>
      <el-form-item label="新语言" prop="newLanguage">
        <el-select type="password" v-model="dataForm.languageDefault">
          <el-option label="中文" value="cn"></el-option>
          <el-option label="英文" value="en"></el-option>
          <el-option label="日文" value="jp"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {updateUserLanguage} from '@/api/user.js'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          languageDefault: ''
        },
        language: localStorage.getItem('locale')
      }
    },
    computed: {
      userName: {
        get() {
          return this.$store.state.user.name
        }
      },
      mainTabs: {
        get() {
          return this.$store.state.common.mainTabs
        },
        set(val) {
          this.$store.commit('common/updateMainTabs', val)
        }
      }
    },
    methods: {
      // 初始化
      init() {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 修改用户默认语言
      dataFormSubmit() {
        updateUserLanguage(this.dataForm).then(({data}) => {
          if (data.code == 0) {
            this.$router.replace({name: 'login'})
          }
        })
      }
    }
  }
</script>

