// pages/t-water/t-water.js
var myRequest = require("../../utils/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {
      styleIsolation: 'apply-shared',
    },
    projectId: '',
    deviceCodeData: [],
    formData: [],
    sampleBackData: [],
    sampleTypeData: [],
    detectionItemsData: [],
    detectionReportData: [],
    reportIssuedData: [],
    reportUseData: [],
    // asOfTime: new Date().getTime(),
    // asOfTimeVisible: false,
    start: '2000-01-01 00:00:00',
    end: '2999-12-31 00:00:00',
    dateVisible: false,
    date: new Date().getTime(), // 支持时间戳传入
    dateText: '',
    value1: '',
    first: 0,
    deviceCode: '',
    deviceName: '',
  },
  onChange1(e) {
    this.setData({ value1: e.detail.value });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.projectId = wx.getStorageSync('projectId');
    this.getDeviceData();
    this.getDictData();
  },
  selectDevice()
  {
    wx.navigateTo({
      url: '../search-site/search-site?type=shuizhi',
    })
  },
  showPicker(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const { value } = e.detail;
    const { mode } = this.data;
    console.log('confim', value);
    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
      'formData.asOfTime': e.detail.value,
    });
    
    this.hidePicker();
  },
  onColumnChange(e) {
    console.log('pick', e.detail.value);
  },


  //获取站点
  getDeviceData() {
    var that = this;
    myRequest.myRequest('inspection/deviceList', {
      projectId: that.data.projectId
    },'get').then(function(res){
      var deviceData = res.data.data;
      var arr = [];
      for (let i = 0; i < deviceData.length; i++) {
        arr.push({label: deviceData[i].name, value: deviceData[i].code})
      }
      that.setData({
        deviceCodeData: arr,
      });
      console.log(that.data.deviceData)
    }).catch(function(res){
      console.log(res);
    })
  },
  onWaterDate(e) {
    this.setData({
      'formData.asOfTime': e.detail,
    })
  },
  // 获取字典数据
  getDictData() {
    var that = this;
    myRequest.myRequest('waterApply/dict', {}).then(function(res){
      var dictData = res.data.data;
      var sampleBack = [];
      var sampleType = [];
      var detectionItems = [];
      var detectionReport = [];
      var reportIssued = [];
      var reportUse = [];
      for (let i = 0; i < dictData.sampleBack.length; i++) {
        sampleBack.push({label: dictData.sampleBack[i].dataValue, value: dictData.sampleBack[i].dataCode})
      }
      for (let i = 0; i < dictData.sampleType.length; i++) {
        sampleType.push({label: dictData.sampleType[i].dataValue, value: dictData.sampleType[i].dataCode})
      }
      for (let i = 0; i < dictData.detectionItems.length; i++) {
        detectionItems.push({label: dictData.detectionItems[i].dataValue, value: dictData.detectionItems[i].dataCode})
      }
      for (let i = 0; i < dictData.detectionReport.length; i++) {
        detectionReport.push({label: dictData.detectionReport[i].dataValue, value: dictData.detectionReport[i].dataCode})
      }
      for (let i = 0; i < dictData.reportIssued.length; i++) {
        reportIssued.push({label: dictData.reportIssued[i].dataValue, value: dictData.reportIssued[i].dataCode})
      }
      for (let i = 0; i < dictData.reportUse.length; i++) {
        reportUse.push({label: dictData.reportUse[i].dataValue, value: dictData.reportUse[i].dataCode})
      }
      that.setData({
        sampleBackData: sampleBack,
        sampleTypeData: sampleType,
        detectionItemsData: detectionItems,
        detectionReportData: detectionReport,
        reportIssuedData: reportIssued,
        reportUseData: reportUse,
      })
      console.log(that.data.sampleBack);
    }).catch(function(res){
      console.log(res);
    })
  },
  dispatch() {
    var first = this.data.first;
    if (first < 3) {
      this.setData({
        first: this.data.first + 1
      })
    }
  },
  onDetectionItemsChange(e) {
    var detectionItems = e.detail.value;
    this.setData({
      [`formData.detectionItems`]: detectionItems.join(',')
    })
  },
  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value, label } = e.detail;

    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: label.join(' '),
      [`formData.${key}`]: value[0]
    });
    console.log(this.data.formData)
  },
  changeSampleQuantity(e) {
    this.setData({
      'formData.sampleQuantity': e.detail.value
    });
  },
  changeRemark(e) {
    this.setData({
      'formData.remark': e.detail.value
    });
  },
  showPicker(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onCityPicker(e) {
    var filed = e.currentTarget.dataset.filed;
    console.log(filed)
    this.setData({
      [`${filed}Visible`]: true,
    });
  },
  onFirstChange(e) {
    this.setData({ first: e.detail.current });
  },
  submitForm() {
    const { deviceCode, sampleBack, sampleType, detectionItems, detectionReport, reportIssued, reportUse, asOfTime, sampleQuantity, remark } = this.data.formData;
    if (detectionItems == undefined || detectionItems == '') {
      wx.showToast({
        title: '请选择检测项目',
        icon: 'none',
      })
      return false;
    }
    if (deviceCode == undefined || deviceCode == '') {
      wx.showToast({
        title: '请选择站点',
        icon: 'none',
      })
      return false;
    }
    if (sampleBack == undefined || sampleBack == '') {
      wx.showToast({
        title: '请选择样品取回',
        icon: 'none',
      })
      return false;
    }
    if (sampleType == undefined || sampleType == '') {
      wx.showToast({
        title: '请选择样品类型',
        icon: 'none',
      })
      return false;
    }
    
    if (detectionReport == undefined || detectionReport == '') {
      wx.showToast({
        title: '请选择监测报告',
        icon: 'none',
      })
      return false;
    }
    if (reportIssued == undefined || reportIssued == '') {
      wx.showToast({
        title: '请选择报告出具',
        icon: 'none',
      })
      return false;
    }
    if (reportUse == undefined || reportUse == '') {
      wx.showToast({
        title: '请选择报告用途',
        icon: 'none',
      })
      return false;
    }
    if (asOfTime == undefined || asOfTime == '') {
      wx.showToast({
        title: '请选择截至时间',
        icon: 'none',
      })
      return false;
    }
    if (sampleQuantity == undefined || sampleQuantity == '') {
      wx.showToast({
        title: '请输入样品数量',
        icon: 'none',
      })
      return false;
    }
    if (remark == undefined || remark == '') {
      wx.showToast({
        title: '请输入备注',
        icon: 'none',
      })
      return false;
    }
    myRequest.myRequest('waterApply/add', {
      deviceCode: deviceCode,
      sampleQuantity: sampleQuantity,
      sampleBack: sampleBack,
      sampleType: sampleType,
      detectionItems: detectionItems,
      detectionReport: detectionReport,
      reportIssued: reportIssued,
      reportUse: reportUse,
      asOfTime: asOfTime,
      remark: remark,
    },'post').then(function(res){
      wx.showToast({
        title: '提交成功',
        icon: 'none',
      })
      wx.navigateTo({
        url: '../index/index',
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var deviceCode = this.data.deviceCode;
    this.setData({
      [`formData.deviceCode`]: deviceCode
    })
  },

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

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

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

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

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




