import { alertInfo, alertSuccess } from "../../../../../../utils/index";
import { createTenementWxUser } from "../../../../../../service/tenement";
import request from "../../../../../../utils/request"
const { OK } = request
// pages/workBench/components/account/components/editModal/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    type: String,
    title: String,
    visible: Boolean,
    onCancel: Function,
    park: String,
    tenement: String,
    parentPhone:String, 
    parentName:String, 
    parentId:String, 
  },
  observers: {
    "parentName,parentId,parentPhone": function(newName, newId, newPhone) {
      this.setData({ data: { name: newName, id: newId, phone: newPhone } })
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    data: { name: "", phone: "", id: "" }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async onSubmit() {
      const { data = {}, tenement, park, type } = this.data;
      if (!data.phone) {
        alertInfo("请输入手机号")
        return
      }
      if (!data.name) {
        alertInfo("请输入昵称")
        return
      }
      const { code, message } = await createTenementWxUser({ ...data, tenement, park })
      if (code !== OK) {
        alertInfo(message)
        this.triggerEvent("cancel")
        return;
      }
      alertSuccess("操作成功")
      this.triggerEvent("ok")
      this.setData({ data: {} })
      return;
      
      
    },
    onChange(e) {
      const { name } = e.currentTarget.dataset;
      const newData = this.data.data;
      newData[name] = e.detail;
      this.setData({
        data: newData,
      })
    },
    onCancel() {
      this.setData({ data: {} })
      this.triggerEvent("cancel")
    }
  }
})