﻿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 { Location } from '@angular/common';

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-detail',
    template: require('./tfabrica.crud.detail.component.html')
})
export class TfabricaCrudDetailComponent implements OnInit {

    public report: TfabricaCrudReport;
    public operation: string;
    public subTitle: string;
    public row: any;
    public loading: boolean;

    constructor(
        private _http: Http,
        private _sharedService: TfabricaSharedService,
        private _crudService: TfabricaCrudService,
        private _location: Location,
        public dialog: MdDialog,
        public snackBar: MdSnackBar
    ) {

        this.report = this._crudService.getReport();
        this.row = this._crudService.getSelectedRow();
        console.log(this.row);

        this.operation = this._crudService.getOperation();
        if (this.operation == "I") this.subTitle = "Insert";
        if (this.operation == "U") this.subTitle = "Update";
    }

    ngOnInit() {
    }

    public back() {
        this._location.back();
    }

    public save() {

        let that = this;

        that.loading = true;

        this._crudService.updateData(that.report, that.row, that.operation).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) {

                    this.snackBar.open("Updated Succesfully!", "", {
                        duration: 2000,
                    });

                } 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) {

            }
        });
    }

}