import {
    ApiModel
} from "../../utils/api"
const _Http = new ApiModel();
import {
    TestVerify
} from "../../utils/verify";
const _Verify = new TestVerify();
const processingData = require("../../utils/processingData");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        tabsActive: 0, //tabs选中项
        annunciateList: [], //通告列表
        sadClassify: [], //供需分类
        sadList: [], //供需列表
        brandSelect: 0, //品牌关注榜选择
        brandList0: [],
        brandList1: [],
        recruitName: "", //招商用户名
        recruitPhone: "", //招商手机号
        region: [], //地址
        recruitExplain: "", //备注
        newProductIndex1: 0,
        newProductIndex2: 0,
        repetitionSubmit: false, //是否重复提交招商表单
        tiveList: [], //直播列表
        portal_middle_1: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //获取管理端发布通告
        _Http.basic({
            "classname": "publicmethod.homepage.homepage",
            "method": "getNewestNoticeList",
            "content": {}
        }).then(res => {
            console.log(res)
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.data,
                    icon: "none"
                });
            } else {
                processingData.imageType(res.data, 'cover');
                let list = [];
                if (res.data.length > 3) {
                    list = res.data.slice(0, 3)
                } else {
                    list = res.data;
                }
                this.setData({
                    annunciateList: list
                })
            }
        });
        //获取供需分类
        _Http.basic({
            "classname": "publicmethod.homepage.homepage",
            "method": "query_typeselectList",
            "content": {
                "siteid": "BWJ"
            }
        }).then(res => {
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.data,
                    icon: "none"
                });
            } else {
                res.data.unshift({
                    ftype: "全部"
                })
                // return console.log(res.data)
                this.setData({
                    sadClassify: res.data
                })
            }
        });
        //获取全部供需列表 
        this.query_supplyanddemandList('');
        //获取直播列表
        _Http.basic({
            "classname": "publicmethod.live.live",
            "method": "getHomeLive",
            "content": {
                "getdatafromdbanyway": true,
                "livestatus": "live"
            }
        }, false).then(res => {
            console.log("直播列表", res)
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.data,
                    icon: "none"
                });
            } else {
                this.setData({
                    tiveList: res.data
                })
            }
        })
        //获取轮播图
        setTimeout(() => {
            this.getBanner()
        }, 300)
        //新发布商品
        this.getHotProductsList();
        // 获取品牌关注榜
        this.getHotAgentsList('沙发布')
    },
    /* 品牌关注榜切换 */
    concernsChange(e) {
        const {
            index
        } = e.target.dataset;
        this.setData({
            brandSelect: index
        })
    },
    /* 去报名展会 */
    toApply() {
        const data = this.data.portal_middle_1;
        wx.navigateTo({
            url: '/pages/scanQRCodes/register?id=' + data[0].fdataid,
        })
    },
    /* 获取轮播图 */
    getBanner() {
        let imgList = wx.getStorageSync('bannerDataList');
        console.log("imgList", imgList)
        if (imgList != "") {
            const bannerList = imgList.filter(value => value.flocation == 'portal_head');
            const portal_middle_1 = imgList.filter(value => value.flocation == 'portal_middle_1');
            /* const portal_middle_2 = imgList.filter(value => value.flocation == 'portal_middle_2');
            const portal_middle_3 = imgList.filter(value => value.flocation == 'portal_middle_3');
            const portal_middle_4 = imgList.filter(value => value.flocation == 'portal_middle_4'); */
            console.log(portal_middle_1)
            this.setData({
                swiperBannerList: bannerList[0].banner,
                portal_middle_1: portal_middle_1[0].banner
            });
            /* portal_middle_2: portal_middle_2[0].banner,
                portal_middle_3: portal_middle_3[0].banner,
                portal_middle_4: portal_middle_4[0].banner, */
        } else {
            setTimeout(() => {
                this.getBanner()
            }, 500)
        }
    },
    //跳转通过详情页
    toAnnunciateDetails(e) {
        const {
            index
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/annunciate/details?id=' + this.data.annunciateList[index].tnoticeid + '&type=2',
        })
    },
    //表单提交
    submitForm() {
        if (this.data.repetitionSubmit) {
            wx.showModal({
                title: "提示",
                content: "已提交过信息，是否继续",
                success: (res) => {
                    if (!res.confirm) return;
                    this.insertBusiness()
                }
            })
        } else {
            this.insertBusiness();
        }
    },
    insertBusiness() {
        let add = {
            "province": "",
            "city": "",
            "county": "",
        }
        if (this.data.region.length > 0) {
            const a1 = this.data.region;
            add.province = a1[0]
            add.city = a1[1]
            add.county = a1[2]
        }
        _Http.basic({
            "classname": "publicmethod.homepage.homepage",
            "method": "insertBusiness",
            "content": {
                "province": add.province,
                "city": add.city,
                "county": add.county,
                "fname": this.data.recruitName,
                "fphone": this.data.recruitPhone,
                "fnote": this.data.recruitExplain
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            wx.showToast({
                title: "提交成功"
            });
            this.setData({
                repetitionSubmit: true,
                recruitExplain: '',
                recruitName: "",
                recruitPhone: "",
                region: []
            })
        })
    },
    //手机号码
    phoneNumber(e) {
        if (!_Verify.phoneNumber(e.detail.value)) return wx.showToast({
            title: '请输入11位手机号码',
            icon: "none"
        });
        this.setData({
            recruitPhone: e.detail.value
        })
    },
    //招商表单输入
    recruitInput(e) {
        const {
            name
        } = e.currentTarget.dataset;
        this.setData({
            [name]: _Verify.Eliminate(e.detail.value)
        })
    },
    //省市区选择器
    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value
        })
    },
    //新发布商品
    getHotProductsList() {
        let arr = ["窗帘", "沙发布"];
        for (let i = 0; i < arr.length; i++) {
            _Http.basic({
                "classname": "publicmethod.homepage.homepage",
                "method": "getNewProduct",
                "content": {
                    "getdatafromdbanyway": true,
                    "fprodclassname": arr[i],
                }
            }).then(res => {
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                if (i == 0) {
                    this.setData({
                        newProduct1: res.data
                    })
                } else {
                    this.setData({
                        newProduct2: res.data
                    })
                }
            })
        };
    },
    /* 获取品牌关注榜 */
    getHotAgentsList(ftype) {
        _Http.basic({
            "classname": "publicmethod.homepage.homepage",
            "method": "getHotAgentsList",
            "content": {
                "ftype": ftype
            }
        }, false).then(res => {
            console.log("获取品牌关注榜", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            if (ftype == '沙发布') {
                this.setData({
                    brandList0: res.data
                })
            } else if (ftype == '窗帘布') {
                this.setData({
                    brandList1: res.data
                })
            }
        })
        if (ftype == '沙发布') this.getHotAgentsList('窗帘布')
    },
    /* 跳转供需详情 */
    toDetails(e) {
        const {
            index
        } = e.currentTarget.dataset;
        const data = this.data.sadList[index];
        wx.navigateTo({
            url: './details?tagentsid=' + data.tagentsid + '&tsupplyanddemandid=' + data.tsupplyanddemandid,
        })
    },
    /* 搜索 */
    toSearch() {
        wx.navigateTo({
            url: './search',
        })
    },
    /* 供需分类切换 */
    tabsOnChange(e) {
        const {
            title,
            index
        } = e.detail;
        this.setData({
            tabsActive: index
        })
        console.log(title)
        if (title == '全部') {
            this.query_supplyanddemandList('')
        } else {
            this.query_supplyanddemandList(title);
        }
    },
    /* 拉取供需分类列表 */
    query_supplyanddemandList(type) {
        _Http.basic({
            "classname": "publicmethod.homepage.homepage",
            "method": "query_supplyanddemandList",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": 1,
                "pageSize": 20,
                "siteid": "BWJ",
                "where": {
                    "ftype": type
                }
            }
        }, false).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            this.setData({
                sadList: processingData.annunciateCheckdate(res.data)
            })
        })
    },
    /* 新品轮播图 */
    newProduct1(e) {
        this.setData({
            newProductIndex1: e.detail.current
        })
    },
    newProduct2(e) {
        this.setData({
            newProductIndex2: e.detail.current
        })
    },
    /* 跳转登录 */
    toLogin() {
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.usercenter.usermsg.usermsg",
            "method": "query_usermsg",
            "content": {}
        }).then(res=>{
            console.log("个人信息",res)
            if(res.msg=='失败'){
                wx.navigateTo({
                    url: '/pages/login/index',
                })
            }else{
                wx.switchTab({
                    url: '/pages/tabbar-pages/home/index',
                })
                // getApp().initSocket();
            }
        })
        /*  */
        // getApp().getUserProfile();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})