<#ibizinclude>
../@MACRO/CONTROL/CONTROL_HEADER.tsx.ftl
</#ibizinclude>

    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public getDatas(): any[] {
        return this.selectedArray;
    }

    /**
     * 获取单项树
     *
     * @returns {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public getData(): any {
        return null;
    }

    /**
     * 显示处理提示
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    @Prop({ default: true }) public showBusyIndicator?: boolean;

    /**
     * 传入url值
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    private url: string = '<#ibizinclude>../@MACRO/CONTROL/URL.tsx.ftl</#ibizinclude>';
    
    /**
     * 结果集
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    @Prop() public searchAction!: string;
    // 加载
    @Prop() public loadAction?: string;
    // 更新
    @Prop() public updateAction?: string;
    // 删除
    @Prop() public removeAction?: string;

    /**
     * 是否单选
     *
     * @type {boolean}
     * @memberof Market
     */
    @Prop() public isSingleSelect?: boolean;

    /**
     * 当前页
     *
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public pageNumber: number = 1;

    /**
     * 数据
     *
     * @type {any[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public items: any[] = [];

    /**
     * 是否支持分页
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public isEnablePagingBar: boolean = ${ctrl.isEnablePagingBar()?c};;

    /**
     * 分页条数
     *
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public pageSize: number = ${ctrl.getPagingSize()?c};

    /**
     * 总条数
     *
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public totalRecord: number = 0;

    /**
     * 选中数组
     * @type {Array<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public selectedArray: Array<any> = [];

    /**
     * 当前页
     *
     * @type {number}
     * @memberof Main
     */
    public curPage: number = 1;

    /**
     * 分页条数
     *
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public limit: number = ${ctrl.getPagingSize()?c};

    /**
     * Vue声明周期,组件挂载完毕
     *
     * @memberof Container
     */
    public mounted () {
        <#if !ctrl.isEnablePagingBar()>
        this.$el.addEventListener('scroll', ()=> {
            if( this.$el.scrollTop +  this.$el.clientHeight  >=  this.$el.scrollHeight) {
                this.loadMore();
            }
        })
        </#if>
    }

    /**
     * Vue声明周期,组件创建完毕
     *
     * @memberof Container
     */
    public created() {
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(this.name, tag)) {
                    return;
                }
                if (Object.is(action,'load')) {
                    this.refresh(data)
                }
            });
        }
    }

    /**
	 * 加载更多
	 *
	 * @memberof Mob
	 */
    public loadMore(){
        if(this.totalRecord>this.items.length)
        {
            this.curPage = ++this.curPage;
            this.load({});
        }
    }

    /**
     * 刷新
     *
     * @param {*} [opt={}]
     * @memberof Main
     */
    public refresh(opt: any = {}) {
        this.curPage = 1;
        this.items = [];
        this.load(opt);
    }

    /**
     * vue 生命周期
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public destroyed() {
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
        <#if destroyed_block??>
        ${destroyed_block}
        </#if>
    }

    /**
     * 表格数据加载
     *
     * @private
     * @param {*} [arg={}]
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    private load(opt: any = {}): void {
        const arg: any = {...opt};

        const page: any = {};
        Object.assign(page, { current: this.curPage, size: this.limit });
        Object.assign(arg, { page: page });

        const parentdata: any = {};
        this.$emit('beforeload', parentdata);
        Object.assign(arg, parentdata);

        this.serialNumber++;
        const serialnumber = this.serialNumber;
        this.addSerialNumber(this.searchAction, serialnumber);

        const post: Promise<any> = this.$http.post(this.url + this.searchAction, arg, this.showBusyIndicator, serialnumber);
        post.then((response: any) => {
            const { serialnumber: _serialnumber } = response;
            const lastserialnumber = this.getSerialNumber(this.searchAction);
            if (_serialnumber && lastserialnumber && _serialnumber < lastserialnumber) {
                return;
            }

            if (!response || response.status !== 200) {
                if (response.errorMessage) {
                    this.$Notice.error({ title: '错误', desc: response.errorMessage });
                }
                return;
            }
            const data: any = response.data;
            if (Object.keys(data).length > 0) {
                let datas = JSON.parse(JSON.stringify(data.records));
                datas.map((item: any) => {
                    Object.assign(item, { isselected: false });
                });
                this.items.push(...datas);
                this.totalRecord = data.total;
            }
            this.$emit('load', this.items);
        }, (response: any) => {
            const { serialnumber: _serialnumber } = response;
            const lastserialnumber = this.getSerialNumber(this.searchAction);
            if (_serialnumber && lastserialnumber && _serialnumber < lastserialnumber) {
                return;
            }

            if (response && response.status === 401) {
                return;
            }
            this.$Notice.error({ title: '错误', desc: response.errorMessage });
        });
    }

   /**
     * 删除
     *
     * @param {any[]} datas
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public async remove(datas: any[]): Promise<any> {
        if (datas.length === 0) {
            return;
        }
        let dataInfo = '';
        datas.forEach((record: any, index: number) => {
            let srfmajortext = record.srfmajortext;
            if (index < 5) {
                if (!Object.is(dataInfo, '')) {
                    dataInfo += '、';
                }
                dataInfo += srfmajortext;
            } else {
                return false;
            }
        });

        if (datas.length < 5) {
            dataInfo = dataInfo + '共' + datas.length + '条数据';
        } else {
            dataInfo = dataInfo + '...' + '共' + datas.length + '条数据';
        }

        const removeData = () => {
            let keys: any[] = [];
            datas.forEach((data: any) => {
                keys.push(data.srfkey);
            });

            this.serialNumber++;
            const serialnumber = this.serialNumber;
            this.addSerialNumber(this.removeAction, serialnumber);

            const post: Promise<any> = this.$http.post(this.url + this.removeAction, { srfkeys: keys.join(';') }, this.showBusyIndicator, serialnumber);
            return new Promise((resolve: any, reject: any) => {
                post.then((response: any) => {
                    const { serialnumber: _serialnumber } = response;
                    const lastserialnumber = this.getSerialNumber(this.removeAction);
                    if (_serialnumber && lastserialnumber && _serialnumber < lastserialnumber) {
                        return;
                    }

                    if (!response || response.status !== 200) {
                        this.$Notice.error({ title: '', desc: '删除数据失败,' + response.info });
                        return;
                    } else {
                        this.$Notice.success({ title: '', desc: '删除成功!' });
                    }
                    this.load({});
                    this.$emit('remove', null);
                    this.selectedArray = [];
                    resolve(response);
                }).catch((response: any) => {
                    const { serialnumber: _serialnumber } = response;
                    const lastserialnumber = this.getSerialNumber(this.removeAction);
                    if (_serialnumber && lastserialnumber && _serialnumber < lastserialnumber) {
                        return;
                    }

                    if (response && response.status === 401) {
                        return;
                    }
                    if (!response || !response.status || !response.data) {
                        this.$Notice.error({ title: '错误', desc: '系统异常' });
                        reject(response);
                        return;
                    }

                    const { data: _data } = response;
                    this.$Notice.error({ title: _data.title, desc: _data.message });
                    reject(response);
                });
            });
        }

        dataInfo = dataInfo.replace(/[null]/g, '').replace(/[undefined]/g, '').replace(/[ ]/g, '');
        this.$Modal.confirm({
            title: '警告',
            content: '确认要删除 ' + dataInfo + ',删除操作将不可恢复?',
            onOk: () => {
                removeData();
            },
            onCancel: () => { }
        });
        return removeData;
    }

    /**
     * 选择数据
     * @memberof ${srfclassname('${ctrl.codeName}')}
     *
     */
    public handleClick(args: any) {
        args.isselected = !args.isselected;
        if(this.isSingleSelect) {
            this.items.forEach((item:any) =>{
                if(item.srfkey !== args.srfkey){
                    item.isselected =false;
                }
            })
        }
        this.selectchange();
    }

    /**
     * 双击数据
     * @memberof ${srfclassname('${ctrl.codeName}')}
     *
     */
    public handleDblClick(args: any) {
        this.$emit('rowdblclick', args);
    }

    /**
     * 触发事件
     * @memberof ${srfclassname('${ctrl.codeName}')}
     *
     */
    public selectchange() {
        this.selectedArray = [];
        this.items.map((item: any) => {
            if (item.isselected) {
                this.selectedArray.push(item);
            }
        });
        this.$emit('selectionchange', this.selectedArray);
    }


    /**
     * 内容绘制
     *
     * @returns
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public render() {
        return (
            <div class={["app-data-view", this.items.length > 0 ? '' : 'app-data-empty' ]}>
                <#if ctrl.render??>
                ${ctrl.render.code}
                <#else>
                {
                    this.items.length > 0 ?
                    <row gutter={20} type="flex" justify="start">
                        {this.items.map((item: any,index:number) => {
                            return (
                                <i-col <#if ctrl.getCardColLG() gt 0> lg={${ctrl.getCardColLG()?c}}</#if><#if ctrl.getCardColMD() gt 0> md={${ctrl.getCardColMD()?c}}</#if><#if ctrl.getCardColSM() gt 0> sm={${ctrl.getCardColSM()?c}}</#if><#if ctrl.getCardColXS() gt 0> xs={${ctrl.getCardColXS()?c}}</#if> style="<#if ctrl.getCardHeight() gt 0>height: ${ctrl.getCardHeight()?c}<#else>min-height: 170</#if>px;<#if ctrl.getCardWidth() gt 0> width: ${ctrl.getCardWidth()?c}px;</#if>margin-bottom: 10px;">
                                    <div class={[ item.isselected === true ? 'isselected' : false, 'single-card-data' ]} on-click={() => { this.handleClick(item) }} on-dblclick={() =>{this.handleDblClick(item)}}>
                                        <#if ctrl.getItemPSLayoutPanel()??>
                                        <#assign panel = ctrl.getItemPSLayoutPanel()>
                                        <layout_${panel.getName()} name='${panel.name}' data={item}></layout_${panel.getName()}>
                                        <#elseif ctrl.itemRender??>
                                        ${ctrl.itemRender.code}
                                        <#else>
                                        {item.srficonpath ? <img src={item.srficonpath} class="single-card-img" />:<img src="./assets/img/noimg.jpg" class="single-card-img" />}
                                        <div class="single-card-default">
                                            <span>{item.srfmajortext}</span>
                                        </div>
                                        </#if>
                                    </div>
                                </i-col>
                            );
                        })}
                    </row>:
                    '暂无数据'
                }
                </#if>
            </div>
        );
    }
}