import * as React from "react";
import { FieldsControl as CoreFieldsControl } from "../../../Controls/FieldsControl";
import { RecordField, ListRecordsCriteria, FieldQuery, Pagination } from "../../../Models";
import { RecordService } from "../../../Services";
import { LoadingService } from "../../../Services/LoadingService";

export class FieldsControl extends React.Component<any, any> {
    private _recordService: RecordService = new RecordService();
    private _loadingService = new LoadingService();
    constructor(props: any) {
        super(props);

    }

    private _setColWidth(id: any): string {
        switch (id) {
            case 157: {
                return "ms-Grid-col ms-sm12 ms-lg12";
            }
            case 155: {
                return " ms-Grid-col ms-sm12 ms-lg6";
            }
            case 159: {
                return "ms-Grid-col ms-sm12 ms-lg6";
            }
            case 163: {
                return "ms-Grid-col ms-sm12 ms-lg6";
            }
            case 164: {
                return "ms-Grid-col ms-sm12 ms-lg12";
            }
            case 165: {
                return "ms-Grid-col ms-sm12 ms-lg12";
            }
            case 189: {
                return "ms-Grid-col ms-sm12 ms-lg6";
            }
            case 202: {
                return "ms-Grid-col ms-sm12 ms-lg12";
            }
            case 256: {
                return "ms-Grid-col ms-sm12 ms-lg6";
            }
            case 257: {
                return "ms-Grid-col ms-sm12 ms-lg6";
            }
            case 258: {
                return "ms-Grid-col ms-sm12 ms-lg12";
            }

            default: {
                return "ms-Grid-col ms-sm12 ms-lg12";
            }
        }
    }

    private _loadRooms(field: RecordField) {
        let criteria: ListRecordsCriteria = new ListRecordsCriteria();
        criteria.asLookup = false;
        criteria.entityId = field.extraData;
        criteria.query = new Array<FieldQuery>();
        criteria.pagination = new Pagination();
        criteria.pagination.start = 0;
        criteria.pagination.count = 99999;
        this._loadingService.show();
        this._recordService.listRecords(criteria).then(result => {
            field.records = result;
            this.forceUpdate();
            this._loadingService.hide();

        }, error => {
            alert(error);
            this._loadingService.hide();

        });
    }

    public render(): JSX.Element {

        if (this.props.fields)
            this.props.fields.forEach((field: RecordField) => {

                if (field.id == 309 && field.records == null) {
                    this._loadRooms(field);
                }

                if (field.id == 319 || field.id == 320)
                    field.hidden = true;

            });

        return (
            <div><CoreFieldsControl
                {...this.props} fields={this.props.fields}
                setColWidth={this._setColWidth.bind(this)}></CoreFieldsControl>
            </div>
        )
    }
}