<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">
      <el-form-item label="姓名:" prop="name" v-focus>
        <el-input v-model="ruleForm.name" size="small" :placeholder="ruleForm.name" :disabled="editStatus" ref="nameEdit"></el-input>
        <div class="handle">
          <span v-if="editStatus" @click="editStatus = false">编辑</span>
          <span v-else @click="save('name')">保存</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-item label="邮箱" prop="email" v-emailfocus>
        <el-input v-model="ruleForm.email" size="small" :placeholder="ruleForm.email" :disabled="EmailStatus" ref="email"></el-input>
        <div class="handle">
          <span v-if="EmailStatus" @click="EmailStatus = false">编辑</span>
          <span v-else @click="save('email')">保存</span>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'baseInfo',
  data() {
    return {
      edit: {
        "classname": "common.usercenter.usercenter",
        "method": "updateUserMsg",
        "content": {
          "name":'',
          "phonenumber":'',
          "email":''
        }
      },
      ruleForm: {
        name:'',
        phonenumber:'',
        pass:'******',
        email:''
      },
      rules: {
        name:[
         { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        phonenumber:[
         { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern:/^1[3-9]\d{9}$/, message: '请输入正确手机号码',trigger: 'blur' }
        ],
        email:[
         { required: true, message: '请输入邮箱', trigger: 'blur' },
          { pattern: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/, message: '请输入正确邮箱',trigger: 'blur' }
        ],
        pass:[
         { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      },
      editStatus:true,
      EmailStatus: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.editStatus = true       
            vm.context.$refs.ruleForm.clearValidate()     
          }
        }
        el.handle = handle
        document.addEventListener('click',handle)
      },
      unbind(el) {
        document.removeEventListener('click',el.handle)
      }
    },
    emailfocus: {
      bind(el,node,vm) {
        let handle = (e) => {
          if(!el.contains(e.target)) {
            vm.context.ruleForm.email = vm.context.userInfo.hr.email
            vm.context.EmailStatus = true  
            vm.context.$refs.ruleForm.clearValidate()     
          }
        }
        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
        this.ruleForm.email = val.hr.email
      }
    },
    editStatus: {
      handler(val) {
        if(!val) {
          this.$nextTick(() => {
            this.$refs.nameEdit.focus()
          })
        }        
      }
    },
    EmailStatus: {
      handler(val) {
        if(!val) {
          this.$nextTick(() => {
            this.$refs.email.focus()
          })
        }        
      }
    }
  },
  methods: {
    save(type) {
      this.$refs.ruleForm.validate(async val => {
        if(!val) return  this.$notify({
          title:'提示',
          message:'请按照要求填写信息',
          type: 'warning'
        })   
        if( type == 'name') {
          if(this.ruleForm.name == this.userInfo.name) {
            this.$notify({
              title:'提示',
              message:'请输入新的名称',
              type:'warning'
            })
            return
          }
          this.userInfo.name = this.ruleForm.name
          this.editStatus = true
          this.edit.content.name = this.ruleForm.name
          this.edit.content.phonenumber = this.ruleForm.phonenumber
          
          let res = await this.$api.requested(this.edit)
          this.tool.showMessage(res)
        } else if( type == 'email' ) {
          if(this.ruleForm.email == this.userInfo.hr.email) {
            this.$notify({
              title:'提示',
              message:'请输入新的邮箱',
              type:'warning'
            })
            return
          }
          this.userInfo.email = this.ruleForm.email
          this.EmailStatus = true
          this.edit.content.name = this.ruleForm.name
          this.edit.content.email = this.ruleForm.email
          this.edit.content.phonenumber = this.ruleForm.phonenumber
          
          let res = await this.$api.requested(this.edit)
          this.tool.showMessage(res)
        }
      })
    }
  },
};
</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>
