// pages/createorder/index.js
const api = require('../../../api/index.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartype:1,
    index:0,
    objectArray: [],
    objdriver: [],
    driverindex:0,
    date: '',
    time: '',       // 初始时间
    userdata:[],
    userindex:0,
    addressindex:'',
    address: [],
    items: [
      {value: 1, name: '光伏'},
      {value: 2, name: '风电'},
      {value: 3, name: '网电'},
    ],
    greenelectricity:0,
    workorderpower:'',
    orderdata:[],
    orderid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
this.getaddress()

    if (options.order!='') {
      this.setData({
        orderid:options.order
      })
     this.getoneworkorder()
    }
   
//  this.getdriver()
// this.getvehicle()
  },
  // 订单详情
  getoneworkorder() {
    const data = {
      workorderId: this.data.orderid
    };
    
    api.request(`/sysworkorder/selectworkorderId`, 'post', data, { isPublic: false })       
      .then((data) => {
        if (data.code == 200) {
       
          this.setData({
            cartype:data.data.chargedischargeType,
            workorderElectricity:data.data.workorderElectricity,
            workorderpower:data.data.workorderPower,
            greenelectricity:data.data.greenelectricity,
            date:data.data.workorderStarttime.slice(0,10),
            time:data.data.workorderStarttime.slice(11,16),
          })
          for (let index = 0; index < this.data.address.length; index++) {
            if (data.data.addressId==this.data.address[index].addressId) {
              this.setData({
               addressindex:index
              })
            }
       }
  
        }
      })
      .catch((err) => {
        console.error('获取工单信息失败：', err);
        this.showInfo('获取工单信息失败');
      });
  },
  // 司机
  getdriver(){
    let data ={

    }
    api.request(`/sysdriver/selectdriverall`, 'post',data,{ isPublic: false })
    .then((data) => {
     console.log(data.data);
     let obj = []

     for (let index = 0; index < data.data.length; index++) {
      let carType = ''
      if (data.data[index].operationType==1) {
        carType = '空闲'
      }else if(data.data[index].operationType==2){
        carType = '工作中'
      }else if(data.data[index].operationType==3){
        carType = '维护中'
      }
      obj.push({
        id:data.data[index].operationId,
        name:data.data[index].operationName,
        subtitle:data.data[index].operationName + ' / ' + carType
      })    
     }
     this.setData({
      objdriver:obj
     })
    })
    .catch((err) => {
   
      console.error('请求失败：', err);
    });
  },
  // 车型
  getvehicle(){
    let data ={}
    api.request(`/syscar/selectcarall`, 'post',data,{ isPublic: false })
    .then((data) => {
      let obj = []
      for (let index = 0; index < data.data.length; index++) {
        let carType = ''
          if (data.data[index].carType==1) {
            carType = '空闲'
          }else if(data.data[index].carType==2){
            carType = '工作中'
          }else if(data.data[index].carType==3){
            carType = '维护中'
          }
          obj.push({
            id:data.data[index].carId,
            name:data.data[index].carId + ' / ' + data.data[index].carMondel,
            subtitle:data.data[index].carId + ' / ' + data.data[index].carMondel + ' / ' + carType

          })
      }
      this.setData({
        objectArray:obj
       })
 console.log(this.data.objectArray);
    })
    .catch((err) => {
   
      console.error('请求失败：', err);
    });
  },

  // 地址
  getaddress(){
    let data ={
      operationId:''
    }
    api.request(`/sysaddress/selectaddress`, 'post',data,{ isPublic: false })
    .then((data) => {
     console.log(data.data);
     this.setData({
      address:data.data
     })
    })
    .catch((err) => {
   
      console.error('请求失败：', err);
    });
  },
  oncar(e){
   console.log(e.currentTarget.dataset.index);
this.setData({
  cartype:e.currentTarget.dataset.index
})
  },
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
    // 时间选择变化
    bindTimeChange(e) {
      this.setData({ time: e.detail.value });
    },
  // 需求电量
  bindKeyInput (e) {
    this.setData({
      workorderElectricity: e.detail.value
    })
  },
  bindintentionInput (e) {
    this.setData({
      workorderpower: e.detail.value
    })
  },
  details(){
   if (this.data.workorderElectricity==''||this.data.date==''||this.data.time==''||this.data.workorderpower=='') {
    wx.showToast({
      title: '请填写完整消息',
      icon: 'none'
    });
    return
   }
    let data ={
      addressId:this.data.address[this.data.addressindex].addressId,
      workorderElectricity:this.data.workorderElectricity,
      chargedischargeType:this.data.cartype,
      greenelectricity:this.data.greenelectricity,
      workorderStarttime:this.data.date+' '+this.data.time,
      workorderpower:this.data.workorderpower
    }
    api.request(`/sysworkorder/addworkorder`, 'post',data,{ isPublic: false })
    .then((data) => {
 console.log(data);
 wx.switchTab({
  url: '/pages/tool/index',
})

    })
    .catch((err) => {
   
      console.error('请求失败：', err);
    });
 
  },
  // 编辑
  editdetails(){
    if (this.data.workorderElectricity==''||this.data.date==''||this.data.time==''||this.data.workorderpower=='') {
     wx.showToast({
       title: '请填写完整消息',
       icon: 'none'
     });
     return
    }
     let data ={
      workorderId:this.data.orderid,
       addressId:this.data.address[this.data.addressindex].addressId,
       workorderElectricity:this.data.workorderElectricity,
       chargedischargeType:this.data.cartype,
       greenelectricity:this.data.greenelectricity,
       workorderStarttime:this.data.date+' '+this.data.time,
       workorderpower:this.data.workorderpower
     }
     api.request(`/sysworkorder/updateworkorder`, 'post',data,{ isPublic: false })
     .then((data) => {
  console.log(data);
  wx.switchTab({
   url: '/pages/tool/index',
 })
 
     })
     .catch((err) => {
    
       console.error('请求失败：', err);
     });
  
   },
   //  司机
   bindectArrayChange(e){
    console.log(e.detail.value);
    this.setData({
      index:e.detail.value
    })
    },
    // 车型
  binddriverChange(e){
  console.log(e.detail.value);
  this.setData({
    driverindex:e.detail.value
  })
  },  
    // 用户
    binduserChange(e){
 console.log(e);
 this.setData({
  userindex:e.detail.value
})
    },
      // 地址
      bindPickerChange(e){
        console.log(e);
        this.setData({
          addressindex:e.detail.value
       })
           },
           radioChange(e) {
            console.log('radio发生change事件，携带value值为：', e.detail.value)
        
            // const items = this.data.items
            // for (let i = 0, len = items.length; i < len; ++i) {
            //   items[i].checked = items[i].value === e.detail.value
            // }
        
            this.setData({
              greenelectricity:e.detail.value
            })
            console.log(this.data.greenelectricity);
          },
          toconfigure(){
    wx.navigateTo({
            url: '/package-order/pages/address/index',
          });
          },
      
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getaddress()

  },

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

  },

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

  },

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

  },

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

  },

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