<?php

namespace Modules\Mall\Facades;

use Illuminate\Contracts\Support\Arrayable;
use Modules\Mall\Models\Address;
use Modules\Mall\Models\GoodsSku;

class Item implements Arrayable
{

    public GoodsSku $model;

    public int $sku_id;

    public int $qty;

    public float $price;

    public int $shop_id;

    public array $source;

    public string $orderby;

    public $address;

    /**
     * Item constructor.
     *
     * @param  \Modules\Mall\Models\GoodsSku  $item
     * @param  Address|null  $address
     * @param  int  $qty
     */
    public function __construct(GoodsSku $item, $address = null, int $qty = 1)
    {
        $this->model   = $item;
        $this->sku_id  = $item->id;
        $this->shop_id = $item->getShopIdAttribute();
        $this->orderby = $item->getShopTypeAttribute().'_'.$item->getShopIdAttribute();
        $this->qty     = $qty;
        $this->price   = $item->getItemPrice();
        $this->address = $address;
    }

    /**
     * Notes:  获取条目总价
     *
     * @Author: <C.Jason>
     * @Date  : 2019/11/21 11:03 上午
     * @return float
     */
    public function total(): float
    {
        return bcmul($this->price, $this->qty, 2);
    }

    /**
     * Notes: 获取总总量
     *
     * @Author: 玄尘
     * @Date  : 2021/5/14 13:20
     * @return float
     */
    public function weight(): float
    {
        return bcmul($this->model->getGoodsWeight(), $this->qty, 2);
    }

    /**
     * Notes: 获取运费
     *
     * @Author: 玄尘
     * @Date  : 2021/5/14 13:34
     */
    public function freight(): int
    {
        return 0;
    }

    /**
     * Notes: 获取shop
     *
     * @Author: 玄尘
     * @Date  : 2021/5/14 14:15
     */
    public function shop(): array
    {
        return [
            'shop_id' => $this->model->getShopIdAttribute(),
            'name'    => $this->model->getShopNameAttribute(),
        ];
    }

    /**
     * Notes: 下单存入order_item表
     *
     * @Author: 玄尘
     * @Date  : 2021/5/18 8:37
     */
    public function getSource(): array
    {
        return $this->model->getSource();
    }

    /**
     * Notes: 转换成数组
     *
     * @Author: <C.Jason>
     * @Date  : 2020/9/23 4:00 下午
     * @return array
     */
    public function toArray(): array
    {
        return [
            'goods_id'     => $this->model->goods_id,
            'goods_sku_id' => $this->sku_id,
            'title'        => $this->model->getGoodsName(),
            'value'        => $this->model->getItemValue(),
            'cover'        => $this->model->cover_url,
            'price'        => $this->price,
            'qty'          => $this->qty,
            'score'        => $this->model->score,
        ];
    }

}
