<template>
<div class="view-container customermedit-view9">
    <card class='view-card view-no-caption' :disHover="true" :padding="0" :bordered="false">
        <div class="content-container">
            <view_meditviewpanel 
                :viewState="viewState"  
                :viewparams="viewparams" 
                :context="context" 
                :showBusyIndicator="true" 
                :saveRefView="saveRefView" 
                @viewdatadirty="onViewDataDirty" 
                @drdatasaved="onDRDataSaved" 
                updateAction=""
                removeAction="RemoveTemp"
                loaddraftAction="GetDraftTemp"
                loadAction=""
                createAction=""
                fetchAction="FetchTempDefault"
                name="meditviewpanel"  
                ref='meditviewpanel' 
                @closeview="closeView($event)">
            </view_meditviewpanel>
        </div>
    </card>
</div>
</template>

<script lang='ts'>
import { Vue, Component, Prop, Provide, Emit, Watch } from 'vue-property-decorator';
import { UIActionTool } from '@/utils';
import { Subject } from 'rxjs';
import CUSTOMERService from '@/service/customer/customer-service';




@Component({
    components: {
    },
})
export default class CUSTOMERMEditView9Base extends Vue {

    /**
     * 实体服务对象
     *
     * @type {CUSTOMERService}
     * @memberof CUSTOMERMEditView9Base
     */
    protected appEntityService: CUSTOMERService = new CUSTOMERService;

    /**
     * 数据变化
     *
     * @param {*} val
     * @returns {*}
     * @memberof CUSTOMERMEditView9Base
     */
    @Emit() 
    protected viewDatasChange(val: any):any {
        return val;
    }

    /**
     * 传入视图上下文
     *
     * @type {string}
     * @memberof CUSTOMERMEditView9Base
     */
    @Prop() protected viewdata!: string;

    /**
     * 传入视图参数
     *
     * @type {string}
     * @memberof CUSTOMERMEditView9Base
     */
    @Prop() protected viewparam!: string;

    /**
     * 视图默认使用
     *
     * @type {boolean}
     * @memberof CUSTOMERMEditView9Base
     */
    @Prop({ default: true }) protected viewDefaultUsage!: boolean;

	/**
	 * 视图标识
	 *
	 * @type {string}
	 * @memberof CUSTOMERMEditView9Base
	 */
	protected viewtag: string = 'a909e7dbc7fa4a2b0ad7bcdc093321a1';

    /**
     * 视图模型数据
     *
     * @type {*}
     * @memberof CUSTOMERMEditView9Base
     */
    protected model: any = {
        srfTitle: '客户多表单编辑视图',
        srfCaption: 'customer.views.meditview9.caption',
        srfSubCaption: '',
        dataInfo: ''
    }

    /**
     * 视图参数变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof CUSTOMERMEditView9Base
     */
    @Watch('viewparam',{immediate: true, deep: true})
    onParamData(newVal: any, oldVal: any) {
        if(newVal){
            Object.assign(this.viewparams, JSON.parse(this.viewparam));
        } 
    }

    /**
     * 处理应用上下文变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof CUSTOMERMEditView9Base
     */
    @Watch('viewdata')
    onViewData(newVal: any, oldVal: any) {
        const _this: any = this;
        if (!Object.is(newVal, oldVal) && _this.engine) {
            _this.parseViewParam();
            _this.engine.load();
        }
        if(!Object.is(newVal, oldVal) ){
    _this.parseViewParam();
    this.viewState.next({ tag: 'meditviewpanel', action: 'load', data: this.context });            
}

    }

    /**
     * 容器模型
     *
     * @type {*}
     * @memberof CUSTOMERMEditView9Base
     */
    protected containerModel: any = {
        view_meditviewpanel: { name: 'meditviewpanel', type: 'MULTIEDITVIEWPANEL' },
    };

    /**
     * 视图状态订阅对象
     *
     * @private
     * @type {Subject<{action: string, data: any}>}
     * @memberof CUSTOMERMEditView9Base
     */
    protected viewState: Subject<ViewState> = new Subject();




    /**
     * 引擎初始化
     *
     * @private
     * @memberof CUSTOMERMEditView9Base
     */
    private engineInit(): void {
    }

    /**
     * 应用上下文
     *
     * @type {*}
     * @memberof CUSTOMERMEditView9Base
     */
    protected context:any = {};

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof CUSTOMERMEditView9Base
     */
    protected viewparams:any = {};

    /**
     * 解析视图参数
     *
     * @private
     * @memberof CUSTOMERMEditView9Base
     */
    private parseViewParam(): void {
        if (!this.viewDefaultUsage && this.viewdata && !Object.is(this.viewdata, '')) {
            Object.assign(this.context, JSON.parse(this.viewdata));
            return;
        }
        const path = (this.$route.matched[this.$route.matched.length - 1]).path;
        const keys: Array<any> = [];
        const curReg = this.$pathToRegExp.pathToRegexp(path, keys);
        const matchArray = curReg.exec(this.$route.path);
        let tempValue: Object = {};
        keys.forEach((item: any, index: number) => {
            Object.defineProperty(tempValue, item.name, {
                enumerable: true,
                value: matchArray[index + 1]
            });
        });
        this.$viewTool.formatRouteParams(tempValue,this.context,this.viewparams);
        //初始化视图唯一标识
        Object.assign(this.context,{srfsessionid:this.$util.createUUID()});
    }

    /**
     * Vue声明周期
     *
     * @memberof CUSTOMERMEditView9Base
     */
    protected created() {
        this.afterCreated();
    }

    /**
     * 执行created后的逻辑
     *
     * @memberof CUSTOMERMEditView9Base
     */    
    protected afterCreated(){
        const secondtag = this.$util.createUUID();
        this.$store.commit('viewaction/createdView', { viewtag: this.viewtag, secondtag: secondtag });
        this.viewtag = secondtag;
        this.parseViewParam();
        
    }

    /**
     * 销毁之前
     *
     * @memberof CUSTOMERMEditView9Base
     */
    protected beforeDestroy() {
        this.$store.commit('viewaction/removeView', this.viewtag);
    }

    /**
     * Vue声明周期(组件初始化完毕)
     *
     * @memberof CUSTOMERMEditView9Base
     */
    protected mounted() {
        this.afterMounted();
    }

    /**
     * 执行mounted后的逻辑
     * 
     * @memberof CUSTOMERMEditView9Base
     */
    protected afterMounted(){
        const _this: any = this;
        _this.engineInit();
        if (_this.loadModel && _this.loadModel instanceof Function) {
            _this.loadModel();
        }
        if(this.formDruipart){
    this.formDruipart.subscribe((res) =>{
        if(Object.is(res.action,'save')){
            let opt ={data:res.data};
            Object.assign(opt,this.context);
            this.viewState.next({ tag:'meditviewpanel', action: 'save', data: opt });
        }
        if(Object.is(res.action,'remove')){
            let opt ={data:res.data};
            Object.assign(opt,this.context);
            this.viewState.next({ tag:'meditviewpanel', action: 'remove', data: opt });
        }
    });
}
this.viewState.next({ tag: 'meditviewpanel', action: 'load', data: this.context });            

    }



    
    /**
     * 打开新建数据视图
     *
     * @param {any[]} args
     * @param {*} [params]
     * @param {*} [$event]
     * @param {*} [xData]
     * @memberof CUSTOMERMEditView9
     */
    protected newdata(args: any[], params?: any, $event?: any, xData?: any) {
    this.$Notice.warning({ title: '错误', desc: '未指定关系视图' });
    }


    /**
     * 打开编辑数据视图
     *
     * @param {any[]} args
     * @param {*} [params]
     * @param {*} [$event]
     * @param {*} [xData]
     * @memberof CUSTOMERMEditView9
     */
    protected opendata(args: any[], params?: any, $event?: any, xData?: any) {
    this.$Notice.warning({ title: '错误', desc: '未指定关系视图' });
    }



    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof CUSTOMERMEditView9Base
     */
    protected closeView(args: any[]): void {
        let _view: any = this;
        if (_view.viewdata) {
            _view.$emit('viewdataschange', [args]);
            _view.$emit('close', [args]);
        } else if (_view.$tabPageExp) {
            _view.$tabPageExp.onClose(_view.$route.fullPath);
        }
    }
        /**
    * 界面关系通讯对象
    *
    * @type {Subject<ViewState>}
    * @memberof IBZSAM02MobMEditView9
    */
    @Prop() public formDruipart!: Subject<ViewState>;




    /**
     * 刷新数据参数
     *
     * @type {number}
     * @memberof CUSTOMERMEditView9
     */
    @Prop() public saveRefView?: number;

    /**
     * 关系数据变化
     *
     * @param {*} $event
     * @memberof CUSTOMERMEditView9
     */
    public onViewDataDirty($event: any) {
        this.$emit('drdatachange', $event);
    }

    /**
     * 关系数据保存执行完成
     *
     * @param {*} $event
     * @memberof CUSTOMERMEditView9
     */
    public onDRDataSaved($event: any) {
        this.$emit('drdatasaved', $event);
    }


}
</script>

<style lang='less'>
@import './customermedit-view9.less';
</style>