import * as React from "react";
import { RouteComponentProps } from "react-router";
import { RecordService } from "../../../../Services";
import { Record, ListRecordsCriteria } from "../../../../Models";
// import { FormDisplay } from "../../Customizations/Controls/FormDisplay";
import { FieldsControl } from "../../../../Customizations/Controls/FieldsControl";

export class RecordDetails extends React.Component<{} & RouteComponentProps<{ entityId: number, id: number }>, any>{
    private _recordService: RecordService = new RecordService();

    constructor(props: {} & RouteComponentProps<{ entityId: number, id: number }>) {
        super(props);
        this.state = {
            record: new Record()
        }
        this.getRecord(props.match.params.entityId, props.match.params.id);
    }

    private getRecord(entityId: number, recordId: number) {
        if (recordId == 0) {
            //debugger;
            this._recordService.getEmptyRecord(entityId)
                .then(result => {
                    result.fields.forEach(f => {
                        if (f.type === 8 && f.extraData !== null) {
                            let criteria = new ListRecordsCriteria();
                            criteria.entityId = f.extraData as number;
                            criteria.asLookup = true;

                            this._recordService.listRecords(criteria).then(result => {
                                f.records = result;
                            });
                        }
                    });
                    this.setState({ record: result });
                }, error => {
                    alert(error);
                });
        }
        else {
            this._recordService.getRecord(entityId, recordId)
                .then(result => {
                    result.fields.forEach(f => {
                        if (f.type === 8 && f.extraData !== null) {
                            let criteria = new ListRecordsCriteria();
                            criteria.entityId = f.extraData as number;
                            criteria.asLookup = true;
                            this._recordService.listRecords(criteria).then(result => {
                                f.records = result;
                            });
                        }
                        else if (f.type === 14 || f.type === 15) {
                            let criteria = f.extraData as ListRecordsCriteria;
                            this._recordService.listRecords(criteria).then(result => {
                                f.records = result;
                            });
                        }
                        else if (f.type === 16) {
                            this._recordService.getWorkflowHistory(entityId, recordId).then(result => {
                                f.workflowHistory = result;
                            });
                        }
                    });
                    this.setState({ record: result });
                }, error => {
                    alert(error);
                });
        }
    }

    public render(): JSX.Element {
        return (
            // <div><FormDisplay
            //     Record={this.state.record} />
            //     </div>
                  <FieldsControl
                  Record={this.state.record} 
                  fields={this.state.record.fields}
                  // onSubmit={this.handleSubmit.bind(this)}
                  actions={this.state.record.actions}
                  // onRef={(ref: any) => (this.fieldsControl = ref)} 
                //   panelMode={true}/>
                />
        );
    }
}