import {
    ApiModel
} from "../../../utils/api";
const _Http = new ApiModel();
const handleList = require("../../../utils/processingData");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isOnShow: true, //是否触发onshow中的刷新列表
        searchFocus: false, //我的需求搜索框焦点
        searchText: "", //我的需求搜索内容
        pageNumber: 1, //获取第几页
        pageTotal: 2, //全部分页数量
        active: 0, //tabs 选中下标
        ftypeList: [], //分类列表
        ftype: "全部", //当前查询分类
        ifShowCommunicationOfNumber: false,
        /* 供需列表 */
        productList: [],
        //轮播图列表
        swiperBannerList: [],
        imdialogs: [], //沟通列表
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        /* 获取所有信息分类 */
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "enterprise.system.supplyanddemand",
            "method": "query_typeselectList",
            "content": {}
        }).then(res => {
            console.log("分类", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            for (let i = 0; i < res.data.length; i++) {
                let attinfos = [];
                for (let k = 0; k < res.data[i].attinfos.length; k++) {
                    if (res.data[i].attinfos[k].ftype == 'default') {
                        attinfos.unshift(res.data[i].attinfos[k])
                    } else if (res.data[i].attinfos[k].ftype == 'click') {
                        attinfos.push(res.data[i].attinfos[k])
                    }
                };
                res.data[i].attinfos = attinfos;
            };
            /* 添加全部 */
            res.data.unshift({
                ftype: "全部",
                attinfos: [{
                    fobsurl: "/static/supplyAndDemand/all.png",
                    ftype: "default"
                }, {
                    fobsurl: "/static/supplyAndDemand/all-c.png",
                    ftype: "click"
                }]
            });
            this.setData({
                ftypeList: res.data
            });
        });
        /* 获取轮播图 */
        const bannerList = wx.getStorageSync('bannerDataList').filter(value => value.flocation == 'supplyanddemand_head');
        this.setData({
            swiperBannerList: bannerList[0].banner
        });
    },
    //跳转聊天
    toLinkUpPage(e) {
        const {
            timdialogid,
            fdatastatus
        } = e.currentTarget.dataset;
        this.setData({
            ifShowCommunicationOfNumber: false
        })
        wx.navigateTo({
            url: '/pages/chatRoom/dialogbox?id=' + timdialogid + '&fdatastatus=' + fdatastatus,
        })
    },
    /* tabs切换接口 */
    getList() {
        if (this.data.active == 0) return this.getSupplyAndDemand();
        if (this.data.active == 1) return this.getSupplyAndDemand(true);
        if (this.data.active == 2) return this.myNeed();
    },
    /* 宫格区点击事件 */
    switchScreenType(even) {
        let {
            type
        } = even.currentTarget.dataset;
        if (type == this.data.ftype) return;
        this.InitializeDataPaging();
        this.setData({
            ftype: type
        });
        this.getSupplyAndDemand()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getTabBar().init();
        //获取列表
        if (this.data.isOnShow) {
            this.getList();
        } else {
            this.setData({
                isOnShow: true
            })
        }
        this.getTabBar().setData({
            'tabbarList[3].fcount': getApp().globalData.msgFcount
        })
    },
    /* 显示沟通数量 */
    showCommunicationOfNumber(e) {
        let {
            item
        } = e.currentTarget.dataset;
        let imdialogs = [];
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": "query_mysupplyanddemandMain",
            "content": {
                "tsupplyanddemandid": item.tsupplyanddemandid
            }
        }, false).then(res => {
            console.log('沟通列表', res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            imdialogs = res.data[0].imdialogs;
            if (imdialogs.length != 0) {
                for (let i = 0; i < imdialogs.length; i++) {
                    let checkdate = imdialogs[i].createdate;
                    if (checkdate != null && checkdate != undefined) imdialogs[i].createdate = checkdate.slice(0, checkdate.lastIndexOf('.'));
                }
                this.setData({
                    ifShowCommunicationOfNumber: true,
                    imdialogs
                })
            } else {
                wx.showToast({
                    title: '暂无对话信息',
                    icon: "none"
                })
            }
        })

    },
    endShowCommunicationOfNumber() {
        this.setData({
            ifShowCommunicationOfNumber: false
        })
    },
    postDemand() {
        wx.navigateTo({
            url: '/pages/announceDemand/index',
        })
    },
    /* 阻止冒泡 */
    stop() {
        /* 所有供需列表中，已过期或已解决 不做操作 */
    },
    toLinkUp(e) {
        const {
            tsupplyanddemandid,
            tenterprise_userid
        } = e.currentTarget.dataset.item;
        if (tenterprise_userid == wx.getStorageSync('userData').userid) return wx.showToast({
            title: '无法与自己对话',
            icon: "none"
        })
        _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,
            })
        })
    },
    /* 供需下架 */
    soldOut(e) {
        const that = this;
        const {
            index
        } = e.target.dataset;
        wx.showModal({
            title: '提示',
            content: '是否确定下架该需求',
            success: function (res) {
                if (res.confirm) {
                    _Http.basic({
                        "accesstoken": wx.getStorageSync('userData').token,
                        "classname": "customer.supplyanddemand.supplyanddemand",
                        "method": "updatesupplyanddemandstatus",
                        "content": {
                            "tsupplyanddemandid": that.data.productList[index].tsupplyanddemandid,
                            "fstatus": "已解决"
                        }
                    }).then(res => {
                        if (res.msg != "成功") return;
                        let productList = that.data.productList;
                        productList[index].fstatus = "已解决"
                        that.setData({
                            productList
                        })
                        wx.showToast({
                            title: "下架成功"
                        })
                    })
                }
            }
        });
    },
    /* 获取供需列表 */
    getSupplyAndDemand(fstatus, condition = '') {
        const ftype = (this.data.ftype != '全部') ? this.data.ftype : "";
        let where = {
            "condition": this.data.searchText, //模糊搜索
            "ftype": ftype, //数据类型
            "fissupply": 0, // 0需 1供
            "fstatus": ""
        };
        /* 暂时不分供需 */
        if (fstatus) {
            where = {
                "condition": this.data.searchText, //模糊搜索
                "ftype": ftype, //数据类型
                "fissupply": 0, // 0需 1供
                "fstatus": "对接中"
            }
        };
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": "query_supplyanddemandList",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": this.data.pageNumber,
                "pageSize": 20,
                "where": where
            }
        },false).then(res => {
            console.log("供需列表", res)
            if (res.msg != "成功") return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.returnList(res);
        })
    },
    /* tabs */
    tabsChange(even) {
        this.setData({
            active: even.detail.index
        })
        /* 初始化分页 */
        this.InitializeDataPaging();
        this.getList();
    },
    /* 跳转商品详情页 */
    jumpForDetails(e) {
        const {
            index
        } = e.currentTarget.dataset,
            id = this.data.productList[index].tsupplyanddemandid;
        wx.navigateTo({
            url: '/pages/tabbar-pages/supplyAndDemand/particulars?tsupplyanddemandid=' + id + '&type=' + this.data.active,
        })
    },
    /* 我的需求 */
    myNeed() {
        //全部加载完成退出请求
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": "query_mysupplyanddemandList",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": this.data.pageNumber,
                "pageSize": 20,
                "where": {
                    "condition": this.data.searchText,
                    "ftype": "",
                    "fissupply": "0"
                }
            }
        }).then(res => {
            if (res.msg != "成功") return wx.showToast({
                title: res.data,
            })
            this.returnList(res);
        })
    },
    /* 返回列表 */
    returnList(res) {
        let data = handleList.checkdate(res.data)
        let productList = handleList.imageType(data, 'default');
        //替换或拼接
        if (res.pageNumber != 1) {
            productList = this.data.productList.concat(productList);
        }
        this.setData({
            productList,
            pageTotal: res.pageTotal,
        });
    },
    /* 我的需求编辑按钮跳转 */
    productEdit(e) {
        const {
            index
        } = e.currentTarget.dataset
        const data = JSON.stringify(this.data.productList[index])
        wx.navigateTo({
            url: '/pages/announceDemand/index?data=' + data,
        })
    },
    /* 我的需求搜索框获得焦点 */
    needSearchFocus() {
        this.setData({
            searchFocus: true
        })
    },
    /* 我的需求搜索框失去焦点 */
    needSearchBlur(e) {
        const {
            value
        } = e.detail;
        //数据比较，防止重复查询
        if (value == this.data.searchText) return this.setData({
            searchFocus: false,
        });
        this.setData({
            searchFocus: false,
            searchText: value
        })
        this.InitializeDataPaging();
        if (this.data.active == 0) {
            this.getSupplyAndDemand();
        } else {
            this.myNeed();
        }
    },
    /* 初始化分页数据 */
    InitializeDataPaging() {
        this.setData({
            pageTotal: 2,
            pageNumber: 1
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({
            isOnShow: true
        })
    },
    /* 阻止刷新列表 */
    stopOnShow() {
        this.setData({
            isOnShow: false
        })
    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({
            pageNumber: this.data.pageNumber + 1
        })
        if (this.data.pageNumber <= this.data.pageTotal) this.getList();
    },

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

    }
})