import * as React from 'react';
import { ViewController } from './ctr';
import GlobalState from './state';
export default class Page extends ViewController {
    constructor(props) {
        super(props);
        this._url = '';
        this._params = {};
        if (!this.props.router)
            return;
        this._router = this.props.router;
        this._url = this.props.location.pathname + this.props.location.search;
        this._params = Object.assign({}, this.props.match && this.props.match.params);
        var search = this.props.location.search.substr(1);
        if (search) {
            search.split('&').forEach(e => {
                var [key, ...value] = e.split('=');
                var valueStr = value[0];
                if (valueStr && valueStr.length) {
                    var val = '';
                    if (value[0].length >= 1) {
                        val = value.join('=');
                    }
                    this._params[key] = decodeURIComponent(val);
                }
            });
        }
    }
    get url() {
        return this._url;
    }
    get pathname() {
        return this.location && this.location.pathname;
    }
    get history() {
        return this.props.history;
    }
    get location() {
        return this.props.location;
    }
    get match() {
        return this.props.match;
    }
    get params() {
        return this._params;
    }
    triggerLoad() {
        this._router && (this._router._current = this);
    }
    triggerRemove() {
        if (this._router && this._router._current === this) {
            this._router._current = null;
        }
    }
    goBack() {
        this.history && this.history.goBack();
    }
    goForward() {
        this.history && this.history.goForward();
    }
    goto(args) {
        var history = this.history;
        if (history) {
            if (typeof args == 'string') {
                args = { url: args };
            }
            var { url, params = {} } = args;
            var pathname = url;
            var search = '';
            var index = url.indexOf('?');
            if (index >= 0) {
                pathname = url.substr(0, index);
                search = url.substring(index);
            }
            for (var i in params) {
                search += '&' + i + '=' + encodeURIComponent(params[i]);
            }
            history.push({ pathname, search });
        }
    }
}
export class Loading extends Page {
    render() {
        return React.createElement("div", null,
            " ",
            this.props.content || '',
            " ");
    }
}
var default_data_page = 10;
export class DataPage extends Page {
    static setDefaultDataPage(page) {
        default_data_page = Number(page) || default_data_page;
    }
    get name() {
        return this.m_name || '';
    }
    set name(value) {
        this.m_name = value || '';
    }
    get dataPage() {
        return this.m_dataPage || default_data_page;
    }
    set dataPage(value) {
        this.m_dataPage = Number(value) || default_data_page;
    }
    get dataPageCount() {
        return Math.ceil(this.total / this.dataPage);
    }
    get data() {
        var name = `${this.name}_data`;
        return this.state[name] || GlobalState.getGlobalState(name) || [];
    }
    set data(value) {
        this.setState({ [`${this.name}_data`]: value || [] });
    }
    get indexPage() {
        return Math.ceil(this.index / this.dataPage);
    }
    get index() {
        return this.m_index || 0;
    }
    set index(value) {
        this.m_index = Number(value) || 0;
    }
    get total() {
        return this.m_total || 0;
    }
    set total(value) {
        this.m_total = Number(value) || 0;
    }
    get length() {
        return this.data.length;
    }
    get hasMore() {
        var data = this.state[`${this.name}_data`];
        if (data && data.length) {
            if (data.length % this.dataPage === 0) {
                return true;
            }
        }
        return false;
    }
    async loadMore() {
        var rawData = this.data;
        this.m_load_data_params = {
            ...this.m_load_data_params,
            limit: [rawData.length, this.dataPage],
        };
        var { value, total } = await this.loadData(this.m_load_data_params);
        this.total = total || value.length;
        this.data = rawData.concat(value);
    }
    async reload(params, page = 0) {
        var dataPage = this.dataPage;
        this.m_load_data_params = {
            ...this.m_load_data_params,
            limit: [Math.max(0, Number(page) || 0) * dataPage, dataPage],
            fetchTotal: 1,
            ...params,
        };
        var { value, total, index } = await this.loadData(this.m_load_data_params);
        this.index = index || 0;
        this.total = total || value.length;
        this.data = value;
    }
    async loadData(params) {
        return { value: [] };
    }
}
