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

Page({
  /**
   * 页面的初始数据
   */
  data: {
    options: [
      {
        name: '请选择',
        type: ''
      },
      {
        name: '已处理',
        type: 1
      },
      {
        name: '未处理',
        type: 0
      }
    ],
    selectIndex: 0,
    selectOption: {} as any,
    detailInf: {} as any,
    show: false,
    currentDate: new Date().getTime(),
    dateTimeStr: '请选择时间',
    marker: '',
    photoList: [] as any,
    uploadUrl: 'api/vehicles/violation/upload',
    showSelect: {} as any,
    yesOrNoOptions: [
      {
        text: '已处理',
        value: 1
      },
      {
        text: '未处理',
        value: 0
      }
    ],
    yesOrNoSelectIndex: -1
  },
  pickerChange(event: any) {
    //下拉框切换是否展示
    let fieldName: any = event.currentTarget.dataset.fieldname;
    let data: any = { ...this.data };
    if (fieldName == 'yesOrNoSelectShow') {
      let { fieldindex } = event.currentTarget.dataset;
      data.changeProjectIndex = fieldindex;
    }
    data.showSelect[fieldName] = true;
    this.setData(data);
  },
  hidePicker() {
    this.setData({
      showSelect: {}
    });
  },
  pickerConfirm(event: any) {
    console.log(event);
    //下拉框点击确认
    let { fieldname, fieldindex } = event.currentTarget.dataset;
    let detail = event.detail;
    let data: any = { ...this.data };
    if (fieldname == 'yesOrNoSelectIndex') {
      data[fieldname] = detail.index;
    } else {
      data.params[fieldname] = detail.value.value;
      data[fieldindex] = detail.index;
    }
    // if (data.params[fieldname]) {
    //   // delete data.requireObj[fieldname];
    //   data.requireObj[fieldname] = '';
    // } else {
    //   data.requireObj[fieldname] = '请填写';
    // }
    console.log(this.data);
    this.setData(data);
    this.hidePicker();
  },
  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
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  getMarker(e: any) {
    this.setData({
      marker: e.detail.value
    });
  },
  returnPic(e: any) {
    console.log(e.detail.photoList);
    let urlList = [] as any;
    e.detail.photoList.forEach((item: any) => {
      urlList.push(item.fileUrl);
    });
    this.setData({
      photoList: urlList
    });
  },
  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 handleResult =
      this.data.yesOrNoSelectIndex == -1
        ? ''
        : this.data.yesOrNoOptions[this.data.yesOrNoSelectIndex].value;
    let parms = Object.assign(this.data.detailInf, {
      handleVoucher: this.data.photoList.join(','),
      remark: this.data.marker,
      handleTime: this.data.dateTimeStr,
      handleResult: handleResult
    });
    if (this.data.yesOrNoSelectIndex == -1) {
      wx.showToast({
        title: '请选择处理结果',
        icon: 'none'
      });
      return;
    }
    if (parms.handleTime == '请选择时间') {
      wx.showToast({
        title: '请选择处理时间',
        icon: 'none'
      });
      return;
    }
    // 验证非必填
    if (parms.remark) {
      let msg = this.checkStr(parms.remark);
      if (msg) {
        wx.showToast({
          title: '备注：' + msg,
          icon: 'none'
        });
        return;
      }
    }
    wx.showLoading({
      title: '提交中',
      icon: 'none'
    });
    putAction('api/vehicles/violation/edit', parms).then((res: any) => {
      wx.hideLoading();
      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() {}
});
