"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var antd_1 = require("antd");
var moment = require("moment");
var React = require("react");
var _ = require("lodash");
var lastStart = null;
var lastEnd = null;
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.opend = false;
        _this.format = 'YYYY-MM-DD';
        // startValue = null;
        // endValue = null;
        _this.state = {
            endOpen: false,
        };
        _this.disabledStartDate = function (startValue) {
            var endValue = lastEnd;
            if (!startValue || !endValue) {
                return false;
            }
            return startValue.valueOf() > endValue.valueOf();
        };
        _this.disabledEndDate = function (endValue) {
            var startValue = lastStart;
            if (!endValue || !startValue) {
                return false;
            }
            return endValue.valueOf() <= startValue.valueOf();
        };
        _this.onStartChange = function (value, str) {
            lastStart = moment(str);
            if (value == null) {
                _this.callBack();
            }
        };
        _this.onEndChange = function (value, str) {
            lastEnd = moment(str);
            if (value == null) {
                _this.callBack();
            }
        };
        _this.handleStartOpenChange = function (open) {
            if (!open) {
                _this.setState({ endOpen: true });
            }
            else {
                if (!_this.opend) {
                    _this.opend = true;
                }
                else {
                    _this.callBack();
                }
            }
        };
        _this.handleEndOpenChange = function (open) {
            _this.setState({ endOpen: open });
            if (!open) {
                _this.opend = false;
                window.setTimeout(function () {
                    _this.callBack();
                }, 20);
            }
        };
        return _this;
    }
    default_1.prototype.componentDidMount = function () {
        if (this.props.showTime) {
            this.format = 'YYYY-MM-DD HH:mm:ss';
        }
    };
    default_1.prototype.callBack = function () {
        this.props.onChange(lastStart ? lastStart.format(this.format) : '', lastEnd ? lastEnd.format(this.format) : '');
    };
    default_1.prototype.render = function () {
        var _this = this;
        var defaultStart = { defaultValue: null, allowClear: this.props.allowClear };
        var defaultEnd = { defaultValue: null, allowClear: this.props.allowClear };
        if (_.has(this.props, 'defaultStart') && lastStart == null) {
            lastStart = moment(this.props.defaultStart);
            defaultStart.defaultValue = moment(this.props.defaultStart);
        }
        if (_.has(this.props, 'defaultEnd') && lastEnd == null) {
            lastEnd = moment(this.props.defaultEnd);
            defaultEnd.defaultValue = moment(this.props.defaultEnd);
        }
        return React.createElement("div", { className: "filter-date-group filter-date-group-" + (this.props.showTime ? 'time' : 'date') },
            React.createElement(antd_1.DatePicker, __assign({}, defaultStart, { disabledDate: function (val) { return _this.disabledStartDate(val); }, showTime: this.props.showTime, format: this.format, placeholder: this.props.showTime ? '开始时间' : '开始日期', onChange: function (val, str) { return _this.onStartChange(val, str); }, onOpenChange: function (open) { return _this.handleStartOpenChange(open); }, size: "default", style: { marginRight: 4 } })),
            React.createElement(antd_1.DatePicker, __assign({}, defaultEnd, { disabledDate: function (val) { return _this.disabledEndDate(val); }, showTime: this.props.showTime, format: this.format, placeholder: this.props.showTime ? '结束时间' : '结束日期', onChange: function (val, str) { return _this.onEndChange(val, str); }, open: this.state.endOpen, onOpenChange: function (open) { return _this.handleEndOpenChange(open); }, size: "default" })));
    };
    return default_1;
}(React.PureComponent));
exports.default = default_1;
