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

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [
            {
                tab: "发布中",
                code: 'release'
            },
            {
                tab: "已下架",
                code: 'remove'
            }
        ],
        currentTab: 'release',
        listLoading: false,
        customerId: null,
        // 我发布的需求----
        // item行加载
        itemLoading: 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 => {
            console.log(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,
            listLoading: true,
            page: 1,
        })
        this.getListData();
    },
    // 置顶/取消置顶需求
    productIsOnTop(e) {
        let item = e.currentTarget.dataset.item;
        let that = this;
        if (item.isOnTop) {
            wx.showModal({
                title: '提示',
                content: "是否确认取消置顶？",
                confirmText: "确认",
                cancelText: "取消",
                success(res) {
                    if (res.confirm) {
                        that.productIsOnTopPost(item);
                    }
                }
            })
        } else {
            that.productIsOnTopPost(item);
        }
    },
    // 置顶/取消置顶需求Fn
    productIsOnTopPost(item) {
        let { businessCommunicationDemandId, isOnTop } = item;
        this.showLoading();
        $request.post(`/businessCommunicationDemand/setOnTopOrDown.action`,
            { businessCommunicationDemandId, isOnTop: isOnTop ? 0 : 1, customerId: this.data.customerId }
        ).then(res => {
            wx.hideLoading()
            if (res.status == 0) {
                wx.showToast({
                    title: isOnTop ? '取消置顶成功' : '置顶成功',
                    icon: 'success',
                    duration: 2000
                })
                let listData = this.data.listData;
                listData.forEach(el => {
                    if (el.businessCommunicationDemandId == businessCommunicationDemandId) {
                        el.isOnTop = isOnTop ? false : true
                    }
                })
                this.setData({
                    listData
                })
            }else if(res.status == 106){
                wx.showToast({
                    title: res.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        }).catch(error => {
            console.log(error, 'error appletLogin')
            wx.hideLoading()
        })
    },
    // 上架/下架需求
    productRelease(e) {
        let item = e.currentTarget.dataset.item;
        let that = this;
        if (item.state == 0) {
            wx.showModal({
                title: '提示',
                content: "是否确认下架？",
                confirmText: "确认",
                cancelText: "取消",
                success(res) {
                    if (res.confirm) {
                        that.productReleasePost(item);
                    }
                }
            })
        } else {
            that.productReleasePost(item);
        }
    },
    // 上架/下架需求 Fn
    productReleasePost(item) {
        this.showLoading();
        let { businessCommunicationDemandId, state } = item;
        // 上架传0 下架传-1 
        $request.post(`/businessCommunicationDemand/outDemand.action`,
            { businessCommunicationDemandId, state: state != 0 ? 0 : -1  }
        ).then(res => {
            wx.hideLoading()
            if (res.status == 0) {
                wx.showToast({
                    title: state == 0 ? '下架成功' : '上架成功',
                    icon: 'success',
                    duration: 2000
                })
                let listData = this.data.listData;
                listData.forEach((el, inx) => {
                    if (el.businessCommunicationDemandId == businessCommunicationDemandId) {
                        listData.splice(inx, 1);
                    }
                })
                this.setData({
                    listData
                })
            }
        }).catch(error => {
            console.log(error, 'error appletLogin')
            wx.hideLoading() 
        })
    },
    // 删除需求
    productDel(e) {
        let item = e.currentTarget.dataset.item;
        let that = this;
        wx.showModal({
            title: '提示',
            content: "是否确认删除？",
            confirmText: "确认",
            cancelText: "取消",
            success(res) {
                if (res.confirm) {
                    that.productDelPost(item);
                }
            }
        })
    },
    // 删除需求 Fn
    productDelPost(item) {
        let { businessCommunicationDemandId } = item;
        this.showLoading();
        $request.post(`/businessCommunicationDemand/deleteDemand.action`,
            { businessCommunicationDemandId }
        ).then(res => {
            wx.hideLoading()
            if (res.status == 0) {
                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 2000
                })
                let listData = this.data.listData;
                listData.forEach((el, inx) => {
                    if (el.businessCommunicationDemandId == businessCommunicationDemandId) {
                        listData.splice(inx, 1);
                    }
                })
                this.setData({
                    listData
                })
            }
        }).catch(error => {
            console.log(error, 'error appletLogin')
            wx.hideLoading()
        })
    },
    // 修改需求
    productEdit(e) {
        let item = e.currentTarget.dataset.item;
        let that = this;
        wx.navigateTo({
            url: '/pages/releaseModule/index',
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', { listDetail: item })
            }
        })
    },
    // 数据加载提示
    showLoading(title) {
        wx.showLoading({
            title: title ? title : '加载中',
            mask: true
        })
    },
    // 显示下架原因
    showOutReason(e){
        let outReason = e.currentTarget.dataset.outreason;
        wx.showModal({
            title: '下架原因',
            content: outReason,
            showCancel: false,
        })
    },
    
    getListData() {
        let { page, size, customerId, keyword, currentTab } = this.data;
        $request.get('/businessCommunicationDemand/getDemandByStateAndCustomerId.action',
            { page, size, customerId, keyword, state: currentTab }
        ).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,
                    })
                } else {
                    this.setData({
                        onRefresh: true,
                        itemLoading: false,
                        page: page + 1
                    })
                }
                console.log('-------------个人主页 beg-------------------')
                console.log(this.data.listData);
                console.log('-------------个人主页 end-------------------')
            }
            this.setData({
                listLoading: false,
            })
        }).catch(error => {
            console.log(error, 'error appletLogin')
        })
    },
    // 
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.onRefresh) {
            this.setData({
                itemLoading: true
            })
            this.getListData();
        }
    },

})