<script lang="ts" setup>
import {
  computed,
  ref,
  onMounted,
  watch,
  reactive,
  onBeforeMount,
  getCurrentInstance,
  h
} from 'vue'
import { onBeforeRouteUpdate } from 'vue-router'
import { useDialogueStore, useTalkStore } from '@/store'
import {
  NDropdown,
  NIcon,
  NInput,
  NPopover,
  NTabs,
  NTab,
  NCard,
  NButton,
  NPagination
} from 'naive-ui'
import { Search, Plus } from '@icon-park/vue-next'
import TalkItem from './TalkItem.vue'
import Skeleton from './Skeleton.vue'
import { ServeClearTalkUnreadNum } from '@/api/chat'
import GroupLaunch from '@/components/group/GroupLaunch.vue'
import { getCacheIndexName } from '@/utils/talk'
import { ISession } from '@/types/chat'
import { useSessionMenu } from '@/hooks'
import customModal from '@/components/common/customModal.vue'
import xSearchForm from '@/components/x-naive-ui/x-search-form/index.vue'
import xNDataTable from '@/components/x-naive-ui/x-n-data-table/index.vue'
import flTree from '@/components/flnlayout/tree/flnindex.vue'
import { processError, processSuccess } from '@/utils/helper/message.js'
import chatAppSearchList from '@/components/search/searchList.vue'

const currentInstance = getCurrentInstance()
const $request = currentInstance?.appContext.config.globalProperties?.$request

const {
  dropdown,
  onContextMenuTalkHandle,
  onContextMenu: onContextMenuTalk,
  onCloseContextMenu,
  onToTopTalk
} = useSessionMenu()

const dialogueStore = useDialogueStore()
const talkStore = useTalkStore()
const isShowGroup = ref(false)
const searchKeyword = ref('')
const topItems = computed((): ISession[] => talkStore.topItems)
const unreadNum = computed(() => talkStore.talkUnreadNum)

//自定义搜索
const renderChatAppSearch = () => {
  return h(
    chatAppSearchList,
    {
      // searchResultKey: 'user_infos',
      // searchItem: {
      //   avatar:
      //     'https://e-cdn.fontree.cn/fonchain-main/prod/image/18248/avatar/a0b2bee7-947f-465a-986e-10a1b2b87032.png',
      //   created_at: '2025-03-27 14:44:23',
      //   erp_user_id: 18248,
      //   id: 44,
      //   mobile: '18994430450',
      //   nickname: '周俊耀'
      // },
      // searchText: '周'
      searchResultPageSize: 3,
      listLimit: true,
      apiRequest: ServeSeachQueryAll,
      searchText: '王',
      onClickSearchItem: (searchText, searchResultKey, talk_type, receiver_id, res) => {
        console.log(searchText, searchResultKey, talk_type, receiver_id)
        const result = JSON.parse(decodeURIComponent(res))
        console.log(result)
      }
    },
    {}
  )
}

//ES搜索聊天记录-主页搜索什么都有、指定用户、指定群、群与用户概览
const ServeSeachQueryAll = () => {
  let url = '/api/v1/elasticsearch/query-all'
  let params = {}
  let config = {
    baseURL: import.meta.env.VITE_BASE_API
  }
  return $request.HTTP.components.postDataByParams(url, params, config).then((res) => {
    console.log(res)
  })
}

const state = reactive({
  isShowAddressBookModal: false, // 是否显示通讯录模态框
  customModalStyle: {
    width: '1288px',
    height: '846px',
    backgroundColor: '#F9F9FD'
  }, //自定义模态框样式
  addressBookSearchConfig: [
    {
      label: '姓名',
      key: 'nickName',
      type: 'input',
      valueType: 'string'
    }
  ], // 通讯录搜索配置
  groupChatListSearchConfig: [
    {
      label: '群聊名称',
      key: 'groupName',
      type: 'input',
      valueType: 'string'
    }
  ], // 群聊列表搜索配置
  treeData: [],
  expandedKeys: [],
  clickKey: 3,
  treeRefreshCount: 0,
  treeSelectData: {},
  addressBookColumns: [
    {
      title: '姓名 【工号】',
      field: 'nickName',
      width: 200,
      render(row, index) {
        return row.nickName + '【' + row.jobNum + '】'
      }
    },
    {
      title: '岗位名称',
      field: 'positionName',
      width: 400,
      ellipsis: true,
      render(row, index) {
        let positionNames = Array.isArray(row.depPositions)
          ? row.depPositions.flatMap((dep) =>
              Array.isArray(dep.positions) ? dep.positions.map((pos) => pos.name) : []
            )
          : []
        return positionNames.join(' , ')
      }
    },
    {
      title: '操作',
      field: 'action',
      width: 200,
      align: 'center',
      fixed: 'right',
      render(row, index) {
        return h(
          NButton,
          {
            size: 'small',
            text: true,
            color: '#46299d',
            onClick: () => handleEnterChat(row)
          },
          { default: () => '进入聊天' }
        )
      }
    }
  ], // 通讯录表格列
  groupChatListColumns: [
    {
      title: '群聊名称',
      field: 'groupName',
      width: 200,
      render(row, index) {
        return row.groupName
      }
    },
    {
      title: '群类型',
      field: 'groupType',
      width: 400,
      ellipsis: true,
      render(row, index) {
        return row.groupType
      }
    },
    {
      title: '操作',
      field: 'action',
      width: 200,
      align: 'center',
      fixed: 'right',
      render(row, index) {
        return h(
          NButton,
          {
            size: 'small',
            text: true,
            color: '#46299d',
            onClick: () => handleEnterChat(row)
          },
          { default: () => '进入聊天' }
        )
      }
    }
  ], // 群聊列表表格列
  addressBookData: [], // 通讯录表格数据
  groupChatListData: [], // 群聊列表表格数据
  addressBookTableHeight: 524, // 通讯录表格高度
  addressBookTableWidth: 800, // 通讯录表格宽度
  addressBookPage: 1, // 通讯录表格页码
  addressBookPageSize: 10, // 通讯录表格每页条数
  addressBookTotal: 0, // 通讯录表格总条数
  addressBookSearchNickName: '', // 通讯录搜索条件-姓名
  addressBookCurrentTab: 'employeeAddressBook', // 通讯录当前tab
  groupChatListPage: 1, // 群聊列表表格页码
  groupChatListPageSize: 10, // 群聊列表表格每页条数
  groupChatListTotal: 0, // 群聊列表表格总条数
  groupChatListSearchGroupName: '', // 群聊列表搜索条件-群聊名称
  chatSearchOptions: [
    {
      key: 'chatSearch',
      type: 'render',
      render: renderChatAppSearch
    }
  ] // 聊天搜索选项
})

const items = computed((): ISession[] => {
  if (searchKeyword.value.length === 0) {
    return talkStore.talkItems
  }

  return talkStore.talkItems.filter((item: ISession) => {
    let keyword = item.remark || item.name

    return keyword.toLowerCase().indexOf(searchKeyword.value.toLowerCase()) != -1
  })
})
setTimeout(() => {
  console.log('items.value', items.value)
}, 1000)
watch(
  () => talkStore,
  (newValue, oldValue) => {
    console.log(newValue)
  },
  { deep: true, immediate: true }
)
watch(
  () => state.addressBookSearchNickName,
  (newValue, oldValue) => {
    // console.log(newValue, 'newValue')
    if (newValue) {
      state.addressBookTableWidth = 1142
      state.addressBookPage = 1
    } else {
      state.addressBookTableWidth = 800
      state.clickKey = 3
      state.treeRefreshCount++
    }
    getDepPoisUser()
  }
)

// 列表加载状态
const loadStatus = computed(() => talkStore.loadStatus)

// 当前会话索引
const indexName = computed(() => dialogueStore.index_name)

// 切换会话
const onTabTalk = (item: ISession, follow = false) => {
  if (item.index_name === indexName.value) return

  searchKeyword.value = ''

  // 更新编辑信息
  dialogueStore.setDialogue(item)

  // 清空消息未读数
  if (item.unread_num > 0) {
    ServeClearTalkUnreadNum({
      talk_type: item.talk_type,
      receiver_id: item.receiver_id
    }).then(() => {
      talkStore.updateItem({
        index_name: item.index_name,
        unread_num: 0
      })
    })
  }

  // 设置滚动条跟随
  if (follow) {
    const el = document.getElementById('talk-session-list')
    if (el) {
      let index = talkStore.findTalkIndex(item.index_name)

      el.scrollTo({
        top: index * 66 + index * 5,
        behavior: 'smooth'
      })
    }
  }
}

const onReload = () => {
  talkStore.loadTalkList()
}

// 初始化加载
const onInitialize = () => {
  let index_name = getCacheIndexName()

  index_name && onTabTalk(talkStore.findItem(index_name), true)
}

// 路由更新事件
onBeforeRouteUpdate(onInitialize)

onBeforeMount(() => {
  getTreeData()
})

onMounted(() => {
  onInitialize()
})

// 点击显示通讯录模态框
const showAddressBookModal = () => {
  state.isShowAddressBookModal = true
}
const handleTreeClick = ({ selectedKey, tree }) => {
  // console.log(tree)
  state.clickKey = tree.key
  state.treeSelectData = tree
  state.addressBookPage = 1
  getDepPoisUser()
}
const calcTreeData = (data) => {
  for (let item of data) {
    item.key = item.ID
    item.label = item.name
    item.title = item.name
    if (item.sons) {
      item.children = item.sons
      calcTreeData(item.children)
    }
    delete item.ID
    delete item.name
    delete item.sons
  }
}
// 获取组织树数据
const getTreeData = () => {
  let url = '/department/v2/tree/filter'
  let params = {}
  $request.HTTP.components.postDataByParams(url, params).then(
    (res) => {
      if (res.status === 0 && Array.isArray(res.data.nodes)) {
        let data = res.data.nodes
        calcTreeData(data)
        state.treeData = data
        state.treeRefreshCount++
        getDepPoisUser()
      } else {
        processError(res.msg || '获取失败！')
      }
    },
    () => {
      processError('获取失败！')
    },
    () => {
      processError('获取失败！')
    }
  )
}
// 获取部门下的人员
const getDepPoisUser = () => {
  let url = '/user/v2/list'
  let params = {
    departmentId: state.addressBookSearchNickName ? undefined : state.clickKey,
    page: state.addressBookPage,
    pageSize: state.addressBookPageSize,
    status: 'notactive',
    nickName: state.addressBookSearchNickName
  }
  $request.HTTP.components.postDataByParams(url, params).then((res) => {
    // console.log(res)
    if (res.status === 0 && Array.isArray(res.data.data)) {
      state.addressBookData = res.data.data || []
      state.addressBookTotal = res.data.count
    }
  })
}
//点击进入对应的聊天
const handleEnterChat = (row) => {
  console.log(row)
}
//处理页数变化
const handleAddressBookPagination = (page) => {
  state.addressBookPage = page
  getDepPoisUser()
}
//处理每页条数变化
const handleAddressBookPaginationSize = (pageSize) => {
  state.addressBookPageSize = pageSize
  state.addressBookPage = 1
  getDepPoisUser()
}
//处理通讯录搜索
const changeAddressBookSearch = (value) => {
  if (!value.nickName?.trim()) {
    state.addressBookSearchNickName = ''
  } else {
    state.addressBookSearchNickName = value.nickName
  }
}
//处理通讯录tab切换
const handleAddressBookTabChange = (value) => {
  console.log(value, 'value')
  state.addressBookCurrentTab = value
}
//处理群聊列表搜索
const changeGroupChatListSearch = (value) => {
  console.log(value, 'value')
}
//处理群聊列表页数变化
const handleGroupChatListPagination = (value) => {
  console.log(value, 'value')
  state.groupChatListPage = value
}
//处理群聊列表每页条数变化
const handleGroupChatListPaginationSize = (value) => {
  console.log(value, 'value')
  state.groupChatListPageSize = value
}
</script>

<template>
  <!-- 右键菜单 -->
  <n-dropdown
    class="dropdown-menus"
    :show="dropdown.show"
    :x="dropdown.x"
    :y="dropdown.y"
    :options="dropdown.options"
    @select="onContextMenuTalkHandle"
    @clickoutside="onCloseContextMenu"
  />

  <section class="el-container is-vertical height100">
    <!-- 工具栏目 -->
    <header class="el-header header-tools">
      <n-dropdown
        trigger="click"
        :options="state.chatSearchOptions"
        style="width: 248px; height: 677px; overflow-y: scroll;"
      >
        <n-input
          placeholder="搜索好友 / 群聊"
          v-model:value.trim="searchKeyword"
          round
          clearable
          style="width: 78%;"
        >
          <template #prefix>
            <n-icon :component="Search" />
          </template>
        </n-input>
      </n-dropdown>
      <n-button circle @click="isShowGroup = true">
        <template #icon>
          <n-icon :component="Plus" />
        </template>
      </n-button>
      <img
        style="width: 19px; height: 20px; cursor: pointer;"
        src="@/assets/image/chatList/addressBook.png"
        alt=""
        @click="showAddressBookModal"
      />
    </header>

    <!-- 置顶栏目 -->
    <!-- <header class="el-header header-top" v-show="loadStatus == 3 && topItems.length > 0">
      <n-popover v-for="item in topItems" :key="item.index_name" placement="bottom" trigger="hover">
        <template #trigger>
          <div
            class="top-item pointer"
            @click="onTabTalk(item, true)"
            :class="{
              active: item.index_name == indexName
            }"
          >
            <im-avatar :src="item.avatar" :size="34" :username="item.name" />

            <span class="icon-mark robot" v-show="item.is_robot == 1"> 助 </span>

            <span class="icon-mark group" v-show="item.talk_type == 2 && item.is_robot == 0">
              群
            </span>

            <span class="text">{{ item.remark || item.name }}</span>
          </div>
        </template>
        <span> {{ item.remark || item.name }} </span>
      </n-popover>
    </header> -->

    <!-- 标题栏目 -->
    <!-- <header
      v-show="loadStatus == 3 && talkStore.talkItems.length > 0"
      class="el-header header-badge"
      :class="{ shadow: false }"
    >
      <p>会话记录({{ talkStore.talkItems.length }})</p>
      <p>
        <span class="badge unread" v-show="unreadNum">{{ unreadNum }}未读</span>
      </p>
    </header> -->

    <main id="talk-session-list" class="el-main me-scrollbar me-scrollbar-thumb">
      <template v-if="loadStatus == 2"><Skeleton /></template>
      <template v-else>
        <TalkItem
          v-for="item in items"
          :key="item.index_name"
          :data="item"
          :avatar="item.avatar"
          :username="item.remark || item.name"
          :active="item.index_name == indexName"
          @tab-talk="onTabTalk"
          @top-talk="onToTopTalk"
          @contextmenu.prevent="onContextMenuTalk($event, item)"
        />
      </template>
    </main>
  </section>

  <GroupLaunch v-if="isShowGroup" @close="isShowGroup = false" @on-submit="onReload" />

  <customModal
    v-model:show="state.isShowAddressBookModal"
    title="通讯录"
    :style="state.customModalStyle"
    :customCloseBtn="true"
    :closable="false"
  >
    <template #content>
      <div class="custom-modal-content">
        <n-card style="padding: 0 12px;">
          <n-tabs
            type="line"
            @update:value="handleAddressBookTabChange"
            tab-style="font-size: 16px; font-weight: 600;color: #8B8B8B;"
          >
            <n-tab name="employeeAddressBook">员工通讯录</n-tab>
            <n-tab name="groupChatList">群聊列表</n-tab>
          </n-tabs>
          <xSearchForm
            v-if="state.addressBookCurrentTab == 'employeeAddressBook'"
            :search-config="state.addressBookSearchConfig"
            customInputPlaceholder="请输入姓名"
            @change="changeAddressBookSearch"
            :cols="3"
          ></xSearchForm>
          <xSearchForm
            v-if="state.addressBookCurrentTab == 'groupChatList'"
            :search-config="state.groupChatListSearchConfig"
            customInputPlaceholder="请输入群聊名称"
            @change="changeGroupChatListSearch"
            :cols="3"
          ></xSearchForm>
          <div
            class="addressBook-content"
            v-if="state.addressBookCurrentTab == 'employeeAddressBook'"
          >
            <div class="addressBook-tree" v-if="!state.addressBookSearchNickName">
              <fl-tree
                :data="state.treeData"
                :expandedKeys="state.expandedKeys"
                :refreshCount="state.treeRefreshCount"
                :clickKey="state.clickKey"
                @triggerTreeClick="handleTreeClick"
              ></fl-tree>
            </div>
            <div class="addressBook-table">
              <xNDataTable
                :columns="state.addressBookColumns"
                :data="state.addressBookData"
                :style="{
                  height: `${state.addressBookTableHeight}px`,
                  width: `${state.addressBookTableWidth}px`
                }"
                flex-height
              ></xNDataTable>
              <div class="addressBook-pagination">
                <n-pagination
                  v-model:page="state.addressBookPage"
                  v-model:page-size="state.addressBookPageSize"
                  :item-count="state.addressBookTotal"
                  show-quick-jumper
                  show-size-picker
                  :page-sizes="[10, 20, 50]"
                  :on-update:page="handleAddressBookPagination"
                  :on-update:page-size="handleAddressBookPaginationSize"
                >
                  <template #prefix="{ itemCount }"> 共 {{ itemCount }} 条记录 </template>
                </n-pagination>
              </div>
            </div>
          </div>
          <div class="groupChatList-content" v-if="state.addressBookCurrentTab == 'groupChatList'">
            <div class="groupChatList-table">
              <xNDataTable
                :columns="state.groupChatListColumns"
                :data="state.groupChatListData"
                :style="{
                  height: '523px',
                  width: '1148px'
                }"
                flex-height
              ></xNDataTable>
              <div class="groupChatList-pagination">
                <n-pagination
                  v-model:page="state.groupChatListPage"
                  v-model:page-size="state.groupChatListPageSize"
                  :item-count="state.groupChatListTotal"
                  show-quick-jumper
                  show-size-picker
                  :page-sizes="[10, 20, 50]"
                  :on-update:page="handleGroupChatListPagination"
                  :on-update:page-size="handleGroupChatListPaginationSize"
                >
                  <template #prefix="{ itemCount }"> 共 {{ itemCount }} 条记录 </template>
                </n-pagination>
              </div>
            </div>
          </div>
        </n-card>
      </div>
    </template>
  </customModal>
</template>

<style lang="less" scoped>
.header-tools {
  height: 60px;
  flex-shrink: 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-around;
  padding: 0 8px;
}

.header-badge {
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 10px;

  &.shadow {
    box-shadow: 0 2px 6px 0 rgb(31 35 41 / 5%);
  }

  .unread {
    background-color: #ff4d4f;
    color: white;
    cursor: pointer;
  }
}

.header-top {
  padding: 5px 8px;
  padding-right: 0;
  padding-right: 8px;
  -webkit-justify-content: space-between;
  -ms-flex-pack: justify;
  justify-content: space-between;
  grid-gap: 0 14px;
  grid-template-columns: repeat(auto-fill, 32px);
  display: grid;
  box-sizing: border-box;

  .top-item {
    flex-basis: 46px;
    flex-shrink: 0;
    height: 56px;
    margin: 3px 2px 3px 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    position: relative;

    .icon-mark {
      position: absolute;
      height: 25px;
      width: 25px;
      font-size: 14px;
      display: flex;
      align-items: center;
      justify-content: center;
      right: -12px;
      bottom: 15px;
      transform: scale(0.6);
      border-radius: 50%;

      &.group {
        color: #3370ff;
        background-color: #e1eaff;
      }

      &.robot {
        color: #dc9b04 !important;
        background-color: #faf1d1 !important;
      }
    }
    &.active {
      .text {
        color: rgb(80 138 254);
      }
    }

    .text {
      display: inline-block;
      height: 20px;
      font-size: 12px;
      transform: scale(0.9);
      text-align: center;
      line-height: 20px;
      word-break: break-all;
      overflow: hidden;
    }
  }
}

html[theme-mode='dark'] {
  .header-badge {
    &.shadow {
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
    }
  }
}

.custom-modal-content {
  box-sizing: border-box;
  width: 100%;
  padding: 0 12px;
  :deep(.n-tabs-tab--active) {
    color: #46299d !important;
  }
  .addressBook-content {
    display: flex;
    flex-direction: row;
    gap: 20px;
    .addressBook-tree {
      width: 328px;
      height: 524px;
      overflow: auto;
      border: 1px solid #efeff5;
      border-radius: 4px;
      padding: 12px 20px;
      box-sizing: border-box;
    }
    .addressBook-table {
      :deep(.n-data-table-th) {
        background-color: #46299d;
        color: #fff;
      }
      .addressBook-pagination {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding: 22px 0 0;
        box-sizing: border-box;
      }
    }
  }
  .groupChatList-content {
    display: flex;
    flex-direction: row;
    gap: 20px;
    .groupChatList-table {
      :deep(.n-data-table-th) {
        background-color: #46299d;
        color: #fff;
      }
      .groupChatList-pagination {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding: 22px 0 0;
        box-sizing: border-box;
      }
    }
  }
}
</style>
