<script setup lang="ts">
import { type LocationQueryValue, useRoute } from 'vue-router'
import { get1, get2, get3, get4 } from '@/api/home'
import { provide, ref } from 'vue'
import CompanyDetailSmallBox from '@/components/CompanyDetailSmallBox.vue'
const route = useRoute()
const k:any = route.query.n
const currentShowList = ref<any>([]);
function removeDuplicatesById(arr:any, key:any) {
  return arr.reduce((accumulator:any, current:any) => {
    if (!accumulator.some((item:any) => item[key] === current[key])) {
      accumulator.push(current);
    }
    return accumulator;
  }, []);
}
const getData =async ()=>{
  if(k == 1){
    console.log(1)
    const res1 = await get1({pageNum:1,pageSize:1000});
    const bo = removeDuplicatesById(res1.records, 'administrativePartyName')
    const result = bo.map((item:any)=>{
      return ({
        name: item.administrativePartyName,
        time: item.licenseDecisionDate?.split('T')[0],
        unifiedSocialCreditCode:item.unifiedSocialCreditCode
      })
    })
    currentShowList.value = result;
  }else if(k == 2){
    const res2 = await get2({pageNum:1,pageSize:1000});
    const bo = removeDuplicatesById(res2.records, 'administrativePartyName')
    const result = bo.map((item:any)=>{
      return ({
        name: item.administrativePartyName,
        time: item.penaltyDecisionDate?.split('T')[0],
        unifiedSocialCreditCode:item.unifiedSocialCreditCode
      })
    })
    currentShowList.value = result;
  }else if(k == 3){
    const res3 = await get3({pageNum:1,pageSize:1000});
    const bo = removeDuplicatesById(res3.records, 'subjectName')
    const result = bo.map((item:any)=>{
      return ({
        name: item.subjectName,
        time: item.statisticsTime?.split('T')[0],
        unifiedSocialCreditCode:item.unifiedSocialCreditCode
      })
    })
    currentShowList.value = result;
  }else if(k == 4){
    const res4:any = await get4({pageNum:1,pageSize:1000});
    const bo = removeDuplicatesById(res4.records, 'enterpriseName')
    console.log(bo)
    const result = bo.map((item:any)=>{
      return ({
        name: item.enterpriseName,
        time:item.filingTime?.split('T')[0],
        unifiedSocialCreditCode:item.unifiedSocialCreditCode
      })
    })
    currentShowList.value = result;
  }
}
const init = () => {
  getData();
}
const showConpanyDetail = (item:any)=>{
  if(k == 1){
    toCompanyDetailItemMethod.value = get1;
    dialogName.value = '行政许可'
  }else if(k == 2){
    toCompanyDetailItemMethod.value = get2;
    dialogName.value = '行政处罚'
  }else if(k == 3){
    toCompanyDetailItemMethod.value = get3;
    dialogName.value = '守信红名单'
  }else if(k == 4){
    toCompanyDetailItemMethod.value = get4;
    dialogName.value = '失信企业'
  }
  toCompanyDetailItem.value = item;

  companyDetailIsShow.value = true
}
const companyDetailIsShow = ref(false);
const toCompanyDetailItem = ref();
const toCompanyDetailItemMethod = ref();
const closeDialog = ()=>{
  toCompanyDetailItem.value = null;
}
const dialogName = ref('')
provide('isShow',companyDetailIsShow)
provide('name',dialogName)
init();
</script>

<template>
  <div class="conpanyListContainer pt-20px pb-40px">
    <div class="w-912px bg-white mr-a ml-a pt-20px border-solid border-1px border-color-#e6e6e6 rounded-10px">
      <div v-for="(item,index) in currentShowList" :key="index" @click="showConpanyDetail(item)" class="cursor-pointer">
        <div class="text-20px p-20px flex justify-between">
          <div>{{item.name}}</div>
          <div class="text-gray text-20px">{{item.time}}</div>
        </div>
        <div class="h-1px bg-gray"></div>
      </div>
    </div>
    <company-detail-small-box @closeDialog="closeDialog" :item="toCompanyDetailItem" :getItemDetailMethod="toCompanyDetailItemMethod" />
  </div>
</template>

<style scoped>
.conpanyListContainer{
  background-color: #ecf0f9;
}
</style>