// index.js
// 获取应用实例
const app = getApp()
const $request = require('../../utils/request.js');
const $util = require('../../utils/util.js');
Page({
    data: {
        currentIndex: 0,
        keyword: "",
        // tab
        tabData: [
            {
                text: "全部",
                value: "all"
            },
            {
                text: "我关注的",
                value: "my"
            },
            {
                text: "工商类",
                value: "工商类"
            },
            {
                text: "财税类",
                value: "财税类"
            },
            {
                text: "资质类",
                value: "资质类"
            },
        ],
        currentTab: "all",
        // 更多tab
        moreTabOption: [
            {
                text: "公司转让",
                value: "公司转让"
            },
            {
                text: "知识产权",
                value: "知识产权"
            },
            {
                text: "银行服务",
                value: "银行服务"
            },
            {
                text: "法律服务",
                value: "法律服务"
            },
            {
                text: "其他",
                value: "其他"
            },
        ],
        moreTab: "gengduo",
        moreTitle: "更多分类",
        // list数据加载
        // list块加载
        listLoading: true,
        // item行加载
        itemLoading: false,
        // 数据是否加载完成
        isFinished: false,
        listData: [],
        page: 1,
        size: 10,
        total: 0,
        // 是否加载数据，true加载，false不加载
        onRefresh: true,
        // 当前登录用户已设置的关注类型
        attentionTypeIds: "",
        // 用来判断关注显示按钮
        paramIsInterest: 0,
        customerId: null,
    },
    onLoad() {
    },
    onShow() {
        this.setData({
            currentIndex: 0
        })
        this.toSearch();
        const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null;
        if (businessCommunicationCustomer) {
            let attentionTypeIds = businessCommunicationCustomer.attentionTypeIds;
            if (attentionTypeIds) {
                attentionTypeIds = attentionTypeIds.split(',');
            }else{
                attentionTypeIds = [];
            }
            this.setData({
                attentionTypeIds,
                customerId: businessCommunicationCustomer.customerId
            })
        }
    },
    toggleMore() {
        this.selectComponent('#item').toggle();
    },
    // 搜索框输入同步值
    bindKeyInput: function (e) {
        this.setData({
            keyword: e.detail.value
        })
    },
    // 点击搜索
    toSearch(e) {
        this.setData({
            listData: [],
            onRefresh: true,
            isFinished: false,
            listLoading: true,
            page: 1,
        })
        this.getListData();
    },
    // tab切换
    tabChange(e) {
        let dataset = e.currentTarget.dataset;
        // 如果实在加载中点击无效
        if (this.data.listLoading) {
            return
        }
        // 如果当前tab是选中直接点击无效
        if (this.data.currentTab == dataset.value) {
            return
        }
        this.setData({
            currentTab: dataset.value,
            moreTitle: "更多分类",
            moreTab: "gengduo"
        })
        this.toSearch();
    },
    // 更多tab切换
    moreTabChange(e) {
        // 如果实在加载中点击无效
        if (this.data.listLoading) {
            return
        }
        // 如果当前tab是选中直接点击无效
        if (this.data.moreTab == e.detail) {
            return
        }
        let moreTitle = "";
        this.data.moreTabOption.forEach(el => {
            if (el.value == e.detail) {
                moreTitle = el.text;
            }
        })
        this.setData({
            currentTab: "more",
            moreTab: e.detail,
            moreTitle
        })
        this.toSearch();
    },
    // 进入详情
    goDetails(e) {
        wx.navigateTo({
            url: '/pages/index/components/listDetails/Details?businessCommunicationDemandId=' + e.detail,
        })
    },
    // 获取tab数据
    getAllType() {
        $request.get('/businessCommunicationType/getAllType.action').then(res => {
            if (res.status == 0) {
                let { tabData, moreTabOption } = this.data;
                let datas = res.data;
                datas.forEach(el => {
                    if (tabData.length < 5) {
                        tabData.push({
                            text: el.typeName,
                            value: el.typeName
                        })
                    } else {
                        moreTabOption.push({
                            text: el.typeName,
                            value: el.typeName
                        })
                    }
                })
                this.setData({
                    tabData,
                    moreTabOption
                })
                this.getListData();
            }
        }).catch(error => {
            console.log(error, 'error appletLogin')
        })
    },
    // 获取list数据
    getListData(isInterest) {
        let { page, size, keyword, currentTab, moreTab } = this.data;
        let type = currentTab;
        if (currentTab == 'more') {
            type = moreTab;
        }
        if (type == 'all') {
            type = "";
        }
        if (currentTab == 'my') {
            if (app.globalData.businessCommunicationCustomer) {
                type = app.globalData.businessCommunicationCustomer.attentionTypeIds;
                if(!type){
                    this.setData({
                        listLoading: false
                    })
                    return
                }
            }
        }
        let paramIsInterest = 0;
        if (isInterest) {
            paramIsInterest = isInterest;
        }
        this.setData({
            paramIsInterest
        })
        console.log(`本次接口：type`,type)
        $request.get('/businessCommunicationDemand/getDemandByKeywordOrType.action',
            { page, size, keyword, type, isInterest: paramIsInterest }
        ).then(res => {
            // console.log(res);

            let tempListData = this.data.listData;
            if (res.status == 0) {
                let datas = res.data;
                // 先push数据
                tempListData.push(...datas.demandList);
                tempListData.forEach(el => {
                    el.createdOn = $util.formatTime(new Date(el.createdOn), true);
                    el.createdOn = el.createdOn.replaceAll('/', '-');
                    if (typeof el.type == 'string') {
                        el.type = el.type.split(',');
                    }
                })
                // 设置总数
                this.setData({
                    listData: tempListData,
                    total: res.data.total,
                })
                // 如果数据大于了返回的总数
                if (tempListData.length >= this.data.total) {
                    // 停止累加数据
                    this.setData({
                        onRefresh: false,
                        itemLoading: false,
                        isFinished: true,
                    })
                } else {
                    this.setData({
                        onRefresh: true,
                        itemLoading: false,
                        isFinished: false,
                        page: page + 1
                    })
                }
            }
            this.setData({
                listLoading: false
            })
            if (this.data.attentionTypeIds && this.data.currentTab == 'my') {
                if (this.data.attentionTypeIds.length > 0 && this.data.total == 0) {
                    this.getListData(1);
                }
            }
        }).catch(error => {
            console.log(error, 'error appletLogin')
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.onRefresh) {
            this.setData({
                itemLoading: true
            })
            this.getListData();
        }
    },
    // 设置关注类型喜好
    setAttention() {
        if (!app.globalData.customerId) {
            app.goLogin();
            return
        }
        this.setData({
            currentTab: 'my',
            moreTitle: "更多分类",
            moreTab: "gengduo"
        })
        let customerid = app.globalData.customerId;
        wx.navigateTo({
            url: "/pages/myModule/components/myAttention/myAttention",
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('customerid', { customerid })
            }
        })
    },
    /**
    * 用户点击右上角分享给朋友
    */
    onShareAppMessage() {
        return {
            title: "分享你一个需求交流平台！推荐你一起来关注~",
            imageUrl: '/images/home/ShareApp-index.png',
            path: 'pages/index/index'
        }
    },
})
