import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { SequenceService } from 'src/app/services/api/sequence.service';
import { ExcelService } from 'src/app/services/excel.service';
import * as moment from 'moment';
import { HttpErrorResponse } from '@angular/common/http';
@Component({
  selector: 'app-sequencegenarator',
  templateUrl: './sequencegenarator.component.html',
  styleUrls: ['./sequencegenarator.component.scss']
})
export class SequencegenaratorComponent implements OnInit {
  loading = false;
  selected: any[] = [];
  workflow_data;
  rowSelected :any= {};
  modaldelete=false;
  error;
  alldata;
  modaladd=false;
  modaledit=false;
  submitted = false;

  public entryForm: FormGroup;
  constructor(private seqservice:SequenceService,private router: Router, private toastr: ToastrService,private _fb: FormBuilder,
    private route: ActivatedRoute,private excel: ExcelService) { }

  ngOnInit(): void {
    this.getall();
    this.entryForm = this._fb.group({
      prefix:[null],
sequence_size:[null],
suffix:[null],
starting_no:[null],
seperator:[null],

sequence_name:[null],
sequence_code:[null],
demonstration:[null],

current_no:[null]
       });
  }
  getall(){
    this.seqservice.getAll().subscribe((data)=>{
      console.log(data);
      this.alldata=data;
      if(this.alldata.lenght==0){
        this.error="No data"
      }
    },(error) => {
      console.log(error);
      if(error){
       this.error="No data Available OR server Error";
     }

    })
  }
  // onSubmit(){
    // console.log(this.entryForm.value);
    // this.seqservice.create(this.entryForm.value).subscribe((data)=>{
    //   console.log(data);
    //   if (data) {
    //     this.toastr.success('Added successfully');
    //         }
    // },
    // (error) => {
    // console.log('Error in adding data...',+error);
    // if(error){
    //   this.toastr.error('Not added Data');
    // }

    // });
    // this.modaladd=false;
  // }


  onSubmit() {
    console.log(this.entryForm.value);
    this.submitted = true
    if (this.entryForm.invalid) {
      return
    }
    this.onCreate();

  }


  onCreate() {
    this.modaladd = false;
    this.seqservice.create(this.entryForm.value).subscribe(
      (data) => {
        console.log(data);
        if (data || data.status >=200 && data.status <=299) {
          this.toastr.success("Added Succesfully");
          this.ngOnInit();
        }
      },(error) => {
        if(error.status >=200 && error.status <=299){
          this.toastr.success("Added Succesfully");
          this.ngOnInit();
        }
        if(error.status >=400 && error.status <=499){
          this.toastr.error("Not Added");
          this.ngOnInit();
        }
        if(error.status >=500 && error.status <=599){
          this.toastr.error("Server Error");
          this.ngOnInit();
        }
      });
  }


  goToAdd(){
    this.modaladd=true;

  }

  onExport() {
    this.excel.exportAsExcelFile(this.alldata, 'user_',
      moment().format('YYYYMMDD_HHmmss'))
  }

  goToEdit(row) {
    this.rowSelected = row;
    console.log(row)
    this.modaledit=true;
  }
  onDelete(row) {
    this.rowSelected = row;
    this.modaldelete=true;
  }

  delete(id)
  {
    this.modaldelete = false;
    console.log("in delete  "+id);
    this.seqservice.delete(id).subscribe((data)=>{
      console.log(data);
      if (data == null || data.status >=200 && data.status <=299) {
        this.toastr.success('Deleted successfully');
        this.ngOnInit();
      }
  },
  (error) => {
    console.log(error);
    if(error.status >=200 && error.status <=299){
      this.toastr.success('Deleted successfully');
      this.ngOnInit();
    }
    if(error.status >=400 && error.status <=499){
      this.toastr.error('Not Deleted Data Getting Some Error');
      this.ngOnInit();
    }
    if(error.status >=500 && error.status <=599){
      this.toastr.error('Server Error');
      this.ngOnInit();
    }
  });

  }
  onUpdate(id){
    this.modaledit=false;
this.seqservice.update(id,this.rowSelected).subscribe((data)=>{
console.log(data);
if (data || data.status >=200 && data.status <=299) {
  this.toastr.success("Update Succesfully");
  this.ngOnInit();
}
},(error: HttpErrorResponse) => {
console.log(error?.message);
if(error.status >=200 && error.status <=299){
  this.toastr.success("Update Succesfully");
  this.ngOnInit();
}
if(error.status >=400 && error.status <=499){
  this.toastr.error("Not Update");
  this.ngOnInit();
}
if(error.status >=500 && error.status <=599){
  this.toastr.error("Server Error");
  this.ngOnInit();
}
});
  }
}
