// miniprogram/pages/applog/applog.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultDay: [
      new Date().getTime(),
      new Date().getTime() + 24 * 60 * 60 * 1000,
    ],
    projectoption: [],
    devtypeList: [],
    personoption: [],
    installtimeFmt: '',
    show: false,
    devcode: '',
    timeShow:true,
    project: '',
    devtype: '',
    installPerson: '',
    minDate: new Date(2018, 0, 1).getTime(),
    // maxDate: new Date(2018, 0, 31).getTime(),
    showSelectProject: false,
  },
  onDisplay() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  //格式化时间
  formatDate(date) {
    date = new Date(date);
    // return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    var seperator1 = "-";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate =
      date.getFullYear() +
      seperator1 +
      month +
      seperator1 +
      strDate;
    return currentdate;

  },
  //查询时间区域设置
  onConfirm(event) {

    // this.setData({
    //   show: false,
    //   installtimeFmt: this.formatDate(event.detail),
    // });

    const [start, end] = event.detail;
    this.setData({
      show: false,
      installtimeFmt: `${this.formatDate(start)}~${this.formatDate(end)}`,
    });
  },
  // 点击清空按钮
  clean: function() {
    this.setData({
        devcode:'',
        devtype:'',
        installPerson:'',
        installtimeFmt:'',
        project: this.data.showSelectProject === false ? this.data.project : '',
      })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
//    await this.setData({
//         timeShow:true,
//     })
    
    const {devcode,devtype,installPerson,installtimeFmt,project} = JSON.parse(options.params)
    this.setData({
        devcode:devcode || '',
        devtype:devtype || '',
        installPerson:installPerson || '',
        installtimeFmt:installtimeFmt || '',
        project:project || '',
      })

    var _this = this

    if(app.globalData.role === 'repair' && app.globalData.isConfigProject === '0') {
        _this.setData({
            'showSelectProject': true,
        })
    } else {
        _this.setData({
            'showSelectProject': false,
            'project': app.globalData.selectProjectName
        })
    }
    /**
     * 获取所属项目下拉列表
     */
    wx.request({
      method: "POST",
      url: app.globalData.httpsUrl + "project/getProject",
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success(res) {
        if (res.data.code == 200) {
          var projectList = res.data.data
          var projectArr = [];
          for (var i = 0; i < projectList.length; i++) {
            var project = {
              text: projectList[i].projectName,
              value: projectList[i].projectName
            };
            projectArr.push(project);
          }
          _this.setData({
            projectoption: projectArr
          })
        }
      }
    })
    /**
     * 获取设备类型下拉列表
     */
    wx.request({
      method: "POST",
      url: app.globalData.httpsUrl + "deviceType/deviceType",
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success(res) {
        if (res.data.code == 200) {
          var TypeList = res.data.data
          var typeArr = [];
          for (var i = 0; i < TypeList.length; i++) {
            var type = {
              text: TypeList[i].productName,
              value: TypeList[i].productName
            };
            typeArr.push(type);
          }
          _this.setData({
            devtypeList: typeArr
          })
        }
      }
    })


    //获取人员下拉列表
    wx.request({
      method: "POST",
      data:{
        tips:'repair'
      },
      url: app.globalData.httpsUrl + "deviceType/getUser",
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success(res) {
        if (res.data.code == 200) {
          var users = res.data.data
          var personArr = [];
          for (var i = 0; i < users.length; i++) {
            var person = {
              text: users[i].name,
              value: users[i].name
            };
            personArr.push(person);
          }
          _this.setData({
            personoption: personArr
          })
        }
      }
    })

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  scan() {

    var _this = this;
    wx.scanCode({
      success(res) {
        var devcode = res.result
        _this.setData({
          devcode: devcode,
        });
      }
    })
  },

  confirm(event) {
    var that = this;
    var formValue = event.detail;
    var formName = event.target.dataset.id;
    that.setData({
      [formName]: formValue
    })
  },
  //设置项目
  selectprojectValue(event) {
    var that = this;
    that.setData({
      project: event.detail
    })
    console.log('this.data', this.data.project);
  },

  //设置设备类型
  selectDevType(event) {
    var that = this;
    that.setData({
      devtype: event.detail
    })
  },

  //设置人员
  selectpersonValue(event) {
    var that = this;
    that.setData({
      installPerson: event.detail
    })
  },
  //查询
  query: function (event) {
    var data = {
      devcode: this.data.devcode,
      installtimeFmt: this.data.installtimeFmt,
      project: this.data.project,
      devtype: this.data.devtype,
      installPerson: this.data.installPerson
    }
    wx.reLaunch({
      url: '../applog/applog?params=' + JSON.stringify(data)
    })
  },
})