// pages/eventReport/eventReportDetail/eventReportDetail.ts
import { getAction, deleteAction } from '../../../api/base';
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    item: {} as any
  },

  // 变更tab
  changeTab(e: any) {
    console.log(e.currentTarget.dataset.tab);
    this.setData({
      tabActive: e.currentTarget.dataset.tab
    });
  },
  getDetail(id: string) {
    let params = { id: id };
    getAction('api/event/info/getById', params).then((res: any) => {
      if (res.code == 200) {
        console.log(res.result);

        res.result.fileUrlList = [];
        if (res.result.fileUrl) {
          for (let i = 0; i < res.result.fileUrl.split(',').length; i++) {
            let pic = res.result.fileUrl.split(',')[i];
            res.result.fileUrlList.push({
              url: pic,
              name: ''
            });
          }
        }
        if (res.result.isHandle == 1) {
          res.result.eventProgress.handleFileList = [];
          if (res.result.eventProgress.handleFile) {
            for (
              let i = 0;
              i < res.result.eventProgress.handleFile.split(',').length;
              i++
            ) {
              let pic = res.result.eventProgress.handleFile.split(',')[i];
              res.result.eventProgress.handleFileList.push({
                url: pic,
                name: ''
              });
            }
          }
        }
        this.setData({
          item: res.result
        });
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  delItem() {
    Dialog.confirm({
      title: '提示',
      message: '是否确认删除'
    })
      .then(() => {
        // on confirm
        let params = { id: this.data.item.id };
        wx.showLoading({
          title: '加载中'
        });
        deleteAction('api/event/info/delete', params).then((res: any) => {
          wx.hideLoading();
          if (res.code == 200) {
            wx.showToast({
              title: '删除成功',
              icon: 'none'
            });
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
                fail: e => {
                  console.log(e);
                }
              });
            }, 500);
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none'
            });
          }
        });
      })
      .catch(() => {
        // on cancel
      });
  },
  toFeedback() {
    let that = this;
    wx.navigateTo({
      url: '../eventReportFeedback/eventReportFeedback',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('eventName', { ...that.data.item });
      },
      fail: e => {
        console.log(e);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // // 接收传参
    const eventChannel = this.getOpenerEventChannel();

    eventChannel &&
      eventChannel.on &&
      eventChannel.on('eventName', data => {
        console.log('接收参数', data);
        // data.fileUrlList = [];
        // if (data.fileUrl) {
        //   for (let i = 0; i < data.fileUrl.split(',').length; i++) {
        //     let pic = data.fileUrl.split(',')[i];
        //     data.fileUrlList.push({
        //       url: pic,
        //       name: ''
        //     });
        //   }
        // }
        // this.setData({
        //   item: data
        // });
        this.getDetail(data.id);
      });
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
