<?php

namespace App\Http\Controllers\Api\mini;

use App\Models\InnerDevice;
use App\Models\InnerDeviceNamesModel;
use App\Models\Option;
use App\Models\Order;
use App\Models\OrderDevice;
use App\Models\WorkPoint;
use Illuminate\Http\Request;

class InnerDeviceController extends BaseController
{
    protected $model;

    public function __construct()
    {
        $this->model = new InnerDevice();
    }

    public function get(Request $request)
    {
        if($request->input('type') == 'drop_menu') {
            $items = $this->model->get()->pluck('name')->unique()->map(function($item) {
                return collect(['text' => $item, 'value' => $item]);
            });
            $items = $items->prepend(collect(['text' => '设备名称', 'value' => '']));
            return $this->success(['data' => $items]);
        }
        $items = $this->model->with('device_name', 'spec', 'project');
        $equal_items = ['work_point_id', 'project_id'];
        $project_id = $request->input('project_id');
        if(!empty($project_id)){
            $device_ids = OrderDevice::where('project_id',$project_id)->whereHas('order',function ($query){
                $query->where('status','=',2)->where('level','=',2)->whereIn('last_project_role_id',[5,7]);
            })->pluck('inner_device_id')->toArray();
            $items = $items->whereIn('id',$device_ids);
        }
        foreach ($equal_items as $equal_item) {
            if($request->input($equal_item)) {
                $items = $items->where($equal_item, '=', $request->input($equal_item));
            }
        }
        if($request->input('status') == 'using') {
            $using_id = Option::get('inner_devices', 'status', 'using');
            $items = $items->where(['status'=>$using_id]);
        }
        $items = $items->get();
        return $this->success(['data' => $items]);
    }

    public function search(Request $request)
    {
        $items = $this->model->with('workPoint', 'spec', 'device', 'project');
        $equal_items = [ 'device_name_id', 'spec_name'];
        foreach ($equal_items as $item) {
            if($request->input($item)) {
                $items = $items->where($item, '=', $request->input($item));
            }
        }
        $like_items = ['number'];
        foreach ($like_items as $item) {
            if($request->input($item)) {
                $keyword = '%' . $request->input($item) . '%';
//                $items = $items->where($item, 'like', $keyword);
                $items->where(function ($query) use ($keyword) {
                    $query->where('number', 'like', $keyword)
                        ->orWhere('spec_name', 'like', $keyword);
                });
            }
        }
        $in_items = ['project_ids', 'work_point_ids'];
        $key_items = ['project_id', 'work_point_id'];
        foreach ($in_items as $key => $item) {
            if($request->input($item)) {
                $ids = collect($request->input($item))->filter(function($id) {
                    return $id;
                });
                if($ids->count() > 0) {
                    $items = $items->whereIn($key_items[$key], $ids);
                }
            }
        }
//        $items = $items->orderBy('created_at');
        $stat_items = [];
        if($request->input('stat')) {
            $stat_items = $this->getStat($items);
        }


        $status_field = $request->input('status');
        if($status_field) {
            $free_id = Option::get('inner_devices', 'status', $status_field, 'id');
            $items = $items->where('status', $free_id);
        }

        $items = $items->paginate();
        foreach($items as $item) {
            $item->status = Option::find($item->status);
        }
        return $this->success(['data' => $items->items(), 'stat' => $stat_items]);
    }

    public function getStat($items)
    {
        $items = $items->get();
        $types = ['free', 'using', 'repair', 'scrap'];
        $result = [];
        foreach ($types as $type) {
            $id = Option::get('inner_devices', 'status', $type);
            $num = $items->where('status', $id)->count();
            $result[$type] = $num;
        }
        return $result;
    }

    public function detail(Request $request)
    {
        $item = $this->model->with('workPoint', 'spec', 'device', 'device_name', 'project')->find($request->input('id'));
        if(!$item) $this->error(['msg' => '未找到设备']);
        $item['status'] = Option::find($item['status']);
        return $this->success(['data' => $item]);
    }

    //设备三级联动
    public function getDeviceName(Request $request)
    {
        $items = InnerDeviceNamesModel::select( 'name as text','id as value')->get();
        $items = $items->prepend($this->transObject(['text' => '设备名称', 'value' => '']));

        return $this->success(['data' => $items]);
    }

    public function transObject($items)
    {
        return json_decode(json_encode($items));
    }
}
