<template>
  <el-form ref="form" :model="form" :rules="rules" label-width="100px">
    <el-form-item :label="$t('center.security.oldpwd')" prop="oldpwd">
      <el-col :span="4">
        <el-input v-model="form.oldpwd" show-password type="password"
                  :placeholder="$t('input_please',{keyword:$t('center.security.oldpwd')})"/>
      </el-col>
    </el-form-item>
    <el-form-item :label="$t('center.security.newpwd')" prop="newpwd">
      <el-col :span="4">
        <el-input v-model="form.newpwd" show-password type="password"
                  :placeholder="$t('input_please',{keyword:$t('center.security.newpwd')})"/>
      </el-col>
    </el-form-item>
    <el-form-item :label="$t('center.security.confirmpwd')" prop="confirmpwd">
      <el-col :span="4">
        <el-input v-model="form.confirmpwd" show-password type="password"
                  :placeholder="$t('input_confirm',{keyword:$t('center.security.newpwd')})"/>
      </el-col>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">{{$t('button.submit')}}</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
  import Vue from 'vue'

  export default Vue.extend({
    name: 'security',
    data() {
      let that = this
      return {
        form: {
          oldpwd: '',
          newpwd: '',
          confirmpwd: ''
        },
        rules: {
          oldpwd: [
            {
              required: true,
              message: this.$t('input_please', { keyword: this.$t('center.security.oldpwd') }),
              trigger: 'blur'
            },
            { min: 3, max: 5, message: this.$t('center.security.rules.password', { min: 3, max: 5 }), trigger: 'blur' }
          ],
          newpwd: [
            {
              required: true,
              message: this.$t('input_please', { keyword: this.$t('center.security.newpwd') }),
              trigger: 'blur'
            },
            {
              validator: (rule, value, callback) => {
                 if (value === that.form.oldpwd) {
                  callback(new Error(rule.msg.b))
                } else {
                  callback()
                }
              },
              msg: {
                b: this.$t('center.security.tip.same_err')
              }
            },
            { min: 3, max: 5, message: this.$t('center.security.rules.password', { min: 3, max: 5 }), trigger: 'blur' }
          ],
          confirmpwd: [
            {
              required: true,
              message: this.$t('input_confirm', { keyword: this.$t('center.security.newpwd') }),
            },
            {
              validator: (rule, value, callback) => {
                if (value !== that.form.newpwd) {
                  callback(new Error(rule.msg.a))
                } else {
                  callback()
                }
              },
              msg: {
                a: this.$t('account.form.tip.tow_diff')
              }
            },
            { min: 3, max: 5, message: this.$t('center.security.rules.password', { min: 3, max: 5 }), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      onSubmit() {
        let that=this
        this.$refs.form.validate((valid: boolean) => {
          if (valid) {
            this.GLOBAL.fetchJSON('/v1/api/users/update','PUT',{
              oldpwd:this.form.oldpwd,
              newpwd:this.form.newpwd
            },function(res) {
                if(res.code==='200'){
                  that.$message.info(that.$t('center.security.tip.ok').toString())
                  that.$cookies.remove(that.GLOBAL.user_key)
                  that.$store.commit('read/reset')
                  that.$store.commit('menus/none')
                  that.$router.push(that.localePath('/account'))
                }else if(res.msg!==null){
                  that.$message.error(res.msg)
                }
                else{
                  that.$message.error(that.$t('center.security.tip.fail').toString())
                }
            })

          } else {
            this.$message.error({
              message:this.$t('form_err').toString(),
              duration:2000
            })
            return false
          }
        })

      }
    }
  })
</script>
