// pages/c-dispatch/c-dispatch.js
var myRequest = require("../../utils/api.js");
Page({
  data: {
    checked: false,
    first: 0,
    originFiles: [],
    gridConfig: {
      column: 4,
      width: 160,
      height: 160,
    },
    config: {
      count: 1,
    },
    singleSelect: {
      value: '',
      options: [],
    },
    keys: {
      value: 'code',
      label: 'name',
    },
    dictData: [],
    projectId: '',
    deviceData: '',
    label: '请选择站点',
    formData: {},
    fileList: {
      environment: [],
      road: [],
      fence: [],
      greening: [],
      manholeCover: [],
      aisleBoard: [],
      billboard: [],
      maintenance: [],
      liftPump: [],
      valveShaft: [],
      measuringWell: [],
      air: [],
      digestiveJuices: [],
      sludge: [],
      microbial: [],
      settlingBasin: [],
      samplingPool: [],
      external: [],
      internal: [],
      circuitSystem: [],
      relay: [],
      remoteModule: [],
      display: [],
      flowMeter: [],
      electricMeter: [],
      pipeline: [],
      fan: [],
      liquidState: [],
      intoWater: [],
      currentSituation: [],
      waterQuality: [],
      intakeGrille: [],
      waterQualityInspection: [],
      drainage: [],
      deviceCode: '',
      deviceName: ''
    },
  },
  onLoad() {
    this.setFormData();
    this.data.projectId = wx.getStorageSync('projectId');
    this.getDictData();
    // this.getDeviceData();
  },
  onShow() {
    var deviceCode = this.data.deviceCode
    
    this.setData({
      'formData.deviceCode': deviceCode,
    });
    console.log(this.data.formData)
  },
  setFormData() {
    var pream = JSON.stringify({exception: '0', dispose: '1', img: ''});
    this.setData({
      ['formData.environment']: pream,
      ['formData.road']: pream,
      ['formData.fence']: pream,
      ['formData.greening']: pream,
      ['formData.manholeCover']: pream,
      ['formData.aisleBoard']: pream,
      ['formData.processCard']: pream,
      ['formData.billboard']: pream,
      ['formData.maintenance']: pream,
      ['formData.liftPump']: pream,
      ['formData.valveShaft']: pream,
      ['formData.measuringWell']: pream,
      ['formData.air']: pream,
      ['formData.digestiveJuices']: pream,
      ['formData.sludge']: pream,
      ['formData.microbial']: pream,
      ['formData.settlingBasin']: pream,
      ['formData.samplingPool']: pream,
      ['formData.external']: pream,
      ['formData.internal']: pream,
      ['formData.circuitSystem']: pream,
      ['formData.relay']: pream,
      ['formData.remoteModule']: pream,
      ['formData.display']: pream,
      ['formData.flowMeter']: pream,
      ['formData.electricMeter']: pream,
      ['formData.pipeline']: pream,
      ['formData.fan']: pream,
      ['formData.liquidState']: pream,
      ['formData.intoWater']: pream,
      ['formData.currentSituation']: pream,
      ['formData.waterQuality']: pream,
      ['formData.intakeGrille']: pream,
      ['formData.waterQualityInspection']: pream,
      ['formData.drainage']: pream,
    })
  },
  onFirstChange(e) {
    this.setData({ first: e.detail.current });
  },
  // 获取字典数据
  getDictData() {
    var that = this;
    myRequest.myRequest('inspection/dict', {}).then(function(res){
      var dictData = res.data.data;
      that.setData({
        dictData: dictData
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  //选择站点触发
  handleSingleSelect(e) {
    if (this.data.formData.deviceCode) {
      wx.showModal({ 
        title: '提示',
        content: '是否确认切换站点?',
        success: res => {
          if (res.confirm) {
            this.setData({
              'singleSelect.value': e.detail.value,
              'formData.deviceCode': e.detail.value,
            });
            this.setSelectLabel(e.detail.value)
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
      })
    } else {
      this.setData({
        'singleSelect.value': e.detail.value,
        'formData.deviceCode': e.detail.value,
      });
      this.setSelectLabel(e.detail.value)
    }
  },
  setSelectLabel(value) {
    var options = this.data.singleSelect.options;
    for (let i = 0; i < options.length; i++) {
      if(options[i].code == value) {
        this.setData({
          label: options[i].name
        })
      }
    }
    console.log(this.data.label)
  },
  search() {
    wx.navigateTo({
      url: '../search-site/search-site?type=xunjian',
    })
  },
  //获取站点
  getDeviceData() {
    var that = this;
    myRequest.myRequest('inspection/deviceList', {
      projectId: that.data.projectId
    },'get').then(function(res){
      var deviceData = res.data.data;
      that.setData({
        'singleSelect.options': deviceData,
        'singleSelect.value': deviceData[0].id,
      });
    }).catch(function(res){
      console.log(res);
    })
  },
  submitForm() {
    var pream = JSON.parse(JSON.stringify(this.data.formData));
    if (pream.deviceCode == undefined) {
      wx.showToast({
        title: '请选择站点',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    myRequest.myRequest('inspection/add', pream,'post').then(function(res){
      wx.showToast({
        title: '提交成功',
        icon: 'none',
      })
      wx.navigateTo({
        url: '/pages/index/index',
      })
    }).catch(function(res){
      console.log(res);
    })
  },
  dispatch() {
    var first = this.data.first;
    if (first < 3) {
      this.setData({
        first: this.data.first + 1
      })
    }
  },
  handleChange(e) {
    this.setData({
      checked: e.detail.checked,
    });
  },
  handleAdd(e) {
    var filed = e.currentTarget.dataset.filed;
    var that = this;
    const { files } = e.detail;
    var fileLists = that.data.fileList[filed];
    var fieldData = that.data.formData[filed];
    var pream = JSON.parse(fieldData);
    for (let i = 0; i < files.length; i++) {
      myRequest.updateImg({file:files[i].url}).then(function(res){
        fileLists.push({url:files[0].url,type:'image',percent:100});
        pream.img = pream.img + res + ',';
        that.setData({
          ['formData.'+filed+'']: JSON.stringify(pream),
          ['fileList.'+filed+'']: fileLists,
        })
      }).catch(function(res){
        console.log(res);
      })
    }
  },
  handleSuccess(e) {
    var filed = e.currentTarget.dataset.filed;
    const { files } = e.detail;
    this.setData({
      
    });
  },
  handleRemove(e) {
    var filed = e.currentTarget.dataset.filed;
    const { index } = e.detail;
    const { fileList } = this.data;
    fileList[filed].splice(index, 1);
    this.setData({
      fileList,
    });
  },

  changeAbnormal(e) {
    var filed = e.currentTarget.dataset.filed;
    var data = JSON.parse(this.data.formData[filed]);
    data.exception = e.detail.value;
    this.setData({
      ['formData.'+filed]: JSON.stringify(data)
    })
  },
  changeState(e) {
    var filed = e.currentTarget.dataset.filed;
    var data = JSON.parse(this.data.formData[filed]);
    data.dispose = e.detail.value;
    this.setData({
      ['formData.'+filed]: JSON.stringify(data)
    })
  }
});

