<?php

namespace App\Http\Controllers;

use App\Models\CheckRecord;
use App\Models\CheckTrack;
use App\Models\DeviceRecord;
use App\Models\PartRecord;
use App\Models\SoftwareRecord;
use Illuminate\Http\JsonResponse;
use Pour\Base\Uni;

class CheckController extends Controller
{

    public function __construct()
    {
        $this->middleware('jwt.auth');
    }

    /**
     * 盘点
     * @param $string
     * @return JsonResponse
     */
    public function check($string): JsonResponse
    {
        $item = explode(':', $string)[0];
        $id = explode(':', $string)[1];
        if (!empty($item) && !empty($id)) {
            $check_record = CheckRecord::where('check_item', $item)
                ->where('status', 0)
                ->first();
            if (empty($check_record)) {
                $return = Uni::rr(404, '没有找到相对应的盘点任务');
                return response()->json($return);
            }
            switch ($item) {
                case 'part':
                    $item = PartRecord::where('id', $id)->first();
                    break;
                case 'software':
                    $item = SoftwareRecord::where('id', $id)->first();
                    break;
                default:
                    $item = DeviceRecord::where('id', $id)->first();
            }
            if (empty($item)) {
                $return = Uni::rr(404, '没有找到对应的物品');
            } else {
                $check_track = CheckTrack::where('check_id', $check_record->id)
                    ->where('item_id', $item->id)
                    ->first();
                $return = Uni::rr(200, '查询成功', $check_track);
            }
        } else {
            $return = Uni::rr(404, '参数不完整');
        }
        return response()->json($return);
    }

    /**
     * 盘点动作
     * @return JsonResponse
     */
    public function checkDo(): JsonResponse
    {
        $track_id = request('track_id') ?? null;
        $check_option = request('option') ?? null;
        if (!empty($track_id) && !empty($check_option)) {
            $user = auth('api')->user();
            $check_track = CheckTrack::where('id', $track_id)->first();
            if (empty($check_track)) {
                $return = Uni::rr(404, '没有找到盘点内容');
            } else {
                $check_track->status = $check_option;
                $check_track->checker = $user->id;
                $check_track->save();
                $return = Uni::rr(200, '操作成功');
            }
        } else {
            $return = Uni::rr(404, '参数不完整');
        }
        return response()->json($return);
    }
}
