<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Shop;
use Illuminate\Http\Request;

class DmaShopController extends Controller
{
    use ResponseJson;

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

    public function store(Request $request)
    {
        $shop_id = $request->input('shop_id');
        $shop = Shop::find($shop_id);
        if(empty($shop)) return $this->failure("没有商品信息");
        $data = $request->all();
        $res = $shop->dmaShop()->exists();
        if ($res) return $this->failure("添加失败，商品已添加");
        $shop->dmaShop()->create($data);
        return $this->success('ok');
    }

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

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

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