// pages/project-user/index.js
import http from '../../utils/http'
import util from '../../utils/util'
import api from '../../utils/api'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    list: [],
    keyword: '',
    project: null,
    showAction: false,
    role: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id ? options.id : 1
    this.setData({
      id: id
    })
  },

  change: function() {
    var user = this.getUser()
    var id = user.id ? user.id : -1
    wx.navigateTo({
      url: '/pages/create-project-role/index?id=' + user.id,
    })
  },

  getUser() {
    var list = this.data.list
    for(var i = 0; i < list.length; ++i) {
      if(list[i].selected) {
        return list[i]
      }
    }
    return null
  },

  deleteUser: function() {
    var user = this.getUser()
    var id = user ? user.id : -1
    var that = this
    http({
      url: 'projects/deleteUser',
      data: {
        id: id
      },
      success: function (res) {
        if (res.code == 0) {
          util.success('操作成功')
          setTimeout(function() {
            that.getList()
          }, 300)
        } else {
          util.error('操作失败')
        }
      }
    })
  },

  delete: function () {
    var that = this
    Dialog.confirm({
        title: '确认删除该成员吗',
        message: '删除该成员后，该成员将不再显示在该项目中，请再次确定是否需要删除该成员',
      })
      .then(() => {
        that.deleteUser()
      })
      .catch(() => {
        // on cancel
      });
  },

  selectUser: function (e) {
    var index = e.currentTarget.dataset.index
    var list = this.data.list
    var item = list[index]
    if(item.project_role.level >= this.data.project.role.level) return false
    for (var i = 0; i < list.length; ++i) {
      if (i != index) list[i].selected = false
    }
    list[index].selected = !list[index].selected
    this.setData({
      list,
      showAction: list[index].selected
    })
    // this.updateShowAction()
  },

  updateShowAction: function () {
    var list = this.data.list
    for (var i = 0; i < list.length; ++i) {
      if (list[i].selected) {
        this.setData({
          showAction: true
        })
        return false;
      }
    }
    this.setData({
      showAction: false
    })
  },

  getList: function () {
    var that = this
    http({
      url: 'projects/getUsers',
      data: {
        id: this.data.id,
        keyword: this.data.keyword
      },
      success: function (res) {
        if (res.code == 0) {
          res.data.reverse()
          that.setData({
            list: res.data
          })
          that.updateShowAction()
        }
      }
    })
  },

  getProject() {
    var that = this
    http({
      url: 'projects/detail',
      data: {
        id: this.data.id
      },
      success: function (res) {
        if (res.code == 0) {
          that.setData({
            project: res.data
          })
        }
      }
    })
  },

  clear: function () {
    this.setData({
      keyword: ''
    })
  },

  blur: function () {
    var that = this
    setTimeout(function () {
      that.getList()
    }, 300)
  },

  updateInput: function (e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail.value
    })
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})