<template>
    <div class="app-after-time">
        <span v-if="diffTime =='minutes'">{{curvalue}}{{$t('components.appaftertime.minutesago')}}</span>
        <span v-if="diffTime =='hours'">{{curvalue}}{{$t('components.appaftertime.hoursago')}}</span>
        <span v-if="diffTime =='days'">{{curvalue}}{{$t('components.appaftertime.dayago')}}</span>
        <span v-if="diffTime =='mouth'">{{curvalue}}{{$t('components.appaftertime.monthsago')}}</span>
        <span v-if="diffTime =='years'">{{curvalue}}{{$t('components.appaftertime.yearsago')}}</span>
        <span v-if="!diffTime">&nbsp;</span>
    </div>
</template>

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

@Component({})
export default class AppAfterTime extends Vue  {
    /**
     * 属性项名称
     *
     * @type {string}
     * @memberof AppAfterTime
     */
    @Prop() name!: string;

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

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

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

    protected formStateEvent: Subscription | undefined;
    
    /**
     * 表单绑定数据
     * 
     * @type {any}
     * @memberof AppAfterTime
     */
    @Model('change') value:any;

    /**
     * 当前值
     * 
     * @type {any}
     * @memberof AppAfterTime
     */
    curvalue: string = '';

    /**
     * 毫秒差
     * 
     * @type {any}
     * @memberof AppAfterTime
     */
    diffTime:any='';

    /**
     * 值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppAfterTime
     */
    @Watch('value')
    onValueChange(newVal: any, oldVal: any) {
        this.transTime();
    }

    /**
     * Vue声明周期(处理组件的输入属性)
     *
     * @memberof AppAfterTime
     */
    created(){
        if(this.formState){
            this.formStateEvent =  this.formState.subscribe(({type,data})=>{ 
                if(Object.is('load',type)){
                    this.transTime();
                }
            })
        }
        this.transTime();
    }

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

    /**
     * 处理时间
     * 
     * @memberof AppAfterTime
     */
    transTime(){
        if(this.value){
            let dateString:any = 'YYYY年MM月DD日' || 'YYYY年MM月DD日 HH时mm分ss秒' || 'YYYY-MM-DD HH:mm:ss' || 'YYYY-MM-DD' || 'YYYY/MM/DD HH:mm:ss' || 'YYYY/MM/DD';
            let oldTime = moment(this.value,dateString).valueOf();
            let nowTime = moment().valueOf();
            let diffTime = nowTime - oldTime;
            if(diffTime < 3600000){
                this.curvalue = Math.ceil(diffTime/60000)+'';
                this.diffTime = 'minutes';
            }else if(diffTime < 86400000){
                this.curvalue = Math.ceil(diffTime/3600000)+'';
                this.diffTime = 'hours';
            }else if(diffTime < 2592000000){
                this.curvalue = Math.floor(diffTime/86400000)+'';
                this.diffTime = 'days';
            }else if(diffTime < 31104000000){
                this.curvalue = Math.floor(diffTime/2592000000)+'';
                this.diffTime = 'mouth';
            }else{
                this.curvalue = Math.floor(diffTime/31104000000)+'';
                this.diffTime = 'years';
            }
        }
    }
}
</script>

<style>
    .app-after-time{
        margin-left: 6px;
    }
</style>