<template>
 	<view class="agentCheck">
    <u-dropdown class="up-dropdown">
      <u-dropdown-item v-model="agentId" @change="changeAgentId" :title="agentName" :options="agentList"></u-dropdown-item>
    </u-dropdown>
  </view>
</template>
<script setup>
import { ref, computed } from "vue";
import { useAppStore } from "@/stores/app";
import { getAgentList } from "@/api/home.js";
import { onLoad } from '@dcloudio/uni-app'
const appStore = useAppStore();
const agentId = ref('');
const agentList = ref([]);
defineExpose({
  initAgentId,
  agentId
});

//计算属性
const agentName = computed(()=>{
  return agentList.value.find(item=>item.value == agentId.value)?.label || '';
})
onLoad(()=>{
  // agentListFn();
});
async function agentListFn(){
	await getAgentList().then(res=>{
		if(res.code == 200){
      let rows = res.rows || [];
			agentList.value = rows.map(item=>{
        return {
          label: item.agentName,
          value: item.agentId
        }
      });
		}
	})
}
async function initAgentId(){
  await agentListFn();
  console.log('initAgentId',agentList.value,appStore.agentId);
  agentId.value = appStore.agentId || agentList.value[0].value;
  appStore.agentId = agentId.value;
}
function changeAgentId(){
  console.log('changeAgentId',agentId.value);
  appStore.agentId = agentId.value;
  appStore.sessionId = '';
}

</script>
<style lang="scss" scoped>
.agentCheck{
  width: 100vw;
  position: relative;
  z-index: 20;
}
</style>
