// 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, // 用户信息
    inUpkeepImgUrl: [] as any, // 保养中图片
    afterUpkeepImgUrl: [] as any, // 保养后图片
    uploadUrl: 'api/vehicles/repair/fileUpload', // 上传图片地址
    echoInUpkeep: null as any, // 保养中回显图片
    echoAfterUpkeep: null as any // 保养后回显图片
  },

  // 输入报修内容
  textareaInput(e: any) {
    this.setData({
      checkIdea: e.detail.value
    });
  },
  selectRadio(e: any) {
    this.setData({
      checkResult: e.currentTarget.dataset.inf
    });
  },
  // 提交申请
  submit() {
    // 保养中图片
    let inRepairImgList = JSON.parse(
      JSON.stringify(this.data.inUpkeepImgUrl)
    ) as any;
    // 保养后图片
    let afterRepairImgList = JSON.parse(
      JSON.stringify(this.data.afterUpkeepImgUrl)
    ) as any;
    // 如何有回显的图片且未删除
    if (this.data.echoInUpkeep) {
      this.data.echoInUpkeep.forEach((item: any) => {
        inRepairImgList.push(item);
      });
    }
    if (this.data.echoAfterUpkeep) {
      this.data.echoAfterUpkeep.forEach((item: any) => {
        afterRepairImgList.push(item);
      });
    }
    if (inRepairImgList == 0) {
      wx.showToast({
        title: '请上传保养中照片',
        icon: 'none'
      });
      return;
    }
    if (afterRepairImgList == 0) {
      wx.showToast({
        title: '请上传保养后照片',
        icon: 'none'
      });
      return;
    }
    let params = {
      id: this.data.upkeepObject.id, //id
      flowStatus: this.data.upkeepObject.flowStatus, //当前流程状态
      checkFlag: 1, //审核状态   1-》通过， 2-》不通过
      upkeepBill: {},
      upkeepAssign: {},
      currentUpkeepCheck: {},
      upkeepOffer: {
        id: this.data.upkeepObject.upkeepOffer.id,
        repairDetailList: this.data.upkeepObject.upkeepOffer.repairDetailList,
        materialCost: this.data.upkeepObject.upkeepOffer.materialCost, // 材料费
        totalWorkHours: this.data.upkeepObject.upkeepOffer.totalWorkHours, //总计工时
        totalWorkHoursCost:
          this.data.upkeepObject.upkeepOffer.totalWorkHoursCost, //工时费用总计
        totaCost: this.data.upkeepObject.upkeepOffer.totaCost, //总计
        offerContent: this.data.upkeepObject.upkeepOffer.offerContent, //内容说明
        beforeUpkeepImgUrl: this.data.upkeepObject.upkeepOffer
          .beforeUpkeepImgUrl
          ? this.data.upkeepObject.upkeepOffer.beforeUpkeepImgUrl.join(',')
          : '', //保养前图片
        inUpkeepImgUrl: inRepairImgList ? inRepairImgList.join(',') : '', //保养中图片
        afterUpkeepImgUrl: afterRepairImgList
          ? afterRepairImgList.join(',')
          : '', //保养后图片
        attachmentUpkeepList:
          this.data.upkeepObject.upkeepOffer.attachmentUpkeepList //单据附件,
      }
    };
    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', {
            projectName: this.data.upkeepObject.upkeepBill?.projectId_dictText,
            plateNumber: this.data.upkeepObject.upkeepBill?.plateNumber,
            date: dayjs().format('YYYY-MM-DD HH:mm:ss'),
            id: this.data.upkeepObject.id,
            flowStatus: this.data.upkeepObject.flowStatus
          });
          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'
        });
      });
  },
  // 获取图片上传后的数据 保养中
  returnPicOne(e: any) {
    let list = [] as any;
    e.detail.photoList.forEach((item: any) => {
      list.push(item.fileUrl);
    });
    this.setData({
      inUpkeepImgUrl: list
    });
  },
  // 保养后
  returnPicTwo(e: any) {
    let list = [] as any;
    e.detail.photoList.forEach((item: any) => {
      list.push(item.fileUrl);
    });
    this.setData({
      afterUpkeepImgUrl: list
    });
  },
  // 删除保养中回显图片
  delInImg(e: any) {
    let that = this;
    let { index } = e.currentTarget.dataset;
    let { echoInUpkeep } = this.data;
    wx.showModal({
      title: '提示',
      content: '是否删除当前照片',
      success(res) {
        if (res.confirm) {
          echoInUpkeep.splice(index, 1);
          that.setData({
            echoInUpkeep: echoInUpkeep
          });
        }
      }
    });
  },
  // 删除保养后回显图片
  delAfterImg(e: any) {
    let that = this;
    let { index } = e.currentTarget.dataset;
    let { echoAfterUpkeep } = this.data;
    wx.showModal({
      title: '提示',
      content: '是否删除当前照片',
      success(res) {
        if (res.confirm) {
          echoAfterUpkeep.splice(index, 1);
          that.setData({
            echoAfterUpkeep: echoAfterUpkeep
          });
        }
      }
    });
  },
  getInf() {
    // 获取信息
    let info = wx.getStorageSync('upkeepObject');
    wx.removeStorage({ key: 'upkeepObject' });
    console.log('数据有没有问题', info);

    // let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      upkeepObject: info
    });
    // 回显的图片
    if (info.upkeepOffer.inUpkeepImgUrl) {
      this.setData({
        echoInUpkeep: info.upkeepOffer.inUpkeepImgUrl // 回显附件
      });
    }
    if (info.upkeepOffer.afterUpkeepImgUrl) {
      this.setData({
        echoAfterUpkeep: info.upkeepOffer.afterUpkeepImgUrl // 回显附件
      });
    }
  },
  showImage(event: any) {
    let { index, image } = event.currentTarget.dataset;
    let list = [] as any;
    console.log(123);

    if (image == 'echoInUpkeep') {
      list = this.data.echoInUpkeep;
    } else if (image == 'echoAfterUpkeep') {
      list = this.data.echoAfterUpkeep;
    }
    wx.previewImage({
      current: list[index | 0], // 当前显示图片的http链接  默认urls[0]
      urls: list // 需要预览的图片http链接列表
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

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

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

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

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

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

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

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