<?php

namespace App\Http\Controllers\Admin;

use App\Exports\NurtureShopsExport;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\NurtureShop;
use App\Models\Shop;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class NurtureShopController extends Controller
{
    use ResponseJson;

    public function index(Request $request)
    {
        $shops = Shop::with('shopNurture')->keyword()->nurture()->orderByDesc("id");
        if ( $request->nopage) {
            $shops = $shops->get();
        }elseif ($request->export){
            $shops = $shops->get()->toArray();
            return Excel::download(new NurtureShopsExport($shops), '商品列表.xlsx');
        }else{
            $shops = $shops->paginate();
        }
//        $shops = new AgentShopCollection($shops);
        return $this->success('ok', $shops);
    }

    public function store(Request $request)
    {
        $data = $request->all();
        $shop = NurtureShop::where('shop_id', $data['shop_id'])->first();
        if ($shop) return $this->failure("商品已存在");
        $shop = NurtureShop::create($data);
        return $this->success('ok', $shop);
    }

    public function update(Request $request, Shop $shop)
    {
        $data = $request->all();
        $shop->shopNurture->update($data);
        return $this->success('ok');
    }

    public function show(Request $request, Shop $shop)
    {
        $shop->shopNurture;
        return $this->success('ok', $shop);
    }

    public function destroy(Request $request, Shop $shop)
    {
        $shop->shopNurture?->delete();
        return $this->success('ok');
    }
}
