// pages/create-order/index.js
import http from '../../utils/http'
import util from '../../utils/util'
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['维修记录填写', '维修配件添加'],
    tabIndex: 0,
    work_points: [],
    pointIndex: -1,
    id: -1,
    project: null,
    remark: '',
    inner_devices: [],
    deviceIndex: -1,
    parts: [],
    part_name: '',
    part_change: '',
    part_price: '',
    showAdd: false,
    order_id: '',
    // create/edit
    type: 'create',
    // create/edit
    dialog_type: 'create',
    default_dates: [],
    showDevice: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id ? options.id : 1
    var type = options.type ? options.type : 'create'
    var order_id = options.order_id ? options.order_id : ''
    this.setData({
      id,
      type,
      order_id
    })
    api.getProject(this)
    api.getByName(this, 'work-points/get', 'work_points');
    api.getByName(this, 'inner-devices/get', 'inner_devices', {project_id:id});
  },

  switchChecked(e) {
    var name = e.currentTarget.dataset.name
    var items = this.data[name]
    var index = e.currentTarget.dataset.index
    for(var i = 0; i < items.length; ++i) {
      items[i].checked = false;
    }
    items[index].checked = true;
    this.setData({
      [name]: items,
      deviceIndex: index
    })
  },

  submit: function(e) {
    // if(this.data.pointIndex < 0) {
    //   util.error('需求工点必填');
    //   return false;
    // }
    if(this.data.deviceIndex < 0) {
      util.error('维修设备必填');
      return false;
    }
    // var work_point = this.data.work_points[this.data.pointIndex]
    var device = this.data.inner_devices[this.data.deviceIndex]
    var url = 'repair-devices/create'
    http({
      url: url,
      data: {
        project_id: this.data.id,
        // work_point_id: work_point.id,
        inner_device_id: device.id,
        money: this.data.money,
        reason: this.data.reason,
        day: this.data.day,
        remark: this.data.remark,
        parts: this.data.parts
      },
      success: function(res) {
        if(res.code == 0) {
          util.success('操作成功')
        }
      }
    })
  },

  switchTab: function(e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    })
  },

  delete: function(e) {
    var parts = this.data.parts
    var index = e.currentTarget.dataset.index
    parts.splice(index, 1)
    this.setData({
      parts
    })
  },

  add: function() {
    if(!this.data.part_name) {
      util.error('维修部位必填');
      return false;
    }
    if(!this.data.part_change) {
      util.error('更换配件必填');
      return false;
    }
    if(!this.data.part_price) {
      util.error('配件价格必填');
      return false;
    }
    var parts = this.data.parts
    var part = {
      name: this.data.part_name,
      change: this.data.part_change,
      price: this.data.part_price
    }
    parts.push(part)
    this.setData({
      parts
    })
  },

  onChange: function(e) {
    var name = e.currentTarget.dataset.name
    var val = e.detail.value
    
    this.setData({
      [name]: val
    })

    if(name == 'pointIndex') {
      var work_point = this.data.work_points[val]
      var work_point_id = work_point ? work_point.id : ''
      var that = this
      api.getByName(this, 'inner-devices/get', 'inner_devices', {
        work_point_id: work_point_id
      }, function() {
        that.setData({
          deviceIndex: -1
        })
      });
    }
  },

  switchShow(e) {
    var name = e.currentTarget.dataset.name
    var val = !this.data[name]
    this.setData({
      [name]: val
    })
  },

  switchShowAdd: function(e) {
    var show = e.currentTarget.dataset.show
    if(show) {
      this.setData({
        part_name: '',
        part_change: '',
        part_price: '',
      })
    }
    this.setData({
      showAdd: show,
      dialog_type: 'create'
    })
  },

  switchShowDate: function(e) {
    this.setData({
      showDate: e.currentTarget.dataset.show
    })
  },

  confirmDate: function(e) {
    this.switchShowDate(e)
    var [start_date, end_date] = e.detail;
    start_date = util.formatDate(start_date)
    end_date = util.formatDate(end_date)
    this.setData({
      start_date,
      end_date
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})