"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentFinderType = void 0;
const jquery_1 = __importDefault(require("jquery"));
const Modal_1 = __importStar(require("../../Modal"));
const util_1 = require("./util");
const DocumentFinderResult_1 = __importDefault(require("./DocumentFinderResult"));
const DocumentFinderForm_1 = __importDefault(require("./DocumentFinderForm"));
const types_1 = require("./types");
Object.defineProperty(exports, "DocumentFinderType", { enumerable: true, get: function () { return types_1.DocumentFinderType; } });
const errorText = 'Помилка на сайті';
const defaultProps = {
    title: 'Пошук документів'
};
class DocumentFinder {
    static create(props) {
        return new DocumentFinder(Object.assign(Object.assign({}, defaultProps), props));
    }
    constructor(props) {
        this.props = props;
        this.state = {
            num: '',
            name: '',
            month: '',
            year: '',
            dateBegin: null,
            dateEnd: null,
            error: null,
            result: [],
            onSubmit: this.handleSubmit.bind(this),
            onReset: this.handleReset.bind(this),
            onSelectItem: this.handleSelectItem.bind(this)
        };
        this.formClass = new DocumentFinderForm_1.default(this.state);
        this.resultClass = new DocumentFinderResult_1.default(this.state);
    }
    handleSubmit() {
        this.state.error = null;
        const jqxhr = jquery_1.default.ajax({
            url: '/ua/core.api.document',
            method: 'POST',
            data: {
                p_num: this.state.num,
                p_name: this.state.name,
                p_month: this.state.month,
                p_year: this.state.year,
                p_date_begin: (0, util_1.dateToChar)(this.state.dateBegin),
                p_date_end: (0, util_1.dateToChar)(this.state.dateEnd)
            },
            dataType: 'json'
        });
        jqxhr.done((data) => {
            if (data.ok) {
                this.state.result = data.result.map((item) => {
                    return Object.assign(Object.assign({}, item), { lawDate: (0, util_1.charToDate)(item.lawDate), beginDate: (0, util_1.charToDate)(item.beginDate), endDate: (0, util_1.charToDate)(item.endDate) });
                });
            }
            else {
                this.state.error = errorText;
            }
        });
        jqxhr.fail(() => {
            this.state.error = errorText;
        });
        jqxhr.always(() => {
            this.render();
        });
    }
    handleReset() {
        this.state.num = '';
        this.state.name = '';
        this.state.month = '';
        this.state.year = '';
        this.state.dateBegin = null;
        this.state.dateEnd = null;
        this.state.error = null;
        this.state.result = [];
        this.render();
    }
    handleSelectItem(item) {
        this.props.elements.forEach(element => {
            switch (element.type) {
                case types_1.DocumentFinderType.DOC_NUM:
                    element.target.val(item.lawNum);
                    break;
                case types_1.DocumentFinderType.DOC_DATE:
                    element.target.val((0, util_1.dateToChar)(item.lawDate));
                    break;
                case types_1.DocumentFinderType.DOC_TYPE:
                    element.target.val(item.lawType);
                    break;
                case types_1.DocumentFinderType.DOC_CODE:
                    element.target.val(item.lawCode);
                    break;
                default:
            }
        });
        Modal_1.default.close();
    }
    openModal(form) {
        Modal_1.default.open(this.props.title, Modal_1.ModalSize.full);
        if (form && form.num) {
            this.state.num = form.num;
        }
        if (form && form.date) {
            this.state.dateBegin = (0, util_1.charToDate)(form.date);
            this.state.dateEnd = (0, util_1.charToDate)(form.date);
        }
        this.render();
    }
    render() {
        const html1 = (0, jquery_1.default)('<div class="row"><div class="col-3"></div><div class="col-9 pre-scrollable"></div></div>');
        html1.find('.col-3').append(this.formClass.render());
        html1.find('.col-9').append(this.resultClass.render());
        Modal_1.default.setBody(html1.get(0));
    }
}
exports.default = DocumentFinder;
