<?php
/**
 * PageDetail.php
 *
 * @copyright  2022 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     Edward Yang <yangjin@guangda.work>
 * @created    2022-08-11 18:45:02
 * @modified   2022-08-11 18:45:02
 */

namespace Beike\Shop\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class InquiryDetail extends JsonResource{
    /**
     * @param Request $request
     * @return array
     * @throws \Exception
     */
    public function toArray($request):array{
        $productsku = $this->productsku;
        if($this->productsku == NULL){
            return [
                'id'              => $this->id,
                'product_sku_id'  => $this->product_sku_id,
                'contacts'        => $this->contacts,
                'email'           => $this->email,
                'content'         => $this->content,
                'product_sku_sku' => '',
                'product_name'    => '',
                'product_images'  => [],
                'product_id'      => '',
                'created_at'      => time_format($this->created_at),
                'updated_at'      => time_format($this->updated_at),
            ];
        }
        $product     = $productsku->product;
        $description = $product->description;
        return [
            'id'              => $this->id,
            'product_sku_id'  => $this->product_sku_id,
            'contacts'        => $this->contacts,
            'email'           => $this->email,
            'content'         => $this->content,
            'product_sku_sku' => $productsku->sku,
            'product_name'    => $description->name ?? '',
            'product_images'  => array_map(function($image){
                return [
                    'preview' => image_resize($image,500,500),
                    'popup'   => image_resize($image,800,800),
                    'thumb'   => image_resize($image,150,150),
                ];
            },$product->images ?? []),
            'product_id'      => $productsku->product_id,
            'created_at'      => time_format($this->created_at),
            'updated_at'      => time_format($this->updated_at),
        ];
    }
}
