<template>
  <u-popup :show="show" mode="right" :closeable="true" @close="handleClose">
    <view class="c-dialog-filter">
      <view class="title">{{isShipment? '客户' : '供应商'}}筛选</view>
      <view class="search">
        <u-search
          :placeholder="`请输入${isShipment? '客户' : '供应商'}名称 / 卡号搜索`"
          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.name }}</view>
                  <view class="contact-address">{{ cItem.cardCode }}</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>
  </u-popup>
</template>
<script setup lang="ts">
import { CustomerApi, SupplierApi } from "@/services";

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

const keyword = ref("");

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();
// [
//   {
//     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) => {
  emit("changeUser", item);
  emit("handleDialog", false);
};

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

const getList = () => {
  if (props.isShipment) {
    CustomerApi.getCustomUserListLettera({ 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;
          }, []);
        }
      }
    );
  } else {
    SupplierApi.getSupplierUserListLettera({ 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;
          }, []);
        }
      }
    );
  }
};

watch(
  () => props.isShipment,
  (newValue, oldValue) => {
    getList();
  }
);
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: 22rpx;
    .contact-address {
      color: rgba(0, 0, 0, 0.65);
    }
  }
}
</style>
