// pages/workBench/components/tenement/components/kaihu/index.js
import { alertInfo, alertSuccess } from "../../../../../../utils/index"
import { createBackTenement, updateTenementBackInfo } from "../../../../../../service/tenement"
import request from "../../../../../../utils/request"
import { payWays, feeType as feeTypeList } from "../../../../../../utils/data";
const { OK } = request;
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    park: String,
    visible: Boolean,
    defaultValue: Object,
    editType: String,
    title: String,
  },
  observers: {
    'defaultValue': function(defaultValue) {
      const { fullName, shortName, phone, id, feeType, address, building, buildingName, contact,} = defaultValue;
      this.setData({
        name: fullName,
        shortName,
        phone, id, feeType, address, building, buildingName, contact,
        feeTypeName: feeTypeList[feeType]
      })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    onBuildingFocus() {
      const { park } = this.data;
      if (!park) {
        alertInfo("请先选择园区")
        return;
      }
      this.setData({
        show: true,
        title: "建筑",
        type: 'building'
      })
    },
    onFeeTypeFocus() {
      const { park } = this.data;
      if (!park) {
        alertInfo("请先选择园区")
        return;
      }
      this.setData({
        show: true,
        title: "收费类型",
        type: 'feeType'
      })
    },
    onConfirm(e) {
      const { type, data, way } = e.detail;
      switch(type) {
        case "building":
          this.setData({
            building: data.id,
            buildingName: data.name,
            show: false,
          })
          return;
        case "feeType":
          this.setData({
            feeTypeName: data,
            feeType: way,
            show: false,
          })
      }
    },

    onCancel() {
      this.setData({ show: false })
    },
    onClose() {
      this.triggerEvent("close")
    },
    onChange(e) {
      const { name } = e.currentTarget.dataset;
      this.setData({
        [name]: e.detail
      })
    },
    clear() {
      this.setData({
        name: "", shortName: "", address: "", contact: "", phone: "", 
        building: "", feeType: "", buildingName: "", feeTypeName: "", id,
      })
    },
    async onSubmit() {
      const { name, shortName, address, contact, phone, building, feeType, park, editType, id } = this.data;
      if (editType === "add") {
        const { code, message } = await createBackTenement(park, { name, shortName, address, contact, phone, building, feeType })
        if (code !== OK) {
          alertInfo(message)
          return
        }
        alertSuccess("开户成功")
        this.triggerEvent("ok")
        return;
      }
      const { code, message } = await updateTenementBackInfo(park, id,  { name, shortName, address, contact, phone, building, feeType })
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      alertSuccess("编辑成功")
      this.triggerEvent("ok")
    }
  },
  
})