import store from '../../store'
import create from '../../utils/create'
import fetch from '../../utils/http'
import { get as getApi } from '../../config/api'

const app = getApp()

create(store, {
  onShow() {
    wx.setNavigationBarTitle({
      title: '收藏的楼盘'
    })
  },
  onLoad () {
    const userInfo = app.globalData.UserInfo

    fetch({
      ...getApi('user.building', { openid: userInfo.openid }),
      query: { pageNum: 1, pageSize: 10 },
    }).then(({ data }) => {
      const list = data.records || []
      this.setData({ list })
    }).catch((err) => {
      wx.showToast({
        title: '获取列表失败',
        icon: 'error',
      })
    })
  },
  data: {
    list: [],
  },
  DeleteItem(e) { // 确认删除逻辑
    const buildingId = e.target.dataset.index
    const userInfo = app.globalData.UserInfo
    const _self = this

    wx.showModal({
      title: '确认',
      content: '确认要删除当前记录?',
      success(res) {
        if (res.confirm) {
          
          fetch({
            ...getApi('user.delBuilding', { openid: userInfo.openid }),
            query: { buildingId },
          }).then(() => {
            const list = _self.data.list.filter(x => x.buildingId !== buildingId)

            _self.setData({ list })
          }).catch((err) => {
            wx.showToast({
              title: '删除失败',
              icon: 'error',
            })
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})