import { getAction } from '../../../api/base';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    userInfo: {} as any, // 用户信息
    upkeepObject: {} as any, // 维修单数据
    info: '', // 列表查询的保养计划信息
    userInf: ''
  },
  getInf() {
    // 获取信息
    let { info } = wx.getStorageSync('info');
    wx.removeStorage({ key: 'info' });
    let userInfo = wx.getStorageSync('userInfo');
    if (info) {
      this.setData({
        userInfo: userInfo,
        info: info
      });
      this.getRepairObject(info.id);
    }
  },
  // 获取维修单数据
  getRepairObject(id: string) {
    if (id) {
      getAction(`api/vehicles/upkeepPlan/getUpkeepPlanObjectById?id=${id}`)
        .then((res: any) => {
          if (res.code == 200) {
            let data = res.result;
            this.setData({
              upkeepObject: data
            });
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none'
            });
          }
        })
        .catch((err: any) => {
          wx.showToast({
            title: err.message,
            icon: 'none'
          });
        });
    }
  },
  // 变更tab
  changeTab(e: any) {
    this.setData({
      tabActive: e.currentTarget.dataset.tab
    });
  },

  bindCancel() {
    // 作废
    wx.setStorageSync('upkeepObject', this.data.upkeepObject);
    wx.navigateTo({
      url: '../cancel/cancel',
      fail: e => {
        console.log(e);
      }
    });
  },
  // 返回
  goBack() {
    let pageList = getCurrentPages();
    if (pageList.length != 1) wx.navigateBack();
    else {
      if (this.data.userInfo.type == '1') {
        wx.reLaunch({
          url: '/pages/index/index'
        });
      } else if (this.data.userInfo.type == '2') {
        wx.reLaunch({
          url: '/pages/manufacturerIndex/manufacturerIndex'
        });
      } else if (this.data.userInfo.type == '3') {
        wx.reLaunch({
          url: '/pages/projectIndex/projectIndex'
        });
      } else if (this.data.userInfo.type == '4') {
        wx.reLaunch({
          url: '/pages/maintenanceIndex/maintenanceIndex'
        });
      } else {
        wx.reLaunch({
          url: '/pages/login/login'
        });
      }
    }
  },
  bindSend() {
    // 审核/审批
    wx.setStorageSync('upkeepObject', this.data.upkeepObject);
    wx.navigateTo({
      url: '../check/check',
      fail: e => {
        console.log(e);
      }
    });
  },
  // 编辑
  bindEdit() {
    wx.setStorageSync('type', {
      type: 'edit',
      info: this.data.upkeepObject
    });
    wx.navigateTo({
      url: '../UpkeepPlanApply/UpkeepPlanApply',
      fail: e => {
        console.log(e);
      }
    });
  },
  // 图片预览
  showImage(event: any) {
    let { index, image } = event.currentTarget.dataset;
    let list = [] as any;
    if (image == 'repairImgUrl') {
      list = this.data.upkeepObject.repairInfo.repairImgUrl;
    } else if (image == 'attachmentList') {
      this.data.upkeepObject.upkeepPlan.attachmentList.forEach((item: any) => {
        list.push(item.fileUrl);
      });
    }
    wx.previewImage({
      current: list[index | 0], // 当前显示图片的http链接  默认urls[0]
      urls: list // 需要预览的图片http链接列表
    });
  },
  getUserInf() {
    this.setData({
      userInf: wx.getStorageSync('userInfo')
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 不放onReady 是因为有可能切出去然后状态改了得跳转到别的页面
    this.getInf();
    this.getUserInf();
  },

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

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

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

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

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