import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { CommonFunctionService } from 'src/app/core/common/common-function.service';
import { API } from 'src/app/core/services/api.service';

@Component({
  selector: 'app-vehicle-master-search',
  templateUrl: './vehicle-master-search.component.html',
})
export class VehicleMasterSearchComponent implements OnInit {

  Result;
  cols;
  vehicleSearchForm:FormGroup;
    vehicleTypeList = [
    { key: 'Standard', value: 'STANDARD' },
    { key: 'Sedan', value: 'SEDAN' },
    { key: 'Minivan', value: 'MINIVAN' },
    { key: 'Minbus', value: 'MINBUS' },
    { key: 'Luxury Sedan', value: 'LUXURYSEDAN' },
    { key: 'Luxury Minivan', value: 'LUXURYMINIVAN' },
    { key: 'SUV', value: 'SUV' },
    { key: 'Minicoach', value: 'MINICOACH' },
    { key: 'Bus', value: 'BUS' }
  ];

    constructor(public fb: FormBuilder, private apiserv: API, public commonMasterSer: CommonFunctionService, private router: Router) {}

    ngOnInit(): void {
     this.cols = [
      { field: 'activityVehicle_id', header: 'Vehicle Id' },
      { field: 'vehicleType', header: 'Vehicle Type' },
      { field: 'maxPassengerCapacity', header: 'Max Passenger Capacity' },
      { field: 'maxLuggageCapacity', header: 'Max Luggage Capacity' },
      { field: 'maxLuggageCapacity', header: 'Vehicle Supplier' },
      { field: 'vehicleTransferType', header: 'Vehicle Transfer Type' },
      { field: 'vehicleName', header: 'Vehicle Name' },
      { field: 'vehicleNumberPlate', header: 'Vehicle Number Plate' },
      { field: 'fuelType', header: 'Fuel Type' },
      { field: 'status', header: 'Status' },
      { field: 'createdBy', header: 'Created By' },
      { field: 'Custom_created_on', header: 'Created On' },
      { field: 'updatedBy', header: 'Updated By' },
      { field: 'Custom_updated_on', header: 'Updated On' },
    ];
    this.initform();
  }

  initform(){
    this.vehicleSearchForm = this.fb.group({
      vehicleType:'',
      vehicleTransferType:'',
      maxPassengerCapacity:'',
      maxLuggageCapacity:''
    })
  }

  Search(){
    let val = this.vehicleSearchForm.getRawValue();
    let ReqObj = {
      vehicleType: val.vehicleType ? val.vehicleType : '',
      vehicleTransferType: val.vehicleTransferType ? val.vehicleTransferType : '',
      maxPassengerCapacity: val.maxPassengerCapacity ? val.maxPassengerCapacity : '',
      maxLuggageCapacity: val.maxLuggageCapacity ? val.maxLuggageCapacity : '',
    }
    this.apiserv.Post('/ActivityVehicle/GetAll', ReqObj).subscribe(res => {
      if (res) {
        this.Result = res.data ? res.data :[];
        if (res.data) {
          this.Result.forEach(e => {
            e['status'] = e.isActive ? 'Active' : 'Deactive';
            e['Custom_updated_on'] = this.commonMasterSer.convertDateWithTimeToShow(e.updatedOn);
            e['Custom_created_on'] = e['createdOn'] ?  this.commonMasterSer.convertDateWithTimeToShow(e.createdOn) : e['createdOn'];
          });
        }
      }
    })
  }
 
  Reset(){
    this.vehicleSearchForm.reset();
  }
 Edit(Id){
    this.router.navigateByUrl(`ActivityConfiguration/VehicleMaster/Edit/${Id}`);
  }
 ManageLang(Id){
    this.router.navigateByUrl(`ActivityConfiguration/VehicleMaster/Language/${Id}`);
  }
  updateStatus(id, val) {
    let obj = {
      id: id,
      status: val ? false : true,
    };
    this.apiserv.Post('/ActivityVehicle/UpdateStatus', obj).subscribe(res => {
      if (res) {
        this.commonMasterSer.showMessage(res['status'], res['message']);
        res['status'] ? this.Search() : '';
      }
    });
  }
}
