import { VantComponent } from '../common/component';
import { commonProps, inputProps, textareaProps } from './props';
const util = require('../../utils/util.js')
VantComponent({
    field: true,
    classes: ['input-class', 'right-icon-class', 'label-class'],
    props: Object.assign(Object.assign(Object.assign(Object.assign({}, commonProps), inputProps), textareaProps), { size: String, icon: String, label: String, error: Boolean, center: Boolean, isLink: Boolean, leftIcon: String, rightIcon: String, autosize: [Boolean, Object], required: Boolean, iconClass: String, clickable: Boolean, inputAlign: String, customStyle: String, errorMessage: String, arrowDirection: String, showWordLimit: Boolean, errorMessageAlign: String, readonly: {
            type: Boolean,
            observer: 'setShowClear',
        }, clearable: {
            type: Boolean,
            observer: 'setShowClear',
        }, border: {
            type: Boolean,
            value: true,
        }, titleWidth: {
            type: String,
            value: '6.2em',
        }, 
        copyValue:{
            type:String,
            value:null
        },
        showInput:{
            type:Boolean,
            value:true
        }
        }),
    data: {
        focused: false,
        innerValue: '',
        showClear: false,
    },
    created() {
        this.value = this.data.value;
        this.setData({ innerValue: this.value });
    },
    methods: {
        onInput(event) {
            const { value = '' } = event.detail || {}; 
            // 2021-09-29 去除复制粘贴
            if (this.data.type == 'number' || this.data.type == 'digit'){
                let reg = /^[0-9.]*$/ 
                if (!reg.test(value)){
                    this.setData({ innerValue: '' });
                    this.value = {}
                    return
                }                
            } 
            this.value = value;
            this.setShowClear();
            this.emitChange();
        },
        /**
        * @desc : 复制
        * @author : 周兴
        * @date : 2022/8/16 12:16
        */
        toCopy(e) {
            // 外露事件
            this.triggerEvent('tap',e)
            if(!this.data.copyValue)return;
            util.toCopy(this.data.copyValue, this.data.value);
        },
        onFocus(event) {
            this.focused = true;
            this.setShowClear();
            this.$emit('focus', event.detail);
        },
        onBlur(event) {
            this.focused = false;
            this.setShowClear();
            this.$emit('blur', event.detail);
        },
        onBindInput(event){
          this.$emit('onBindInput', event.detail);
        },
        onClickIcon() {
            this.$emit('click-icon');
        },
        onClear() {
            this.setData({ innerValue: '' });
            this.value = '';
            this.setShowClear();
            wx.nextTick(() => {
                this.emitChange();
                this.$emit('clear', '');
            });
        },
        onConfirm(event) {
            const { value = '' } = event.detail || {};
            this.value = value;
            this.setShowClear();
            this.$emit('confirm', value);
        },
        setValue(value) {
            this.value = value;
            this.setShowClear();
            if (value === '') {
                this.setData({ innerValue: '' });
            }
            this.emitChange();
        },
        onLineChange(event) {
            this.$emit('linechange', event.detail);
        },
        onKeyboardHeightChange(event) {
            this.$emit('keyboardheightchange', event.detail);
        },
        emitChange() {
            let value = this.value;
            // 转大写
            if(value){
              value = value.toUpperCase();
            }
            this.setData({ value: value });
            wx.nextTick(() => {
                this.$emit('input', value);
                this.$emit('change', value);
            });
        },
        setShowClear() {
            const { clearable, readonly } = this.data;
            const { focused, value } = this;
            this.setData({
                showClear: !!clearable && !!focused && !!value && !readonly,
            });
        },
        noop() { },
    },
});
