//index.js
//获取应用实例
import fetch from '../../utils/http'

const app = getApp()
const $api = require('../../config/api.js').$api

Page({
  onShow() {},
  onLoad(e) {
    this.setData({
      Id: e.id,
      Type: e.type - 0
    })
    if (e.type - 0 === 1) {
      wx.setNavigationBarTitle({
        title: '项目详情'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '活动详情'
      })
    }
  },
  data: {
    UserInfo: app.globalData.UserInfo,
    Id: null,
    Type: null,
    ShowPopup: false,
    FormData: {
      Name: '',
      PhoneNum: '',
      IdCard: ''
    },
    SubmitOff: true,
    ActivityDetail: {
      Title: '示例标题',
      Publisher: '示例发布人',
      CreateDate: '2019-02-02',
      Content: ''
    },
    CommentList: []
  },
  onReady() {
    if (this.data.Type === 1) {
      this.GetProjectDetail()
    } else {
      this.GetActivityDetail()
      this.GetCommentList()
    }
  },
  AddComment() { // 添加评论
    wx.navigateTo({
      url: '/pages/ActivityDetail/comment?id=' + this.data.Id
    })
  },
  ShowSignUpPopup() { // 打开报名弹窗
    this.setData({
      ShowPopup: true
    })
  },
  ClosePopup() { // 关闭报名弹窗
    this.setData({
      ShowPopup: false,
      FormData: {
        Name: '',
        PhoneNum: '',
        IdCard: ''
      }
    })
  },
  FormInput(e) { // 输入form数据
    this.setData({
      FormData: {
        ...this.data.FormData,
        [e.target.dataset.name]: e.detail.value
      }
    })
  },
  Submit() { // 提交报名信息
    if (this.data.SubmitOff) {
      this.setData({
        SubmitOff: false
      })

    }
  },
  GetCommentList() { // 获取评论列表
    fetch({
      url: $api.comment.list.url + '?pageNum=1&pageSize=10000&commentType=activity&mainId=' + this.data.Id,
      method: $api.comment.list.method
    }).then((res) => {
      if (res.code === 200) {
        console.log(res.data.records)
        let CommentList = []
        res.data.records.map((item, index) => {
          CommentList.push({
            FloorUserIcon: item.avatar,
            FloorUserName: item.customerName,
            CreateDate: item.createDate,
            Content: item.commentContent,
            ImgList: item.commentImgList,
            LikeNum: '0',
            Childs: []
          })
          let aChild = []
          item.childComment.map((subItem) => {
            aChild.push({
              Name: subItem.customerName,
              CreateDate: subItem.createDate,
              Content: subItem.commentContent,
              LikeNum: '0',
              ImgList: subItem.commentImgList
            })
          })
          CommentList[index].Childs = aChild
        })
        this.setData({
          CommentList: CommentList
        })
      }
    })
  },
  GetActivityDetail() { // 获取活动详情
    fetch({
      url: $api.activity.detail.url.replace(':id', this.data.Id),
      method: $api.activity.detail.method
    }).then((res) => {
      if (res.code === 200) {
        this.setData({
          ActivityDetail: {
            Title: res.data.title,
            Publisher: '江北公司',
            CreateDate: res.data.activityDate,
            Content: res.data.context
          }
        })
      }
    })
  },
  GetProjectDetail() { // 获取项目详情
    fetch({
      url: $api.dynamic.detail.url.replace(':id', this.data.Id),
      method: $api.dynamic.detail.method
    }).then((res) => {
      if (res.code === 200) {
        this.setData({
          ActivityDetail: {
            Title: res.data.title,
            Publisher: '发布人',
            CreateDate: res.data.createDate,
            Content: res.data.url
          }
        })
      }
    })
  }
})