<!-- @format -->

<script setup>
import dayjs from 'dayjs'
import { useMessage } from './useMessage'
import { deleteContactApi } from '@/api/model/message'

const { contacts, selectContact, getContactList, selectedContact, handleClick }
  = useMessage()
const activeName = ref('all')
function onSelectContact(item) {
  selectContact(item)
}
async function deleteContact(item) {
  try {
    await deleteContactApi({
      sessionId: item.sessionId,
    })
    await getContactList()
    ElMessage.success('删除成功')
  }
  catch (error) {
    console.log(error)
  }
}
</script>

<template>
  <div class="bg-#FAFAFA ">
    <el-tabs v-model="activeName" class="contact-list-tabs" @tab-click="handleClick">
      <el-tab-pane label="All" name="all" />
      <el-tab-pane label="Unread" name="unread" />
    </el-tabs>
    <div class="overflow-y-auto h-800px">
      <div
        v-for="item in contacts"
        :key="item.sessionId"
        class="py-12px px-20px hover:bg-#fff cursor-pointer"
        :class="{ '!bg-#fff': selectedContact.sessionId === item.sessionId }"
        @click="onSelectContact(item)"
      >
        <div>
          <div class="flex  items-center">
            <div class="flex items-center">
              <div class="mr-20px">
                <el-avatar
                  v-if="item.sessionType === '2'"
                  :src="item.brandInfo?.brandLogo"
                  class="!w-48px !h-48px"
                />
                <img
                  v-if="item.sessionType === '1'"
                  src="@/assets/images/avatar.png"
                  class="b-rd-50% w-48px h-48px"
                >
              </div>
            </div>
            <div class="flex-1">
              <div class="flex justify-between items-center">
                <div class="fw-600 text-16px text-#333">
                  {{
                    item.sessionType === "2"
                      ? item.brandInfo?.brandName
                      : "客服消息"
                  }}
                </div>
                <div class="text-12px text-#999">
                  {{
                    dayjs(item.lastMessage?.sendTime).format("HH:mm")
                  }}
                </div>
              </div>
              <div class="text-14px text-#999 mt-4px line-clamp-1 lh-18px">
                {{ item.lastMessage?.content }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang='less' scoped>
.contact-list-tabs {
  background-color: #FAFAFA;
  border-radius: 10px;
  padding: 30px;
  padding-top: 0px;
  padding-bottom: 0px;
  ::v-deep(.el-tabs__header){
    .el-tabs__item{
      color: #333;
      font-size: 16px;
      font-weight: 500!important;
      padding-right: 10px;
      padding-left: 10px;
      &.is-active {
        color: #C58C64!important;
      }
    }
    .el-tabs__active-bar{
      background-color: #C58C64!important;
    }
  }
}
</style>
