<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '一键填报',
  },
  needLogin: true,
}
</route>
<template>
  <view class="flex flex-col h-screen">
    <view class="flex-1 bg-[#f8f8f8] pb-safe flex flex-col items-center pt-[48rpx]">
      <Badge :value="badgeValue">
        <button
          plain
          class="w-[188rpx]! h-[58rpx]! min-w-[unset]! text-[24rpx]! flex! items-center! justify-center! rounded-[88rpx]! color-[#1580FF]! border-[#1580FF]! border-[2rpx]"
          @click="show = true"
        >
          条件筛选
          <view class="i-carbon-chevron-down"></view>
        </button>
      </Badge>

      <image
        class="w-[286rpx] h-[286rpx] mt-[134rpx] mix-blend-darken"
        src="https://api.static.ycymedu.com/sub/images/autoFill/auto-fill.jpg"
        mode="widthFix"
        @click="show = true"
      ></image>
    </view>
    <ActionSheet
      v-model:show="show"
      title="筛选"
      @close="close"
      :show-close="true"
      custom-header-class="text-[36rpx]! text-[#303030]! font-medium!"
    >
      <FilterMenu ref="filterMenuRef" @change="handleChange" />
      <template #footer>
        <view class="flex items-center px-[32rpx]">
          <button
            plain
            class="border-[#f5f5f5]! flex-auto bg-[#f5f5f5]! rounded-[8rpx]! text-[#1580FF]! text-[32rpx]! font-normal! mr-[22rpx]"
            @click="close"
          >
            清空
          </button>
          <button
            class="border-[#1580FF]! flex-auto bg-[#1580FF]! rounded-[8rpx]! text-[#fff]! text-[32rpx]! font-normal!"
            @click="handleResult"
          >
            确定
          </button>
        </view>
      </template>
    </ActionSheet>
  </view>
</template>

<script lang="ts" setup>
import FilterMenu from '@/pages-sub/home/components/FilterMenu.vue'
import Badge from '@/pages-sub/components/badge/Badge.vue'
import ActionSheet from '@/pages-sub/components/ActionSheet.vue'
import { ref } from 'vue'
import { getUniversityListByFilter } from '@/service/index/api'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()

const show = ref(false)
const badgeValue = ref(1)

const filterMenuRef = ref()

const handleChange = (params: any) => {
  badgeValue.value = params.length
}

const handleResult = () => {
  let _params = filterMenuRef.value.handleConfirm()

  let params = {
    p: userStore.userInfo.estimatedAchievement.sp,
    location: userStore.userInfo.estimatedAchievement.provinceCode,
    subjects: userStore.userInfo.estimatedAchievement.subjectGroup.split(','),
    score: +userStore.userInfo.estimatedAchievement.expectedScore,
    batchname: _params.choosePhase,
    nature: _params.chooseNature,
    province: _params.chooseRegion,
    subjectType: _params.chooseUniType,
  }

  getUniversityListByFilter(params).then((res) => {
    if (res.code === 200) {
      let collegeList = (res.result as { rows: any[] }).rows
      let wishList = collegeList.map((item) => {
        let _major = {
          ...item,
          name: item.universityName,
          vItems: item.majorItems.map((majorItem: any) => ({
            ...majorItem,
            name: majorItem.majorName,
            items: majorItem.planItems,
          })),
        }

        return _major
      })
      userStore.sortWishCollegeList({ list: wishList })
      uni.navigateTo({
        url: `/pages-sub/home/wishesList/wishesList?typeName=一键填报&editType=add`,
      })
    }
  })
}

const close = () => {
  filterMenuRef.value.clearForm()
  badgeValue.value = 1
  show.value = false
}

onShow(() => {
  userStore.clearWishListId()
  userStore.clearWishList()
})
</script>

<style lang="scss" scoped>
.tbn-border {
  border-radius: 68rpx !important;
  border: 2rpx solid #1580ff !important;
  background-color: #fff !important;
}
</style>
