<template>
    <view class="index">
        <view class="title">游戏大厅</view>

        <view class="card_list">
            <view
                class="card_item"
                v-for="item in list as any[]"
                :key="item.ID"
                :style="{ backgroundImage: `url(${item.cover})` }"
                @click="toPage(item)"
            ></view>
        </view>
    </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import Taro from '@tarojs/taro'
import './index.scss'

Taro.useLoad((options) => {
    Taro.setStorageSync('uid', options.uid)
    if (Taro.getStorageSync('uid')) {
        Taro.showToast({
            title: `登陆成功`,
            icon: 'none',
        })
        getList()
    } else {
        Taro.showToast({
            title: '登陆失败，请重新进入游戏大厅',
            icon: 'none',
        })
    }
})

const list = ref([])

const getList = async () => {
    Taro.request({
        url: `${process.env.TARO_APP_HOME}`,
        method: 'POST',
        success: ({ data: res }) => {
            list.value = res.data.data.sort((a: any, b: any) => b.sort - a.sort) || []
        },
    })
}

const toPage = (item: any) => {
    Taro.setStorageSync('gameItem', item)
    Taro.navigateTo({
        url: `/pages/game_detail/index?id=${item.ID}`,
    })
}
</script>
