const app = getApp()
const moment = require("../../utils/moment.min")
import WxValidate from '../../utils/wxValidate'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
        // dialog弹框
        dialog: { title: '发帖', show: false, type: '1' },
        id: '',
        // 详情
        info: {},
        // 用户信息
        user: {},
    },
    initValidate() {
        const rules = { title: { required: true }, content: { required: false } }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = { title: { required: '请输入话题名称', }, content: { required: '请输入信息内容' } };
        this.WxValidate = new WxValidate(rules, messages)
    },
    // 返回
    back: function () {
        wx.navigateBack({ delta: 1 })
    },
    // 打开弹框
    toDialog: function () {
        const that = this;
        that.setData({ dialog: { title: '发帖', show: true, type: '1' } })
    },
    // 关闭弹框
    toClose: function () {
        const that = this;
        that.setData({ dialog: { title: '发帖', show: false, type: '1' } })
    },
    // 发帖
    onSubmit: async function (e) {
        const that = this;
        const params = e.detail.value;
        const user = that.data.user;
        params.create_time = moment().format('YYYY-MM-DD');
        params.openid = user.openid;
        params.name = user.name;
        let stick = [...that.data.info.stick, params];
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList[0];
            wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
            return false
        } else {
            const arr = await app.$post(`/newCourt/api/topic/${that.data.id}`, { stick: stick });
            if (arr.errcode == '0') {
                wx.showToast({ title: `发帖完成`, icon: 'success', duration: 2000 });
                that.toClose(); that.watchLogin();
                that.setData({ form: {} })
            }
            else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        that.setData({ id: options.id || '62ea3510a97c2931cbbec91e' })

        //验证规则函数
        that.initValidate();
    },
    // 监听用户是否登录
    watchLogin: async function () {
        const that = this;
        wx.getStorage({
            key: 'user',
            success: async res => {
                this.setData({ user: res.data });
                if (that.data.id) {
                    const arr = await app.$get(`/newCourt/api/topic/${that.data.id}`);
                    if (arr.errcode == '0') {
                        that.setData({ info: arr.data });
                        let content = arr.data.content.replace(/\<img/gi, '<img style="width:100%;height:auto;margin: 5px 0"');
                        this.setData({ 'info.content': content });
                    }
                }
            },
            fail: res => {
                wx.redirectTo({ url: '/pages/index/index', })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 监听用户是否登录
        this.watchLogin();
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})