// pages/myModule/components/myRelease/myRelease.js
const $request = require('../../../../utils/request.js');
const $util = require('../../../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        listLoading: false,
        customerId: null,
        // 我发布的需求----
        // item行加载
        itemLoading: false,
        // 数据是否加载完成
        isFinished: false,
        listData: [],
        page: 1,
        size: 10,
        total: 0,
        // 是否加载数据，true加载，false不加载
        onRefresh: true,
        // 搜索值
        keyword: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const eventChannel = this.getOpenerEventChannel()
        // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
        eventChannel.on('customerid', data => {
            this.setData({
                customerId: data.customerid,
                listLoading: true,
            })
            this.getListData();
        })
    },
    // tab切换
    tabChange(e) {
        let code = e.currentTarget.dataset.code;
        this.setData({
            currentTab: code
        })
        this.toSearch();
    },
    // 搜索框输入同步值
    bindKeyInput: function (e) {
        this.setData({
            keyword: e.detail.value
        })
    },
    toSearch() {
        this.setData({
            listData: [],
            onRefresh: true,
            isFinished: false,
            listLoading: true,
            page: 1,
        })
        this.getListData();
    },

    // 进入详情
    goDetails(e) {
        wx.navigateTo({
            url: '/pages/index/components/listDetails/Details?businessCommunicationDemandId=' + e.detail,
        })
    },
    // 删除收藏
    delCollect(e) {
        let businessCommunicationDemandId = e.currentTarget.dataset.id;
        let customerId = this.data.customerId;
        console.log({ customerId, businessCommunicationDemandId });
        wx.showLoading({
            title: '操作中...',
            mask: true
        })
        $request.post("/businessCommunicationCollect/deleteCollect.action",
            { customerId, businessCommunicationDemandId }
        ).then(res => {
            wx.hideLoading()
            if (res.status == 0) {
                let listData = this.data.listData;
                listData.forEach((el, inx) => {
                    if (el.businessCommunicationDemandId == businessCommunicationDemandId) {
                        listData.splice(inx, 1);
                    }
                })
                this.setData({
                    listData
                })
            }
        }).catch(err => {
            console.log(error, 'error appletLogin')
        })
    },
    // 数据加载提示
    showLoading(title) {
        wx.showLoading({
            title: title ? title : '加载中',
            mask: true
        })
    },
    getListData() {
        let { page, size, customerId, keyword } = this.data;
        $request.get('/businessCommunicationDemand/getDemandByCustomerId.action',
            { page, size, customerId, keyword }
        ).then(res => {
            let tempListData = this.data.listData;
            if (res.status == 0) {
                let datas = res.data;
                console.log(datas, 'datas')
                // 先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: datas.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,
            })
        }).catch(error => {
            console.log(error, 'error appletLogin')
        })
    },
    // 
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.onRefresh) {
            this.setData({
                itemLoading: true
            })
            this.getListData();
        }
    },
})