// components/password/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
    },
    length: {
      type: Number,
      value: 6,
    },
    title: {
      type: String,
      value: '请输入密码',
    },
    showBtn: {
      type: Boolean,
      value: false
    },
    btnTitle: {
      type: String,
      value: '确 认',
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    password: '',
    showKeyFocus:true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClose() {
      this.setData({
        show: false,
        password: ''
      })
    },
    //失去输入密码焦点
    onblur() {
      console.log('失去焦点');
      this.setData({
        showKeyFocus: false
      })
    },
    //模拟点击唤起键盘
    onShowBlur() {
      console.log('唤起键盘焦点');
      this.setData({
        showKeyFocus: true
      })
    },
    //输入完成自动失去焦点
    onInput(e) {
      let {
        length
      } = this.data
      let {
        cursor
      } = e.detail
      if (cursor == length) {
        console.log('输入完成');
        wx.hideKeyboard()
        this.okResult()
      }
    },
    /* 确认密码返回事件给父组件 */
    okResult() {
      let {
        password
      } = this.data
      this.triggerEvent("onResult", {
        password: password
      });
    },

    onClickOk(){
      let {
        password,
        length
      } = this.data
      if(password && String(password).length == length){
        this.triggerEvent("onClickBack", {
          password: password
        });
        return
      }
    }
  },
  lifetimes:{
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
      console.log('子组件————————detached')
      this.setData({
        password:''
      })
    },
  },
})