<template>
  <div class="conversation-new">
    <div class="header-bar">
      <span class="label">企微主体：</span>
      <el-select
        v-model="selectedCorpId"
        placeholder="请选择企微主体"
        clearable
        filterable
        @change="handleCorpChange"
        style="width: 300px"
      >
        <el-option
          v-for="corp in corpList"
          :key="corp.corpId"
          :label="corp.corpName"
          :value="corp.corpId"
        />
      </el-select>
      <div class="tips" v-if="!selectedCorpId">请先选择企微主体，才能使用会话功能</div>
    </div>

    <el-tabs v-model="activeTab" type="border-card" v-if="selectedCorpId">
      <el-tab-pane label="按员工查询" name="employee">
        <!-- 传递完整的 corp 对象 -->
        <EmployeeQueryTab :corp="currentCorp" />
      </el-tab-pane>
      <el-tab-pane label="按聊天内容查询" name="content">
        <ContentSearchTab :corpId="selectedCorpId" />
      </el-tab-pane>
    </el-tabs>
    <div v-else class="empty-tip">请先选择企业微信主体</div>
  </div>
</template>

<script>
import { allCorp } from "@/api/qw/qwCompany";
import EmployeeQueryTab from "./EmployeeQueryTab.vue";
import ContentSearchTab from "./ContentSearchTab.vue";

export default {
  name: "ConversationNew",
  components: { EmployeeQueryTab, ContentSearchTab },
  data() {
    return {
      activeTab: "employee",
      selectedCorpId: "",
      corpList: [],
    };
  },
  computed: {
    currentCorp() {
      return this.corpList.find(corp => corp.corpId === this.selectedCorpId) || null;
    }
  },
  created() {
    this.fetchCorpList();
  },
  methods: {
    async fetchCorpList() {
      try {
        const res = await allCorp();
        this.corpList = res.data || [];
        if (this.corpList.length > 0) {
          this.selectedCorpId = this.corpList[0].corpId;
        }
      } catch (e) {
        console.error("获取企业主体列表失败", e);
      }
    },
    handleCorpChange() {
      // 子组件会通过 watch 监听 corp 变化
    },
  },
};
</script>

<style scoped>
.conversation-new {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 16px;
  background: #f0f2f5;
  box-sizing: border-box;
}
.header-bar {
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.label {
  font-size: 14px;
  color: #606266;
}
.tips {
  color: #909399;
  font-size: 14px;
}
.empty-tip {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 16px;
}
</style>
