// mycomponent/payKeyboard/numberKeyboard/numberKeyboard.js
Component({
  /**
   * 组件的属性列表
   */
  externalClasses: ['complate-class', 'close-btn-class', 'complate-text-class'],
  properties: {
    show: {
      value: false,
      type: Boolean
    },
    extraKey: {
      value: '',
      type: String
    },
    theme: {
      value: "default",
      type: String
    },
    title: {
      value: '',
      type: String
    },
    closeButtonText: {
      value: '',
      type: String
    },
    amountModel: {
      value: false,
      type: Boolean
    },
    numberKeyBoardCloseFn: {
      type: Function
    },
    maxlength: {
      value: 999,
      type: Number
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    passKeyArr: [{
      value: 1,
      type: 'number'
    },
    {
      value: 2,
      type: 'number'
    },
    {
      value: 3,
      type: 'number'
    },
    {
      value: 4,
      type: 'number'
    },
    {
      value: 5,
      type: 'number'
    },
    {
      value: 6,
      type: 'number'
    },
    {
      value: 7,
      type: 'number'
    },
    {
      value: 8,
      type: 'number'
    },
    {
      value: 9,
      type: 'number'
    },
    {
      value: 'icon-jianpan',
      type: 'hidden'
    },
    {
      value: 0,
      type: 'number'
    },
    {
      value: 'icon-fontclass-guanbi',
      type: 'deleteBtn'
    },
    ],
    passValueArr: []

  },

  ready() {
    if (this.data.extraKey != '') {
      let obj = {
        value: this.data.extraKey,
        type: "number"
      };
      let passKeyArr = this.data.passKeyArr;
      passKeyArr.splice(9, 1, obj);
      this.setData({
        passKeyArr
      });
    }

    if (this.data.theme == "custom") {
      let passKeyArr = this.data.passKeyArr;
      passKeyArr.pop();
      let obj1 = {
        value: 0,
        type: "number"
      };
      let obj2 = {
        value: ".",
        type: "number"
      };
      passKeyArr.splice(9, 1, obj1);
      passKeyArr.splice(10, 1, obj2);
      this.setData({
        passKeyArr
      });
    }

  },

  /**
   * 组件的方法列表
   */
  methods: {
    handClickPass(evt) {
      let {
        value,
        type
      } = evt.currentTarget.dataset;

      if (type == "number") {
        if (this.data.passValueArr.length >= this.data.maxlength) {
          let floatNum = this.getFloatNum();
          if ((value == "." && this.data.amountModel) || (this.data.amountModel && floatNum != -1 && floatNum < 2)) {
            //这两种情况不return
          } else {
            return;
          }
        }
        if (this.data.amountModel && value == ".") {
          if (this.data.passValueArr.length == 0) return;
          if (this.data.passValueArr.includes(".")) return;
        }
        if (this.data.amountModel) {
          let floatNum = this.getFloatNum();
          if (floatNum >= 2) return;
        }
        this.data.passValueArr.push(value);
      } else if (type == "deleteBtn") {
        this.data.passValueArr.pop();
      } else if (type == "hidden") {
        this.handHiddenKeyboard();
        return;
      }
      let passValueArr = this.data.passValueArr;
      this.setData({
        passValueArr
      });
      this.triggerEvent("input", {
        passValueArr,
        value,
        type
      });
    },
    handHiddenKeyboard() {
      let numberKeyBoardCloseFn = this.data.numberKeyBoardCloseFn;
      if (numberKeyBoardCloseFn && (typeof numberKeyBoardCloseFn == 'function')) {
        let flag = numberKeyBoardCloseFn();
        if (!flag) return;
      }
      this.setData({
        show: false
      });
      this.triggerEvent("hide");
    },
    //获取当前小数点后有几位
    getFloatNum() {
      let index = this.data.passValueArr.indexOf(".");
      if (index >= 0) {
        index++;
        let len = this.data.passValueArr.length;
        index = len - index;
      }
      return index;
    }

  }
});