import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController,MenuController } from 'ionic-angular';
import { Myservice } from "../../providers/myservice";
import { Storage } from "@ionic/storage";
import { user, search } from "../../models/mymodel";
import { TranslateService } from '@ngx-translate/core';

@IonicPage()
@Component({
  selector: 'page-searchride',
  templateUrl: 'searchride.html',
})
export class SearchridePage {
  local: user
  searches: Array<search>
  searchText: string;
  lang: string = 'en';

  constructor(public navCtrl: NavController, public navParams: NavParams, public viewCtrl: ViewController, private myservice: Myservice, private storage: Storage, private translate: TranslateService, private menu:MenuController) {
  }

  ionViewDidEnter() {
    this.menu.swipeEnable(false)
    this.storage.get('lang').then(lang => {
      if (lang != null) {
        this.translate.use(lang)
        this.lang = lang;
      }
      else {
        this.translate.use('en')
      }
    })

    this.myservice.show_loader()
    this.storage.get('user_data').then(data => {
      if (data) {
        this.local = data
        this.myservice.load_post({ userid: this.local.id }, 'getSearches').subscribe(response => {
          this.myservice.hide_loader()
          if (response.status == 'success') {
            this.searches = response.data
          }
          else {
            this.translate.get(['search.' + response.message]).subscribe(value => {
              this.myservice.show_alert('', value['search.' + response.message])
            })
          }
        })
      }
    })
  }

  setDirection(data) {
    this.viewCtrl.dismiss(data);
  }

  dismiss() {
    this.viewCtrl.dismiss();
  }

  ionViewWillLeave() {
    this.menu.swipeEnable(true)
  }
}
