﻿import { Component, OnInit, ViewChild } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from "@angular/http";
import { Router, ActivatedRoute, Params } from '@angular/router';
import { MdDialog, MdDialogRef, MdDialogConfig } from '@angular/material';
import { MdSnackBar } from '@angular/material';

import { TfabricaCrudService } from './tfabrica.crud.service';
import { TfabricaCrudReport } from './tfabrica.crud.report.model';
import { TfabricaSharedService } from '../main/tfabrica.shared.service';

import { TfabricaMessageDialogComponent } from '../main/tfabrica.message.dialog.component';

import { TfabricaMessage } from '../models/tfabrica.message.model';

import { TfabricaCrudSelectFieldsComponent } from './tfabrica.crud.selectfields.component';
import { TfabricaCrudFilterFieldsComponent } from './tfabrica.crud.filterfields.component';

@Component({
    selector: 't-crud-read',
    template: require('./tfabrica.crud.read.component.html')
})
export class TfabricaCrudReadComponent implements OnInit {

    public report: TfabricaCrudReport;
    public loading: boolean;

    @ViewChild('trightfiltersmenu') filtersmenu;
    @ViewChild('trightfieldsmenu') fieldsmenu;

    constructor(
        private _http: Http,
        private _sharedService: TfabricaSharedService,
        private _crudService: TfabricaCrudService,
        public dialog: MdDialog,
        public snackBar: MdSnackBar
    ) { 
        this.initReportData();
    }

    ngOnInit() {

    }

    public initReportData() {
        let that = this;

        this.report = new TfabricaCrudReport();

        that._sharedService.returnSelectedVoice().properties.forEach(function (entry) {
            console.log(entry);
            if (entry.key == "Report") {
                //console.log(JSON.parse(entry.value.toString()));
                that.report.setFromJson(JSON.parse(entry.value.toString()));
                that._crudService.initFromReport(that.report);
            }
        });

        if (that.report.executeImmediately) {
            this.extractData();
        }

    }

    public extractData() {
        let that = this;
        
        that.loading = true;

        this._crudService.readData(that.report).subscribe(
            reportData => {

                that.report = reportData;

                console.log(that.report);
                console.log(that.report.fields);


                that.loading = false;
                //this.router.navigate(['/main', { outlets: { 'main': ['home'] } }]);

                if (that.report.success) {

                } else {
                    this.snackBar.open(that.report.message, "", {
                        duration: 2000,
                    });

                    let newMsg = new TfabricaMessage();
                    newMsg.message = that.report.message;
                    newMsg.success = false;
                    this.displayMessage(newMsg);
                }

            },
            err => {
                // Log errors if any
                console.log(err);
                that.loading = false;
            });
    }

    public displayMessage(message) {
        //this.filtersmenu.toggle();
        let dialogConfig = new MdDialogConfig();
        dialogConfig.data = message;

        let dialogRef = this.dialog.open(TfabricaMessageDialogComponent, dialogConfig);
        dialogRef.afterClosed().subscribe(result => {
            console.log(result);
            if (result != null) {

            }
        });
    }


    public openSearch() {
        //this.filtersmenu.toggle();
        let dialogConfig = new MdDialogConfig();
        //dialogConfig.width = "95%";
        //dialogConfig.height = "95%";

        let dialogRef = this.dialog.open(TfabricaCrudFilterFieldsComponent, dialogConfig);
        dialogRef.afterClosed().subscribe(result => {
            console.log(result);
            if (result != null) {
            }

            this.extractData();
        });    
    }

    public startSearch() {
        this.filtersmenu.toggle();
    }


    public openFieldsList() {
        //this.fieldsmenu.toggle();

        let dialogConfig = new MdDialogConfig();

        let dialogRef = this.dialog.open(TfabricaCrudSelectFieldsComponent, dialogConfig);
        dialogRef.afterClosed().subscribe(result => {
            console.log(result);
            if (result != null) {
                this.report.setFields(result);
                this._crudService.setFields(this.report.fields);
            }
        });    

    }

    public setFieldsList() {
        this.fieldsmenu.toggle();
    }


}