const app = getApp()

import Notify from '@vant/weapp/notify/notify';

import {
    selfFormatTimeYMD,
    selfFormatTimeHM
} from "../../../utils/util.js"

import {
    getEquipmentByUserIdRq,
    openDoorRq,
    getOpenDoorRecordRq
} from "../../../api/meeting/equipment.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        IMG_NAME: app.IMG_NAME,
        activeNames: [0],
        userDetail: {},
        equipmentDataList: [],
        openRecordList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        let userDetail = wx.getStorageSync('user')
        _this.setData({
            userDetail,
            ...options,
        })
        // 页面初始化 options为页面跳转所带来的参数
        wx.setNavigationBarTitle({
            title: '远程门禁'
        })
        // 获取设备数据
        _this.getEquipmentData();
        // 获取-开门记录数据
        _this.getOpenRecordData()
    },

    // 获取设备数据
    getEquipmentData() {
        let _this = this;
        getEquipmentByUserIdRq(_this.data.userDetail.id).then(res => {
            console.log('getEquipmentData', res);
            let equipmentDataList = res.data
            equipmentDataList = equipmentDataList.map(item => {
                let statusClass = "offline";
                // equipmentStatus
                if (item.equipmentStatus == '在线') {
                    statusClass = "online";
                } else if (item.equipmentStatus == '离线') {
                    statusClass = "offline";
                }
                item.statusClass = statusClass;
                return item
            })
            _this.setData({
                equipmentDataList
            })
        })
    },

    // 获取-开门记录数据
    getOpenRecordData() {
        let _this = this;
        getOpenDoorRecordRq({
            userId: _this.data.userDetail.id,
            pageNum: 1,
            pageSize: 5
        }).then(res => {
            console.log('getOpenRecordData', res);
            let openRecordList = res.data.records
            openRecordList = openRecordList.map(item => {
                item.createTimeYMD = selfFormatTimeYMD(item.createTime)
                item.createTimeHM = selfFormatTimeHM(item.createTime)
                return item;
            })
            _this.setData({
                openRecordList
            })
        })
    },

    // 开门
    openDoor(e) {
        console.log('openDoor', e);
        let _this = this;
        let userId = _this.data.userDetail.id;
        let deviceId = e.currentTarget.dataset.deviceid
        let roomId = e.currentTarget.dataset.roomid
        let status = e.currentTarget.dataset.status
        if (status != '在线') {
            Notify({
                type: 'danger',
                message: '设备不在线!'
            });
            return;
        }
        openDoorRq({
            roomId,
            deviceId,
            userId
        }).then(res => {
            console.log('openDoor', res);
            if (res.code == 0) {
                Notify({
                    type: 'success',
                    message: res.msg
                });
                // 获取-开门记录数据
                _this.getOpenRecordData()
            } else {
                Notify({
                    type: 'danger',
                    message: res.msg
                });
            }
        })
    },

    // 跳转-开门记录
    jumpOpenDoorRecord() {
        wx.navigateTo({
            url: "/pages/meeting/accessControl/accessControlRecord/accessControlRecord"
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})