<?php

namespace App\Http\Resources;

use App\Models\Admin;
use App\Models\AgentOrder;
use Illuminate\Http\Resources\Json\JsonResource;

class AgentShopResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $user = auth()->user();
        $shop_agent = $this->shopAgent;
        $sku = $shop_agent->getSkus();
        $data = [
            'id'=>$this->id,
            'pic'=>$this->icon,
            'title'=>$this->title,
            'sub_title'=>$this->sub_title,
            'origin_price'=>$this->price,
            'price'=>count($sku)?$sku[0]['price']:$this->price,
            'ratio'=>$this->shopAgent->ratio,
            'freight'=>$this->shopAgent->freight,
            'sku' =>$sku,
            'lotto_code'=>$shop_agent->lotto_code,
            'is_show'=>$shop_agent->is_show,
            'payment_id'=>$this->shopAgent->payment_id,
            'is_open_policy' => $this->is_open_policy,
            'policy_title' => $this->policy_title,
            'policy' => $this->policy,
        ];
        $uri = $request->route()->uri;
        if ($uri != 'api/h5/agent/shops' && $uri != 'api/admin/agent/shops') {
            $data['banners'] = $this->banners;
            $data['describe'] = $this->describe;
        }

        if ($uri == 'api/admin/agent/shops') {
            $data['payment'] = $this->shopAgent->payment;

        }

        if ($request->route()->getName() =='h5.agent.shop') {
            $data['qrcode'] = $user->getShopAgentQrcode($this->id);
            $data['share_qrcode'] = $user->getShopAgentQrcode($this->id,"H5");
            $data['recive'] = AgentOrder::owner($user->id)->select('id','name','mobile','address')->orderByDesc("id")->first()?:null;

        }

        if ($request->route()->getName() == "h5.agent.shop" || $request->route()->getName() == "h5.agent.shops" ) {
//            $data['sku'] = $user->getGoodsSku($sku);
            $data['first_order'] =  AgentOrder::owner($user->id)->where('shop_id', $this->id)->paid()->exists()?0:1;
        }
        return $data;
    }
}
