<?php
namespace App\Domains\Virtual\Http\Controllers;

use App\Core\Controller;
use Illuminate\Http\Request;
use App\Exceptions\NotExistException;
use App\Domains\Virtual\Services\PackageService;
use App\Domains\Virtual\Repositories\PackageRepository;

class PackageController extends Controller
{
    protected $request;
    protected $packageService;

    /**
     * 构造函数，自动注入.
     */
    public function __construct(Request $request, PackageService $packageService)
    {
        $this->request = $request;
        $this->packageService = $packageService;
    }

    /**
     * 套餐列表.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $conditions = $this->request->all();
        $conditions['limit'] = $this->request->get('limit', 20);

        $packages = $this->packageService->index($conditions);

        return res($packages, '套餐列表', 201);
    }

    /**
      * 创建.
      *
      * @return \Illuminate\Http\Response
      */
    public function create()
    {
        $attributes = $this->request->all();

        $account = $this->packageService->store($attributes);

        return res($account, '创建成功');
    }

    /**
     * 编辑.
     *
     * @return \Illuminate\Http\Response
     */
    public function update($id)
    {
        $attributes = $this->request->all();
        $attributes['id'] = $id;

        $account = $this->packageService->store($attributes);

        return res($account, '修改成功');
    }

    /**
     * 删除.
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        $ids = $this->request->ids();

        $this->packageService->destroy($ids);

        return res(true, '删除成功');
    }
}
