// packageE/others/case_library/case_detail/caseLibraryDetail.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    view_show: false,
    id: "",
    uid: "", //会员ID
    LoginStatus: "", //会员信息
    reply_id: "", //回复ID
    reply_type: "", //回复类型
    message_show: false,
    reply_show: false,
    datas: {},
    screen_shot_src: [], //截图
    label: [],
    hotCase: [],
    second_category: [],
    comment: [], //评论
    ad3: {},
    star_value: 4,
    reply_text: "",
    commit_text: "",
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
      });
    }
    try {
      let mid = wx.getStorageSync("uid");
      this.setData({
        uid: mid,
      });
    } catch (err) {
      console.log(err);
    }
    this.getDatas();
  },
  previewImage(e) {
    var index = e.currentTarget.dataset.index; // 这里获取到的是一张本地的图片
    wx.previewImage({
      current: this.data.screen_shot_src[index], // 当前显示图片的链接
      urls: this.data.screen_shot_src // 需要预览的图片链接列表
    });
  },
  gotoOther(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url:
        "/packageE/others/case_library/case_detail/caseLibraryDetail?id=" + id,
    });
  },
  tapLink(e) {
    let link = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: `${link}`,
    });
  },
  changeReplyLength(e) {
    let index = e.currentTarget.dataset.ind;
    let comment = this.data.comment;
    comment[index].is_show = comment[index].is_show ? false : true;
    this.setData({
      comment: comment,
    });
  },
  openMeaage() {
    this.setData({
      reply_show: false,
      reply_id: "",
      reply_text: "",
      message_show: true,
    });
  },
  messageText(e) {
    this.setData({
      commit_text: e.detail.value,
    });
  },
  sendText(e) {
    this.setData({
      reply_text: e.detail.value,
    });
  },
  delComment(e) {
    let str = e.currentTarget.dataset.str;
    let id = e.currentTarget.dataset.id;
    this.del(str, id);
  },
  del(str, id) {
    let that = this;
    wx.showModal({
      title: "提示",
      content: "确定删除此留言?",
      success(res) {
        if (res.confirm) {
          let urlStr = app.getNetAddresss(
            "plugin.case-library.api.release.del-comment"
          );
          app._postNetWork({
            url: urlStr,
            data: {
              id: id,
              comment_type: str,
            },
            success: function (resdata) {
              var res = resdata.data;
              if (res.result == 1) {
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                  duration: 1000,
                });
                that.getDatas();
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                  duration: 1000,
                });
              }
            },
            fail: function (res) {
              console.log(res);
            },
          });
        }
      },
    });
  },
  openReply(e) {
    let id = e.currentTarget.dataset.id;
    let num = e.currentTarget.dataset.num;
    this.setData({
      message_show: false,
      commit_text: "",
      reply_show: true,
      reply_id: id,
      reply_type: num,
    });
  },
  sendMessage(e) {
    let that = this;
    let json = {};
    let url = "";
    let type = e.currentTarget.dataset.num;
    // 回复
    if (type == 1) {
      json = {
        reply_id: that.data.reply_id,
        reply_type: that.data.reply_type,
        contents: that.data.reply_text,
      };
      url = "plugin.case-library.api.release.reply-comment";
    }
    // 评论
    else {
      json = {
        case_id: that.data.id,
        level: that.data.star_value,
        contents: that.data.commit_text,
      };
      url = "plugin.case-library.api.release.release-comment";
    }
    let urlStr = app.getNetAddresss(url);
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
          that.setData({
            star_value: 4,
            commit_text: "",
            reply_text: "",
            message_show: false,
            reply_id: "",
            reply_show: "",
          });
          that.getDatas();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.datas.case_name,
      path: "/packageG/index/index?id=" + this.data.id,
      imageUrl: this.data.datas.img_src,
    };
  },
  getDatas() {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.case-library.api.case.get-case-detail"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: that.data.id,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            view_show: true,
            datas: res.data.case,
            screen_shot_src: res.data.case.screen_shot_src || [],
            label: res.data.case.label || [],
            hotCase: res.data.hotCase,
            second_category: res.data.case.second_category,
            comment: res.data.case.comment,
          });
          console.log(that.data.datas.id);
          // 加入是否展开更多回复状态
          that.data.comment.forEach((item, index) => {
            that.data.comment[index].is_show = false;
          });
          let dainfo = that.data.datas;
          that.data.ad3 =
            res.data.advert.ad3.length && res.data.advert.ad3.length == 0
              ? {}
              : res.data.advert.ad3;
          that.setData({
            comment: that.data.comment,
            five: String(
              (
                (Number(dainfo.five_level) / Number(dainfo.total_level)) *
                100
              ).toFixed(2)
            ),
            four: String(
              (
                (Number(dainfo.four_level) / Number(dainfo.total_level)) *
                100
              ).toFixed(2)
            ),
            three: String(
              (
                (Number(dainfo.three_level) / Number(dainfo.total_level)) *
                100
              ).toFixed(2)
            ),
            two: String(
              (
                (Number(dainfo.two_level) / Number(dainfo.total_level)) *
                100
              ).toFixed(2)
            ),
            one: String(
              (
                (Number(dainfo.one_level) / Number(dainfo.total_level)) *
                100
              ).toFixed(2)
            ),
            ad3: that.data.ad3,
          });
          // console.log(
          //   that.data.five,
          //   that.data.four,
          //   that.data.three,
          //   that.data.two,
          //   that.data.one
          // );
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
