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

@Component({
  selector: 'app-model-master',
  templateUrl: './model-master.component.html',
  styleUrls: ['./model-master.component.scss']
})
export class ModelMasterComponent 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) { }
 // Manufacturer:{type:String},
              // Model:{type:String},
              // ModelName:{type:String},
              // Engine:{type:String},
              // Capacity:{type:String},
              // NoOfTypers:{type:String} -->
  ngOnInit() {
    this.RTOForm=this.fb.group({
      Manufacturer:[],
      Model:[],
      ModelName:[],
      Engine:[],
      Capacity:[],
      NoOfTypers:[],
      Transmission:[],
      Category:[],  
  })
  this.getRTOdata()
  }

  getRTOdata(){ 
    this.contactService.get('/model_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('/model_master/update',data).subscribe((res=>{
        console.log(res);
        this.title="Submit"
        this.getRTOdata();
        this.isEdit=!this.isEdit
      }))
    }else{
      this.contactService.post('/model_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('/model_master/delete?_id='+item._id).subscribe(res=>{
      this.getRTOdata()
    })
  }

}
