<template>
    <div :class="curClassName" :style="curStyle">
        <template v-if="containerModel.length > 0">
            <div v-for="name of containerModel" class="simpleflex-item__pos" :style="getItemPosStyle(name)">
                <slot :name="name"></slot>
            </div>
        </template>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator';

@Component({})
export default class AppSimpleFlexContainer extends Vue {
    /**
    * 名称
    *
    * @type {string}
    * @memberof AppSimpleFlexContainer
    */
    @Prop() public name!: string;

    /**
     * 下标
     *
     * @type {number}
     * @memberof AppSimpleFlexContainer
     */
    @Prop({ default: 0 }) public index?: number;

    /**
     * 布局模型详情
     *
     * @type {string}
     * @memberof AppSimpleFlexContainer
     */
    @Prop() public layoutModelDetails: any;

    /**
     * 布局模型详情变更
     * @param newVal 
     * @param oldVal 
     */
    @Watch('layoutModelDetails')
    onLayoutModelDetailsChange(newVal: any, oldVal: any) {
        this.initSimpleFlexContainer();
    }

    /**
     * 插槽对象
     *
     * @memberof AppSimpleFlexContainer
     */
    public containerModel: any[] = [];

    /**
     * 项名称
     *
     * @type {*}
     * @memberof AppSimpleFlexContainer
     */
    get itemName() {
        return this.index ? `${this.name}_${this.index}` : this.name;
    }

    /**
     * 初始化SIMPLEFLEX容器
     *
     * @memberof SimpleFlexContainer
     */
    public initSimpleFlexContainer() {
        this.containerModel = [];
        const curLayoutModel = this.layoutModelDetails[this.itemName];
        if (curLayoutModel && curLayoutModel.details && curLayoutModel.details.length > 0) {
            curLayoutModel.details.forEach((key: string) => {
                this.containerModel.push(key);
            })
        }
    }

    /**
    * 当前容器样式类
    */
    get curClassName() {
        const layoutModel = this.layoutModelDetails[this.itemName];
        if (layoutModel) {
            return `app-simpleflex-container ${this.itemName} ${layoutModel.sysCss}`;
        }
    }

    /**
     * 当前容器样式
     */
    get curStyle() {
        const layoutModel = this.layoutModelDetails[this.itemName];
        if (layoutModel) {
            return layoutModel.getElementStyle();
        }
    }

    /**
     * 获取项布局样式
     */
    public getItemPosStyle(name: string) {
        let layoutModel: any;
        if (this.index) {
            layoutModel = this.layoutModelDetails[`${name}_${this.index}`];
        } else {
            layoutModel = this.layoutModelDetails[name];
        }
        if (layoutModel) {
            return layoutModel.getCommonLayoutStyle();
        }
    }


}
</script>
<style lang='less'>
@import 'app-simpleflex-container.less';
</style>