import { fhGetSickroomList, fhGetSickAreaList, fhGetWardList } from '../../../api/week-hospital/index'

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _selectedSickroomId: null,
    _selectedSickAreaId: null,
    _selectedWardId: null,
    selectedBedNumber: null,

    _sickroomData: [],
    _sickAreaData: [],
    _wardData: [],
    bedData: [],

    multiArray: [['科室'], ['病区'], ['病房']],
    multiIndex: [0, 0, 0],

    _query: {
      sickroomId: null,
      sickAreaId: null,
      pageNumber: 1,
      pageSize: 100
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad({ hospitalId }) {
    this.setData({
      hospitalId
    })
    this.getSickroomList()
  },

  async getSickroomList() {
    const { results: { results } } = await fhGetSickroomList(this.data.hospitalId, this.data._query)
    this.setData({
      _sickroomData: results,
      ['multiArray[0]']: results.map(item => item.sickroomName),
      _selectedSickroomId: results[0].id,
      ['_query.sickroomId']: results[0].id
    })
    this.getSickAreaList()
  },

  async getSickAreaList() {
    const { results: { results } } = await fhGetSickAreaList(this.data.hospitalId, this.data._query)
    this.setData({
      _sickAreaData: results,
      ['multiArray[1]']: results.length ? results.map(item => item.sickAreaName) : [],
      _selectedSickAreaId: results.length ? results[0].id : null,
      ['_query.sickAreaId']: results.length ? results[0].id : null
    })
    this.getWardList()
  },

  async getWardList() {
    const { results: { results } } = await fhGetWardList(this.data.hospitalId, this.data._query)
    this.setData({
      _wardData: results,
      ['multiArray[2]']: results.length ? results.map(item => item.bingfangName) : [],
      _selectedWardId: results.length ? results[0].id : null,
      bedData: results.length ? this.getBedData(results[0].bedCount) : []
    })
  },

  getBedData(bedCount) {
    const bedArray = Array.from({ length: bedCount }, (_, index) => index + 1)
    return bedArray.map(item => item + '号床')
  },

  selectBed({ currentTarget: { dataset: { bedNumber } } }) {
    this.setData({
      selectedBedNumber: bedNumber
    })
  },

  bindMultiPickerChange({ detail: { value } }) {
    console.log('picker发送选择改变，携带值为', value)
    this.setData({
      multiIndex: value
    })
  },

  bindMultiPickerColumnChange({ detail: { column, value } }) {
    console.log('修改的列为', column, '，值为', value)
    this.data.multiIndex[column] = value
    switch (column) {
      case 0:
        if (this.data._sickroomData.length > value) {
          const element = this.data._sickroomData[value]
          this.setData({
            _selectedSickroomId: element.id,
            _selectedSickAreaId: null,
            _selectedWardId: null,
            selectedBedNumber: null,
            ['_query.sickroomId']: element.id,
            ['_query.sickAreaId']: null
          })
          this.getSickAreaList()
        }
        this.data.multiIndex[1] = 0
        this.data.multiIndex[2] = 0
        break
      case 1:
        if (this.data._sickAreaData.length > value) {
          const element = this.data._sickAreaData[value]
          this.setData({
            _selectedSickAreaId: element.id,
            _selectedWardId: null,
            selectedBedNumber: null,
            ['_query.sickAreaId']: element.id
          })
          this.getWardList()
        }
        this.data.multiIndex[2] = 0
        break
      case 2:
        if (this.data._wardData.length > value) {
          const element = this.data._wardData[value]
          this.data._selectedWardId = element.id
          this.setData({
            selectedBedNumber: null,
            bedData: this.getBedData(element.bedCount)
          })
        }
        break
    }
  },

  onFinish() {
    if (!this.data.selectedBedNumber) {
      wx.showToast({
        icon: "none",
        title: "请选择病床号",
        duration: 2000
      })
      return
    }
    wx.removeStorageSync('hospital-info')
    const { _selectedSickroomId, _selectedSickAreaId, _selectedWardId, selectedBedNumber } = this.data
    const query = {
      sickroomId: _selectedSickroomId,
      // 该字段我们后端定义的是sickareaId,所以需要把sickAreaId改成sickareaId
      sickareaId: _selectedSickAreaId,
      bingfangId: _selectedWardId,
      bedNumber: selectedBedNumber
    }
    app.initLoad(query)
    wx.reLaunch({
      url: '/pages/home/index'
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})