<template>
    <drawer
        class="app-drawer"
        :placement="placement"
        :closable="false"
        v-model="isShow"
        :width="width"
        @on-visible-change="onVisibleChange($event)"
        @on-close="close(tempResult)"
    >
        <component
            :is="viewname"
            class="viewcontainer2"
            :staticProps="{ ...this.staticProps, viewDefaultUsage:false}"
            :dynamicProps="{ ...this.dynamicProps }"
            @viewdataschange="dataChange($event)"
            @viewstatechange="stateChange($event)"
            @viewdatasactivated="viewDatasActivated($event)"
            @close="close($event)"
            :ref="viewname">
        </component>
    </drawer>
</template>
<script lang="ts">
import { Vue, Component, Prop } from "vue-property-decorator";
import { Subject } from "rxjs";
import { Util } from '@ibizstudio-ex/runtime';
@Component({
    components: {}
})
export default class AppDrawerCompponent extends Vue {

    /**
     * 视图UI参数
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */
    @Prop() view !:any;

    /**
     * 视图动态参数
     *
     * @type {any}
     * @memberof AppModal
     */
    @Prop({default:{}}) dynamicProps?:any;

    /**
     * 视图静态参数
     *
     * @type {any}
     * @memberof AppModal
     */
    @Prop({default:{}}) staticProps?:any;

    /**
     * 数据传递对象
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */
    subject: null | Subject<any> = new Subject<any>();

    /**
     * 抽屉弹出位置
     *
     * @type {string}
     * @memberof AppDrawerCompponent
     */
    placement:string = '';

    /**
     * 是否显示
     *
     * @type {boolean}
     * @memberof AppDrawerCompponent
     */
    isShow:boolean = false;

    /**
     * 零时结果
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */
    tempResult:any = { ret: '' };

    /**
     * 视图名称
     *
     * @type {string}
     * @memberof AppDrawerCompponent
     */
    viewname:string = '';

    /**
     * 视图宽度
     *
     * @type {number}
     * @memberof AppDrawerCompponent
     */
    width:number = 256;

    /**
     * 视图层级
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */
    zIndex:any = null;

    /**
     * 是否视图数据改变
     *
     * @type {any}
     * @memberof AppDrawerCompponent
     */
    isViewDataChange:boolean = false;

    /**
     * 获取数据传递对象
     *
     * @memberof AppModal
     */
    getSubject(){
        return this.subject;
    }

    /**
     * Vue生命周期created
     *
     * @memberof AppDrawerCompponent
     */
    created() {
        this.viewname = this.view.viewname;
        this.placement = this.view.placement === 'DRAWER_LEFT' ? 'left' : 'right';
        if (this.view.width) {
            if (this.view.width.toString().indexOf('px') > 0) {
                if (!Object.is(this.view.width, '0px')) {
                    this.width = parseInt(this.view.width.toString().slice(0, this.view.width.toString().length - 2));
                } else {
                    this.width = 800;
                }
            } else {
                if (this.view.width !== 0) {
                    this.width = this.view.width;
                } else {
                    this.width = 800;
                }
            }
        } else {
            this.width = 800;
        }
        document.onkeydown = (e) => {
            var keyCode = e.keyCode || e.which || e.charCode;
            if (keyCode == 27) {
                const zIndex = this.$store.getters.getZIndex();
                if (zIndex !== this.zIndex) {
                    return;
                }
                this.isShow = false;
            }
        }
    }

    /**
     * Vue生命周期mounted
     *
     * @memberof AppDrawerCompponent
     */
    mounted() {
        this.isShow = true;
        this.handleZIndex('ivu-drawer-mask', 'ivu-drawer-wrap');
    }

    /**
     * Vue生命周期beforeDestroy
     *
     * @memberof AppDrawerCompponent
     */
    beforeDestroy() {
        if (this.zIndex) {
            this.$store.commit('downZIndex');
        }
    }

    destroyed() {
      this.subject?.complete();
    }

    /**
     * 处理 z-index
     *
     * @memberof AppDrawerCompponent
     */
    handleZIndex(mask: string, wrap: string) {
        this.$store.commit('upZIndex');
        this.zIndex = this.$store.getters.getZIndex();
        const element: Element = this.$el;
        const maskTag: any = element.getElementsByClassName(mask)[0];
        const warpTag: any = element.getElementsByClassName(wrap)[0];
        maskTag.style.zIndex = this.zIndex;
        warpTag.style.zIndex = this.zIndex;
    }

    /**
     * 关闭回调
     *
     * @memberof AppDrawerCompponent
     */
    close(result: any) {
        if (result && Array.isArray(result)) {
            Object.assign(this.tempResult, { ret: 'OK' }, { datas: Util.deepCopy(result) });
        }
        this.isShow = false;
    }

    /**
     * 数据变化回调
     *
     * @memberof AppDrawerCompponent
     */
    dataChange(result: any) {
        this.tempResult = { ret: '' };
        if (result && Array.isArray(result) && result.length > 0) {
            Object.assign(this.tempResult, { ret: 'OK' }, { datas: Util.deepCopy(result) });
        }
    }

    /**
     * 激活数据回调
     *
     * @memberof AppDrawerCompponent
     */
    viewDatasActivated(result: any) {
        if (result && Array.isArray(result) && result.length > 0) {
            this.close(result);
        }
    }

    /**
     * 状态变化回调
     *
     * @memberof AppDrawerCompponent
     */
    stateChange(result: any) {
        if(result.length > 0 && result[0].hasOwnProperty('viewDataChange')){
          this.isViewDataChange = result[0].viewDataChange;
        }
    }

    /**
     * 抽屉显示隐藏回调
     *
     * @memberof AppDrawerCompponent
     */
    onVisibleChange($event: any) {
        const component: any = this.$refs[this.viewname];
        if (component) {
            if (this.isViewDataChange) {
                this.isShow = true;
                const title: any = this.$t('app.tabpage.sureclosetip.title');
                const contant: any = this.$t('app.tabpage.sureclosetip.content');
                this.$Modal.confirm({
                    title: title,
                    content: contant,
                    onOk: () => {
                        this.isShow = false;
                        this.isViewDataChange = false;
                    },
                    onCancel: () => {
                        this.isShow = true;
                    }
                });
            } else {
                this.handleShowState($event);
            }
        }
    }

    /**
     * 处理显示隐藏状态
     *
     * @memberof AppDrawerCompponent
     */
    handleShowState($event: any) {
        if ($event) {
            return;
        }

        setTimeout(() => {
            if (this.subject) {
                if (this.tempResult && Object.is(this.tempResult.ret, 'OK')) {
                    this.subject.next(this.tempResult);
                }
            }
            document.body.removeChild(this.$el);
            this.$destroy();
            this.subject = null;
        }, 500)
    }
}
</script>
<style lang="less">
@import './app-drawer.less';
</style>