<script setup>
import { Search } from '@element-plus/icons-vue'
const form = inject('form')
import { getCategoryTopicList, getCategoryList, searchCategory } from '../api'

const options = ref([])
async function fetchList() {
  const res = await getCategoryList()
  options.value = res.data.trees
}

const topicList = ref([])
async function fetchTopicList() {
  const res = await getCategoryTopicList()
  topicList.value = res.data.items
}

onMounted(() => {
  fetchList()
  fetchTopicList()
})

const keyword = ref('')

const querySearchAsync = async () => {
  const res = await searchCategory({ name: keyword.value })
  return res.data.items
}

const handleSelect = (item) => {
  form.live_commodity_type_id = item.id
}
</script>

<template>
  <div>
    <el-form-item label="选择商品类目" prop="live_commodity_type_id" :rules="[{ required: true, message: '请选择' }]">
      <div class="form-tips">请按照商品类别谨慎选择对应类目，若是错放类目将可能会导致商品封禁。</div>
      <el-autocomplete
        clearable
        placeholder="请输入关键词搜索商品分类"
        size="large"
        value-key="name"
        v-model="keyword"
        :prefix-icon="Search"
        :fetch-suggestions="querySearchAsync"
        @select="handleSelect"
        v-if="false">
        <template #default="{ item }">
          <div class="value">{{ item.name }}</div>
        </template></el-autocomplete
      >
      <p class="topic-category" v-if="false">
        最近创建：
        <template v-for="(item, index) in topicList" :key="item.id">
          <i v-if="index !== 0">｜</i>
          <span @click="form.live_commodity_type_id = item.id">{{ item.name }}</span>
        </template>
      </p>
      <el-cascader-panel
        v-model="form.live_commodity_type_id"
        filterable
        clearable
        placeholder="请输入关键词搜索商品分类"
        size="large"
        :options="options"
        :props="{ label: 'name', value: 'id', emitPath: false }"
        style="width: 100%" />
    </el-form-item>
  </div>
</template>

<style lang="scss">
.topic-category {
  line-height: 40px;
  color: #92939a;
  span {
    margin: 0 10px;
    cursor: pointer;
    &:hover {
      color: var(--main-color);
    }
  }
}
</style>
