<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>
      <el-card v-for="informationType in informationTypes" :key="informationType" class="box-card"
        @click="handleInfoTypeClick(informationType)">
        <div class="text item">
          {{ informationType }}
        </div>
      </el-card>
    </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, type FormInstance } from 'element-plus'
import UploadInformationInfoDialog from './components/upload-information-info-dialog.vue';

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
    }
  })
}

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

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