// packageE/stationNotice/stationNotice.js
const app = getApp();

import dateParserTime from "./dateParserTime";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    unreadCount: 0,
    messageType: [],
    msgTypeUrl: {
      img1:
        "/packageE/stationNotice/stationNoticeLogistics/stationNoticeLogistics",
      img2: "/packageE/stationNotice/stationNoticeExtract/stationNoticeExtract",
      img3: "/packageE/stationNotice/stationNoticeExamine/stationNoticeExamine",
      img4:
        "/packageE/stationNotice/stationNoticeCustomer/stationNoticeCustomer",
      img5:
        "/packageE/stationNotice/stationNoticeProperty/stationNoticeProperty",
      img6: "/packageE/stationNotice/stationNoticeAnchor/stationNoticeAnchor",
      img7: "/packageE/stationNotice/stationNoticeOrder/stationNoticeOrder",
      img8:
        "/packageE/stationNotice/stationNoticeActivity/stationNoticeActivity",
    },
    weekMessage: [],
    page: 1,
    isLoadMore: true,
    total_page: 0,

    msg_body_types: {
      1: {
        title: "标题（商品名）",
        desc: "提示语（快递单号）",
        img: "商品图",
      },
      2: {
        applyTime: "申请时间",
        amounts: "提现金额",
        pay_way_name: "提现方式",
      },
      3: {
        title: "直播间标题",
        desc: "提示语",
        img: "直播间封面图",
      },
      4: {
        title: "标题（您于xx新增粉丝）",
        nickname: "会员昵称",
        pay_time: "付款时间",
        order_sn: "订单编号",
        goodsName: "商品名",
        pay_money: "订单金额",
      },
      5: {
        changeTime: "时间",
        changeType: "类型",
        changeNum: "变动金额",
        afterChange: "变动后",
        order_sn: "订单编号（结算通知才有）",
        order_amount: "订单金额（结算通知才有）",
        mall_amount: "商城销售额（结算通知才有）",
        amount: "分红结算金额（结算通知才有）",
        dividend_amount: "分红奖励金额（结算通知才有）",
      },
      6: {
        title: "直播间标题",
        img: "直播间封面图",
      },
      7: {
        nickname: "昵称",
        time: "时间",
        order_sn: "订单编号",
        goodsName: "商品名",
        pay_money: "订单金额",
        refund_money: "退款金额",
      },
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(dateParserTime);
    this.getIndexData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },
  setMessageRead() {
    let urlStr = app.getNetAddresss(
      "plugin.instation-message.api.message.read-message"
    );
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return this.tips(res.msg);
        this.data.page = 1;
        this.data.isLoadMore = true;
        this.data.total_page = 0;
        this.getIndexData();
      },
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getIndexData() {
    let urlStr = app.getNetAddresss(
      "plugin.instation-message.api.message.index"
    );
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return this.tips(res.msg);
        let messageType = res.data.messageType;
        let weekMessage = res.data.weekMessage.data;
        let unreadCount = 0;
        messageType.forEach((item) => {
          item.last_time = dateParserTime(item.last_time);
          unreadCount += item.no_read;
        });
        weekMessage.forEach((item) => {
          item.created_at = dateParserTime(item.created_at);
        });
        this.data.isLoadMore = true;
        this.data.total_page = res.data.weekMessage.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          messageType,
          unreadCount,
          weekMessage,
        });
      },
    });
  },
  _getMoreData() {
    let that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let urlStr = app.getNetAddresss(
        "plugin.instation-message.api.message.index"
      );
      this.data.page++;
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result != 1) return;
          if (
            res.data.weekMessage.current_page >= res.data.weekMessage.last_page
          ) {
            that.setData({
              isLoadMore: false,
            });
          }
          let weekMessage = res.data.weekMessage.data;
          weekMessage.forEach((item) => {
            item.created_at = dateParserTime(item.created_at);
          });

          let tempWeek = this.data.weekMessage;
          tempWeek.push(...weekMessage);
          this.setData({ weekMessage: tempWeek });
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },
  gotoPages(evt) {
    let item = evt.currentTarget.dataset.item;
    let index = evt.currentTarget.dataset.index;
    let urlStr = app.getNetAddresss(
      "plugin.instation-message.api.message.read-message"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: item._id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return this.tips(res.msg);
        let i = "weekMessage[" + index + "].is_read";
        this.setData({
          [i]: 0,
        });
      },
    });

    let page = item.mini_url;
    if (page == "" || page == null) return;
    wx.navigateTo({
      url: page,
    });
  },
  gotoSetting() {
    wx.navigateTo({
      url: "/packageE/stationNotice/stationNoticeSetting/stationNoticeSetting",
    });
  },
  gotoMessagePage(evt) {
    let index = evt.currentTarget.dataset.index;
    let unreadCount =
      this.data.unreadCount - this.data.messageType[index].no_read;
    let i = "messageType[" + index + "].no_read";
    this.setData({
      [i]: 0,
      unreadCount,
    });
    let url = evt.currentTarget.dataset.url;
    wx.navigateTo({
      url: this.data.msgTypeUrl[url],
    });
  },
  tips(msg) {
    wx.showToast({
      title: msg,
      icon: "none",
      duration: 2000,
    });
    return false;
  },
});
