import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ContactService } from '../contact.service';

@Component({
  selector: 'app-rto-master',
  templateUrl: './rto-master.component.html',
  styleUrls: ['./rto-master.component.scss']
})
export class RtoMasterComponent implements OnInit {
  RtoData;
  RTOForm:FormGroup;
  Indianstate = [ "Andhra Pradesh",
                "Arunachal Pradesh",
                "Assam",
                "Bihar",
                "Chhattisgarh",
                "Goa",
                "Gujarat",
                "Haryana",
                "Himachal Pradesh",
                "Jammu and Kashmir",
                "Jharkhand",
                "Karnataka",
                "Kerala",
                "Madhya Pradesh",
                "Maharashtra",
                "Manipur",
                "Meghalaya",
                "Mizoram",
                "Nagaland",
                "Odisha",
                "Punjab",
                "Rajasthan",
                "Sikkim",
                "Tamil Nadu",
                "Telangana",
                "Tripura",
                "Uttarakhand",
                "Uttar Pradesh",
                "West Bengal",
                "Andaman and Nicobar Islands",
                "Chandigarh",
                "Dadra and Nagar Haveli",
                "Daman and Diu",
                "Delhi",
                "Lakshadweep",
                "Puducherry"]
  constructor(private fb:FormBuilder,private contactService:ContactService) { }

  ngOnInit() {
    this.RTOForm=this.fb.group({
      state:[],
      city:[],
      RTO_Code:[],
      RTO_Name:[],
      status:[]
    })
  this.getRTOdata()
  }

  getRTOdata(){ 
    this.contactService.get('/RTO_master/get').subscribe((res:any)=>{
      this.RtoData=res;
    })

    
  }

  submit(){
    console.log(this.RTOForm.value);
    if(this.isEdit){
      var data={
        body:this.RTOForm.value,
        _id:this.id
      }
       this.contactService.post('/RTO_master/update',data).subscribe((res=>{
        console.log(res);
        this.title="Submit"
        this.getRTOdata();
        this.isEdit=!this.isEdit
      }))
    }else{
      this.contactService.post('/RTO_master/add',this.RTOForm.value).subscribe((res=>{
        console.log(res);
        this.getRTOdata()
      }))

    }
  }
  title="Submit"
  isEdit:Boolean=false;
  id
  edit(value){
    this.id=value._id
    this.RTOForm.patchValue(value);
    this.isEdit=!this.isEdit;
    this.title="Update"
  }

  delete(item){
    this.contactService.get('/RTO_master/delete?_id='+item._id).subscribe(res=>{
      this.getRTOdata()
    })
  }

}
