// mock/parking.js - 停车场管理模拟数据

import Mock from 'mockjs'

// 车位列表数据
Mock.mock(/\/parking\/space\/list/, 'get', () => {
    const list = []
    const buildings = ['A栋', 'B栋', 'C栋']
    const floors = ['B2', 'B1', 'F1', 'F2']
    const types = ['normal', 'vip', 'disabled']
    const statuses = [0, 0, 0, 1, 1, 2, 3] // 更多空闲状态

    for (let i = 1; i <= 100; i++) {
        const building = buildings[Math.floor(Math.random() * buildings.length)]
        const floor = floors[Math.floor(Math.random() * floors.length)]
        const type = types[Math.floor(Math.random() * types.length)]
        const status = statuses[Math.floor(Math.random() * statuses.length)]

        const space = {
            id: i,
            space_code: `${building.charAt(0)}${floor}${String(i).padStart(3, '0')}`,
            space_name: `${building}${floor}-${i}号`,
            building_id: building.charAt(0),
            building_name: building,
            floor_id: floor,
            floor_name: floor + '层',
            space_status: status,
            space_type: type,
            license_plate: status === 1 ? Mock.mock(/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{5}$/) : null,
            entry_time: status === 1 ? Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")') : null,
            parking_fee: status === 1 ? Mock.mock('@float(0, 200, 2, 2)') : 0,
            is_online: Mock.mock('@integer(0, 1)'),
            device_id: `DEV${String(i).padStart(3, '0')}`,
            position_x: Mock.mock('@float(120, 121, 6, 6)'),
            position_y: Mock.mock('@float(30, 31, 6, 6)'),
            position_z: floor.startsWith('B') ? -parseInt(floor.charAt(1)) * 3 : parseInt(floor.charAt(1)) * 3,
            remark: Mock.mock('@csentence(0, 20)'),
            create_time: '2024-01-01 10:00:00',
            update_time: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")')
        }
        list.push(space)
    }

    return {
        code: 200,
        msg: "查询成功",
        data: {
            total: list.length,
            rows: list.slice(0, 10)
        }
    }
})

// 统计数据
Mock.mock(/\/parking\/monitor\/statistics/, 'get', () => {
    const total = 500
    const occupied = Mock.mock('@integer(200, 300)')
    const reserved = Mock.mock('@integer(10, 30)')
    const fault = Mock.mock('@integer(0, 10)')
    const free = total - occupied - reserved - fault

    return {
        code: 200,
        msg: "success",
        data: {
            totalSpaces: total,
            freeSpaces: free,
            occupiedSpaces: occupied,
            reservedSpaces: reserved,
            faultSpaces: fault,
            occupancyRate: ((occupied / total) * 100).toFixed(2),
            todayEntries: Mock.mock('@integer(300, 500)'),
            todayExits: Mock.mock('@integer(280, 480)'),
            todayRevenue: Mock.mock('@float(5000, 10000, 2, 2)')
        }
    }
})

// 实时数据
Mock.mock(/\/parking\/monitor\/realtime/, 'get', () => {
    const recentEntries = []
    const recentExits = []

    // 最近入场
    for (let i = 0; i < 5; i++) {
        recentEntries.push({
            id: 1000 + i,
            license_plate: Mock.mock(/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{5}$/),
            space_code: Mock.mock(/^[ABC][BF][12]\d{3}$/),
            entry_time: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")'),
            space_type: Mock.mock('@pick(["normal", "vip", "disabled"])')
        })
    }

    // 最近出场
    for (let i = 0; i < 5; i++) {
        const entryTime = new Date(Date.now() - Mock.mock('@integer(1, 10)') * 3600000)
        const exitTime = new Date()
        const duration = Math.floor((exitTime - entryTime) / 60000)
        const hours = Math.floor(duration / 60)
        const minutes = duration % 60

        recentExits.push({
            id: 2000 + i,
            license_plate: Mock.mock(/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{5}$/),
            space_code: Mock.mock(/^[ABC][BF][12]\d{3}$/),
            entry_time: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")'),
            exit_time: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")'),
            duration: `${hours}小时${minutes}分`,
            fee: Mock.mock('@float(10, 100, 2, 2)')
        })
    }

    // 楼层车位
    const floorSpaces = []
    const floors = ['B1', 'B2']
    floors.forEach(floor => {
        for (let i = 1; i <= 20; i++) {
            const status = Mock.mock('@integer(0, 3)')
            floorSpaces.push({
                id: i + (floor === 'B1' ? 0 : 20),
                space_code: `A${floor}${String(i).padStart(3, '0')}`,
                floor_id: floor,
                space_status: status,
                license_plate: status === 1 ? Mock.mock(/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{5}$/) : null
            })
        }
    })

    return {
        code: 200,
        msg: "success",
        data: {
            recentEntries,
            recentExits,
            floorSpaces
        }
    }
})

// 报表数据
Mock.mock(/\/parking\/monitor\/report/, 'get', () => {
    const days = 7
    const dates = []
    const occupancyRates = []
    const revenues = []
    const details = []

    for (let i = days - 1; i >= 0; i--) {
        const date = new Date()
        date.setDate(date.getDate() - i)
        const dateStr = date.toISOString().split('T')[0]

        const occupancyRate = Mock.mock('@float(40, 60, 1, 1)')
        const revenue = Mock.mock('@float(4000, 8000, 0, 0)')

        dates.push(dateStr)
        occupancyRates.push(occupancyRate)
        revenues.push(revenue)

        details.push({
            date: dateStr,
            totalSpaces: 500,
            usedSpaces: Math.floor(500 * occupancyRate / 100),
            occupancyRate: occupancyRate,
            entryCount: Mock.mock('@integer(300, 500)'),
            exitCount: Mock.mock('@integer(280, 480)'),
            revenue: revenue
        })
    }

    return {
        code: 200,
        msg: "success",
        data: {
            dates,
            occupancyRates,
            revenues,
            details,
            summary: {
                avgOccupancyRate: (occupancyRates.reduce((a, b) => a + b) / days).toFixed(1),
                totalRevenue: revenues.reduce((a, b) => a + b),
                totalEntries: Mock.mock('@integer(2000, 3000)'),
                totalExits: Mock.mock('@integer(1900, 2900)'),
                peakOccupancyRate: Math.max(...occupancyRates),
                peakDate: dates[occupancyRates.indexOf(Math.max(...occupancyRates))]
            }
        }
    }
})

export default Mock