using EasyTemplate.Tool.Dto.CardManagement;
using EasyTemplate.Tool.Dto.RechargeRecords;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using static EasyTemplate.Page.Pages.Report.FuelTransactionReport;

namespace EasyTemplate.Page.Pages.CardManagement
{
    public partial class PersonalCard : ComponentBase
    {

        protected FilterModel Filter { get; set; } = new();
        protected List<TransactionItem> TransactionData { get; set; } = new();
        protected List<RechargeRecordDto> rechargeRecordDtos { get; set; } = new();
        protected List<CardInfoDto> cardInfoDtos { get; set; } = new();

        protected bool Loading { get; set; }
        protected int TotalCount { get; set; }
        protected int CurrentPage { get; set; } = 1;
        protected int PageSize { get; set; } = 20;

        // ========== 选项数据 ==========

        protected List<string> DeviceOptions { get; set; } = new();
        protected List<string> PlateNumberOptions { get; set; } = new();
        protected List<string> OilTypeOptions { get; set; } = new();
        protected List<string> PaymentMethodOptions { get; set; } = new();
        protected List<string> PaymentStatusOptions { get; set; } = new();
        protected List<string> OrderStatusOptions { get; set; } = new();
        protected List<string> DriverOptions { get; set; } = new();
        protected override void OnInitialized()
        {
            base.OnInitialized();
            InitializeData();
            _ = LoadDataAsync();
        }

        // ========== 初始化方法 ==========

        private void InitializeData()
        {
            // 初始化选项数据
            DeviceOptions = new List<string> { "DEV001", "DEV002", "DEV003", "DEV004" };
            PlateNumberOptions = new List<string> { "粤ACD0045", "粤ACD0046", "粤ACD0047", "粤ACD0048" };
            OilTypeOptions = new List<string> { "92#", "95#", "0#" };
            PaymentMethodOptions = new List<string> { "微信支付", "支付宝", "现金" };
            PaymentStatusOptions = new List<string> { "true", "false" };
            OrderStatusOptions = new List<string> { "已完成", "进行中", "已取消" };
            DriverOptions = new List<string> { "张三", "李四", "王五" };

            // 设置默认时间范围（最近7天）
            Filter.StartTime = DateTime.Today.AddDays(-7);
            Filter.EndTime = DateTime.Today;
        }

        // ========== 数据操作 ==========

        // 重置筛选条件
        protected void ResetFilter()
        {
            Filter = new FilterModel
            {
                StartTime = DateTime.Today.AddDays(-7),
                EndTime = DateTime.Today
            };
            CurrentPage = 1;
        }

        // 加载数据
        protected async Task LoadDataAsync()
        {
            Loading = true;

            try
            {
                // 模拟API调用延迟
                await Task.Delay(300);

                // 生成模拟数据
                TransactionData = GenerateMockData();
                TotalCount = TransactionData.Count(); 
            }
            finally
            {
                Loading = false;
                StateHasChanged();
            }
        }

        // 生成模拟数据
        private List<TransactionItem> GenerateMockData()
        {
            var data = new List<TransactionItem>();
            var random = new Random();
            var startIndex = (CurrentPage - 1) * PageSize;
            CardInfoDto cardInfoDto = new CardInfoDto();
            //cardInfoDto.StationName = "测试站点";
            //cardInfoDto.CompanyName = "测试公司";
            //cardInfoDto.CardType = "公司卡";
            //cardInfoDto.Mobile = "17620434492";
            //cardInfoDto.CardNo = "12321412412312321";
            //cardInfoDto.AccountBalance = 2342.2M;
            //cardInfoDto.IssueTime = DateTime.Now;
            //cardInfoDto.LossTime = DateTime.Now;
            //cardInfoDto.Status = "正常";
            cardInfoDtos.Add(cardInfoDto);
            return data;
        }

        // ========== 格式化方法 ==========

        // 格式化货币显示
        protected string FormatCurrency(decimal? value)
        {
            if (value == null) return "-";
            return value.Value.ToString("C", CultureInfo.CreateSpecificCulture("zh-CN"));
        }

        // 格式化数字显示
        protected string FormatNumber(decimal? value)
        {
            if (value == null) return "-";
            return value.Value.ToString("N2", CultureInfo.CreateSpecificCulture("zh-CN"));
        }

        // 格式化日期时间
        protected string FormatDateTime(DateTime? value)
        {
            if (value == null) return "-";
            return value.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }

        // 格式化支付状态
        protected string FormatPaymentStatus(bool? status)
        {
            if (status == null) return "-";
            return status.Value ? "成功" : "失败";
        }

        // 格式化订单状态
        protected string FormatOrderStatus(string status)
        {
            return string.IsNullOrEmpty(status) ? "-" : status;
        }

        // ========== 事件处理方法 ==========

        // 查询处理
        protected async Task HandleQuery()
        {
            CurrentPage = 1;
            await LoadDataAsync();
        }

        // 重置处理
        protected async Task HandleReset()
        {
            ResetFilter();
            await LoadDataAsync();
        }

        // 页码变化
        protected async Task OnPageChange(int page)
        {
            CurrentPage = page;
            await LoadDataAsync();
        }

        // 每页大小变化
        protected async Task OnPageSizeChange(int pageSize)
        {
            PageSize = pageSize;
            CurrentPage = 1;
            await LoadDataAsync();
        }
    }
}