// pages/search/index.js
import utils from "../../utils/util";
import { queryAllspace } from "../../requests/api";
import Toast from "../../vant-weapp/dist/toast/toast";
import $ from "./../../utils/Tool";
import router from "./../../utils/router";
const app = getApp();
Page({
    data: {
        alllistContent: [],
        projectId: "",
        tenantId: "",
        userId: null,
        currtPage: 0,
        allListnum: 0,
        seachValue: "",
        imgShow: false,
        noSpace: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let projectId = $.store.get("projectId") || $.storage.get("projectId");
        let tenantId = $.store.get("tenantId") || $.storage.get("tenantId");
        let userId = $.store.get("userId") || $.storage.get("userId");

        this.setData({
            currtPage: 0,
            projectId: projectId,
            tenantId: tenantId,
            userId: userId,
        });
        this.getallList();
    },
    onShow() {
        if (this.data.seachValue) { // 搜索条件下
            this.spaceSearch();
        }
    },
    getallList(value) {
        this.setData({ currtPage: this.data.currtPage + 1 }, async () => {
            let data = {
                criteria: {
                    userId: this.data.userId,
                    projectId: this.data.projectId,
                    tenantId: this.data.tenantId,
                },
                page: this.data.currtPage,
                size: 15,
                orders: [
                    {
                        column: "localName",
                        asc: "true",
                    },
                ],
            };
            if (this.data.seachValue) {
                data.criteria.localName = {
                    $like: `%${this.data.seachValue}%`,
                };
            }
            wx.showLoading({
                title: "加载中",
                mask: true,
            });
            // $.loading();
            let res = await queryAllspace(data);
            // $.hideLoading();
            wx.hideLoading();
            this.setData({ allListnum: res.count });
            if (res.count && res.content) {
                res.content.map((item) => {
                    item.picInit = utils.picInit(item.roomFuncType);
                    if (typeof item.isPassengerPass === "undefined") {
                        item.isPassengerPassShow = false;
                    } else {
                        item.isPassengerPassShow = true;
                        item.isPassengerPass = item.isPassengerPass
                            ? "有人"
                            : "无人";
                    }
                });
                this.setData({
                    alllistContent: this.data.alllistContent.concat(
                        res.content
                    ),
                });
            } else {
                Toast("暂无更多");
            }
        });
    },
    // 搜索
    spaceSearch(e) {
        let val = e ? e.detail : this.data.seachValue;
        this.setData(
            { seachValue: val, currtPage: 0, alllistContent: [] },
            () => {
                this.getallList(val);
            }
        );
    },
    listClick(e) {
        let data = e.detail.cardDate;
        data.outLine && (data.outLine = "");
        router.push("adjust", data);
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.allListnum != this.data.alllistContent.length) {
            // this.setData({currtPage:this.data.currtPage+1})
            this.getallList();
        } else {
            Toast("暂无更多");
        }
    },
});
