// pages/vehicleMaintenance/maintainSend/maintainSend.ts
import { getAction } from '../../../api/base';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    currentflowStatus: 0,
    isCollapsed: false, // 默认为非折叠状态
    upkeepObject: {} as any, // 保养单数据
    userInfo: {} as any, // 用户信息
    userInf: ''
  },
  getInf() {
    // 获取信息
    let info = wx.getStorageSync('selectMaintain');
    wx.removeStorage({ key: 'selectMaintain' });
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      currentflowStatus: info.flowStatus,
      userInfo: userInfo
    });
    this.getupkeepObject(info.id);
  },
  // 获取保养单数据
  getupkeepObject(id: string) {
    if (id) {
      getAction(`api/vehicles/upkeepBill/getUpkeepObjectById?id=${id}`)
        .then((res: any) => {
          if (res.code == 200) {
            let data = res.result;
            if (data.upkeepOffer.beforeUpkeepImgUrl)
              data.upkeepOffer.beforeUpkeepImgUrl =
                data.upkeepOffer.beforeUpkeepImgUrl.split(',');
            if (data.upkeepOffer.inUpkeepImgUrl)
              data.upkeepOffer.inUpkeepImgUrl =
                data.upkeepOffer.inUpkeepImgUrl.split(',');
            if (data.upkeepOffer.afterUpkeepImgUrl)
              data.upkeepOffer.afterUpkeepImgUrl =
                data.upkeepOffer.afterUpkeepImgUrl.split(',');
            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) {
    console.log(e.currentTarget.dataset.tab);
    this.setData({
      tabActive: e.currentTarget.dataset.tab
    });
  },

  bindCancel() {
    // 作废
    wx.setStorageSync('upkeepObject', this.data.upkeepObject);
    wx.navigateTo({
      url: '../operation/cancel/cancel',
      fail: e => {
        console.log(e);
      }
    });
  },
  // 返回
  goBack() {
    let pageList = getCurrentPages();
    if (pageList && 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: '../operation/upkeep/upkeep',
      fail: e => {
        console.log(e);
      }
    });
  },
  // 保养结果
  finished() {
    wx.setStorageSync('upkeepObject', this.data.upkeepObject);
    wx.navigateTo({
      url: '../operation/complete/complete',
      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 == 'attachmentList') {
      this.data.upkeepObject.upkeepBill.attachmentList.forEach((item: any) => {
        list.push(item.fileUrl);
      });
    } else if (image == 'beforeUpkeepImgUrl') {
      list = this.data.upkeepObject.upkeepOffer.beforeUpkeepImgUrl;
    } else if (image == 'attachmentUpkeepList') {
      this.data.upkeepObject.upkeepOffer.attachmentUpkeepList.forEach(
        (item: any) => {
          list.push(item.fileUrl);
        }
      );
    } else if (image == 'inUpkeepImgUrl') {
      list = this.data.upkeepObject.upkeepOffer.inUpkeepImgUrl;
    } else if (image == 'afterUpkeepImgUrl') {
      list = this.data.upkeepObject.upkeepOffer.afterUpkeepImgUrl;
    }
    wx.previewImage({
      current: list[index | 0], // 当前显示图片的http链接  默认urls[0]
      urls: list // 需要预览的图片http链接列表
    });
  },
  getUserInf() {
    this.setData({
      userInf: wx.getStorageSync('userInfo')
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const eventChannel = this.getOpenerEventChannel();
    if (eventChannel && !getApp().EventChannel)
      getApp().EventChannel = eventChannel;
  },

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

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

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

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    let pages = getCurrentPages();
    let idx = null,
      num = 0;
    pages.forEach((item: any, index: number) => {
      if (
        item.route == 'pages/UpkeepBill/UpkeepBillRecord/UpkeepBillRecord' ||
        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() {}
});
