const app = getApp()
import {
    meetingRoomDict,
    meetingRoomListRq,
} from "../../../api/meeting/meetingRoom.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        IMG_NAME: app.IMG_NAME,
        roomDataList: [],
        queryParam: {
            meetingTypeDict: {
                list: [],
                value: null,
            },
            personDict: {
                list: [],
                value: null,
            },
            itemDict: {
                list: [],
                value: null,
                title: '设备'
            },
            shapeDict: {
                list: [],
                value: null,
            },
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 页面初始化 options为页面跳转所带来的参数
        if (options.name) {
            wx.setNavigationBarTitle({
                title: options.name
            })
        }

        // 初始化数据
        this.initData();
    },

    // 初始化数据
    initData() {
        let _this = this;
        // 1.会议室类型，2.人数，3.会议室设备，4.形式
        meetingRoomDict("1").then(res => {
            console.log('meetingRoomDict("1")', res);
            // 封装参数
            let list = res.roomContents.map(item => {
                return {
                    text: item.typeName,
                    value: item.typeValue
                }
            })
            list = [{
                text: "类型",
                value: null
            }, ...list]
            // 赋值参数
            let queryParam = _this.data.queryParam;
            queryParam.meetingTypeDict.list = list;
            _this.setData({
                queryParam
            })
            console.log('meetingRoomDict("1")=>', list);
        })
        meetingRoomDict("2").then(res => {
            console.log('meetingRoomDict("2")', res);
            // 封装参数
            let list = res.roomContents.map(item => {
                return {
                    text: item.capacityNum + '人',
                    value: item.capacityNum
                }
            })
            list = [{
                text: "人数",
                value: null
            }, ...list]
            // 赋值参数
            let queryParam = _this.data.queryParam;
            queryParam.personDict.list = list;
            _this.setData({
                queryParam
            })
            console.log('meetingRoomDict("2")=>', list);
        })
        meetingRoomDict("3").then(res => {
            console.log('meetingRoomDict("3")', res);
            // 封装参数
            let list = res.roomItem.map(item => {
                return {
                    text: item.name,
                    value: item.id
                }
            })
            // 赋值参数
            let queryParam = _this.data.queryParam;
            queryParam.itemDict.list = list;
            _this.setData({
                queryParam
            })
            console.log('meetingRoomDict("3")=>', list);
        })
        meetingRoomDict("4").then(res => {
            console.log('meetingRoomDict("4")', res);
            // 封装参数
            let list = res.roomContents.map(item => {
                return {
                    text: item.shape,
                    value: item.shape
                }
            })
            list = [{
                text: "形式",
                value: null
            }, ...list]
            // 赋值参数
            let queryParam = _this.data.queryParam;
            queryParam.shapeDict.list = list;
            _this.setData({
                queryParam
            })
            console.log('meetingRoomDict("4")=>', list);
        })

        // 数据-会议室列表
        this.meetingRoomList()
    },

    // 查询条件变动
    dictChange(e) {
        console.log('dictChange', e);
        let queryParam = this.data.queryParam;
        queryParam[e.currentTarget.dataset.type].value = e.detail;
        this.setData({
            queryParam
        })
        // 数据-会议室列表
        this.meetingRoomList()
    },

    // 设备-多选
    dictSwitchChange(e) {
        console.log('dictSwitchChange', e);
        let select = e.detail;
        let name = e.currentTarget.dataset.name;
        let id = e.currentTarget.dataset.id;
        let queryParam = this.data.queryParam;
        queryParam.itemDict.list = queryParam.itemDict.list.map(item => {
            if (item.value == id) {
                item.select = select;
            }
            return item
        })
        // 计算选择数量
        let selectCount = queryParam.itemDict.list.filter(item => item.select).length;
        console.log('selectCount', selectCount);
        if (selectCount > 0) {
            queryParam.itemDict.title = '设备+' + selectCount;
        } else {
            queryParam.itemDict.title = '设备';
        }
        this.setData({
            queryParam
        })
        // 数据-会议室列表
        this.meetingRoomList()
    },
    // 设备多选收起
    itemDictConfirm() {
        this.selectComponent('#itemSelect').toggle();
    },

    // 数据-会议室列表
    meetingRoomList() {
        let _this = this;
        let queryParam = _this.data.queryParam;
        let roomItemList = queryParam.itemDict.list.filter(item => item.select).map(item => {
            return {
                "id": item.value,
                "name": item.text
            }
        })
        let param = {
            "type": queryParam.meetingTypeDict.value,
            "capacityNum": queryParam.personDict.value,
            "roomItemList": roomItemList,
            "shape": queryParam.shapeDict.value,
        }
        // 会议室列表数据
        meetingRoomListRq(param).then(res => {
            console.log('meetingRoomListRq', res);
            _this.setData({
                roomDataList: res.rows
            })
        })
    },

    // 会议室
    jumpMeetingRoom(e) {
        console.log('jumpMeetingRoom', e);
        wx.navigateTo({
            url: "/pages/meeting/meetingRoom/meetingRoom?id=" + e.currentTarget.dataset.id,
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})