// pages/backlog/accidentFeedback/accidentFeedback.ts
import { putAction } from '../../../api/base';
const dayjs = require('dayjs');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    detailInf: {} as any,
    currentDate: new Date().getTime(),
    dateTimeStr: '请选择时间',
    options: [
      {
        name: '请选择',
        type: ''
      },
      {
        name: '是',
        type: 1
      },
      {
        name: '否',
        type: 0
      }
    ],
    selectIndex: 0,
    selectOption: {} as any,
    marker: '',
    photoList: {
      accidentCertificatePhoto: [],
      compensationAgreementPhoto: [],
      lossAssessmentPhoto: [],
      otherPhoto: []
    } as any,
    uploadUrl: 'api/vehicles/accidentInfo/upload'
  },
  bindPicker(e: any) {
    console.log(e.detail.value);
    let index = Number(e.detail.value);
    this.setData({
      selectIndex: index,
      selectOption: this.data.options[index]
    });
  },
  showDateTimePicker() {
    this.setData({
      show: true
    });
  },
  dateOnInput(event: any) {
    console.log(event.detail);

    this.setData({
      currentDate: event.detail,
      dateTimeStr: dayjs(new Date(event.detail)).format('YYYY-MM-DD HH:mm'),
      show: false
    });
  },
  getMarker(e: any) {
    this.setData({
      marker: e.detail.value
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  returnPic(e: any) {
    console.log(e.detail.photoList);
    let urlList = [] as any;
    e.detail.photoList.forEach((item: any) => {
      urlList.push(item.fileUrl);
    });
    let tmp = this.data.photoList;
    tmp[e.currentTarget.dataset.key] = urlList;
    this.setData({
      photoList: tmp
    });
  },

  checkStr(str: string) {
    let returnStr = '';
    let spaceRegex = /^\S*$/;
    let regex = /[!@#$%^&*?><|\\\\]/;
    let tooLong = str.length > 32;
    if (!spaceRegex.test(str)) {
      returnStr = '不支持含有空格';
    } else if (regex.test(str)) {
      returnStr = '不支持特殊字符';
    } else if (tooLong) {
      returnStr = '输入长度不得超过32个字符';
    } else {
      returnStr = '';
    }
    return returnStr;
  },
  bindSend() {
    let parms = this.data.detailInf;
    parms.accidentProgress = Object.assign(parms.accidentProgress, {
      accidentCertificatePhoto:
        this.data.photoList.accidentCertificatePhoto.join(','),
      compensationAgreementPhoto:
        this.data.photoList.compensationAgreementPhoto.join(','),
      lossAssessmentPhoto: this.data.photoList.lossAssessmentPhoto.join(','),
      otherPhoto: this.data.photoList.otherPhoto.join(','),
      remark: this.data.marker,
      closingTime: this.data.dateTimeStr,
      caseClosed: this.data.selectOption.type
    });
    if (this.data.selectIndex == 0) {
      wx.showToast({
        title: '请选择是否结案',
        icon: 'none'
      });
      return;
    }
    if (parms.accidentProgress.closingTime == '请选择时间') {
      wx.showToast({
        title: '请选择结案时间',
        icon: 'none'
      });
      return;
    }
    // 验证非必填
    if (parms.accidentProgress.remark) {
      let msg = this.checkStr(parms.accidentProgress.remark);
      if (msg) {
        wx.showToast({
          title: '备注：' + msg,
          icon: 'none'
        });
        return;
      }
    }
    console.log(parms);
    putAction('api/vehicles/accidentInfo/smallprogramEdit', parms).then(
      (res: any) => {
        if (res.code == 200) {
          console.log(res.result);
          // setTimeout(() => {
          //   wx.navigateBack({ delta: 2 });
          // }, 1500);
          wx.navigateTo({
            url: '../backlogFeedback/backlogFeedback',
            fail: e => {
              console.log(e);
            }
          });
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      }
    );
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // 接收传参
    const eventChannel = this.getOpenerEventChannel();

    eventChannel &&
      eventChannel.on &&
      eventChannel.on('eventName', (data: any) => {
        console.log('接收参数', data);
        this.setData({
          detailInf: data
        });
      });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

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

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

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

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

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