// pages/msg/push/index.js
import utils from "../../../utils/util.js"
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topBarH: app.globalData.CustomBar,
    baseUrl: app.FILE_SERVER_URL,
    uuid: utils.uuid(),
    files: [],

    showTypePop: false,
    showWorkPop: false,
    TypeData: ['检查', '任务', '政策', '学习', '预警', '系统'],
    workers: [],
    checkedNodes: [], //选中人员
    pros: {
      key: 'performclassname',
      val: 'listperformid'
    },
    postData: {},
    isDetail: false,
    isUserDetail: false,
  },

  /**
   * 显示/关闭弹窗
   */
  popModal: function (params) {
    if (this.data.isDetail) return
    const type = params.currentTarget.dataset.type;
    this.data[type] = !this.data[type];
    this.setData(this.data)
  },

  /**
   * 通知类型
   */
  typeChange: function (params) {
    const index = params.currentTarget.dataset.index;
    this.data.postData.type = index + 1;
    this.data.showTypePop = false;
    this.setData(this.data)
  },

  /**
   * 通知内容
   */
  inputContent: function (e) {
    this.data.postData.content = e.detail.value;
  },

  /**
   * 获取接收人员
   */
  getWorker: function (params) {
    app.axios("GET", "app", "/notice/getNoticePerformList", {
      govNoticeId: this.data.uuid,
      userId: app.globalData.userInfo.userId
    }).then(res => {
      if (res.code == 1) {
        this.data.workers = res.data;
        this.setData(this.data)
      }
    })
  },

  /**
   * 选择文件
   * @param {*} params 
   */
  chooseFile: function (params) {
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success: (res) => {
        const tempFilePaths = res.tempFiles;
        const path = tempFilePaths[0].path;
        this.postImg(path)
      }
    })
  },

  /**
   * 上传图片
   */
  postImg(filePath) {
    wx.showLoading({
      title: '文件上传中...',
    })
    app.uploadFile(this.data.uuid, app.FileType.msgAcs, filePath).then(res => {
      wx.hideLoading();
      this.getFiles();
    })
  },

  /**
   * 获取整改前图片
   */
  getFiles: function () {
    app.axios("GET", "common", "/upload/getFile", {
      otcid: this.data.uuid,
      otctype: app.FileType.msgAcs
    }).then(res => {
      if (res.code == 1) {
        this.data.files = res.data;
        this.setData(this.data)
      }
    })
  },

  /**
   * 删除图片
   * @param {*} params 
   */
  deleteFile: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '删除提示',
      content: '是否要删除该附件？',
      success: (res) => {
        if (res.confirm) {
          app.axios("GET", '', "/delFile", {
            deletedId: id,
          }).then(res => {
            if (res.code == 1) {
              this.getFiles();
            }
          })
        }
      }
    })
  },

  /**
   * 发布通知
   */
  pushMsg: function (params) {
    const childComponent = this.selectComponent('#tree');
    this.data.postData.userId = app.globalData.userInfo.userId;
    this.data.postData.govNoticeId = this.data.uuid;
    this.data.postData.listperformids = childComponent.data.allChoiceIdList;
    if (!this.data.postData.type) {
      wx.showToast({
        title: '请选择通知类型',
        icon: 'none'
      })
      return
    }
    if (!this.data.postData.content) {
      wx.showToast({
        title: '请选择通知内容',
        icon: 'none'
      })
      return
    }
    if (!this.data.postData.listperformids) {
      wx.showToast({
        title: '请选择接收对象',
        icon: 'none'
      })
      return
    }
    app.axios("POST", "app", "/notice/addGovNotice", this.data.postData).then(res => {
      if (res.code == 1) {
        wx.showToast({
          title: '发布成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1500);
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 获取通知详情
   */
  getDetail: function () {
    app.axios('GET', 'admin', `/notice/getGovNotice/${this.data.uuid}`, {}, false).then(res => {
      if (res.code == 1) {
        this.data.postData = res.data;
        this.setData(this.data);
        this.getFiles();
      }
    })
  },

  /**
   * 预览文件
   */
  filePre: function (params) {
    const url = params.currentTarget.dataset.url;
    wx.downloadFile({
      url: this.data.baseUrl + url,
      success: res => {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function (res) {}
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.isUserDetail = options.user;
    this.data.uuid = options.uuid || utils.uuid();
    this.data.isDetail = options.uuid || false;
    this.getWorker();
    if (this.data.isDetail) {
      this.getDetail();
    }
    this.setData(this.data)
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})