<template>
  <u-popup :show="show" mode="right" :closeable="true" @close="handleClose">
    <view class="c-dialog-filter">
      <view class="title">品牌筛选</view>
      <view class="search">
        <u-search
          :placeholder="`请输入品牌搜索`"
          v-model="keyword"
          @search="handleSearch()"
          @clear="handleSearch()"
        ></u-search>
      </view>
      <view class="dialog-product-layout">
        <!-- 自定义索引列表 -->
        <view class="address-book-container">
          <!-- 左侧通讯录 -->
          <scroll-view
            class="scroll-container"
            :scroll-y="true"
            :scroll-into-view="toView"
            :scroll-with-animation="true"
          >
            <view
              class="address-book"
              v-for="(item, index) in addressBook"
              :key="index"
              :id="item.name"
            >
              <view class="address-book-index">{{ item.name }}</view>
              <view
                class="contact-container"
                v-for="(cItem, index) in item.list"
                :key="index"
              >
                <!-- <img
                  class="contact-img"
                  src="http://www.lixia.gov.cn/picture/0/s_97b76c734a6f40f8abba95615cbff1e1.jpg"
                  alt=""
                /> -->
                <view
                  class="contact-detail-container"
                  @click="handleClick(cItem)"
                >
                  <view class="contact-name">{{ cItem.brandName }}</view>
                  <view class="contact-address">
                    <u-icon
                      v-if="checkMap[cItem.id] && checkMap[cItem.id].check"
                      name="checkbox-mark"
                    ></u-icon
                  ></view>
                  <!-- <view class="contact-phone">{{ item.phone }}</view> -->
                </view>
              </view>
            </view>
          </scroll-view>

          <!-- 右侧字母导航条 -->
          <view class="letter-nav">
            <view
              class="item"
              :class="{ active: toView === item }"
              v-for="(item, index) in indexList"
              :key="index"
              @click="toSelectIndex(item)"
              >{{ item }}</view
            >
          </view>
        </view>
      </view>

      <view class="btn-box-fix-btn">
        <view
          ><u-button type="primary" shape="circle" @click="handleOk"
            >确定</u-button
          ></view
        >
      </view>
    </view>
  </u-popup>
</template>
<script setup lang="ts">
import { VehicleApi } from "@/services";

const props = defineProps<{
  show: boolean;
}>();
const emit = defineEmits(["handleDialog", "handleChange"]);
const handleClose = () => {
  emit("handleDialog", false);
};

const keyword = ref("");

const checkMap = ref<any>({});

const indexList = [
  "A",
  "B",
  "C",
  "D",
  "E",
  "F",
  "G",
  "H",
  "I",
  "J",
  "K",
  "L",
  "M",
  "N",
  "O",
  "P",
  "Q",
  "R",
  "S",
  "T",
  "U",
  "V",
  "W",
  "X",
  "Y",
  "Z",
  "#",
];
const toView = ref("");
const addressBook = ref<any>([
  // {
  //   name: "A",
  //   list: [
  //     { id: 180, name: "a" },
  //     { id: 181, name: "aa" },
  //   ],
  // },
]);
// [
//   {
//     id: "A",
//     data: [
//       {
//         zh_title: "阿联酋迪拉姆",
//         en_title: "aa",
//         address: "910289591",
//         phone: "111111",
//       },
//       {
//         zh_title: "阿尔巴尼亚列克",
//         en_title: "aaaaa",
//         address: "ALL",
//         phone: "222222",
//       },
//     ],
//   }
// ];

const toSelectIndex = (item: any) => {
  toView.value = item;
};

const handleClick = (item: any) => {
  if (checkMap.value[item.id]) {
    checkMap.value[item.id].check = !checkMap.value[item.id].check;
    if (!checkMap.value[item.id].check) {
      delete checkMap.value[item.id];
    }
  } else {
    checkMap.value[item.id] = { ...item, check: true };
  }
  console.log(checkMap.value);
};

const handleSearch = () => {
  getList();
};

const getList = () => {
  VehicleApi.getBrand({ name: keyword.value }).then((res: any) => {
    if (res.code === 200) {
      addressBook.value = res.data.reduce((pre: any, curr: any) => {
        if (curr.list.length > 0) {
          pre.push(curr);
        }
        return pre;
      }, []);
    }
  });
};

const handleOk = () => {
  if (Object.values(checkMap.value).length <= 5) {
    emit("handleChange", Object.values(checkMap.value));
    emit("handleDialog", false);
  } else {
    uni.showToast({
      title: '最多选择5个品牌',
      icon: 'none',
      mask: true
    })
  }
};

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.c-dialog-filter {
  width: 95vw;
  padding: 25rpx;
  .title {
    font-weight: 500;
    font-size: 32rpx;
    color: #000000;
    text-align: center;
  }
  .search {
    margin: 30rpx 0px;
  }
}
.dialog-product-layout {
  height: 80vh;
  .address-book-container {
    height: 100%;
  }
  .address-book-index {
    font-size: 24rpx;
  }
  .contact-img {
    width: 20px;
    height: 20px;
  }
  .scroll-container {
    height: 100%;
  }
  .letter-nav {
    position: fixed;
    right: 25rpx;
    top: 100px;
    font-size: 22rpx;
    text-align: center;
    .item:hover,
    .active {
      color: $u-primary;
    }
  }
  .contact-container {
    display: flex;
    align-items: center;
    margin: 2%;
  }
  .contact-detail-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 80%;
    font-size: 24rpx;
    line-height: 40rpx;
    .contact-address {
      color: rgba(0, 0, 0, 0.65);
    }
  }
}
.btn-box-fix-btn {
  justify-content: center;
  view {
    width: 70%;
  }
}
</style>
