<?php

namespace App\Transformer\Settlement;

class SettlementTransformer
{
    /**
     * 结算列表
     *
     * @param $data
     * @return array
     */
    public function buildBills($data): array
    {
        return [
            'meta' => getMeta($data),
            'list' => $this->eachBills($data->items())
        ];
    }

    /**
     * @param $data
     * @return array
     */
    public function buildBillOrders($data): array
    {
        if (empty($data)) {
            return [];
        }

        $result = [];
        foreach ($data as $item) {
            $result[] = [
                'id'              => getProp($item, 'id'),
                'price'           => getProp($item, 'price'),
                'send_order_id'   => getProp($item, 'send_order_id'),
                'send_order_name' => (string)getProp($item, 'send_order_name'),
                'created_at'      => $item->created_at->format('Y-m-d H:i:s'),
            ];
        }
        return $result;
    }

    /**
     * 提现列表
     *
     * @param $data
     * @return array
     */
    public function buildWithdrawCashes($data): array
    {
        return [
            'meta' => getMeta($data),
            'list' => $this->eachWithdrawCashes($data->items())
        ];
    }

    /**
     * @param $bills
     * @return array
     */
    private function eachBills($bills): array
    {
        if (empty($bills)) {
            return [];
        }

        $result = [];
        foreach ($bills as $bill) {
            $result[] = [
                'date'             => getProp($bill, 'date'),
                'recharge_amount'  => getProp($bill, 'recharge_amount'),
                'rate'             => getProp($bill, 'rate'),
                'service_amount'   => getProp($bill, 'service_amount'),
                'settlement_price' => getProp($bill, 'settlement_price'),
            ];
        }

        return $result;
    }

    /**
     * @param $bills
     * @return array
     */
    private function eachWithdrawCashes($bills): array
    {
        if (empty($bills)) {
            return [];
        }

        $result = [];
        foreach ($bills as $bill) {
            $result[] = [
                'amount'       => getProp($bill, 'amount'),
                'status'       => getProp($bill, 'status'),
                'created_at'   => $bill->created_at->format('Y-m-d H:i:s'),
                'bank_account' => getProp($bill, 'bank_account'),
                'account_name' => getProp($bill, 'account_name'),
                'pay_time'     => getProp($bill, 'pay_time'),
            ];
        }

        return $result;
    }
}