// pages/vehicleMaintenance/maintainSend/maintainSend.ts
import { postAction } from '../../../../api/base';
const dayjs = require('dayjs');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    checkIdea: '', // 报修内容
    upkeepObject: '' as any, // 保养单数据
    checkResult: '' as any, // 评审结果
    userInfo: '' as any, // 用户信息
    checkPeople: '' //审批人
  },

  // 输入报修内容
  textareaInput(e: any) {
    this.setData({
      checkIdea: e.detail.value
    });
  },
  selectRadio(e: any) {
    this.setData({
      checkResult: e.currentTarget.dataset.inf
    });
  },
  // 提交申请
  submit() {
    if (this.data.checkResult) {
      if (this.data.checkResult == '2' && !this.data.checkIdea) {
        wx.showToast({
          title:
            this.data.upkeepObject.flowStatus == 4
              ? '请填写评审意见'
              : this.data.upkeepObject.flowStatus == 5
              ? '请填写审批意见'
              : '请填写复核意见',
          icon: 'none'
        });
        return;
      }
      let params = {
        id: this.data.upkeepObject.id, //id
        flowStatus: this.data.upkeepObject.flowStatus, //当前流程状态
        checkFlag: this.data.checkResult, //审核状态   1-》通过， 2-》不通过
        upkeepBill: {},
        upkeepAssign: {},
        upkeepOffer: {},
        currentUpkeepCheck: {
          checkResult: this.data.checkResult, //审核结果/保养结果
          checkPeople: this.data.userInfo.realname, //审批人
          checkIdea: this.data.checkIdea //审核意见
        }
      };
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      postAction('api/vehicles/upkeepBill/submitUpkeepObject', params)
        .then((res: any) => {
          if (res.code == 200) {
            wx.hideLoading();
            wx.showToast({
              title: '提交成功',
              icon: 'none'
            });
            wx.setStorageSync('message', {
              checkPeople: this.data.userInfo.realname,
              date: dayjs().format('YYYY-MM-DD HH:mm:ss'),
              checkIdea: this.data.checkIdea,
              checkResult: this.data.checkResult,
              price: this.data.upkeepObject.upkeepOffer.repairCost, // 保养金额
              id: this.data.upkeepObject.id,
              flowStatus: this.data.upkeepObject.flowStatus,
              checkFlag: this.data.checkResult // 通过结果
            });
            wx.redirectTo({
              url: '../message/message',
              fail: e => {
                console.log(e);
              }
            });
          } else {
            wx.hideLoading();
            wx.showToast({
              title: res.message,
              icon: 'none'
            });
          }
        })
        .catch((err: any) => {
          wx.hideLoading();
          wx.showToast({
            title: err.message,
            icon: 'none'
          });
        });
    } else {
      wx.showToast({
        title:
          this.data.upkeepObject.flowStatus == 4
            ? '请填写评审意见'
            : this.data.upkeepObject.flowStatus == 5
            ? '请填写审批意见'
            : '请填写复核意见',
        icon: 'none'
      });
    }
  },
  getInf() {
    // 获取信息
    let info = wx.getStorageSync('upkeepObject');
    wx.removeStorage({ key: 'upkeepObject' });
    let userInfo = wx.getStorageSync('userInfo');
    let title = '';
    if (info.flowStatus == 4) title = '评审';
    else if (info.flowStatus == 10) title = '复核';
    wx.setNavigationBarTitle({
      title: title
    });
    this.setData({
      upkeepObject: info,
      userInfo: userInfo,
      checkPeople: userInfo.realname
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

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

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

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

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

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

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