// ==UserScript==
// @name        T-明略-会议室预定
// @namespace   Violentmonkey Scripts
// @match       https://www.jianjian.work/hysPC/*
// @match       https://www.jianjian.work/index.html
// @match       https://www.jianjian.work/pcgroup/purchase.html*
// @grant       none
// @version     1.0
// @author      -
// @require     https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js
// @require     https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js
// @description 2021/8/19 上午10:55:46
// ==/UserScript==
function isInter(arr1, arr2) {
    return !(arr2[1] < arr1[0] || arr2[0] > arr1[1]);
}

function isAnyInter(arr1, arr2) {
    for (let arr1Element of arr1) {
        if (isInter(arr1Element, arr2)) {
            return true
        }
    }
    return false
}

(function () {

    let date = '2021-12-15'
    let startTime = 10; // 几点
    let hours = 1; // 持续几个小时
    startTime = startTime * 12
    let endTime = startTime + hours * 12
    let capacityMin = 4; // 会议室容纳人数最少多少
    let capacityMax = 20; // 会议室容纳人数最多多少

    console.log("欢迎进入北京明略会议室预定");

    if (location.href.startsWith("https://www.jianjian.work/index.html")) {
        document.querySelector(".login_btn").click();
    }
    if (location.href.startsWith("https://www.jianjian.work/pcgroup/purchase.html")) {
        location.href = "https://www.jianjian.work/hysPC/index.html#/index/home";
    }
    if (location.href.startsWith("https://www.jianjian.work/hysPC/")) {
        // 获取会议室
        let roomUrl = "https://www.jianjian.work/room/base?_=1629341394435";
        axios.get(roomUrl)
            .then(res => {
                res = res.data;
                if (!res.success) {
                    console.log("获取会议室列表异常");
                    return
                }
                let states = res.model[88085];
                // console.table(states);

                _.forEach(states, room => {
                    let roomId = room.id;
                    // console.log(`正在查询${roomName}`);
                    let url = `https://www.jianjian.work/building/${roomId}/rooms?date=${date}&startDate=&endDate=&_=1629339983635`;
                    axios.get(url)
                        .then(res => {
                            res = res.data;
                            if (!res.success || res.model.rooms.length === 0) {
                                return
                            }
                            let room2 = res.model.rooms;
                            for (const room2Key of room2) {
                                if (room2Key.capacity < capacityMin || room2Key.capacity > capacityMax || room2Key.closeInfo != null) {
                                    continue
                                }
                                let events = room2Key.events;
                                if (room2Key.name.startsWith("A")) {
                                    continue
                                }
                                let arr_all = []
                                for (const eventsKey of events) {
                                    arr_all.push([parseInt(eventsKey.durationNew[0]), parseInt(eventsKey.durationNew[1])])
                                }
                                if (!isAnyInter(arr_all, [startTime + 1, endTime - 1])) {
                                    console.log(`!!!已找到：${room2Key.name}；容纳：${room2Key.capacity}人；标签：${room2Key.tools}`);
                                }
                            }
                        })
                })
            })
    }


})();
