<template>
  <div class="hm-input" :class="{'hm-input--suffix': showSuffix}">
      <!-- 如果传了show-password,判断是否需要切换 密码的显示，如果不传，不判断 -->
      <input 
      :type="showPassword ? (passwordVisible ? 'text' : 'password') : type" 
      class="hm-input__inner"
      :class="{'is-disabled':disabled}"
      :disabled="disabled"
      :name="name"
      :placeholder="placeholder" 
      :value="value"
      @input="handleInput">
      <span class="hm-input__suffix" v-if="showSuffix">
          <i class="hm-input__icon icon-fang-guanbi-quxiao-guanbi" v-if="clearable && value" @click="clear" ></i>
          <i class="hm-input__icon icon-fang-yanjing" v-if="showPassword" @click="handlePassword" :class="{'hm-icon-view-active': passwordVisible}"></i>
      </span>
  </div>
</template>

<script>
export default {
    name: 'HmInput',
    data(){
        return {
            // 用于控制是否显示密码框
            passwordVisible: false
        }
    },
    props: {
        placeholder: {
            type: String,
            default: ''
        },
        type: {
            type: String,
            default: 'text'
        },
        name:{
            type: String ,
            default: ''
        },

        disabled: {
            type: Boolean,
            default: false
        },
        value: {
            type: String,
            default: ''
        },
        clearable: {
            type: Boolean,
            default: false
        },
        showPassword: {
            type: Boolean,
            default: false
        }
    },
    methods: {
        handleInput(e){
            this.$emit('input',e.target.value)
        },
        clear(){
            // 把内容清空
            this.$emit('input','')
        },
        handlePassword(){
            this.passwordVisible = !this.passwordVisible
        }
    },
    computed: {
        // 一旦传小图标，就会有这个类名 hm-input--suffix
        showSuffix(){
            return this.clearable || this.showPassword
        }
    }
}
</script>

<style lang="scss" scoped>
.hm-input{
    width: 100%;
    position: relative;
    font-size: 14px;
    display: inline-block;
    .hm-input_inner{
      -webkit-appearance: none;
      background-color: #fff;
      background-image: none;
      border: 1px solid #dcdfe6;
      border-radius: 4px;
      box-sizing: border-box;
      color: #606266;
      display: inline-block;
      font-size: inherit;
      height: 40px;
      line-height: 40px;
      outline: none;
      padding: 0 15px;
      transition: border-color .2s cubic-bezier(.645,045,.355,1);
      width: 100%;
 
      &:focus{
        outline: none;
        border-color: #409eff;
      }
      // input禁用样式
      &.is-disabled{
        background-color: #f5f7fa;
        border-color: #e4e7ed;
        color: #c0c4cc;
        cursor:not-allowed;
      }
    }
  }
.hm-input--suffix{
    .hm-input__inner {
        padding-right: 30px;
    }
    .hm-input__suffix{
        // position: absolute;
        position: relative;
        right: 20px;
        height: 100%;
        top: 0;
        line-height: 40px;
        text-align: center;
        color: #c0c4cc;
        transition: all .3s;
        z-index: 900;
        i{
        color: #c0c4cc;
        font-size: 14px;
        cursor: pointer;
        transition: color .2s cubic-bezier(.645,.045,.355,1);
        }
        .hm-icon-view-active {
             color:blue;
        }
    }
  }

</style>