<?php

namespace App\Http\Controllers\admin;

use App\Constants\HttpStatus;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Exception;
use App\Models\Level as LevelModel;

class Level extends Controller
{

    /**
     * @api {get} /admin/level 会员等级列表
     *
     */
    function list()
    {
        try {
            $page = request()->input('page', 1);
            $limit = request()->input('limit', 15);
            
            $query = new LevelModel();

            $count = $query->count();
            $list = $query
                 ->forPage($page, $limit)
                ->orderBy("level")
                ->get()->toArray();
        } catch (ValidationException $e) {
            return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
        } catch (Exception $e) {
            return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
        }
        return $this->success(['total' => $count, 'data' => $list]);
    }

    /**
     * @api {post} /admin/level/update 修改
     */
    public function update()
    {
        DB::beginTransaction();
        try {
            $params = request()->validate([
                'id' => ['nullable'],
                'level' => ['required'],
                'level_name' => ['required'],
                'img' => ['required'],
                'recharge' => ['required'],
            ]);
            $id = $params['id'] ?? null;

            LevelModel::updateOrCreate(
                ['id' => $id],
                [
                    'level' => $params['level'],
                    'level_name' => $params['level_name'],
                    'img' => $params['img'],
                    'recharge' => $params['recharge'],
                ],
            );
            DB::commit();
        } catch (ValidationException $e) {
            DB::rollBack();
            return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
        } catch (Exception $e) {
            DB::rollBack();
            return $this->error(intval($e->getCode()), $e->getMessage());
        }
        return $this->success();
    }


    /**
     * @api {post} /admin/level/delete 删除
     */
    function delete()
    {
        DB::beginTransaction();
        try {
            request()->validate([
                'id' => ['required', 'integer', 'min:1', 'max:99999999'],
            ]);
            $id = request()->input('id', 0);
            $info = LevelModel::where('id', $id)->first();
            if (!$info) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
            $info->delete();
            DB::commit();
        } catch (ValidationException $e) {
            DB::rollBack();
            return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
        } catch (Exception $e) {
            DB::rollBack();
            return $this->error(intval($e->getCode()));
        }
        return $this->success();
    }


}