<?php

namespace App\Http\Controllers\Deed;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Services\Deed\DeedService;
use Illuminate\Http\Request;

class DeedController extends Controller
{
    /**
     * 活跃的契约
     * @param  Request  $request
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function ings(Request $request)
    {
        $uid = Auth::auth();
        $page = $request->get('page') ?? 1;
        $pages = array(
            'limit' => 20,
            'page' => $page
        );
        $ds = new DeedService();
        $data = $ds->ings($uid, $pages);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 不活跃的契约
     * @param  Request  $request
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function succs(Request $request)
    {
        $uid = Auth::auth();
        $page = $request->get('page') ?? 1;
        $pages = array(
            'limit' => 20,
            'page' => $page
        );
        $ds = new DeedService();
        $data = $ds->succs($uid, $pages);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 查看契约信息
     * @param  int  $deed_id
     * @return array
     */
    public function show(int $deed_id)
    {
        $ds = new DeedService();
        $data = $ds->get($deed_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 查看契约信息
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function getbytua(Request $request)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'type' => 'required',
            'ack' => 'required',
        ]);
        $type = $request->post('type');
        $ack = $request->post('ack');
        $ds = new DeedService();
        $data = $ds->getByTUA($type, $uid, $ack);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 查看契约信息
     * @param  Request  $request
     * @return array
     * @throws \App\Exceptions\AlertException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function getbytuw(Request $request)
    {
        $uid = Auth::auth();
        $this->validate($request, [
            'type' => 'required',
            'way' => 'required',
            'way_id' => 'required',
        ]);
        $type = $request->post('type');
        $way = $request->post('way');
        $way_id = $request->post('way_id');
        $ds = new DeedService();
        $data = $ds->getByTUW($type, $uid, $way, $way_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }
}
