<template>
  <el-dialog
    title="修改用户默认语言"
    :visible.sync="visible"
    width="300px"
    :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 filterable v-model="dataForm.languageDefault" clearable>
          <el-option :label="item.languageName" :value="item.languageCode" v-for="(item,index) in languageList "
                     :key="index"></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'
  // import PubSub from 'pubsub-js'
  import {
    searchSysLanguage
  } from "@/api/sysLanguage.js"

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          languageDefault: ''
        },
        language: localStorage.getItem('locale'),
        languageList: []
      }
    },
    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: {
      // 获取多语言列表
      getLanguageList() {
        searchSysLanguage({ languageCode: this.$i18n.locale}).then(({data}) => {
          this.languageList = data.rows
        })
      },
      // 初始化
      init() {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 修改用户默认语言
      dataFormSubmit() {
        updateUserLanguage(this.dataForm).then(({data}) => {
          if (data.code == 0) {
            language: localStorage.setItem('locale',this.dataForm.languageDefault)
            this.$router.go(0);
           // PubSub.publish('search',this.dataForm.languageDefault )
           // this.$router.replace({name: 'login'})
          }
        })
      }
    } ,created() {
      this.getLanguageList()
    },
  }
</script>

