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

    /**
     * 页面的初始数据
     */
    data: {
        recommendationList: [], //推荐列表
        particulars: {}, //详情
        ceiling: false, //是否吸顶
        Heitop: 0, //吸顶高度
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let method = "query_supplyanddemandMain";
        if (options.type == 2) method = "query_mysupplyanddemandMain";
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": method,
            "content": {
                "tsupplyanddemandid": options.tsupplyanddemandid
            }
        }).then(res => {
            console.log("供需详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            let checkdate = res.data[0].checkdate;
            if (checkdate != '' && checkdate != null && checkdate != undefined) res.data[0].checkdate = checkdate.slice(0, checkdate.lastIndexOf('.'));
            const particulars = handleList.imageType(res.data[0], "default");
            this.setData({
                particulars
            })
        });
        /* 获取推荐列表 */
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": "query_supplyanddemandList",
            "content": {
                "getdatafromdbanyway": true,
                "pageNumber": 1,
                "pageSize": 10,
                "where": {}
            }
        }).then(res => {
            console.log("查询列表", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            const data = handleList.checkdate(res.data)
            const recommendationList = handleList.imageType(data, "default");
            this.setData({
                recommendationList
            })
        })
    },
    /* 跳转商品详情页 */
    jumpForDetails(e) {
        const {
            index
        } = e.currentTarget.dataset,
            id = this.data.recommendationList[index].tsupplyanddemandid;
        wx.navigateTo({
            url: '/pages/tabbar-pages/supplyAndDemand/particulars?tsupplyanddemandid=' + id,
        })
    },
    /* 预览图片 */
    previewImage(e) {
        let imageList = this.data.particulars.attinfos,
            urls = [];
        for (let i = 0; i < imageList.length; i++) {
            urls.push(imageList[i].fobsurl)
        }
        const {
            index
        } = e.currentTarget.dataset
        wx.previewImage({
            urls: urls,
            current: index
        })
    },
    /* 阻止冒泡 */
    stop() {

    },
    /* 一键联系 */
    toLinkUp(e) {
        const {
            tsupplyanddemandid,
            tenterprise_userid
        } = e.currentTarget.dataset.item;
        if (tenterprise_userid == wx.getStorageSync('userData').userid) return;
        _Http.basic({
            "accesstoken": wx.getStorageSync('userData').token,
            "classname": "customer.supplyanddemand.supplyanddemand",
            "method": "OpenImDialog",
            "content": {
                "tsupplyanddemandid": tsupplyanddemandid
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            wx.navigateTo({
                url: '/pages/chatRoom/dialogbox?id=' + res.data[0].timdialogid,
            })
        })
    },
    callOut(e){
        const {fphonenumber} = e.currentTarget.dataset.item;
        wx.makePhoneCall({
            phoneNumber:fphonenumber
        }).catch((e) => {})
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let data = this.data.particulars,
            obj = {
                title: '[' + data.ftype + ']' + data.ftitle,
                imageUrl: (data.attinfos.length != 0) ? data.attinfos[0].fobsurl : '',
                path: '/pages/portal/details?tagentsid=' + data.tagentsid + '&tsupplyanddemandid=' + data.tsupplyanddemandid
            }
            console.log(obj)
        return obj
    },
})