<template>
  <div class="h-full flex flex-col bg-white p-2">
    <p class="font-bold text-lg leading-10 border-b-2">资料库</p>
    <el-form class="pt-2" ref="formRef" :model="form" inline>
      <el-form-item label="资料名称" prop="type">
        <el-input v-model="form.type"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="onSearch" type="primary">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row style="margin-bottom: 18px">
      <el-col :span="24">
        <el-button type="primary" @click="handleImport">导入</el-button>
      </el-col>
    </el-row> 
    <el-space wrap :size="15">
      <el-badge  v-for="informationType in informationTypes" :key="informationType" class="item1">
      <el-button class="box-card"
        @click="handleInfoTypeClick(informationType)">
           {{ informationType }}
      </el-button>
        <template #content="{ value }">
              <div class="custom-content">
                <el-icon  @click="deleteInfo(informationType)" >
                  <Close />
                </el-icon>
              </div>
            </template>

      </el-badge>
    </el-space>
    <UploadInformationInfoDialog :visible="importVisible" @onImportChange="onImportChange" />
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive, unref } from 'vue'
import { getInformationTypeListApi } from '@/api/customer'
import { useRouter } from 'vue-router'
import { ElMessage,ElMessageBox, type FormInstance } from 'element-plus'
import UploadInformationInfoDialog from './components/upload-information-info-dialog.vue';
import { Back ,Close} from '@element-plus/icons-vue'
import { deleteInformation } from '@/api/customer'


const loading = ref(false)
const router = useRouter()

const informationTypes = ref([])
const formRef = ref<FormInstance>()
const form = reactive({
  type: '',
  currentPage: 1,
  pageSize: 10,
})
const importVisible = ref(false)

const queryCustomer = async () => {
  loading.value = true
  try {
    const { data } = await getInformationTypeListApi(unref(form))
    informationTypes.value = data.result
  } catch (error) {

  } finally {
    loading.value = false
  }
}

const onReset = () => {
  if (formRef?.value) {
    formRef?.value.resetFields()
  }
  onSearch()
}

const onSearch = () => {
  queryCustomer()
}

const handleImport = () => {
  importVisible.value = true
}

const onImportChange = (type: any) => {
  importVisible.value = false
  if (type != 'close') {
    onReset()
  }
}

const handleInfoTypeClick = (informationType: any) => {
  router.push({
    path: "/customer-excel-list",
    query: {
      informationType: informationType
    }
  })
}

const deleteInfo = async (informationType: any) => {
  await ElMessageBox.confirm('资源库一旦删除无法恢复，是否确认删除', '提示', {
    confirmButtonText: '删除',
    cancelButtonText: '取消',
    type: 'warning'
  })
  const params = {
    nodeLabel: informationType,
  }
  const { data } = await deleteInformation(params)
  queryCustomer()
}


onMounted(() => {
  queryCustomer()
})
</script>

<style lang="scss" scoped>
:deep(.el-card) {
  cursor: pointer;
}

.item1 {
  margin-top: 0px;
  margin-right: 5px;
}

.custom-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}
</style>