<template>
  <div class="base-info">
    <p class="title">基本信息</p>
    <el-form :model="ruleForm" 
             :rules="rules" 
             ref="ruleForm" 
             label-width="104px"
             label-position="left" 
             class="demo-ruleForm"
             :show-message="false">
      <el-form-item label="姓名:" prop="name" v-focus>
        <el-input v-model="ruleForm.name" size="small" :placeholder="ruleForm.name" :disabled="status" ref="nameEdit"></el-input>
        <div class="handle" v-if="tool.checkAuth($route.name,'update')">
          <span v-if="status" @click="status = false">编辑</span>
          <span v-else @click="save()">保存</span>
        </div>
      </el-form-item>
      <el-form-item label="手机号:" prop="phonenumber">
        <el-input v-model="ruleForm.phonenumber" size="small" :placeholder="ruleForm.phonenumber" :disabled="true"></el-input>
        <slot :data="userInfo" name="phoneEdit"></slot>
      </el-form-item>
      <!-- <el-form-item label="密码:" prop="pass">
        <el-input v-model="ruleForm.pass" size="small" placeholder="******" :disabled="true"></el-input>
        <slot :data="userInfo" name="passEdit"></slot>
      </el-form-item> -->
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'baseInfo',
  data() {
    return {
      edit: {
        "classname": "common.usercenter.usercenter",
        "method": "updateUserMsg",
        "content": {
          "name":'',
          "phonenumber":'',
        }
      },
      ruleForm: {
        name:'',
        phonenumber:'',
        pass:'******'
      },
      rules: {
        name:[
         { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        phonenumber:[
         { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        pass:[
         { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      },
      status:true
    };
  },
  directives: {
    //监听name input的焦点失去
    focus: {
      bind(el,node,vm) {
        let handle = (e) => {
          if(!el.contains(e.target)) {
            vm.context.ruleForm.name = vm.context.userInfo.name
            vm.context.status = true            
          }
        }
        el.handle = handle
        document.addEventListener('click',handle)
      },
      unbind(el) {
        document.removeEventListener('click',el.handle)
      }
    }
  },
  props:['userInfo'],
  computed:{
  },
  watch:{
    userInfo: {
      handler(val) {
        this.ruleForm.name = val.name
        this.ruleForm.phonenumber = val.phonenumber
      }
    },
    status: {
      handler(val) {
        if(!val) {
          this.$nextTick(() => {
            this.$refs.nameEdit.focus()
          })
        }        
      }
    }
  },
  methods: {
    save() {
      if(this.ruleForm.name == this.userInfo.name) {
        this.$notify({
          title:'提示',
          message:'请输入新的名称',
          type:'warning'
        })
        return
      }
      this.userInfo.name = this.ruleForm.name
      this.status = true
      this.edit.content.name = this.ruleForm.name
      this.edit.content.phonenumber = this.ruleForm.phonenumber
      this.$api.requested(this.edit).then( res => {
        if(res.code == 1) {
          this.$notify({
            title:'提示',
            message:'名称修改成功',
            type:'success'
          })
        }
      })
    }
  },
};
</script>

<style scoped>
  .base-info {
    margin-bottom: 60px;
  }
  .base-info .title {
    font-size: 16px;
    font-weight: bold;
    color: #333333;
    margin-bottom: 30px;
  }
  .el-input {
    width: 500px;
  }
  /deep/.el-form-item__content {
    display: flex;
  }
  span {
  font-size: 14px;
  font-weight: 400;
  color: #3874F6;
  margin-left: 30px;
  cursor: pointer;
}
</style>
