"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);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var _ = require("lodash");
var moment = require("moment");
var index_1 = require("../index");
var antd_1 = require("antd");
var DateRange_1 = require("../Table/DateRange");
var AutoComplete_1 = require("./AutoComplete");
var DEFSIZE = 'default';
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.query = {};
        return _this;
    }
    default_1.prototype.componentDidMount = function () {
        _.extend(this.query, this.props.default);
    };
    default_1.prototype.checkbox = function (field) {
        var _this = this;
        var param = {
            defaultValue: []
        };
        if (_.has(this.props.default, field.field)) {
            param.defaultValue = this.props.default[field.field].split(',');
        }
        if (field.conf) {
            _.extend(param, field.conf);
        }
        return React.createElement(index_1.Checkbox.Group, __assign({}, param, { options: field.options.map(function (item) { return { label: item.name, value: item.value }; }), onChange: function (e) { return _this.setValue(field, e.join(',')); } }));
    };
    default_1.prototype.radio = function (field) {
        var _this = this;
        var param = {
            defaultValue: '',
        };
        if (_.has(this.props.default, field.field)) {
            param.defaultValue = this.props.default[field.field];
        }
        if (field.conf) {
            _.extend(param, field.conf);
        }
        return React.createElement(index_1.Radio.Group, __assign({}, param, { size: DEFSIZE, onChange: function (e) { return _this.setValue(field, e.target.value); } }), field.options.map(function (item) { return React.createElement(index_1.Radio, { value: item.value }, item.name); }));
    };
    default_1.prototype.select = function (field) {
        var _this = this;
        var param = {
            defaultValue: undefined,
        };
        if (_.has(this.props.default, field.field)) {
            param.defaultValue = this.props.default[field.field];
        }
        if (field.conf) {
            _.extend(param, field.conf);
        }
        return React.createElement(index_1.Select, __assign({}, param, { size: DEFSIZE, style: { width: field.width - 90 }, placeholder: "\u8BF7\u9009\u62E9" + field.label, allowClear: true, onChange: function (e) { return _this.setValue(field, e); } }), field.options.map(function (item) { return React.createElement(index_1.Select.Option, { value: item.value }, item.name); }));
    };
    default_1.prototype.multiselect = function (field) {
        var _this = this;
        var param = {
            defaultValue: []
        };
        if (_.has(this.props.default, field.field)) {
            param.defaultValue = this.props.default[field.field].split(',');
        }
        if (field.conf) {
            _.extend(param, field.conf);
        }
        return React.createElement(index_1.Select, __assign({}, param, { mode: "multiple", allowClear: true, size: DEFSIZE, style: { width: field.width - 90, height: 22 }, onChange: function (e) { return _this.setValue(field, e); }, placeholder: "\u8BF7\u9009\u62E9" + field.label }), field.options.map(function (item) { return React.createElement(index_1.Select.Option, { value: item.value }, item.name); }));
    };
    default_1.prototype.treeselect = function (field) {
        var _this = this;
        var param = {
            placeholder: '请选择' + field.label,
            defaultValue: undefined,
        };
        if (_.has(this.props.default, field.field)) {
            param.defaultValue = this.props.default[field.field];
        }
        if (field.conf) {
            _.extend(param, field.conf);
        }
        return React.createElement(antd_1.TreeSelect, __assign({}, param, { size: DEFSIZE, style: { width: field.width - 90 }, dropdownStyle: { maxHeight: 400, overflow: 'auto' }, treeDefaultExpandAll: true, onChange: function (e) { return _this.setValue(field, e); } }));
    };
    default_1.prototype.text = function (field) {
        var _this = this;
        var param = {
            defaultValue: '',
        };
        if (_.has(this.props.default, field.field)) {
            param.defaultValue = this.props.default[field.field];
        }
        if (field.conf) {
            _.extend(param, field.conf);
        }
        return React.createElement(index_1.Input, __assign({}, param, { size: DEFSIZE, placeholder: "\u8BF7\u8F93\u5165" + field.label, style: { width: field.width - 90 }, onChange: function (e) { return _this.setValue(field, e.target.value); } }));
    };
    default_1.prototype.number = function (field) {
        var _this = this;
        var param = {
            defaultValue: '',
        };
        if (_.has(this.props.default, field.field)) {
            param.defaultValue = this.props.default[field.field];
        }
        if (field.conf) {
            _.extend(param, field.conf);
        }
        return React.createElement(index_1.InputNumber, __assign({}, param, { size: DEFSIZE, placeholder: "\u8BF7\u8F93\u5165" + field.label, style: { width: field.width - 90 }, onChange: function (e) { return _this.setValue(field, e); } }));
    };
    default_1.prototype.timerange = function (field) {
        var _this = this;
        var def = {};
        var _a = __read(field.field.split(','), 2), startField = _a[0], endField = _a[1];
        if (_.has(this.props.default, startField)) {
            def.defaultStart = this.props.default[startField];
        }
        if (_.has(this.props.default, endField)) {
            def.defaultEnd = this.props.default[endField];
        }
        return React.createElement(DateRange_1.default, __assign({}, field.conf, def, { showTime: true, onChange: function (start, end) { return _this.setValue(field, [start, end]); } }));
    };
    default_1.prototype.daterange = function (field) {
        var _this = this;
        var def = {};
        var _a = __read(field.field.split(','), 2), startField = _a[0], endField = _a[1];
        if (_.has(this.props.default, startField)) {
            def.defaultStart = this.props.default[startField];
        }
        if (_.has(this.props.default, endField)) {
            def.defaultEnd = this.props.default[endField];
        }
        // let def = _.has(this.props.default, field.field) ? this.props.default[field.field] : '';
        return React.createElement(DateRange_1.default, __assign({}, field.conf, def, { showTime: false, onChange: function (start, end) { return _this.setValue(field, [start, end]); } }));
    };
    default_1.prototype.month = function (field) {
        var _this = this;
        var param = {};
        if (_.has(this.props.default, field.field)) {
            param.defaultValue = moment(this.props.default[field.field]);
        }
        if (field.conf) {
            _.extend(param, field.conf);
        }
        return React.createElement(index_1.DatePicker.MonthPicker, __assign({}, param, { placeholder: "\u8BF7\u9009\u62E9\u6708\u4EFD", format: "YYYY-MM", onChange: function (e) { return _this.setValue(field, e.format('YYYY-MM-01')); }, style: { width: field.width - 90 }, size: DEFSIZE }));
    };
    default_1.prototype.autocomplete = function (field) {
        var _this = this;
        return React.createElement(AutoComplete_1.default, __assign({}, field.conf, { style: { width: field.width - 90 }, size: DEFSIZE, onChange: function (val) { return _this.setValue(field, val); }, placeholder: "\u8BF7\u9009\u62E9" + field.label }));
    };
    default_1.prototype.setValue = function (field, value) {
        switch (field.type) {
            case 'text':
                this.query[field.field] = value;
                break;
            case 'checkbox':
                this.query[field.field] = value;
                break;
            case 'radio':
                this.query[field.field] = value;
                break;
            case 'select':
                this.query[field.field] = value;
                break;
            case 'number':
                this.query[field.field] = value;
                break;
            case 'timerange':
                var _a = __read(field.field.split(','), 2), startField = _a[0], endField = _a[1];
                this.query[startField] = value[0];
                this.query[endField] = value[1];
                break;
            case 'daterange':
                var _b = __read(field.field.split(','), 2), startField = _b[0], endField = _b[1];
                this.query[startField] = value[0];
                this.query[endField] = value[1];
                break;
            case 'multiselect':
                this.query[field.field] = value.join(',');
                break;
            case 'treeselect':
                this.query[field.field] = value;
                break;
            case 'month':
                this.query[field.field] = value;
            case 'autocomplete':
                this.query[field.field] = value;
            default:
                this.query[field.field] = value;
                break;
        }
        if (this.props.onchange) {
            this.props.onchange(this.query);
        }
    };
    default_1.prototype.renderItem = function (field) {
        switch (field.type) {
            case 'text':
                return this.text(field);
            case 'checkbox':
                return this.checkbox(field);
            case 'radio':
                return this.radio(field);
            case 'select':
                return this.select(field);
            case 'multiselect':
                return this.multiselect(field);
            case 'treeselect':
                return this.treeselect(field);
            case 'number':
                return this.number(field);
            case 'timerange':
                return this.timerange(field);
            case 'daterange':
                return this.daterange(field);
            case 'month':
                return this.month(field);
            case 'autocomplete':
                return this.autocomplete(field);
            default:
                return this.text(field);
        }
    };
    default_1.prototype.renderField = function (field) {
        return React.createElement(index_1.Col, { style: { width: field.width, display: 'inline-block' } },
            React.createElement("span", { className: "col-label" }, field.label),
            React.createElement("span", { className: "col-input" }, this.renderItem(field)));
    };
    default_1.prototype.renderButton = function (btn) {
        var _this = this;
        var param = {};
        if (btn.upload) {
            param.text = btn.label;
            if (btn.icon) {
                param.icon = btn.icon;
            }
            if (_.isString(btn.upload)) {
                param.url = btn.upload;
            }
            if (btn.accept) {
                param.accept = btn.accept;
            }
            // let url = _.isString(btn.upload) ? btn.upload : undefined;
            return React.createElement(index_1.Col, { style: { width: btn.width }, className: "col-action" },
                React.createElement(index_1.FileUpload.Single, __assign({}, param, { text: btn.label, callback: btn.onClick })));
        }
        if (btn.icon) {
            param.icon = btn.icon;
        }
        if (btn.type) {
            param.type = btn.type;
        }
        param.onClick = function () {
            btn.onClick(_this.query);
        };
        return React.createElement(index_1.Col, { style: { width: btn.width }, className: "col-action" },
            React.createElement(index_1.Button, __assign({}, param, { size: DEFSIZE }), btn.label));
    };
    default_1.prototype.renderRow = function (row, rowHeight) {
        var _this = this;
        if (rowHeight === void 0) { rowHeight = 0; }
        var style = { width: '100%' };
        if (rowHeight) {
            style.height = rowHeight;
        }
        return React.createElement(index_1.Row, { style: style }, row.map(function (field) {
            if (_.has(field, 'show') && field.show === false) {
                return React.createElement(React.Fragment, null);
            }
            if (_.has(field, 'field')) {
                return _this.renderField(field);
            }
            else {
                return _this.renderButton(field);
            }
        }));
    };
    default_1.prototype.render = function () {
        var _this = this;
        return React.createElement(index_1.Layout, { className: "from-panel" }, this.props.rows.map(function (row) { return _this.renderRow(row, 0); }));
    };
    return default_1;
}(React.PureComponent));
exports.default = default_1;
