// pages/vehicleMonitoring/component/vehicleSearch/vehicleSearch.ts
import { getAction } from '../../../../api/base';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    plateNumber: '',
    searchList: []
  },
  getPlateNumber(e: any) {
    let val = e.detail.value;
    let parms = {
      plateNumber: val
    };
    getAction('api/monitor/vehicle/listByAll', parms)
      .then((res: any) => {
        if (res.code == 200) {
          console.log(res.result);
          let tmpArr = res.result;
          for (let i = 0; i < tmpArr.length; i++) {
            let item: any = tmpArr[i];
            item.valueTxt = this.getHilightStrArray(item.plateNumber, val);
          }
          this.setData({
            searchList: tmpArr,
            plateNumber: val
          });
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      })
      .catch((err: any) => {
        this.setData({
          loading: false
        });
        wx.showToast({
          title: err.message,
          icon: 'none'
        });
      });
  },
  // 返回一个使用key切割str后的数组，key仍在数组中
  getHilightStrArray(str: string, key: string) {
    return str.replace(new RegExp(`${key}`, 'g'), `%%${key}%%`).split('%%');
  },
  selectOption(e: any) {
    console.log(e.currentTarget.dataset);
    if (wx.getStorageSync('isGlobal') == 1) {
      wx.setStorageSync('selectCar', e.currentTarget.dataset.item);
      wx.navigateTo({
        url: '../../realtime/realtime',
        fail: e => {
          console.log(e);
        }
      });
    } else {
      // 传参，跳转
      wx.setStorageSync('selectCarInf', e.currentTarget.dataset.item);
      wx.navigateBack({
        delta: 1,
        fail: e => {
          console.log(e);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
