<?php

namespace App\Http\Controllers\System\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\System\DictionaryService;

class Dictionary extends Controller
{
    protected $dictionaryService;

    public function __construct(DictionaryService $dictionaryService)
    {
        $this->dictionaryService = $dictionaryService;
    }

    public function index(Request $request)
    {
        $result = $this->dictionaryService->getList($request->all());
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $result
        ]);
    }

    public function all()
    {
        $dictionaries = $this->dictionaryService->getAll();
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $dictionaries
        ]);
    }

    public function show(int $id)
    {
        $dictionary = $this->dictionaryService->getById($id);
        if (!$dictionary) {
            return response()->json([
                'code' => 404,
                'message' => '字典不存在',
                'data' => null
            ], 404);
        }

        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $dictionary
        ]);
    }

    public function store(Request $request)
    {
        try {
            $dictionary = $this->dictionaryService->create($request->all());
            return response()->json([
                'code' => 200,
                'message' => '创建成功',
                'data' => $dictionary
            ], 201);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => null
            ], 422);
        }
    }

    public function update(Request $request, int $id)
    {
        try {
            $dictionary = $this->dictionaryService->update($id, $request->all());
            return response()->json([
                'code' => 200,
                'message' => '更新成功',
                'data' => $dictionary
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => null
            ], 422);
        }
    }

    public function destroy(int $id)
    {
        try {
            $this->dictionaryService->delete($id);
            return response()->json([
                'code' => 200,
                'message' => '删除成功',
                'data' => null
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 400,
                'message' => $e->getMessage(),
                'data' => null
            ], 400);
        }
    }

    public function batchDelete(Request $request)
    {
        $this->dictionaryService->batchDelete($request->input('ids', []));
        return response()->json([
            'code' => 200,
            'message' => '批量删除成功',
            'data' => null
        ]);
    }

    public function batchUpdateStatus(Request $request)
    {
        $this->dictionaryService->batchUpdateStatus(
            $request->input('ids', []),
            $request->input('status', true)
        );
        return response()->json([
            'code' => 200,
            'message' => '批量更新状态成功',
            'data' => null
        ]);
    }

    public function getItemsList(Request $request)
    {
        $result = $this->dictionaryService->getItemsList($request->all());
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $result
        ]);
    }

    public function getAllItems()
    {
        $items = $this->dictionaryService->getAllItems();
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $items
        ]);
    }

    public function storeItem(Request $request)
    {
        try {
            $item = $this->dictionaryService->createItem($request->all());
            return response()->json([
                'code' => 200,
                'message' => '创建成功',
                'data' => $item
            ], 201);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => null
            ], 422);
        }
    }

    public function updateItem(Request $request, int $id)
    {
        try {
            $item = $this->dictionaryService->updateItem($id, $request->all());
            return response()->json([
                'code' => 200,
                'message' => '更新成功',
                'data' => $item
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 422,
                'message' => $e->getMessage(),
                'data' => null
            ], 422);
        }
    }

    public function destroyItem(int $id)
    {
        $this->dictionaryService->deleteItem($id);
        return response()->json([
            'code' => 200,
            'message' => '删除成功',
            'data' => null
        ]);
    }

    public function batchDeleteItems(Request $request)
    {
        $this->dictionaryService->batchDeleteItems($request->input('ids', []));
        return response()->json([
            'code' => 200,
            'message' => '批量删除成功',
            'data' => null
        ]);
    }

    public function batchUpdateItemsStatus(Request $request)
    {
        $this->dictionaryService->batchUpdateItemsStatus(
            $request->input('ids', []),
            $request->input('status', true)
        );
        return response()->json([
            'code' => 200,
            'message' => '批量更新状态成功',
            'data' => null
        ]);
    }
}
