import { watch, ref } from "vue"
import { getApi } from "@/api/tobAcco_api.js"

export const useCommonData = (form, props) => {
  // 场所列表
  const csmclist = ref([])
  // 建筑物列表
  const jzwlist = ref([])
  // 部位列表
  const bwlist = ref([])
  // 楼层列表
  const lclist = ref([])

  //根据操作人 查询 场所
  function getcsData() {
    getApi({}, "/mosty-jcgl/site/getSiteListByUser").then(res => {
      csmclist.value = res
    })
  }

  //获取场所选择
  const cahngecsmc = val => {
    const currentItem = csmclist.value.find(item => item.id == val)
    if (!currentItem) return
    form.value.sitename = item.placename
    form.value.buildingname = ""
    form.value.buildingid = ""
    form.value.publicarea = ""
    form.value.floorname = ""
    form.value.floorid = ""
    form.value.partname = ""
    form.value.partid = ""
    getjzwlist(item.id)
  }

  //	根据placeid，查询建筑物
  function getjzwlist(placeid) {
    getApi({ placeid }, "/mosty-jcgl/building/queryBuildingByPlaceid").then(res => {
      jzwlist.value = res ? res : []
    })
  }

  //(新)根据建筑物id查询部位
  function getbw2data(id) {
    getApi({ id }, "/mosty-jcgl/part/getListByBuildid").then(res => {
      bwlist.value = res ? res : []
    })
  }
  //根据建筑物查询楼层
  function getlcdata(id) {
    getApi({ id }, "/mosty-jcgl/floor/queryFloorByBuildId").then(res => {
      lclist.value = res ? res : []
    })
  }

  //获取建筑物名称
  function cahngejzwmc(val) {
    const currentItem = jzwlist.value.find(item => item.id == val)
    if (!currentItem) {
      form.value.buildingid = ""
      form.value.publicarea = ""
      return
    }
    form.value.buildingid = currentItem.id
    form.value.publicarea = currentItem.publicarea
    form.value.floorid = ""
    form.value.partid = ""
    // form.value.buildingname = currentItem.buildingname
    // form.value.floorname = ""
    // form.value.partname = ""
    if (currentItem.publicarea == "1") {
      getbw2data(currentItem.id)
    } else {
      getlcdata(currentItem.id)
    }
  }

  //获取楼层名称
  function cahngelcmc(val) {
    const currentItem = lclist.value.find(item => item.id == val)
    if (!currentItem) {
      form.value.floorid = ""
      form.value.floorname = ""
      return
    }
    form.value.partname = ""
    form.value.partid = ""
    getbwdata(currentItem.id)
    // form.value.floorid = item.id
    // form.value.floorname = item.floorname
    // lclist.value.forEach((item) => {
    //   if (item.id == val) {
    //     form.value.floorid = item.id
    //     form.value.floorname = item.floorname
    //     form.value.partname = ""
    //     form.value.partid = ""
    //     getbwdata(item.id)
    //   }
    // })
  }

  // (新)根据楼层id查询部位
  function getbwdata(id) {
    getApi({ id }, "/mosty-jcgl/part/getListByFloorid").then(res => {
      bwlist.value = res ? res : []
    })
  }

  //获取当前部位
  const cahngebw = val => {
    // bwlist.value.forEach((item) => {
    //   if (item.id == val) {
    //     FromData.value.partid = item.id
    //     FromData.value.partname = item.partname
    //   }
    // })
  }

  getcsData()

  watch(
    () => props.placeName,
    () => {
      form.value.placeName = props.placeName
      form.value.placeid = props.placeid
      if (props.placeid) {
        getjzwlist(props.placeid)
      }
    },
    { immediate: true }
  )

  return { csmclist, jzwlist, lclist, bwlist, getcsData, cahngecsmc, cahngejzwmc, cahngelcmc, cahngebw }
}
