<template>
  <div class="fire-search-container">
    <div class="search-row">
      <!-- 火情描述输入框 -->
      <div class="search-item" v-for="(items, index) in searchShowData" :key="index">
        <label class="search-label">{{ items.label }}</label>
        <el-input v-if="items.type === 'input'" v-model="items.content" clearable :disabled="!isShow"
          :placeholder="items.placeholder" />
        <el-select v-else-if="items.type === 'select'" v-model="items.content" :placeholder="items.placeholder"
          clearable>
          <el-option v-for="building in items.options" :key="building.value" :label="building.label"
            :value="building.value" />
        </el-select>
        <el-autocomplete v-else-if="items.type === 'autocomplete'" v-model="items.content"
          :fetch-suggestions="querySearch" :trigger-on-focus="false" clearable class="inline-input w-50"
          placeholder="请输入" @input="getAllListInfoFn(items)" />
        <el-date-picker v-else v-model="items.content" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
          :default-value="defaultDateRange" @change="getAllListInfoFn" :disabled-date="disableFutureDates"
          style="max-width: 220px;" />

      </div>
    </div>
    <div class="operation-buttons" v-if="isShow">
      <!-- 操作按钮组 -->

      <div class="search-buttons">
        <el-button type="default" style="width: 45%;" @click="resetSearchForm">
          重置
        </el-button>
        <el-button type="primary" style="width: 45%;" @click="submitSearch">
          查询
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, reactive, ref, watch } from 'vue';
import { ElInput, ElSelect, ElOption, ElButton, ElMessage, dayjs } from 'element-plus';
import { buildingGetBuildCodeListApi, buildingGetBuildListByBuildNameApi, buildingGetTotalFloorNumListApi, buildingGetBuildNameListApi } from '../../../api/build'
import { floorGetFloorNoListAPI } from '../../../api/floor'
import useAppStore from '../../../store/module/app';
const useAppStoreInstance = useAppStore()
const props = defineProps(["searchShowData"])

const isShow = computed(() => {
  console.log("按钮操作显示标识", props.searchShowData)
  if (props.searchShowData.length > 0) {
    return props.searchShowData[0].isShow
  } else {
    ElMessage.error('按钮操作显示标识错误')
  }

})
const Year = dayjs(Date.now()).format('YYYY')
const Month = dayjs(Date.now()).format('MM')
const Day = dayjs(Date.now()).format('DD')
const items = ref({
  content: []
})

// 默认值设置（本月第一天到今天）
const defaultDateRange = [
  dayjs().startOf('month'), // 本月1号
  dayjs()                  // 今天
]

// 禁用未来日期
const disableFutureDates = (current) => {
  return current > dayjs().endOf('day')
}

const restaurants = ref([])
const querySearch = (queryString, cb) => {
  console.log('queryString:', restaurants.value);
  const results = queryString
    ? restaurants.value.filter(createFilter(queryString))
    : restaurants.value
  // call callback function to return suggestions
  cb(results)
}
const createFilter = (queryString) => {
  return (restaurant) => {
    // 确保 restaurant 和 restaurant.value 存在
    if (!restaurant?.value) return false;

    // 确保 queryString 是字符串
    const searchText = String(queryString || '').toLowerCase();
    const itemText = String(restaurant.value).toLowerCase();

    return itemText.includes(searchText); // 或 startsWith()
  }
}

const handleSelect = (item) => {
  console.log(item.click)
  restaurants.value = item.options
}

const getAllListInfoFn = (item) => {
  // console.log(item.click,'-*--*-*-*-*-*-')
  switch (item.click) {
    case 'buildingGetBuildListByBuildNameFn':
      buildingGetBuildListByBuildNameFn(item)
      break;
    case 'buildingGetBuildCodeListApiFn':
      buildingGetBuildCodeListApiFn(item)
      break;
    case 'buildingGetTotalFloorNumListFn':
      buildingGetTotalFloorNumListFn(item)
      break;
    case 'buildingGetBuildNameListFn':
      buildingGetBuildNameListFn(item)
      break;
    case 'floorGetFloorNoListFn':
      floorGetFloorNoListFn(item)
      break;
    default:
      console.log('没有匹配的api')
      break;
  }
}
const buildingGetBuildListByBuildNameFn = (item) => {
  // console.log(item,'buildingGetBuildCodeListFn被触发')
  buildingGetBuildListByBuildNameApi({ buildName: item.content }).then(res => {
    // console.log(res,'buildingGetBuildCodeListFn返回数据')
    if (res.code == 200) {
      restaurants.value = res.data
      // console.log(restaurants.value,'buildingGetBuildCodeListFn8999999999999返回数据')
    }
  })
};

const buildCodeTraverse = (item) => {
  let buildCodeList = []
  item.forEach(item => {
    // console.log(item,'buildCodeTraverse被触发')
    buildCodeList.push({ value: item })
  })
  return buildCodeList
}
const buildingGetBuildCodeListApiFn = (item) => {
  buildingGetBuildCodeListApi({ buildCode: item.content }).then(res => {
    // console.log(res,'buildingGetBuildCodeListFn返回数据')
    if (res.code == 200) {
      restaurants.value = buildCodeTraverse(res.data)
      // console.log(restaurants.value,'buildingGetBuildCodeListFn8999999999999返回数据')
    }
  })
};
// 返回的数据不对应当[楼层数]不是【建筑名称】
const buildingGetTotalFloorNumListFn = (item) => {
  buildingGetTotalFloorNumListApi({ totalFloorNum: item.content }).then(res => {
    console.log(res, 'buildingGetTotalFloorNumListFn返回数据')
    if (res.code == 200) {
      restaurants.value = buildCodeTraverse(res.data)
      console.log(restaurants.value, 'buildingGetTotalFloorNumListFn8999999999999返回数据')
    }
  })
}

const buildingGetBuildNameListFn = (item) => {
  buildingGetBuildNameListApi({ buildName: item.content }).then(res => {
    // console.log(res,'buildingGetBuildNameListFn返回数据')
    if (res.code == 200) {
      restaurants.value = buildCodeTraverse(res.data)
      // console.log(restaurants.value,'buildingGetBuildNameListFn8999999999999返回数据')
    }
  })
}

const floorGetFloorNoListFn = (item) => {
  floorGetFloorNoListAPI({ buildId: useAppStoreInstance.PUSHPARAMETER.buildId, floorNo: item.content }).then(res => {
    // console.log(res,'floorGetFloorNoListFn返回数据')
    if (res.code == 200) {
      restaurants.value = buildCodeTraverse(res.data)
      // console.log(restaurants.value,'floorGetFloorNoListFn8999999999999返回数据')
    }
  })
}
// 监听变化
// watch(() => props.isBuildingEditDrawer, (newVal, oldVal) => {
//   console.log('抽屉已打开，监听变化');
//   initFormData()
// }, { immediate: true });

// 提交搜索
const submitSearch = () => {
  // 搜索逻辑处理
  console.log('搜索参数:', props.searchShowData);
  // 这里可以添加API调用等实际搜索操作
};
// 定义一个重置搜索表单的函数
const resetSearchForm = () => {
  // 遍历props.searchShowData数组
  props.searchShowData.forEach(item => {
    // 将每个item的content属性置为空字符串
    item.content = ''
  })
}

</script>



<style scoped>
.fire-search-container {
  background-color: #fff;
  padding: 15px;
  border-radius: 8px;
  display: flex;
  flex-direction: row;
  gap: 15px;
  /* box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05); */
}

.search-row {
  /* background-color: #606266; */

  display: flex;
  flex: 1;
  align-items: center;
  justify-items: center;
  justify-content: space-between;
  gap: 15px;
  flex-wrap: wrap;
}

.search-item {
  /* background-color: #606266; */
  display: flex;
  align-items: center;
  flex: 1;
}


.search-label {

  width: 90px;
  margin-right: 8px;
  color: #606266;
  font-size: 14px;
}

/* 使用深度选择器覆盖Element Plus默认样式 */
:deep(.el-input__wrapper),
:deep(.el-select__wrapper) {
  border-radius: 4px;
}

.search-buttons {
  display: flex;
  gap: 10px;
  margin-left: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;

}

.operation-buttons {
  width: 20%;
  padding-left: 5%;


}

/* 响应式适配 */
@media (max-width: 768px) {
  .search-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .search-buttons {
    margin-left: 0;
    margin-top: 10px;
    width: 100%;
    justify-content: flex-end;
  }
}
</style>