let util = require("../../../../../utils/util")
const apiArr = require('../../../../../api/water_filter')
const { postUrl3 } = require("../../../../../utils/util")

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

    /**
     * 页面的初始数据
     */
    data: {
        progress_List: [{
            "name": "HXTH滤芯",
            "progress": 97.41
        },
        {
            "name": "RO膜滤芯",
            "progress": 87.41
        },
        {
            "name": "PP+CB滤芯",
            "progress": 40.41
        },
        {
            "name": "PP棉滤芯",
            "progress": 90.41
        }
        ],
        device_id: "",
        Info: {},
        show: false,
        show2: false,
        show3: false,
        remarks1: "",
        remainingDays: "",
        surplusFlow: "",
    },

    getInfo() {
        let that = this
        util.postUrl(apiArr.deviceDesc, {
            device_id: Number(that.data.device_id)
        }, res => {
            if (res.device_part) {
                res.device_part.forEach(item => {
                    item.shengyuTime = that.getDaysDifference(item.expiration_time)
                    item.parent = Math.round((Number(item.shengyuTime) / Number(item.availableDays) * 100))
                })

                const first = res.device_part.findIndex(item =>
                    item.parts_name.indexOf("PP棉滤芯") !== -1
                );
                if (first !== -1) {
                    const [removed] = res.device_part.splice(first, 1); // 移除该对象
                    res.device_part.unshift(removed); // 插入到首位
                }

                const Second = res.device_part.findIndex(item =>
                    item.parts_name.indexOf("PP+CB") !== -1
                );
                if (Second !== -1) {
                    const [removed] = res.device_part.splice(Second, 1); // 移除该对象
                    res.device_part.splice(1, 0, removed);   // 插入到第二位
                }

                const Third = res.device_part.findIndex(item =>
                    item.parts_name.indexOf("RO") !== -1
                );
                if (Third !== -1) {
                    const [removed] = res.device_part.splice(Third, 1); // 移除该对象
                    res.device_part.splice(2, 0, removed);   // 插入到第二位
                }


                const Fourth = res.device_part.findIndex(item =>
                    item.parts_name.indexOf("后置活性炭") !== -1
                );
                if (Fourth !== -1) {
                    const [removed] = res.device_part.splice(Fourth, 1); // 移除该对象
                    res.device_part.splice(3, 0, removed);   // 插入到第二位
                }
            }
            that.setData({
                Info: res
            })
        })
    },

    getDaysDifference(targetDate) {
        // 获取当前时间
        const now = new Date();
        // 将传递的时间转换为日期对象
        const target = new Date(targetDate);
        // 计算时间差（以毫秒为单位）
        const timeDiff = target.getTime() - now.getTime();
        // 将毫秒转换为天数
        const daysDiff = Math.ceil(timeDiff / (1000 * 60 * 60 * 24));
        return daysDiff;
    },


    //开机
    Open() {
        let that = this
        util.postUrl2(apiArr.openOff, {
            deviceId: that.data.Info.device_id,
            onOff: 1
        }, res => {
            if (res.data.params.sevice_status.value == '10') {
                wx.showToast({
                    title: '开机成功！',
                    icon: "none"
                })
                that.getInfo()
            }
        })
    },
    //关机
    Close() {
        let that = this
        util.postUrl2(apiArr.openOff, {
            deviceId: that.data.Info.device_id,
            onOff: 2
        }, res => {
            if (res.data.params.sevice_status.value == '00') {
                wx.showToast({
                    title: '关机成功！',
                    icon: "none"
                })
                that.getInfo()
            }
        })
    },

    // 拆机
    uninstall() {
        let that = this
        wx.showModal({
            title: '提示',
            content: '请确保设备通电开机状态，该操作将取消设备激活状态',
            complete: (res) => {
                if (res.cancel) {

                }

                if (res.confirm) {
                    that.setData({
                        show: true
                    })
                }
            }
        })
    },
    createUninstall() {
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        let that = this
        util.postUrl(apiArr.UninstallOrder, {
            device_id: that.data.Info.device_id,
            remark: that.data.remarks1
        }, res => {
            console.log(res);
            wx.hideLoading()
            wx.showToast({
                title: '拆机工单创建完成',
                icon: "none"
            })

            setTimeout(()=>{
                wx.navigateTo({
                  url: '/packages/partner/pages/removeOrder/removeOrder',
                })
            },1500)
        })
    },
    // 激活
    active() {
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        let that = this
        util.postUrl2(apiArr.deviceActive, {
            device_id: that.data.Info.device_id
        }, res => {
            if (res.statusCode == 200) {
                wx.showToast({
                    title: '激活成功',
                    icon: "none"
                })
                that.getInfo()

            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: "none"
                })
            }
            wx.hideLoading()
        })
    },
    //时长充值
    recharge1() {
        let that = this
        that.setData({
            show2: true
        })
    },
    deviceDeactivation() {
        let that = this
        if(!that.isPureNumber(that.data.remainingDays)){
            return wx.showToast({
              title: '请输入正确到期日期',
              icon:"none"
            })
        }
        wx.showModal({
            title: '提示',
            content: '确定要充值时长吗？请在设备通电开机的状态下进行操作',
            complete: (res) => {
                if (res.cancel) {

                }

                if (res.confirm) {
                    wx.showLoading({
                        title: '加载中...',
                        mask: true
                    })
                    util.postUrl2(apiArr.deviceDeactivation, {
                        device_id: that.data.Info.device_id,
                        remainingDays: that.data.remainingDays
                    }, res => {
                        wx.hideLoading()
                        if (res.statusCode == 200) {
                            wx.showToast({
                                title: '充值成功',
                                icon: "none"
                            })
                            that.getInfo()
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: "none"
                            })
                        }
                        that.setData({
                            show2: false,
                            remainingDays: ""
                        })
                    })
                }
            }
        })
    },
    changeRemainingDay(e) {
        console.log(e);
        let that = this
        that.setData({
            remainingDays: e.detail.value
        })
    },
    //流量充值
    recharge2() {
        let that = this
        that.setData({
            show3: true
        })
    },
    changeSurplusFlow(e) {
        let that = this
        that.setData({
            surplusFlow: e.detail.value
        })
    },
    deviceDataForce() {
        let that = this

        if(!that.isPureNumber(that.data.surplusFlow)){
            return wx.showToast({
              title: '请输入正确数额',
              icon:"none"
            })
        }
        wx.showModal({
            title: '提示',
            content: '确定要充值流量吗？请在设备通电开机的状态下进行操作',
            complete: (res) => {
                if (res.cancel) {

                }

                if (res.confirm) {
                    util.postUrl2(apiArr.deviceDataForce, {
                        device_id: that.data.Info.device_id,
                        surplusFlow: that.data.surplusFlow
                    }, res => {
                        wx.hideLoading()
                        if (res.statusCode == 200) {
                            wx.showToast({
                                title: '充值成功',
                                icon: "none"
                            })
                            that.getInfo()
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: "none"
                            })
                        }
                        that.setData({
                            show3: false,
                            surplusFlow: ""
                        })
                    })
                }
            }
        })
    },

    isPureNumber(value) {
        return /^\d+(\.\d+)?$/.test(value);
    },

    onClose() {
        this.setData({
            show: false,
            show2: false,
            show3: false
        })
    },

    remarksIpt1(e) {
        console.log(e);
        let that = this
        that.setData({
            remarks1: e.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        console.log(options);
        that.setData({
            device_id: options.device_id
        })
        that.getInfo()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})