import { ViewEngine } from './view-engine';

/**
 * 搜索视图引擎
 *
 * @export
 * @class SearchViewEngine
 * @extends {ViewEngine}
 */
export class SearchViewEngine extends ViewEngine {

    /**
     * 表单部件
     *
     * @protected
     * @type {*}
     * @memberof SearchViewEngine
     */
    protected searchForm: any;

    /**
     * 初始化
     *
     * @param {*} options
     * @memberof SearchViewEngine
     */
    public init(options: any): void {
        this.searchForm = options.searchform;
        super.init(options);

    }

    /**
     * 引擎加载
     *
     * @param {*} [opts={}]
     * @memberof SearchViewEngine
     */
    public load(opts: any = {}): void {
        super.load(opts);
        if (this.getSearchForm()) {
            const tag = this.getSearchForm().name;
            this.setViewState2({ tag: tag, action: 'loaddraft', viewdata: this.view.viewparams });
        }
    }

    /**
     * 事件处理
     *
     * @param {string} ctrlName
     * @param {string} eventName
     * @param {*} args
     * @memberof SearchViewEngine
     */
    public onCtrlEvent(ctrlName: string, eventName: string, args: any): void {
        super.onCtrlEvent(ctrlName, eventName, args);
        if (Object.is(ctrlName, 'searchform') || Object.is(ctrlName, 'quicksearchform')) {
            this.searchFormEvent(eventName, args);
        }
    }

    /**
     * 搜索表单事件
     *
     * @param {string} eventName
     * @param {*} [args={}]
     * @memberof SearchViewEngine
     */
    public searchFormEvent(eventName: string, args: any = {}): void {
        if (Object.is(eventName, 'load')) {
            this.onSearchFormLoad(args);
        }
    }

    /**
     * 搜索表单加载完成
     *
     * @param {*} [args={}]
     * @memberof SearchViewEngine
     */
    public onSearchFormLoad(args: any = {}): void {
    }

    /**
     * 数据部件加载之前
     *
     * @param {*} [arg={}]
     * @memberof SearchViewEngine
     */
    public dataCtrlBeforeLoad(arg: any = {}): void {
        if (this.view && this.view.viewparams && Object.keys(this.view.viewparams).length > 0) {
            Object.assign(arg, this.view.viewparams);
        }
        if (this.getSearchForm()) {
            Object.assign(arg, this.getSearchForm().getData());
        }
        if (this.view && !this.view.isExpandSearchForm) {
            Object.assign(arg, { query: this.view.query });
        }
        // 快速分组和快速搜索栏
        let otherQueryParam:any = {};
        if(this.view && this.view.quickGroupData){
            Object.assign(otherQueryParam,this.view.quickGroupData);
        }
        if(this.view && this.view.quickFormData){
            Object.assign(otherQueryParam,this.view.quickFormData);
        }
        // 自定义查询条件
        const dataCtrl:any = this.getDataCtrl();
        if(dataCtrl && dataCtrl.controlInstance &&  dataCtrl.controlInstance.customCond){
            Object.assign(otherQueryParam, { srfdsscope: dataCtrl.controlInstance.customCond });
        }
        Object.assign(arg,{viewparams:otherQueryParam});
    }


    /**
     * 获取搜索表单
     *
     * @returns {*}
     * @memberof SearchViewEngine
     */
    public getSearchForm(): any {
        return this.searchForm;
    }

    /**
     * 获取数据部件
     *
     * @returns {*}
     * @memberof SearchViewEngine
     */
     public getDataCtrl(): any {}

    /**
     * @description 视图销毁
     * @memberof SearchViewEngine
     */
    public destroyed() {
        super.destroyed();
        this.searchForm = null;
    }

}