<template>
  <div id="user_info_change">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="120px"
             class="demo-ruleForm">
      <el-form-item label="当前手机号码" prop="phone">
        <el-input class="input_class" type="tel" v-model="ruleForm.phone"
                  autocomplete="off" v-on:readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="变更后手机号码" prop="phone">
        <el-input class="input_class" type="tel" v-model="ruleForm.changePhone"
                  autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="变更后用户名" prop="code">
        <el-input class="input_class" type="text" v-model="ruleForm.userName"
                  autocomplete="off"></el-input>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { AxiosRequestData } from '@/data/axios_request'
import cache from '@/utils/cache'

export default {
  name: 'user_info_change',
  data() {
    let checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号码不能为空！'))
      }
      if (!(/^1[3456789]\d{9}$/.test(value))) {
        callback(new Error('手机号码有误，请重填！'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        phone: '',
        changePhone: '',
        userName: ''
      },
      account: '',
      rules: {
        phone: [
          { validator: checkPhone, trigger: 'blur' }
        ],
        userPhone: [
          { validator: checkPhone, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.account = this.$attrs.userInfoChange.account
    this.ruleForm.phone = this.$attrs.userInfoChange.entUserMobile
  },
  methods: {
    // 获取当前用户手机号码
    getUserPhone(phone, account) {
      this.ruleForm.phone = phone
      this.account = account
    },
    // 修改用户信息
    changeUserInfo(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitChangeUserInfo()
        } else {
          return false
        }
      })
    },
    // 取消信息变更
    userInfoCancel(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm.phone = ''
      this.ruleForm.userName = ''
      this.ruleForm.changePhone = ''
    },
    // 提交信息变更
    async submitChangeUserInfo() {
      let token = cache.getToken()
      if (this.ruleForm.phone && token) {
        const url = '/auth/isp/user/updCusAcct'
        let response = await AxiosRequestData(
            url,
            token,
            {
              oriUserMobile: this.ruleForm.phone,
              updUserMobile: this.ruleForm.changePhone,
              updUserName: this.ruleForm.userName,
              mobile: this.account,
              reqChannel: 4
            },
            false,
            1
        )
        if (response) {
          if (response.code === 0) {
            this.$message({
              message: '用户信息变更成功！',
              type: 'success'
            })
            this.$attrs.userInfoChange.userInfoCancel()
          } else {
            this.$message.error(response.msg)
          }
        }
      }
    }
  }
}
</script>

<style scoped>
.input_class {
  width: 200px;
}
</style>
