import { approveUser, getApproveList, getUserInfo, removeUser } from "../../../../service/user"
import { alertInfo, alertSuccess, wxModal } from "../../../../utils/index";
import request from "../../../../utils/request"

const { OK } = request;

Component({
  /**
   * 组件的初始数据
   */
  data: {
    list: []
  },
  lifetimes: {
    attached: function() {
      this.init();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async init() {
      const result = await getApproveList(1)
      const { code, message, data } = result;
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      this.setData({
        list: data
      })
    },
    async setAdmin(e) {
      const { user } = e.currentTarget.dataset;
      const tenement = wx.getStorageSync('tenement');
      await wxModal({ content: `你的管理员身份将转交给${user.name}` })
      const { code, message } = await approveUser({ userId: user.id, type: 2, tenement: tenement?.id })
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      
      wx.switchTab({
        url: '/pages/home/index',
      })
      alertSuccess("转交成功")
    },
    async remove(e) {
      const { user } = e.currentTarget.dataset;
      const tenement = wx.getStorageSync('tenement');
      await wxModal({ content: `将移除${user.name}？` })
      const { code, message } = await removeUser(user.id, tenement?.id)
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      alertSuccess("已移除")
      this.init()
    }
  }
})