import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel();
const handleList = require("../../utils/processingData")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNumber: 1, //当前页码
        pageTotal: 1, //全部页码
        tactivityid: 0, //展会id
        //轮播图列表
        swiperBannerList: [],
        liveList: [], //云展会直播大厅liveList
        copyLiveList: [], //备份
        showLiveList: [], //显示直播列表
        liveClassifyList: [], //直播分类
        liveSelectClassify: '全部', //直播分类选择showLiveList

        agentList: [], //热门展商
        showAgentList: [], //显示热门展商
        agentClassifyList: [], //热门展商分类
        agentSelectClassify: '全部', //展商分类选择

        prodList: [], //热门展品
        fagentcount: null, //展示商
        flivecount: null, //正在直播
        ftotalcustcount: null, //观众总数
        showDownIndex: -1, //显示下拉下标
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            tactivityid: options.id
        })
        let data = {
            "classname": "publicmethod.live.live",
            "method": "agentList",
            "content": {
                "tactivityid": options.id,
                "siteid": "BWJ"
            }
        }
        /* 云展会直播大厅 */
        this.getLiveChannelData()
        /* 热门展商 */
        _Http.basic(data).then(res => {
            console.log("热门展商", res)
            if (res.msg != "成功") {
                wx.showToast({
                    title: '热门展商加载失败，请重新进入页面',
                    icon: "error",
                    duration: 5000
                })
            } else {
                data = JSON.parse(JSON.stringify(res.data));
                const classifyList = this.setClassifyList(data);
                const list = handleList.imageType(data, "brandcover");
                const List = handleList.twoDimensionalArr(list, 4, 10);
                this.setData({
                    agentList: res.data,
                    showAgentList: List,
                    agentClassifyList: classifyList
                })
            }
        });
        /* 热门展品 */
        data.method = "prodList";
        _Http.basic(data).then(res => {
            console.log("热门展品", res)
            if (res.msg != "成功") {
                wx.showToast({
                    title: '热门展品加载失败，请重新进入页面',
                    icon: "error",
                    duration: 5000
                })
            } else {
                const prodList = handleList.twoDimensionalArr(res.data, 6, 10);
                this.setData({
                    prodList,
                })
            }
        });
        /* 获取轮播图 */
        const bannerList = wx.getStorageSync('bannerDataList').filter(value => value.flocation == 'activity_head');
        this.setData({
            swiperBannerList: bannerList[0].banner
        });
    },
    /* 云展会直播大厅 */
    getLiveChannelData() {
        if (this.data.pageTotal < this.data.pageNumber) return;
        _Http.basic({
            "classname": "publicmethod.live.live",
            "method": "getLiveChannelData",
            "content": {
                "tactivityid": this.data.tactivityid,
                "siteid": "BWJ",
                "pageSize": 999,
                "pageNumber": this.data.pageNumber
            }
        }).then(res => {
            console.log("云展会直播大厅", res)
            if (res.msg != "成功") {
                wx.showToast({
                    title: '直播大厅加载失败，请重新进入页面',
                    icon: "none",
                    duration: 5000
                })
            } else {
                let data = JSON.parse(JSON.stringify(res.data.tlivelist));
                const ClassifyList = this.setClassifyList(data);
                /* const liveList = handleList.twoDimensionalArr(data, 4, 10);
                showLiveList: liveList, */
                let liveList = [];
                if (res.data.pageNumber == 1) {
                    liveList = res.data.tlivelist;
                } else {
                    let l = this.data.liveList;
                    liveList = l.concat(res.data.tlivelist)
                }
                wx.stopPullDownRefresh()
                this.setData({
                    liveList,
                    copyLiveList: liveList,
                    liveClassifyList: ClassifyList,
                    fagentcount: res.data.fagentcount,
                    flivecount: res.data.flivecount,
                    fcustcount: res.data.fcustcount,
                    ftotalcustcount: res.data.ftotalcustcount,
                    pageNumber: res.data.pageNumber + 1,
                    pageTotal: res.data.pageTotal
                })
            }
        });
    },
    scrolltolower() {
        this.getLiveChannelData();
    },
    /* 分类 */
    setClassifyList(data) {
        let List = [];
        for (let i = 0; i < data.length; i++) {
            const cur = data[i].flocationclass
            if (cur != null && cur != undefined && cur != '' && cur != [] && cur != {}) List.push(cur)
        };
        let arr = List.reduce((pre, cur) => {
            if (!pre.includes(cur)) {
                return pre.concat(cur)
            } else {
                return pre
            }
        }, [])
        arr.unshift('全部');
        return arr
    },
    fiveSearch(e) {
        const value = e.detail.value.trim();
        if (value) {
            let list = [],
                copyLiveList = this.data.copyLiveList;
            for (let index = 0; index < copyLiveList.length; index++) {
                if (copyLiveList[index].fbrand.includes(value) || copyLiveList[index].faddress.includes(value)) {
                    list.push(copyLiveList[index])
                }
            }
            this.setData({
                liveList: list
            })
        } else {
            this.setData({
                liveList: this.data.copyLiveList
            })
        }
    },
    /* 显示下拉 */
    showPullDown(e) {
        const {
            index
        } = e.target.dataset;
        if (this.data.showDownIndex == index) return this.setData({
            showDownIndex: -1
        });
        this.setData({
            showDownIndex: index
        })
    },
    /* 关闭 */
    closeTheDropDown() {
        this.setData({
            showDownIndex: -1
        })
    },
    /* 选择分类 */
    modeSelect(e) {
        const {
            name,
            index
        } = e.target.dataset;
        if (this.data.showDownIndex == 1) {
            let list = JSON.parse(JSON.stringify(this.data.liveList));
            if (name == '全部') {
                var List = list;
            } else {
                var List = list.filter(value => value.flocationclass == name);
            };
            var liveList = handleList.twoDimensionalArr(List, 4, 10);
            //直播大厅
            this.setData({
                showDownIndex: -1,
                liveSelectClassify: name,
                showLiveList: liveList
            });
        } else {
            //热门展商
            let list = JSON.parse(JSON.stringify(this.data.agentList));
            if (name == '全部') {
                var List = list;
            } else {
                var List = list.filter(value => value.flocationclass == name);
            };
            var agentList = handleList.twoDimensionalArr(List, 4, 10);
            this.setData({
                showDownIndex: -1,
                agentSelectClassify: name,
                showAgentList: agentList
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

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

    },

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

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            pageNumber:1,
            pageTotal:1
        })
        this.getLiveChannelData()
    },

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

    },

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

    }
})