<?php

class item {

    public static function receivedData($data): void
    {
        $package = package::getpack($data['package_id']);

        $cash_payload = [
            'payment_id'  => $data['payment_id'],
            'account_id'  => $data['account_id'],
            'userid'      => $data['userid'],
            'amount'      => $package['cash'],
            'status'      => 0,
            'create_date' => date("Y-m-d H:i:s")
        ];

        self::sendCash($cash_payload);

        $item_payload = [
            'payment_id'  => $data['payment_id'],
            'account_id'  => $data['account_id'],
            'userid'      => $data['userid'],
            'charid'      => $data['charid'],
            'items'       => $package['items'],
        ];

        if(count($package['items']))
            self::sendItem($item_payload);
    }

    public static function sendCash($data): void
    {
        query::Insert(TABLE_CASH_SEND, $data);
    }

    public static function sendItem($data): void
    {
        $size = count($data['items']);

        $payload = [
            'payment_id'  => $data['payment_id'],
            'account_id'  => $data['account_id'],
            'userid'      => $data['userid'],
            'char_id'     => $data['charid'],
            'status'      => 0,
            'create_date' => date("Y-m-d H:i:s")
        ];

        $str = 'item';

        for ($i = 0; $i < $size; $i++) {
            $payload[$str.($i+1).'_id']     = $data['items'][$i]['item_id'];
            $payload[$str.($i+1).'_amount'] = $data['items'][$i]['item_quantity'];
            $payload[$str.($i+1).'_refine'] = $data['items'][$i]['item_refine'];
        }

        query::Insert(TABLE_ITEM_SEND, $payload);
    }
}

?>