// pages/workBenchNew/components/workBenchWxUser/index.js
import request from "../../../../utils/request"
import { getTenementBackInfo, updateUserApp} from "../../../../service/tenement"
import { getBackApproveList, removeUser } from "../../../../service/user"
import { alertInfo, alertSuccess, wxModal } from "../../../../utils/index"
const { OK } = request

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    tenementInfo: {},
  },
  onParkFocus(e) {
    this.setData({
      show: true,
      title: "园区",
      type: 'park'
    })
  },
  onTenementFocus(e) {
    const { park } = this.data;
    if (!park) {
      alertInfo("请先选择园区")
      return;
    }
    this.setData({
      show: true,
      title: "商户",
      type: 'tenement'
    })
  },
  onConfirm(e) {
    const that = this;
    const { type, data } = e.detail;
    switch(type) {
      case "park":
        this.setData({
          park: data.id,
          parkName: data.name,
          show: false,
        })
        break;
      case "tenement":
        this.setData({
          tenement: data.id,
          tenementName: data.name,
          show: false,
        }, () => {
          that.initUserList();
          that.getTenementInfo();
        })
        break;
    }
  },
  async setAdmin(e) {
    const { id, name } = e.currentTarget.dataset;
    const { tenement } = this.data;
    await wxModal({ content: `确认要将${name}设置为管理吗？` })
    const { code, message } = await updateUserApp({ userId: id, type: 2, tenement: tenement })
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    alertSuccess("转交成功")
    this.initUserList();
    this.getTenementInfo();
  },
  onCancel() {
    this.setData({
      show: false,
    })
  },
  handleDelete(e) {
    const { id, name } = e.currentTarget.dataset;
    const { tenement } = this.data;
    const that = this;
    wx.showModal({
      title: '提示',
      content: `确认要移除${name}吗？`,
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          const { code, message } = await removeUser(id, tenement)
          if (code !== OK) {
            alertInfo(message)
            return;
          }
          alertSuccess("删除成功")
          that.initUserList();
        }
      }
    })
  },
  onEditConfirm() {
    this.initUserList()
    this.handleCancel();
  },
  onUpdatePhoneConfirm() {
    this.setData({ parentPhone: "", phone: "", type: "", title: "" })
    this.getTenementInfo();
    this.initUserList()
  },
  async getTenementInfo() {
    const { tenement, park } = this.data;
    const { code, message, tenement: data } = await getTenementBackInfo(park, tenement)
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    this.setData({
      tenementInfo: data,
    })
  },
  async initUserList() {
    const { tenement } = this.data;
    const { code, message, data } = await getBackApproveList(tenement, 1);
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    this.setData({
      list: data,
    })
  },
  handleAddSon() {
    this.setData({
      visible: true,
      type: "add",
      title: "新建子账号"
    })
  },
  handleChangeMain() {
    const { tenementInfo = {} } = this.data;
    this.setData({
      updatePhoneVisible: true,
      type: "update",
      title: "编辑管理员",
      parentPhone: tenementInfo.phone,
      // name: main.WechatUserName,
      // id: main.WechatUserID,
    })
  },
  handleUpdatePhoneCancel() {
    this.setData({
      updatePhoneVisible: false,
      type: "",
      phone: "",
      parentPhone: "",
      name: "",
      id: "",
    })
  },
  handleCancel() {
    this.setData({
      visible: false,
      type: "",
      phone: "",
      name: "",
      id: "",
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})