import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';
import { CustomersAddComponent } from 'app/customers-add/customers-add.component';
import { filter } from 'rxjs-compat/operator/filter';
import { ApiService } from 'app/api.service';
import { CustomersInvitationsAddComponent } from 'app/customers-invitations-add/customers-invitations-add.component';
import { CustomersPrescriptionComponent } from 'app/customers-prescription/customers-prescription.component';
import { CustomersBalanceComponent } from 'app/customers-balance/customers-balance.component';
import { UsersMessageComponent } from 'app/users-message/users-message.component';

@Component({
  selector: 'app-customers',
  templateUrl: './customers.component.html',
  styleUrls: ['./customers.component.scss']
})
export class CustomersComponent implements OnInit {

  public customers: any = [];
  public invitations: any = [];
  public filter: any = { name: '', state: '' }
  public states: any = [];
  public activeCustomers: boolean = true;
  public activeInvitations: boolean = false;

  constructor(private title: Title, public dialog: MatDialog, public apiService: ApiService) {
    this.title.setTitle("Green App - Clientes");
  }

  tabChanged(evt:any) {
    if (evt.index == 0) { 
      this.activeCustomers = true;
      this.activeInvitations = false;
    }
    else if (evt.index == 1) { 
      this.activeCustomers = false;
      this.activeInvitations = true;
    }
  }

  ngOnInit() {
    this.apiService.getStates().subscribe((data:any) => {
      this.states = data.data;
    });    
    this.filterdata();
    this.refreshInvitations();
  }

  refreshInvitations() {
    this.apiService.getCustomerInvitations().subscribe((data:any) => {
      this.invitations = data.data;
    });
  }
  filterdata() {
    this.apiService.getCustomers(this.filter.name, this.filter.state).subscribe((data:any) => {
      this.customers = data.data;
    });
  }
  seePrescription(index: number) {
    const dialogRef = this.dialog.open(CustomersPrescriptionComponent, { width: '60%', data: { id: this.customers[index].id } });
  }
  seeAccount(index: number) {
    const dialogRef = this.dialog.open(CustomersBalanceComponent, { width: '60%', data: { id: this.customers[index].id } });
  }
  editCustomer(index:number) {
    let data = this.customers[index];
    data.type = 'edit';

    const dialogRef = this.dialog.open(CustomersAddComponent, { width: '70%', data: data });

    dialogRef.afterClosed().subscribe(result => {
      this.refreshInvitations();
    });
  }
  addInvitation() {
    const dialogRef = this.dialog.open(CustomersInvitationsAddComponent, {
      width: '40%',
      data: { type:'add', card_code: '', id: ''}
    });

    dialogRef.afterClosed().subscribe(result => { this.refreshInvitations(); });
  }
  editInvitation(invitation: any) {
    invitation.type = 'edit';
    const dialogRef = this.dialog.open(CustomersInvitationsAddComponent, {
      width: '40%',
      data: invitation
    });

    dialogRef.afterClosed().subscribe(result => {
      this.filterdata();
    });
  }
  messageUser(index:string) {
    let id = this.customers[index].id;
    let data: any = { id: id };
    const dialogRef = this.dialog.open(UsersMessageComponent, { width: '50%', data: data });
    dialogRef.afterClosed().subscribe(result => {
      this.filterdata();
    }); 
  }
  deleteInvitation(index: string) {
    let id = this.invitations[index].id;
    this.invitations.splice(index, 1);
    this.apiService.deleteInvitation(id).subscribe((data:any) => {
      if (data.res == "OK") {
        
      } else {
        this.apiService.showError("Error",data.msg);
      }
    });
  }
  deletecustomer(index:string) {
    let id = this.customers[index].id;
    let confirm = this.apiService.confirm("Confirmar","¿Deseas eliminar a "+this.customers[index].first_name+"?");
    confirm.then((data:any)=> { 
      if (data.dismiss=="cancel") { }
      else if (data.value) {
        this.apiService.deleteUser(id).subscribe((data:any)=>{
          if (data.res == "OK") {
            this.customers.splice(index, 1);
          } else {
            this.apiService.showError("Error",data.msg);
          }
        });
      }
    });
  }
  

}
