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

Page({
    /**
     * 页面的初始数据
     */
    data: {
        productList: [], //供需列表
        //轮播图列表
        swiperBannerList: [],
        portal_middle_1: '',
        /* 宫格列表 */
        gridList: [{
            text: '参展入口',
            icon: '/static/home-grid/icon-05.png'
        }, {
            text: '即时通讯',
            icon: '/static/home-grid/icon-01.png'
        }, {
            text: '直播',
            icon: '/static/home-grid/icon-02.png'
        }, {
            text: '通告',
            icon: '/static/home-grid/icon-03.png'
        }, {
            text: '供需广场',
            icon: '/static/home-grid/icon-04.png'
        }],
        /* 圆角按钮tabs列表 */
        roundedList: ["兴趣爱好", "关注板块", "最新发布"],
        partnerList: [], //合作商家列表
        codeMsg: {}, //二维码信息
        MyModelShow: false, //二维码二次确认
        MyModelText: "", //提示框文本
    },
    /* tabs切换 */
    /* tabsSelectedIitem(text) {
        console.log(text.detail)
    }, */
    /* 建立联系 */
    toLinkUp(e) {
        const {
            tsupplyanddemandid,
            tenterprise_userid
        } = e.currentTarget.dataset.item;
        if (tenterprise_userid == wx.getStorageSync('userData').userid) return;
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": "OpenImDialog",
            "content": {
                "tsupplyanddemandid": tsupplyanddemandid
            }
        }).then(res => {
            console.log("创建聊天", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            wx.navigateTo({
                url: '/pages/chatRoom/dialogbox?id=' + res.data[0].timdialogid,
            })
        })
    },
    /* 宫格区跳转 */
    gridJumpPage(e) {
        const {
            name
        } = e.target.dataset;
        if (name == '供需广场') {
            wx.switchTab({
                url: '/pages/tabbar-pages/supplyAndDemand/index',
            })
        } else if (name == '直播') {
            wx.navigateTo({
                url: '/pages/liveStreaming/index',
            })
        } else if (name == '通告') {
            wx.navigateTo({
                url: '/pages/annunciate/index',
            })
        } else if (name == '即时通讯') {
            wx.navigateTo({
                url: '/pages/chatRoom/index',
            })
        } else if (name == '参展入口') {
            const data = this.data.portal_middle_1;
            if (data.length == 0) return wx.showToast({
                title: '暂无展会活动',
                icon: "none"
            })
            wx.navigateTo({
                url: '/pages/scanQRCodes/register?id=' + data[0].fdataid,
            })
        } else {
            wx.showToast({
                title: '功能开发中',
                icon: "none"
            })
        }
    },
    /* 获得展示区标题 */
    getExhibitionTitle(title) {
        const {
            detail
        } = title;
        if (detail == '最新供需') {
            wx.switchTab({
                url: '/pages/tabbar-pages/supplyAndDemand/index',
            })
        } else if (detail == '合作商家') {
            wx.navigateTo({
                url: '/pages/businessPartner/index',
            })
        } else if (detail == '通告') {
            wx.navigateTo({
                url: '/pages/annunciate/index',
            })
        }
    },
    /* 跳转详情 */
    toDetails(e) {
        const {
            index
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/annunciate/details?id=' + this.data.annunciateList[index].tnoticeid + '&type=0',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (query) {
        const that = this;
        /* 获取最新供需列表 */
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": "query_supplyanddemandList",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": 1,
                "pageSize": 8,
                "where": {
                    "fissupply": "0",
                    "fstatus": "待对接"
                }
            }
        }).then(res => {
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                if (query.q) {
                    //有登录态直接加入
                    analyze.setDataUrl(decodeURIComponent(query.q), true);
                    console.log('登录态')
                }
                return;
            }
            console.log("res.data", res.data)
            if (res.data.length >= 1) {
                const data = handleList.getYTD(res.data);
                this.setData({
                    productList: data
                });
            }
            // 获取到二维码原始链接内容
            if (query.q) {
                //有登录态直接加入
                this.ifQrCodeType(analyze.setDataUrl(decodeURIComponent(query.q)));
                console.log('有登录态')
            } else if (wx.getStorageSync('qrCodeMsg')) {
                //判断缓存中是否存在
                this.ifQrCodeType(wx.getStorageSync('qrCodeMsg'));
                //返回数据后清空缓存
                setTimeout(() => {
                    wx.removeStorageSync('qrCodeMsg');
                }, 500)
            };
        });
        /* 获取合作商家 */
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.tagents.tagents",
            "method": "query_cooperation",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": 1,
                "pageSize": 9,
                "where": {
                    "condition": "",
                    "ftype": "",
                    "fstatus": "合作"
                }
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            let data = handleList.imageType(res.data, 'brandlogo')
            const partnerList = handleList.twoDimensionalArr(data, 3);
            this.setData({
                partnerList
            })
        })
        /* 获取通告 */
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.notice.notice",
            "method": "query_noticeList",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": 1,
                "pageSize": 3,
                "where": {
                    "condition": "",
                    "ftype": ""
                }
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            let list = handleList.annunciateCheckdate(res.data),
                data = handleList.imageType(list, 'cover');
            this.setData({
                annunciateList: data
            })
        })
        /* 获取轮播图 */
        const bannerList = wx.getStorageSync('bannerDataList').filter(value => value.flocation == 'home_head'),
            portal_middle_1 = wx.getStorageSync('bannerDataList').filter(value => value.flocation == 'portal_middle_1');
        this.setData({
            swiperBannerList: bannerList[0].banner,
            portal_middle_1: portal_middle_1[0].banner
        });
        //获取信息数量
        this.selectComponent("#gxshuju").unReadMessageCount();
        setTimeout(() => {
            that.getTabBar().setData({
                'tabbarList[3].fcount': getApp().globalData.msgFcount
            })
        }, 500)
        if (wx.getStorageSync('liveUrl')) {
            wx.navigateTo({
                url: '/pages/webView/index?url=' + wx.getStorageSync('liveUrl'),
            })
            wx.removeStorage({
                key: 'liveUrl',
            })
        }
        //建立链接
        getApp().initSocket();
    },
    /* 判断二维码类型 */
    ifQrCodeType(data) {
        let MyModelText = '';
        let tagentsid = wx.getStorageSync('account_list')[0].tagentsid;
        console.log(tagentsid, data.id)
        if (data.id == tagentsid) {
            wx.showToast({
                title: "您已经加入该团队",
                icon: "none"
            })
            wx.removeStorageSync('qrCodeMsg');
            return;
        }
        if (data.type == 'partner') {
            //申请合作
            MyModelText = '是否向“' + data.name + '”发送合作申请';
        } else if (data.type == 'shop') {
            //申请加入团队
            MyModelText = '申请加入“' + data.name + '”团队';
        };
        this.setData({
            MyModelText,
            codeMsg: data,
            MyModelShow: true
        })
        wx.removeStorageSync('qrCodeMsg');
    },
    /* 首页模态框回调 */
    MyShowModelCallBack({
        detail
    }) {
        if (detail == 'true') {
            if (this.data.codeMsg.type == 'partner') {
                //申请合作
                _Http.basic({
                    "accesstoken": wx.getStorageSync('userData').token,
                    "classname": "customer.tagents.tagents",
                    "method": "apply_cooperation",
                    "content": {
                        "tcooperationagentsid": this.data.codeMsg.id
                    }
                }).then(res => {
                    if (res.msg != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    });
                    wx.showToast({
                        title: "合作申请成功",
                    });
                });
            } else if (this.data.codeMsg.type == 'shop') {
                //申请加入团队
                _Http.basic({
                    "classname": "publicmethod.users.Users",
                    "method": "entryTeamApply",
                    "content": {
                        "userid": wx.getStorageSync('userData').userid,
                        "tagentsid": this.data.codeMsg.id
                    }
                }).then(res => {
                    if (res.data != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    });
                    wx.showToast({
                        title: "申请成功",
                        icon: "none"
                    });
                })
            }
        } else {
            this.setData({
                MyModelShow: false,
                codeMsg: ''
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getTabBar().init();
        this.getTabBar().setData({
            'tabbarList[3].fcount': getApp().globalData.msgFcount
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})