<template>
  <view
    @click="type == 'select' && $emit('arrow', true)"
    class="sp-new-input-wrap"
  >
    <view class="label">
      <text v-if="isRequire" class="is-require">*</text>
      {{ label }}
     
    </view>
    <view class="sp-new-input" v-if="type == 'textarea'" style="padding:16rpx">
      <u-input
        v-model="v"
        @input="ch"
        :placeholder="placeholder"
        :type="type"
        :class="{ 'sp-new-input-diabled': disabled }"
        :height="height"
        :disabled="disabled"
      ></u-input>
      
    </view>

    <view class="sp-new-input" v-else>
      <u-icon
        class="sp-new-input-icon"
        v-if="icon"
        :name="icon ? icon : ''"
        color="rgba(190,190,190,1)"
        size="38"
      ></u-icon>

      <u-input
        v-model="v"
        @input="ch"
        :placeholder="placeholder"
        :type="type !== 'password' ? 'text' : type"
        :clearable="false"
        :class="{ 'sp-new-input-diabled': disabled }"
        :maxlength="maxlength"
        :disabled="disabled"
      ></u-input>
      <u-icon style="padding-right:12rpx" @click="scanCode"  v-if="endIcon&&!disabled"  size="60" :name="endIcon"></u-icon>
      <view class="wrap sms" v-if="isSms">
        <u-toast ref="uToast"></u-toast>
        <u-verification-code
          :seconds="seconds"
          @end="end"
          @start="start"
          ref="uCode"
          @change="codeChange"
        ></u-verification-code>
        <span class="btn-txt" @click="getCode">{{ tips }}</span>
      </view>
    </view>

    <view
      v-if="type == 'select'"
      class="arrow"
      @click="$emit('arrow', true)"
    ></view>
    
    <view class="sub">
      <slot name="sub"></slot>
    </view>
  </view>
</template>

<script>
export default {
  name: "spInput",
  props: {
    label: {
      type: String,
      default: "",
    },
    value: {
      type: String,
      default: "",
    },
    placeholder: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "text",
    },
    // 是否开启短信验证
    isSms: {
      type: Boolean,
      default: false,
    },

    isRequire: {
      type: Boolean,
      default: false,
    },
    maxlength: {
      type: Number,
      default: -1,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    icon: {
      type: String,
      default: "",
    },
    height: {
      type: Number,
      default: 100,
    },
    endIcon:{//输入框后面的icon
      type: String,
      default: "",
    }
  },
  data() {
    return {
      v: this.value,
      ele: false,
      getSmsFlg: 0,
      smsTimer: null,
      smsTxt: "获取验证码",
      smsTxtr: "重新发送",
      isCountdown: true,
      tips: "",
      seconds: 10,
    };
  },
  watch: {
    value(v) {
      this.v = v;
    },
  },
  computed: {},
  mounted() {},
  methods: {
    // 发送验证码逻辑
    codeChange(text) {
      this.tips = text;
    },
    getCode() {
      if (this.$refs.uCode.canGetCode) {
        // 模拟向后端请求验证码
        uni.showLoading({
          title: "正在获取验证码",
        });
        setTimeout(() => {
          uni.hideLoading();
          // 这里此提示会被this.start()方法中的提示覆盖
          uni.$u.toast("验证码已发送");
          this.$emit("ch");
          // 通知验证码组件内部开始倒计时
          this.$refs.uCode.start();
        }, 2000);
      } else {
        uni.$u.toast("倒计时结束后再发送");
      }
    },
    end() {
      // uni.$u.toast("倒计时结束");
    },
    start() {
      // uni.$u.toast("倒计时开始");
    },
    // 数据改变
    ch(e) {
      this.$emit("changevalue", e);
    },
    // 扫一扫 方法
    scanCode(){
      uni.scanCode({
        success: (res) => {
          console.log('扫码结果：' + res.result);
          // 扫码成功后的操作，例如：
          // this.handleScanResult(res.result);
        },
        fail: (err) => {
          console.error('扫码失败：' + err);
        }
      });
    }
  },
  model: {
    prop: "value",
    event: "changevalue",
  },

  destroyed() {
    clearInterval(this.smsTimer);
  },
};
</script>

<style>
</style>