<?php

namespace Modules\Account\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use Modules\Account\Services\BillService;

class BillController extends BaseController
{
    protected $billService;

    public function __construct(BillService $billService)
    {
        $this->billService = $billService;
    }

    /**
     * 获取账单列表
     */
    public function index(Request $request)
    {
        try {
            $this->data['data'] = $this->billService->getList($request);
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

        return response()->json($this->data);
    }

    /**
     * 添加账单
     */
    public function add(Request $request)
    {
        try {
            $this->data['data'] = $this->billService->add($request);
            $this->data['message'] = '添加成功';
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

        return response()->json($this->data);
    }

    /**
     * 编辑账单
     */
    public function edit(Request $request)
    {
        try {
            $this->data['data'] = $this->billService->edit($request);
            $this->data['message'] = '编辑成功';
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

        return response()->json($this->data);
    }

    /**
     * 删除账单
     */
    public function delete(Request $request)
    {
        try {
            $this->data['data'] = $this->billService->delete($request);
            $this->data['message'] = '删除成功';
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

        return response()->json($this->data);
    }

    /**
     * 获取账单详情
     */
    public function detail(Request $request)
    {
        try {
            $this->data['data'] = $this->billService->detail($request);
        } catch (\Throwable $th) {
            $this->data['code'] = 0;
            $this->data['message'] = $th->getMessage();
        }

        return response()->json($this->data);
    }
}
