let util = require('../../../../../utils/util')
const apiArr = require('../../../../../api/water_filter')

// packages/WaterPurifier/pages/device/myDevice/myDevice.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page_num: 1,
        page_size: 10,
        flag: false,
        deviceList: []
    },
    //获取用户设备
    getDeviceList() {
        wx.showLoading({
            title: '加载中..',
        })
        let that = this
        util.postUrl(apiArr.getUserList, {
            page_num: that.data.page_num,
            page_size: that.data.page_size
        }, res => {
            wx.hideLoading()

            if (res.rows) {

                res.rows.forEach(item => {
                    item.install_time = item.install_time.slice(0, 10)
                    item.difDay = that.getDaysBetweenDates(item.install_time)
                    item.product_icon = util.img_url + item.product_icon
                })
            }
            let flag
            if (res.rows.length == that.data.page_size) {
                flag = true
            } else {
                flag = false
            }
            that.setData({
                flag,
                deviceList: that.data.deviceList.concat(res.rows || []),
                page_num: that.data.page_num + 1
            })
        })
    },

    getDaysBetweenDates(inputDate) {
        const currentDate = new Date();
        // 将传入的日期字符串转换为日期对象
        const targetDate = new Date(inputDate);
        // 计算两个日期之间的时间差（以毫秒为单位）
        const timeDifference = currentDate - targetDate;
        // 将时间差转换为天数
        const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
        // 返回天数
        return daysDifference;
    },

    changeDef(e) {
        let that = this
        wx.showLoading({
            title: '切换中...',
            mask: true,
        })
        util.postUrl(apiArr.changeDef, {
            device_id: e.currentTarget.dataset.id,
        }, res => {
            wx.setStorageSync('device_id', e.currentTarget.dataset.id)
            that.setData({
                page_num: 1,
                flag: false,
                deviceList: []
            })
            wx.hideLoading()
            wx.showToast({
                title: '修改默认成功',
            })
            that.getDeviceList()
            wx.navigateBack({
                delta: 1
            })



        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let that = this
        that.getDeviceList()
    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let that = this
        if (that.data.flag) {
            that.getDeviceList()
        }
    },

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

    }
})