<script setup>
import { computed } from 'vue'
import bgIcon from '/static/images/bg_icon.png'
import bannerImg from '/static/images/index/banner_1.png'
import bannerImg2 from '/static/images/index/banner_2.png'
import bannerImg3 from '/static/images/index/banner_3.png'
import honestyBanner from '/static/images/index/banner_B.png'
import companyIcon from '/static/images/index/company_bg.png'
import housekeepingRiskIcon from '/static/images/index/housekeeping_risk_bg.png'
import loanCheckIcon from '/static/images/index/loan_check_bg.png'
import marriageRiskIcon from '/static/images/index/marriage_risk_bg.png'
import personalDataIcon from '/static/images/index/personal_data_bg.png'
import preLoanRiskIcon from '/static/images/index/preloan_risk_bg.png'
import rentalInfoBg from '/static/images/index/rentalinfo_bg.png'
import rightIcon from '/static/images/index/right.png'
import indexPromoteIcon from '/static/images/index/tgbg.png'
import indexMyReportIcon from '/static/images/index/wdbg.png'
import indexInvitationIcon from '/static/images/index/yqhy.png'
import { useAppConfig } from '@/composables/useAppConfig'

definePage({ type: 'home', layout: 'home' })

const banners = [bannerImg, bannerImg2, bannerImg3]

const services = [
  { title: '个人大数据', name: 'personalData', bg: personalDataIcon },
  { title: '婚恋风险', name: 'marriage', bg: marriageRiskIcon },
  { title: '入职背调', name: 'backgroundcheck', bg: preLoanRiskIcon },
]

const riskServices = [
  { title: '小微', name: 'companyinfo', bg: companyIcon },
  { title: '家政', name: 'homeservice', bg: housekeepingRiskIcon },
  { title: '贷前', name: 'preloanbackgroundcheck', bg: loanCheckIcon },
  { title: '诚信租赁', name: 'rentalinfo', bg: rentalInfoBg },
]
const { appConfig, loadAppConfig } = useAppConfig()
const queryRetentionDaysText = computed(() => `${appConfig.value.query.retention_days}天`)
void loadAppConfig()

function toInquire(name) {
  uni.navigateTo({
    url: `/pages/inquire?feature=${encodeURIComponent(name)}`,
  })
}

function toInvitation() {
  uni.navigateTo({ url: '/pages/invitation' })
}

function toPromote() {
  uni.navigateTo({ url: '/pages/promote' })
}

function toHistory() {
  uni.navigateTo({ url: '/pages/history-query' })
}
</script>

<template>
  <view class="box-border min-h-screen">
    <view class="relative p-4">
      <swiper class="banner-swiper overflow-hidden rounded-xl" circular autoplay :interval="3000" indicator-dots
        indicator-color="rgba(255,255,255,0.5)" indicator-active-color="#ffffff">
        <swiper-item v-for="(item, index) in banners" :key="index">
          <image :src="item" class="h-full w-full" mode="aspectFill" />
        </swiper-item>
      </swiper>
    </view>

    <view class="px-6">
      <view class="grid grid-cols-3 gap-3">
        <view class="flex flex-col items-center justify-center text-center" @click="toPromote">
          <view
            class="box-content h-16 w-16 flex items-center justify-center rounded-full from-white to-blue-100/10 bg-gradient-to-b p-1 shadow-lg">
            <image :src="indexPromoteIcon" class="h-12 w-12" mode="aspectFit" />
          </view>
          <text class="mt-1 text-center font-bold">
            推广报告
          </text>
        </view>

        <view class="flex flex-col items-center justify-center text-center" @click="toInvitation">
          <view
            class="box-content h-16 w-16 flex items-center justify-center rounded-full from-white to-blue-100/10 bg-gradient-to-b p-1 shadow-lg">
            <image :src="indexInvitationIcon" class="h-12 w-12" mode="aspectFit" />
          </view>
          <text class="mt-1 text-center font-bold">
            邀请下级
          </text>
        </view>

        <view class="flex flex-col items-center justify-center text-center" @click="toHistory">
          <view
            class="box-content h-16 w-16 flex items-center justify-center rounded-full from-white to-blue-100/10 bg-gradient-to-b p-1 shadow-lg">
            <image :src="indexMyReportIcon" class="h-12 w-12" mode="aspectFit" />
          </view>
          <text class="mt-1 text-center font-bold">
            我的报告
          </text>
        </view>
      </view>
    </view>

    <view class="relative p-4 pt-0">
      <view class="grid grid-cols-2 my-4 gap-4" style="grid-template-rows: repeat(2, 1fr);">
        <view v-for="(service, index) in services" :key="index"
          class="relative min-h-18 flex flex-col rounded-xl px-4 py-2 shadow-lg"
          :class="index === 0 ? 'row-span-2' : ''"
          :style="`background: url(${service.bg}) no-repeat; background-size: 100% 100%; background-position: center;`"
          @click="toInquire(service.name)">
          <view class="min-h-18 flex items-end">
            <!-- <text class="text-base text-gray-700 font-semibold">
              {{ service.title }}
            </text> -->
          </view>
        </view>
      </view>

      <scroll-view scroll-x class="risk-scroll my-4 px-1 pb-4 pt-2 -mx-1">
        <view class="inline-flex gap-2">
          <view v-for="(service, index) in riskServices" :key="index"
            class="relative h-24 w-[107px] flex-shrink-0 rounded-xl shadow-lg"
            :style="`background: url(${service.bg}) no-repeat; background-size: 100% 100%; background-position: center;`"
            @click="toInquire(service.name)">
            <view class="h-full flex items-end px-2 py-2">
              <!-- <text class="text-sm text-gray-700 font-semibold">
                {{ service.title }}
              </text> -->
            </view>
          </view>
        </view>
      </scroll-view>

      <view class="mb-3 mt-6 flex items-center">
        <view class="bg-primary h-5 w-1.5 rounded-xl" />
        <text class="ml-2 text-lg text-gray-800">
          诚信专栏
        </text>
      </view>

      <view class="mt-4 overflow-hidden rounded-xl bg-white shadow-xl">
        <image :src="honestyBanner" class="block w-full" mode="widthFix" />
      </view>

      <view class="mt-4 box-border h-14 w-full flex items-center rounded-lg bg-white px-4 text-gray-700 shadow-lg"
        @click="toHistory">
        <view class="mr-4 h-full flex items-center justify-center">
          <image class="h-10 w-10" :src="bgIcon" mode="aspectFit" />
        </view>
        <view class="flex-1">
          <view class="text-gray-800">
            我的历史查询记录
          </view>
          <view class="text-xs text-gray-500">
            查询记录有效期为{{ queryRetentionDaysText }}
          </view>
        </view>
        <image :src="rightIcon" class="h-6 w-6" mode="aspectFit" />
      </view>
    </view>
  </view>
</template>

<style scoped>
.banner-swiper {
  height: 160px;
}

.risk-scroll {
  white-space: nowrap;
}
</style>
