// pages/violationReporting/violationReporting.ts
import { getAction, upload, postAction } from '../../api/base';
import Dialog from '@vant/weapp/dialog/dialog';
const dayjs = require('dayjs');
const isSpecialChar: RegExp = /[\"'{};<>!@#$%^&*|\\\\]/; //特殊字符校验
const moneyNumberExp: RegExp = /^\d{1,9}(\.\d{1,2})?$/; //金额限制整数部分不能大于9位 小数部分不能大于2位
Page({
  /**
   * 页面的初始数据
   */
  data: {
    identificationVal: '',
    allVehicleList: [], //车辆列表
    violationBehavior: [], //违章行为列表
    violationTimestamp: new Date().getTime(),
    requireObj: {
      plateNumber: '',
      projectId: '',
      director: '',
      violationTime: '',
      violationAddress: '',
      violationActText: ''
    },
    params: {
      plateNumber: '',
      projectId: '', //所属项目id
      director: '', //责任人
      violationTime: '', //违章时间
      violationAddress: '', //违章地点
      violationAct: '', //违章行为
      violationActText: '',
      violationActDes: '', //违章行为描述
      violationDriver: '', //违章司机
      violationScore: '', //违章计分
      violationCost: '', //违章费用
      handleTerm: '', //处理期限（天）
      handleTime: '', //处理时间
      handleResult: '', //处理结果（1-已处理、0-未处理）
      handleVoucher: '', //处理凭证（附件url）
      remark: '' //备注
    },
    validate: {
      message: ''
    },
    plateNumberShow: false,
    violationTimeShow: false,
    violationActShow: false,
    plateNumberSearchVal: '',
    violationActDefaultIdx: 0, //违章行为默认值
    plateNumberPickerIndex: 0 //车牌号码默认值
  },
  inputChange(event: any) {
    //获取输入框输入内容
    let fieldName: any = event.currentTarget.dataset.fieldname;
    let data: any = { ...this.data };
    data.params[fieldName] = event.detail.value;
    if (
      fieldName === 'message' ||
      fieldName === 'violationActDes' ||
      fieldName === 'violationDriver' ||
      fieldName === 'violationScore' ||
      fieldName === 'violationCost' ||
      fieldName === 'handleTerm'
    ) {
      if (event.detail.value) {
        if (isSpecialChar.test(event.detail.value)) {
          //非必填特殊符号校验
          data.validate[fieldName] = '内容含有特殊字符';
        } else if (
          fieldName === 'violationCost' &&
          Number(event.detail.value) > 999999
        ) {
          data.validate[fieldName] = '违章费用不能大于999999';
        } else if (
          (fieldName === 'violationScore' || fieldName === 'handleTerm') &&
          Number(event.detail.value) > 32766
        ) {
          data.validate[fieldName] =
            (fieldName === 'violationScore' ? '违章计分' : '处理期限') +
            '不能大于32766';
        } else {
          data.validate[fieldName] = '';
          delete data.validate[fieldName];
        }
      } else {
        data.validate[fieldName] = '';
        delete data.validate[fieldName];
      }
    }
    if (data.params[fieldName]) {
      if (isSpecialChar.test(data.params[fieldName])) {
        data.requireObj[fieldName] = '内容含有特殊字符';
      } else {
        delete data.requireObj[fieldName];
      }
    } else {
      data.requireObj[fieldName] = '此项为必填项';
    }
    this.setData(data);
  },
  pickerChange(event: any) {
    //下拉框切换是否展示
    let fieldName: any = event.currentTarget.dataset.fieldname;
    let data: any = { ...this.data };
    data[fieldName] = true;
    this.setData(data);
  },
  hidePicker() {
    this.setData({
      plateNumberShow: false,
      violationTimeShow: false,
      violationActShow: false
    });
  },
  pickerConfirm(event: any) {
    //下拉框点击确认
    let { fieldname, fieldvalue } = event.currentTarget.dataset;
    let detail = event.detail;
    let data: any = { ...this.data };
    if (fieldname === 'plateNumber') {
      data.params.projectId = detail.value.projectId;
      data.params.projectName = detail.value.projectName;
      data.params.director = detail.value.projectManager;
      data.params[fieldname] = detail.value.text;
      if (data.params.projectId) {
        delete data.requireObj.projectId;
      } else {
        data.requireObj.projectId = '此项为必填项';
      }
      if (data.params.director) {
        delete data.requireObj.director;
      } else {
        data.requireObj.director = '此项为必填项';
      }
    } else if (fieldname === 'violationTime') {
      data.params[fieldname] = dayjs(detail).format('YYYY-MM-DD HH:mm');
      data.violationTimestamp = detail;
    } else {
      data.params[fieldname] = detail.value.text;
      data.params[fieldvalue] = detail.value.value;
    }
    if (data.params[fieldname]) {
      delete data.requireObj[fieldname];
    } else {
      data.requireObj[fieldname] = '此项为必填项';
    }
    this.setData(data);
    this.hidePicker();
  },
  queryAllVehicle(event: any) {
    getAction('api/vehicles/violation/queryVehicleProject', {
      plateNumber: event ? event.detail : ''
    }).then((res: any) => {
      let list = res.result;
      let arr: any = [];
      list.map((item: any) => {
        let obj: any = {
          value: item.plateNumber,
          text: item.plateNumber,
          projectId: item.projectId,
          projectName: item.projectName,
          projectManager: item.projectManager
        };
        arr.push(obj);
      });
      this.setData({
        allVehicleList: arr,
        plateNumberSearchVal: event ? event.detail : '',
        plateNumberPickerIndex: 0
      });
      console.log('车牌号码列表', this.data.allVehicleList);
    });
  },
  getDict() {
    getAction('api/sys/dict/getDictItems/violation_behavior').then(
      (res: any) => {
        this.setData({
          violationBehavior: res.result
        });
        console.log('违章行为列表', this.data.violationBehavior);
      }
    );
  },
  analyzeMessage() {
    let params: any = this.data.params;
    if (!params.message || this.data.validate.message) {
      return;
    }
    postAction('api/vehicles/violation/analyzeMessage', {
      template: params.message
    }).then((res: any) => {
      let result: any = res.result;
      let requireObj: any = this.data.requireObj;
      let busList: any = this.data.allVehicleList;
      let violationBehavior: any = this.data.violationBehavior; //违章行为列表

      let date = result.violationTime
        ? dayjs(dayjs(result.violationTime, 'YYYY-MM-DD HH:mm')).format(
            'YYYY-MM-DD HH:mm'
          )
        : dayjs().format('YYYY-MM-DD HH:mm');
      let busFilterIdx =
        busList.length > 0
          ? busList.findIndex((x: any) => x.text.includes(result.plateNumber))
          : [];
      result.director =
        busFilterIdx != -1 ? busList[busFilterIdx].projectManager : null;
      result.projectId =
        busFilterIdx != -1 ? busList[busFilterIdx].projectId : null;
      result.projectName =
        busFilterIdx != -1 ? busList[busFilterIdx].projectName : null;
      let violationActDefaultIdx, plateNumberPickerIndex;
      plateNumberPickerIndex = busFilterIdx != -1 ? busFilterIdx : 0;
      if (result.violationAct_dictText) {
        let behaviorFilterIndex =
          violationBehavior.length > 0
            ? violationBehavior.findIndex((x: any) =>
                x.text.includes(result.violationAct_dictText)
              )
            : [];
        result.violationAct_dictText =
          behaviorFilterIndex != -1 ? result.violationAct_dictText : null;
        violationActDefaultIdx =
          behaviorFilterIndex != -1 ? behaviorFilterIndex : 0;
      } else {
        violationActDefaultIdx = 0;
      }
      // 新的违章行为
      if (result.isNewBehavior) {
        Dialog.confirm({
          title: '违章行为',
          message: `识别出新的违章行为『${result.newBehavior}』,是否更新？`
        })
          .then(() => {
            postAction('api/vehicles/violation/addViolationBehavior', {
              newBehavior: result.newBehavior
            }).then((data: any) => {
              if (data.success) {
                this.getDict();
                delete requireObj.violationActText;
                this.setData({
                  [`params.violationAct`]: data.message,
                  [`params.violationActText`]: result.newBehavior,
                  requireObj
                });
              } else {
                wx.showToast(data.message);
              }
            });
          })
          .catch(() => {});
      }
      if (result.plateNumber) {
        delete requireObj.plateNumber;
      } else {
        requireObj.projectId = '此项为必填项';
      }
      if (result.violationAct_dictText) {
        delete requireObj.violationActText;
      } else {
        requireObj.violationActText = '此项为必填项';
      }
      if (result.violationTime) {
        delete requireObj.violationTime;
      } else {
        requireObj.violationTime = '此项为必填项';
      }
      if (result.violationAddress) {
        delete requireObj.violationAddress;
      } else {
        requireObj.violationAddress = '此项为必填项';
      }
      if (result.director) {
        delete requireObj.director;
      } else {
        requireObj.director = '此项为必填项';
      }
      if (result.projectId) {
        delete requireObj.projectId;
      } else {
        requireObj.projectId = '此项为必填项';
      }

      this.setData({
        [`params.plateNumber`]: result.plateNumber,
        // [`params.violationAct`]: result.violationAct,
        // [`params.violationActText`]: result.violationAct_dictText,
        [`params.violationAddress`]: result.violationAddress,
        [`params.director`]: result.director,
        [`params.projectId`]: result.projectId,
        [`params.projectName`]: result.projectName,
        [`params.violationTime`]: date,
        [`violationTimestamp`]: date
          ? dayjs(date).valueOf()
          : new Date().getTime(),
        requireObj,
        violationActDefaultIdx,
        plateNumberPickerIndex
      });
      console.log('识别内容', res);
    });
  },
  formSubmit() {
    //提交
    let isRequireOk: any = true;
    let params: any = this.data.params;
    let requireObj: any = this.data.requireObj;
    let validate: any = this.data.validate;
    for (let key in requireObj) {
      if (!requireObj[key] || requireObj[key] === '此项为必填项') {
        isRequireOk = false;
        requireObj[key] = '此项为必填项';
      }
    }
    for (let key in validate) {
      if (validate[key]) {
        isRequireOk = false;
      }
    }
    this.setData({
      requireObj
    });
    if (!isRequireOk) {
      return;
    }
    params.violationScore = Number(params.violationScore);
    params.violationCost = Number(params.violationCost);
    params.handleTerm = Number(params.handleTerm);
    console.log('提交参数', params);
    postAction('api/vehicles/violation/add', params).then((res: any) => {
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 1000
      });
      setTimeout(() => {
        wx.reLaunch({
          url: '/pages/maintenanceIndex/maintenanceIndex'
        });
      }, 1000);
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.queryAllVehicle(null);
    this.getDict();
  },

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

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

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

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

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

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

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