const app = getApp(),
  Api = require("../../../utils/util.js");
  const { API_MONIT_URL,API_BASE_URL,UP_MONIT_IMG } = require('../../../api/api')
Page({
  data: {
    formData: {},
    typeObj: [],
    typeList: [], //物料类型下拉
    specList: [], //物料规格下拉
    type: '',
    specName: '',
    applyNum: ''
  },
  onLoad(options) {    
    this.setData({
      orderId: options.orderId,    
      id: options.id,
      title: options.title, 
      userId: wx.getStorageSync('userId'),
      userName: wx.getStorageSync('name')
    })    
    wx.setNavigationBarTitle({
      title: options.title,
    })
    if(options.id){
      this.getDetail()
    }
    this.getTypeList()
  }, 
  //详情
  getDetail(e) {
    let that = this
    wx.showLoading({
      title: '加载中...',
    })   
    Api.req('blade-platformFittingApply/platformFittingApply/detail', {
      id: this.data.id
    }, 'get').then(res => {
        wx.hideLoading()
        if(res.code == 200){
          let typeId = this.data.typeObj.find(item=>item.dictValue == res.data.fittingVersion).dictKey;
          that.getSpecList(typeId)
          that.setData({
            formData: res.data,
            factoryMarks: res.data.factoryMarks,
            userName: res.data.empName,
            userId: res.data.applicantId,
            code: res.data.fittingCode,
            name: res.data.fittingName,
            type: res.data.fittingVersion,
            specName: res.data.fittingPower,
            applyNum: res.data.applyNum,
            remarks: res.data.applyMarks,
          })
        }
      })
  },
  //校验物料代码
  getMaterialCode(e) {
    let that = this
    if(e.detail.value){    
      wx.showLoading({
        title: '加载中...',
      })   
      Api.req('blade-platformFittingApply/platformFittingApply/getMaterialDetailByMaterialCode', {
        materialCode: e.detail.value
      }, 'get').then(res => {
          wx.hideLoading()
          if(res.msg =='操作成功'){
            that.getSpecList(res.data.type)
            that.setData({
              name: res.data.name,
              type: res.data.materialType,
              specName: res.data.power,
            })
          }else{
            wx.showToast({
              title: '物料代码不存在',
              icon: 'none',
            })
            that.setData({
              name: '',
              type: '',
              specName: '',
            })
          }
        })
    }
  },
  //物料规格
  getSpecList(key){
    Api.req('blade-platformFittingApply/platformFittingApply/getMaterialSpecList', {
      materialType: key //物料类型
      // orderId: options.id
    }, 'get')
    .then(res => {
      if(res.code == 200){
        this.setData({
          specList: res.data,
        })
      }
    }) 
  },
  //物料类型
  getTypeList(){
    Api.req('blade-system/dict/dictionary', {
      code: 'wl_type'
    }, 'get')
    .then(res => {
      if(res.code == 200){
        this.setData({
          typeObj: res.data,
          typeList: res.data.map(itm=>itm.dictValue),
          typeKeyList: res.data.map(itm=>itm.dictKey),
        })
      }
    }) 
  },    

  //文本输入
  inputText: function(e) {
    let name = e.currentTarget.dataset.name
    this.setData({
      [`${name}`]: e.detail.value,      
    })
  },
  //数字输入
  inputNum: function(e) {
    let name = e.currentTarget.dataset.name
    this.setData({
      [`${name}`]: e.detail.value,      
    })
  },

  //下拉选择
  changSelect: function(e) {
    let name = e.currentTarget.dataset.name
    let list = e.currentTarget.dataset.list
    let index = Number(e.detail.value);
    this.setData({
      [`${name}`]: this.data[list][index], 
    })
    if(name == 'type'){
      this.setData({
        specName: ''
      })
      let key = this.data.typeKeyList[index]
      this.getSpecList(key)
    }
  },


//提交
  submit: function(e) {
    var that = this;
    let url = ''
    if(this.data.type == ''){
      wx.showToast({
        title: '物料类型不能为空',
        icon: 'none'
      })
      return
    }else if(this.data.specName ==''){
      wx.showToast({
        title: '物料规格不能为空',
        icon: 'none'
      })
      return
    }else if(this.data.applyNum == '' || !/^[1-9]\d*$/.test(this.data.applyNum)){     
      wx.showToast({
        title: '申请数量只能是大于0的整数',
        icon: 'none',
        duration: 2000
      })
      return
    }    
    wx.showLoading({
      title: '提交中...',
      mask: true
    })
    var data = {
      orderId: this.data.orderId, //订单详情进入的要传
      empUserId: this.data.userId, //服务工程师
      materialCode: this.data.code, //物料代码
      materialName: this.data.name,//物料名称
      fittingVersion: this.data.type,//	物料类型
      materialSpec: this.data.specName, //物料规格
      applyNum: this.data.applyNum, //备件申请数量      
      applyMarks: this.data.remarks, //申请备注
    }
    if(this.data.id){
      data.id = this.data.id
      url = 'blade-platformFittingApply/platformFittingApply/update'
    }else{
      url = 'blade-platformFittingApply/platformFittingApply/save'
    }
    // console.log("执行",data);
    // return
    Api.req(url, data, 'post', '','1')
      .then(res => {
        wx.hideLoading()
        if (res.code == '200') {
          wx.showToast({
            title: '操作成功',
          })
          var page = getCurrentPages();
          let prevPage = page[page.length - 2];
          setTimeout(function() {
            prevPage.getList();
            wx.navigateBack({})
          }, 1000)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            mask: true
          })
        }
      })
  },
  //取消
  cancel(){
    wx.navigateBack()
  },
})