<template>
  <view class="pop-upNotification" :style="{ width: width }">
    <view class="left-classification">
      <view @tap="choice(0)" class="option" :class="light2 === 0 ? 'light2' : ''">推荐</view>
      <view @tap="choice(1)" class="option" :class="light2 === 1 ? 'light2' : ''">行政区</view>
    </view>
    <view class="right-details" v-show="light2 === 0">
      <view class="title-box">
        <text class="nearby-title">我的附近</text>
        <wd-icon
          @tap="refresh"
          class="icon"
          :class="{ rotate: reset }"
          name="refresh"
          size="12px"
        ></wd-icon>
      </view>
      <view class="nearby-card">
        <view @tap="choice2(0, 0)" class="label" :class="light3 === 0 ? 'light3' : ''">附近</view>
        <view @tap="choice2(1, 0.5)" class="label" :class="light3 === 1 ? 'light3' : ''">500m</view>
        <view @tap="choice2(2, 1)" class="label" :class="light3 === 2 ? 'light3' : ''">1km</view>
        <view @tap="choice2(3, 3)" class="label" :class="light3 === 3 ? 'light3' : ''">3km</view>
        <view @tap="choice2(4, 5)" class="label" :class="light3 === 4 ? 'light3' : ''">5km</view>
        <view @tap="choice2(5, 10)" class="label" :class="light3 === 5 ? 'light3' : ''">10km</view>
      </view>
      <text class="nearby-title2 margin">热门地点</text>
      <ul class="ul">
        <li
          v-for="(item, index) in items"
          :key="index"
          :class="{ active: selectedItem === index }"
          @tap="selectItem(index, item)"
        >
          {{ item }}
        </li>
      </ul>
    </view>
    <view class="right-details2" v-show="light2 === 1">
      <ul class="ul">
        <li
          v-for="(item, index) in items1"
          :key="index"
          :class="{ active: selectedItem1 === index }"
          @tap="selectItem1(index)"
        >
          {{ item.name }}
        </li>
      </ul>
      <ul class="ul">
        <li
          v-for="(item, index) in items2"
          :key="index"
          :class="{ active: selectedItem2 === index }"
          @tap="selectItem2(index, item.name)"
        >
          {{ item.name }}
        </li>
      </ul>
    </view>
  </view>
</template>

<script setup>
import { defineProps } from 'vue';
import { getRegion, administration } from '@/api/index';
const props = defineProps({
  width: {
    type: String,
    default: '100%',
  },
});
const reset = ref(false);
const emit = defineEmits(['nearby', 'popular', 'region', 'refresh']);
const light2 = ref(0);
const light3 = ref(0);
const items = ref(['云岩区', '白云区', '乌当区', '花溪区', '清镇市', '开阳县', '修文县']);
const items1 = ref(['云岩区', '白云区', '乌当区', '花溪区', '清镇市', '开阳县', '修文县']);
const items2 = ref([
  '全部',
  '百花新城',
  '人民广场',
  '天水花园',
  '三角花园',
  '中环国际',
  '君悦国际',
]);

const selectedItem = ref(null);
const selectedItem1 = ref(0);
const selectedItem2 = ref(null);
onMounted(() => {
  getPlace();
  getAdministration();
});

// 重置选项
const refresh = () => {
  light3.value = 0;
  selectedItem.value = null;
  selectedItem2.value = null;
  reset.value = true;
  setTimeout(() => {
    reset.value = false;
  }, 400);
  emit('refresh');
};
// 热门地点
const getPlace = () => {
  getRegion({ city: '贵阳' }).then((res) => {
    items.value = res.data;
  });
};
// 行政区
const getAdministration = () => {
  administration({ parentId: '52' }).then((res) => {
    items1.value = res.data;
    items2.value = items1.value[0].children;
  });
};
const choice = (index) => {
  light2.value = index;
};
const choice2 = (index, distance) => {
  light3.value = index;
  emit('nearby', distance);
};
const selectItem = (index, communityName) => {
  selectedItem.value = index;
  selectedItem2.value = null;
  emit('popular', communityName);
};
const selectItem1 = (index) => {
  selectedItem1.value = index;
  items2.value = items1.value[index].children;
};
const selectItem2 = (index, area) => {
  selectedItem2.value = index;
  selectedItem.value = null;
  emit('region', area);
};
</script>

<style lang="scss" scoped>
.pop-upNotification {
  position: absolute;
  top: 100%;
  // width: 710rpx;
  height: 780rpx;
  border-radius: 0rpx 0rpx 16rpx 16rpx;
  background: #ffffff;
  box-shadow: 0rpx 16rpx 16rpx 0rpx rgba(73, 73, 73, 0.14);
  margin: 0 auto;
  z-index: 99;
  display: flex;
  .left-classification {
    width: 176rpx;
    background: #f1f1f1;
    min-height: 780rpx;
    .option {
      text-align: center;
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      font-size: 28rpx;
      font-weight: 500;
      color: #3d3d3d;
    }
    .light2 {
      background: #fff;
      color: #fa5151;
    }
  }
  .right-details {
    width: calc(100% - 176rpx);
    height: 100%;
    .title-box {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 10rpx;
      .nearby-title {
        font-size: 24rpx;
        color: #3d3d3d;
        font-weight: 500;
        font-family: Source Han Sans;
        margin-left: 40rpx;
      }
      .icon {
        margin-right: 40rpx;
      }
      .rotate {
        animation: spin 0.3s linear;
      }
      @keyframes spin {
        from {
          transform: rotate(0deg);
        }
        to {
          transform: rotate(360deg);
        }
      }
    }
    .nearby-title2 {
      font-size: 24rpx;
      color: #3d3d3d;
      font-weight: 500;
      font-family: Source Han Sans;
      margin-left: 40rpx;
    }
    .margin {
      display: block;
      margin-top: 40rpx;
    }
    .nearby-card {
      margin-top: 20rpx;
      margin-left: 60rpx;
      width: 448rpx;
      height: 120rpx;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      .label {
        width: 136rpx;
        height: 48rpx;
        border-radius: 6rpx;
        background: #f1f1f1;
        font-size: 22rpx;
        color: #abaaaa;
        line-height: 48rpx;
        text-align: center;
        box-sizing: border-box;
      }
      .light3 {
        background: #fcdbdb;
        color: #fa5151;
      }
    }
    .ul {
      padding-left: 60rpx;
      overflow-y: scroll;
      height: 65%;
      li {
        list-style-type: none;
        font-size: 22rpx;
        color: #3d3d3d;
        margin-top: 20rpx;
      }
      .active {
        color: #fa5151;
      }
    }
  }
  .right-details2 {
    width: calc(100% - 176rpx);
    height: 100%;
    display: flex;
    .ul {
      padding-left: 60rpx;
      li {
        list-style-type: none;
        font-size: 22rpx;
        color: #3d3d3d;
        margin-top: 20rpx;
      }
      .active {
        color: #fa5151;
      }
    }
  }
}
</style>
