const app = getApp();
var baseUrl = app.globalData.baseUrl;
var network = require("../../utils/net.js");
var timechage = require("../../utils/dateTimeUtil.js");

// var LoadTime1;
// var Number;
// var SecondNumber;
// var Phone;

Page({
  data: {
    orderID: null,
    carNumber: '请选择运输车辆',
    carID: null,
    driverName: '',
    driverID: null,
    driverPhone: null,
    carList: [],
    isNewCar: false,
  },

  onLoad: function(options) {
    var that = this;
    var orderID = parseInt(options.ID);
    var method1 = '/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/OrderRpc/LoadForDriver';
    var params1 = [orderID];
    network.transfer_request(method1, params1, function(res) {
      that.setData({
        orderID: res.result.ID,
        LoadTime1: timechage.formatTimeTwo(res.result.LoadTime.substring(6, 19), "Y/M/D"),
        Number: res.result.Number,
        SecondNumber: res.result.SecondNumber,
      })
    })
  },

  searchCarByInput: function(event) {
    var searchStr = event.detail.value;
    var that = this;
    var method2 = '/MainSystem/B3MiniProgramRpc/Rpcs/BaseInfoRpc/GetCar';
    var params2 = [{
      "InputValue": searchStr,
      "PageIndex": 0,
      "PageSize": 100
    }];
    network.transfer_request(method2, params2, function(res) {
      that.setData({
        carList: res.result,
      })
    })
  },

  findCar: function(searchString) {
    var that = this;
    var method2 = '/MainSystem/B3MiniProgramRpc/Rpcs/BaseInfoRpc/GetCar';
    var params2 = [{
      "InputValue": '',
      "PageIndex": 0,
      "PageSize": 100
    }];
    network.transfer_request(method2, params2, function(res) {
      that.setData({
        carList: res.result,
      })
    })
    this.util();
  },

  // 点击弹出选项值
  itemclick: function(event) {
    var carID = event.currentTarget.dataset.carid;
    var carName = event.currentTarget.dataset.carname;
    // 新增通过车辆ID获取司机信息
    this.getDriverInfo(carID);
    this.setData({
      carID: carID,
      carNumber: carName,
    })
    this.util();
  },

  getDriverInfo: function(carID) {
    var that = this;
    var method = '/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/OrderRpc/GetCarDriver';
    var params = [carID];
    network.transfer_request(method, params, function(res) {
      that.setData({
        driverName: res.result.Name,
        driverID: res.result.ID,
      })
    })
  },

  creatNewCar: function(event) {
    this.util();
    wx.navigateTo({
      url: 'creatNewCar/creatNewCar',
    })
  },

  paiche: function(event) {
    var that = this;
    if (this.data.orderID == null) {
      wx.showModal({
        title: '提示',
        content: '单据ID不能为空',
      })
      return false;
    }
    if (this.data.carNumber == null) {
      wx.showModal({
        title: '提示',
        content: '运输车辆不能为空',
      })
      return false;
    } else if (this.data.driverName == null) {
      wx.showModal({
        title: '提示',
        content: '车辆对应司机为空',
      })
      return false;
    } else {
      var method = '/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/OrderRpc/SetDriverToOrder';
      if (this.data.isNewCar) {
        var params = [{
          Order_ID: this.data.orderID,
          Car_Name: this.data.carNumber,
          Driver_Name: this.data.driverName,
          Dreiver_Phone: this.data.driverPhone,
        }]
      } else {
        var params = [{
          Order_ID: this.data.orderID,
          Car_ID: this.data.carID,
          Driver_ID: this.data.driverID,
        }]
      }

      network.transfer_request(method, params, function(res) {
        wx.showToast({
          title: '派车成功',
          complete: function() {
            wx.navigateBack({
              delta: "/pages/theDriverSend/theDriverSend"
            })
          }
        })
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this
    return {
      title: '',
      path: "/pages/fenxiang/fenxiang?ID=" + this.data.orderID,
      //分享成功后执行
      success: function(res) {
        wx.showModal({
          showCancel: false,
          title: '转发成功',
        })
      },
      fail: function(res) {}
    }
  },

  powerDrawer: function(e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util();
  },

  util: function() {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200, //动画时长  
      timingFunction: "linear", //线性  
      delay: 0 //0则不延迟  
    });
    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;
    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();
    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })
    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function() {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      this.setData({
        showModalStatus: !this.data.showModalStatus,
      });
    }.bind(this), 200)

  },

})