// pages/workBenchNew/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;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  onParkFocus(e) {
    this.setData({
      show: true,
      title: "园区",
      type: 'park'
    })
  },
  onBuildingFocus() {
    const { park } = this.data;
    if (!park) {
      alertInfo("请先选择园区")
      return;
    }
    this.setData({
      show: true,
      title: "建筑",
      type: 'building'
    })
  },
  onChange(e) {
    const { name } = e.currentTarget.dataset;
    this.setData({
      [name]: e.detail
    })
  },
  onFeeTypeFocus() {
    this.setData({
      show: true,
      title: "收费类型",
      type: 'feeType'
    })
  },
  onCancel() {
    this.setData({ show: false })
  },
  async onSubmit() {
    const { name, shortName, address, contact, 
      phone, building, feeType, park, editType, id, parkName,
      
    } = this.data;
    const { code, message, tenement } = await createBackTenement(park, { name, shortName, address, contact, phone, building, feeType })
    if (code !== OK) {
      alertInfo(message)
      return
    }
    alertSuccess("开户成功")
    // wx.navigateBack()
    wx.redirectTo({
      url: `/pages/workBenchNew/components/workBenchTenement/index?park=${park}&parkName=${parkName}&name=${name}&id=${tenement}`,
    })
  },
  handleBack() {
    wx.navigateBack()
  },
  onConfirm(e) {
    const { type, data, way } = e.detail;
    switch(type) {
      case "park":
        this.setData({
          park: data.id,
          parkName: data.name,
          show: false,
        })
        return;
      case "building":
        this.setData({
          building: data.id,
          buildingName: data.name,
          show: false,
        })
        return;
      case "feeType":
        this.setData({
          feeTypeName: data,
          feeType: way,
          show: false,
        })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})