import {
    ApiModel
} from "../../utils/api";
const _Http = new ApiModel();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        iosX: false, //判断是否具有安全距离
        optionItem: 0, //底部选中
        relationList: [], //沟通列表
        discussionGroupList: [], //讨论组列表
        relationListCopy: [], //备份
        discussionGroupListCopy: [], //备份
        searchText: '', //搜索文本
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //是否为带有底部安全距离机型
        let iosX = (getApp().globalData.safeAreaBottom == 0) ? false : true;
        this.setData({
            iosX
        })
    },
    /* 列表搜索 */
    listSearch(e) {
        const value = e.detail.value.trim();
        if (value == this.data.searchText) return;
        this.setData({
            searchText: value
        })
        if (value == '') {
            this.setData({
                relationList: this.data.relationListCopy,
                discussionGroupList: this.data.discussionGroupListCopy
            })
        } else {
            let relationList = this.data.relationListCopy,
                discussionGroup = this.data.discussionGroupListCopy,
                newRelationList = [],
                newDiscussionGroup = [];
            for (let i = 0; i < relationList.length; i++) {
                let data = relationList[i];
                data.users = ''
                if (data.fimdialogname.includes(value)) newRelationList.push(relationList[i])
            };
            for (let i = 0; i < discussionGroup.length; i++) {
                let data = discussionGroup[i];
                data.users = ''
                if (data.fimdialogname.includes(value)) newDiscussionGroup.push(discussionGroup[i])
            }
            this.setData({
                relationList: newRelationList,
                discussionGroupList: newDiscussionGroup
            })
        }
    },
    /* 创建对话 */
    createDialogue() {
        wx.navigateTo({
            url: './create',
        })
    },
    /* 进入对话 */
    enterTheDialogue(e) {
        const {
            item
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: "./dialogbox?id=" + item.timdialogid
        })
    },
    /* 获取聊天列表 */
    queryImdialogList(condition = "") {
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "system.im.imdialog.imdialog",
            "method": "query_imdialogList",
            "content": {
                "getdatafromdbanyway": true,
                "where": {
                    "condition": condition,
                    "fimdialogtype2": "供需"
                }
            }
        }).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 time = (res.data[i].latestnews.length == 0) ? res.data[i].fjoindate.split(" ") : res.data[i].latestnews[0].message.fdatetime.split(" "),
                    date = new Date(),
                    getMonth = (date.getMonth() + 1),
                    Month = (getMonth < 10) ? '0' + getMonth : getMonth,
                    now = date.getFullYear() + '-' + Month + '-' + date.getDate();
                if (now == time[0]) {
                    let a1 = time[1].split(".");
                    res.data[i].fjoindate = a1[0];
                } else {
                    res.data[i].fjoindate = time[0];
                }
            }
            let relationList = [],
                discussionGroupList = [];
            for (let i = 0; i < res.data.length; i++) {
                if (res.data[i].fimdialogtype == '话题') {
                    res.data[i].user = res.data[i].users[0]
                    discussionGroupList.push(res.data[i])
                } else {
                    relationList.push(res.data[i])
                }
            }
            for (let i = 0; i < relationList.length; i++) {
                if (relationList[i].users[0].tenterprise_userid == wx.getStorageSync('userData').userid) {
                    relationList[i].user = relationList[i].users[1]
                } else {
                    relationList[i].user = relationList[i].users[0]
                }
            }
            this.setData({
                relationList,
                relationListCopy: relationList,
                discussionGroupList,
                discussionGroupListCopy: discussionGroupList
            })
        })
    },
    /* 底部选中 */
    footerOption(e) {
        const {
            index
        } = e.currentTarget.dataset;
        this.setData({
            optionItem: index
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

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

    },

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

    },

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

    },

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

    },

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

    }
})