package com.fujica.abk.component;

import com.fujica.abk.ResourceTable;
import com.fujica.abk.model.response.OrderRes;
import com.fujica.abk.utils.Log;
import ohos.agp.components.*;
import ohos.app.Context;

import java.text.DecimalFormat;

/**
 * 订单详情组件
 */
public class OrderDetailComponent extends DirectionalLayout {
    private Component rootLayout;
    private OrderRes orderData;

    /**
     * 构造函数
     */
    public OrderDetailComponent(Context context) {
        super(context);
        initComponent(context);
    }

    /**
     * 初始化组件
     */
    private void initComponent(Context context) {
        // 设置布局属性
        setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
        setOrientation(VERTICAL);

        // 加载布局文件
        rootLayout = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_order_detail, null, false);
        addComponent(rootLayout);
    }

    /**
     * 设置订单数据并更新UI
     */
    public void setOrderData(OrderRes order) {
        this.orderData = order;
        bindData();
    }

    /**
     * 绑定数据到 UI
     */
    private void bindData() {
        if (orderData == null || rootLayout == null) {
            return;
        }

        try {
            // 设置商品名称
            Text goodsNameText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_goods_name);
            if (goodsNameText != null) {
                goodsNameText.setText(orderData.getGoodsName() != null ? orderData.getGoodsName() : "--");
            }

            // 设置业务类型
            Text bizTypeText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_biz_type);
            if (bizTypeText != null) {
                String bizTypeStr = "--";
                if (orderData.getBizType() != null) {
                    if (orderData.getBizType() == 1) {
                        bizTypeStr = "停车";
                    } else if (orderData.getBizType() == 2) {
                        bizTypeStr = "月卡";
                    }
                }
                bizTypeText.setText(bizTypeStr);
            }

            // 设置车场名称
            Text parkNameText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_park_name);
            if (parkNameText != null) {
                parkNameText.setText(orderData.getParkName() != null ? orderData.getParkName() : "--");
            }

            // 设置支付时间
            Text timeText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_pay_time);
            if (timeText != null) {
                timeText.setText(orderData.getPaySuccessTime() != null ? orderData.getPaySuccessTime() : "--");
            }

            // 设置金额
            Text amountText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_amount);
            if (amountText != null) {
                DecimalFormat df = new DecimalFormat("0.00");
                double amount = orderData.getActualAmount() != null ? orderData.getActualAmount() : 0.0;
                amountText.setText("￥" + df.format(amount));
            }
        } catch (Exception e) {
            Log.error("绑定订单详情数据失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

