﻿
import { TfabricaCrudField } from './tfabrica.crud.field.model';

export class TfabricaCrudReport {

    public title: string;

    public executeImmediately: boolean;

    public connectionString: string;
    public selectRead: string;

    public readedRows: any[];

    public fields: TfabricaCrudField[];
    public fieldsToDisplay: TfabricaCrudField[];

    public success: boolean;
    public message: string;

    constructor() {
        this.readedRows = new Array();
        this.fields = new Array();
    }

    public setFromJson(json) {
        this.connectionString = json.connectionString;
        this.fields = json.fields;
        this.readedRows = json.readedRows;
        this.setFieldsToDisplay();
        this.message = json.message;
        this.success = json.success;
        this.title = json.title;
        this.executeImmediately = json.executeImmediately;

        this.selectRead = json.selectRead;

        this.setFields(this.fields);
    }

    public setFieldsToDisplay() {
        let that = this;
        this.fieldsToDisplay = new Array();

        let position = 0;

        this.fields.forEach(function (entry: TfabricaCrudField) {
            position++;
            if (entry.display) {
                if (entry.position == 0 || entry.position == undefined) entry.position = position;
                that.fieldsToDisplay.push(entry);
            }
        });
        this.fieldsToDisplay.sort(TfabricaCrudField.compare);
    }

    public setFields(fields) {
        this.fields = fields;
        this.fields.sort(TfabricaCrudField.compare);
        this.setFieldsToDisplay();
        return this.fields;
    }

    public getFields() {
        this.fields.sort(TfabricaCrudField.compare);
        return this.fields;
    }

    public getFieldsToDisplay() {
        this.fieldsToDisplay.sort(TfabricaCrudField.compare);
        return this.fieldsToDisplay;
    }

}