// pages/vehicleMaintenance/maintainSend/maintainSend.ts
import { getAction } from '../../../api/base';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    currentflowStatus: 0,
    isCollapsed: false, // 默认为非折叠状态
    repairObject: {} as any, // 维修单数据
    userInfo: {} as any // 用户信息
  },
  getInf() {
    // 获取信息
    let info = wx.getStorageSync('selectMaintain');
    wx.removeStorage({ key: 'selectMaintain' });
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      currentflowStatus: info.flowStatus,
      userInfo: userInfo
    });
    this.getRepairObject(info.id);
  },
  // 获取维修单数据
  getRepairObject(id: string) {
    if (id) {
      getAction(`api/vehicles/repair/getRepairObjectById?id=${id}`)
        .then((res: any) => {
          if (res.code == 200) {
            let data = res.result;
            if (data.repairInfo?.repairImgUrl)
              data.repairInfo.repairImgUrl =
                data.repairInfo.repairImgUrl.split(',');
            if (data.repairOffer?.inRepairImgUrl)
              data.repairOffer.inRepairImgUrl =
                data.repairOffer.inRepairImgUrl.split(',');
            if (data.repairOffer?.afterRepairImgUrl)
              data.repairOffer.afterRepairImgUrl =
                data.repairOffer.afterRepairImgUrl.split(',');
            this.setData({
              repairObject: data
            });
          } else {
            console.log('出错了');

            wx.showToast({
              title: res.message,
              icon: 'none'
            });
          }
        })
        .catch((err: any) => {
          console.log('出错了呀');
          wx.showToast({
            title: err.message,
            icon: 'none'
          });
        });
    }
  },
  // 变更tab
  changeTab(e: any) {
    console.log(e.currentTarget.dataset.tab);
    this.setData({
      tabActive: e.currentTarget.dataset.tab
    });
  },
  bindSend() {
    // 维修
    wx.setStorageSync('repairObject', this.data.repairObject);
    wx.navigateTo({
      url: '../operation/repair/repair',
      fail: e => {
        console.log(e);
      }
    });
  },
  // 收起维修明细
  collapse() {
    this.setData({
      isCollapsed: !this.data.isCollapsed
    });
  },
  // 图片预览
  showImage(event: any) {
    let { index, image } = event.currentTarget.dataset;
    let list = [] as any;
    if (image == 'repairImgUrl') {
      list = this.data.repairObject.repairInfo.repairImgUrl;
    } else if (image == 'attachmentList') {
      this.data.repairObject.repairOffer.attachmentList.forEach((item: any) => {
        list.push(item.fileUrl);
      });
    } else if (image == 'inRepairImgUrl') {
      list = this.data.repairObject.repairOffer.inRepairImgUrl;
    } else if (image == 'afterRepairImgUrl') {
      list = this.data.repairObject.repairOffer.afterRepairImgUrl;
    }
    wx.previewImage({
      current: list[index | 0], // 当前显示图片的http链接  默认urls[0]
      urls: list // 需要预览的图片http链接列表
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

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

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    let pages = getCurrentPages();
    let idx = null,
      num = 0;
    pages.forEach((item: any, index: number) => {
      if (
        item.route ==
          'pages/vehicleMaintenance/maintainRecord/maintainRecord' ||
        item.route == 'pages/backlog/backlogList/backlogList'
      )
        idx = index;
    });
    if (idx != null) num = pages.length - 2 - idx;
    console.log(idx, num);
    if (idx != null && num != 0) {
      wx.navigateBack({
        // delta: num,
        delta: 1,
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          if (getApp().EventChannel.emit) getApp().EventChannel.emit('refresh');
        },
        fail: e => {
          console.log(e);
        }
      });
    } else if (idx == null) {
      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'
        });
      }
    }
  },

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

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

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