<template>
    <div class="app-inpu-ip">
        <el-input  
            type="text" 
            size="small"
            :disabled="disabled"
            :readonly="readonly"
            v-model="firstIp"
            @focus="getFocus"
            maxlength="3" />.
        <el-input         
            type="text" 
            size="small"
            :disabled="disabled"
            :readonly="readonly"
            v-model="secIp"
            @focus="getFocus"
            maxlength="3" />.
        <el-input 
            type="text"
            size="small"
            maxlength="3" 
            :disabled="disabled"
            :readonly="readonly"
            @focus="getFocus"
            v-model="thirdIp" />.
        <el-input 
            type="text" 
            size="small"
            maxlength="3"
            :disabled="disabled"
            :readonly="readonly"
            @focus="getFocus"
            v-model="forIp" />
    </div>     
</template>

<script lang='ts'>
import { Component, Vue, Prop, Model, Watch, Inject } from 'vue-property-decorator';
import { Subject, Subscription } from 'rxjs';

@Component({
})
export default class AppInputIp extends Vue {  

    /**
     * 应用上下文
     * 
     * @type {any}
     * @memberof AppInputIp
     */
    @Prop() context: any;

    /**
     * 视图参数
     * 
     * @type {any}
     * @memberof AppInputIp
     */
    @Prop() viewparam: any;

    /**
     * 是否启用禁用
     * 
     * @type {any}
     * @memberof AppInputIp
     */
    @Prop({default: false}) disabled: any;

	/**
	 * 只读模式
	 * 
	 * @type {boolean}
	 */
	@Prop({default: false}) readonly?: boolean;

    /**
     * 表单状态对象
     *
     * @type {Subject<any>}
     * @memberof AppInputIp
     */
    @Inject('formState')
    formState!: Subject<any>

    protected formStateEvent: Subscription | undefined;

    /**表单数据绑定
     * 
     * @type {string}
     * @memberof AppInputIp
     */
    @Model('change') ipdata!: string;

    /**
     * 当前组件是否已获取到焦点
     * 
     * @type {boolean}
     * @memberof AppInputIp
     */
    activeElement: boolean = false;

    /**
     * 获取当前值
     *
     * @type {string}
     * @memberof AppInputIp
     */
    CurValue: any[] = [];

    /**
     * 第一段ip
     * 
     * @type {any}
     * @memberof AppInputIp
     */
    firstIp: any = '';

    /**
     * 第二段ip
     * 
     * @type {any}
     * @memberof AppInputIp
     */
    secIp: any = '';

    /**
     * 第三段ip
     * 
     * @type {any}
     */
    thirdIp: any = '';

    /**
     * 第四段ip
     * 
     * @type {any}
     * @memberof AppInputIp
     */
    forIp: any = '';
   
    /**
     * Vue声明周期(处理组件的输入属性)
     *
     * @memberof AppInputIp
     */
    created(){
        if(this.formState){
             this.formStateEvent = this.formState.subscribe(({type,data})=>{  
                if(Object.is('load',type)){
                    this.loadData();
                }
            })
        }
        this.loadData();
    } 

	  destroyed(): void {
      if (this.formStateEvent) {
        this.formStateEvent.unsubscribe();
      }
	  }

    /**
     * 加载数据
     *
     * @memberof AppInputIp
     */
    loadData(){
        if(this.ipdata){
            let iparr:Array<any> = this.ipdata.split('.');
            this.CurValue = iparr;
            this.firstIp = this.CurValue[0];
            this.secIp = this.CurValue[1];
            this.thirdIp = this.CurValue[2];
            this.forIp = this.CurValue[3];
        }
    }

    /**
     * 监听每段ip变化
     * 
     * @memberof AppInputIp
     */
    @Watch('firstIp')
    FirstIpChange(newVal:any,oldVal:any){
        this.checkIpVal(newVal,oldVal,'firstIp',0);
    }

    @Watch('secIp')
    SecIpChange(newVal:any,oldVal:any){
        this.checkIpVal(newVal,oldVal,'secIp',1);
    }

    @Watch('thirdIp')
    ThirdIpChange(newVal:any,oldVal:any){
        this.checkIpVal(newVal,oldVal,'thirdIp',2);
    }

    @Watch('forIp')
    ForIpChange(newVal:any,oldVal:any){
        this.checkIpVal(newVal,oldVal,'forIp',3);
    }

    /**
     * 输入框获取到焦点时设置该组件已获取到焦点
     * 
     * @memberof AppInputIp
     */
    getFocus(){
        this.activeElement = true;
    }

    /**
     * 验证格式
     * 
     * @memberof AppInputIp
     */
    checkIpVal(newVal:any,oldVal:any,flag:any,index:number){
        if(newVal === '') return
        let val = newVal;
        let reg = /^(([0-9]|([1-9]\d)|(1\d\d)|(2([0-4]\d|5[0-5]))))$/g;
        if(reg.test(val)){
            this.CurValue[index] = val;
            if(val.length == 3 && this.activeElement && index >=0 && index <= 2){
                const inputElement: any = this.$children;
                inputElement[index+1]?.focus();
            } 
        }else{
            if(flag){
                let that:any = this;
                that[flag] = oldVal;
                this.CurValue[index] = oldVal;
            }
        }
        if(this.firstIp && this.secIp && this.thirdIp && this.forIp){
            this.$emit('change',this.firstIp+'.'+this.secIp+'.'+this.thirdIp+'.'+this.forIp);
        }
    }
}
</script>

<style lang='less'>
@import './app-input-ip.less';
</style>
