<?php

namespace App\Http\Controllers\Lumina;

use App\Http\Controllers\Api\SIPHConnectController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Mongo;
use App\Models\Department;
use stdClass;

class WorklistController extends Controller
{
    public function __construct() { login_lumina(); }

    public function index(Request $r){
        // $view['tb_recorder'] = DB::table('tb_recorder')
        // ->orderby('recorder_id', 'desc')
        // ->get();
        // $w[] = array('statusjob', '!=', 'delete');
        // $w[] = array('statusjob', '!=', 'cancel');
        // $view['tb_case'] = Mongo::table('tb_case')->where($w)->limit(50)->orderby("_id","desc")->get();
        // $w[] = array('date', date("Ymd"));

        // $lumina = getCONFIG('lumina');
        $lumina = (object) Mongo::table('tb_lumina')->where('id', 1)->first();
        $def_dep = isset($lumina->default_department) ? $lumina->default_department : "OR";

        $view['tb_caseworklist'] = Mongo::table('tb_caseworklist')
        ->where('department' ,  $def_dep)
        ->orderBy('date', 'desc')->orderBy('time', 'desc')->limit(100)->get();

        $obj = new stdClass();
        $siph_api = new SIPHConnectController;
        $view['tb_caseworklist']->transform(function($wk) use ($obj, $siph_api){
            $obj->procedure = @$wk['proceduredescription']."";
            $match_procedure = $siph_api->match_procedure($obj);
            $wk['match_procedure'] = isset($match_procedure[0]) ? jsonDecode($match_procedure)[0] : get_key_config("procedure", 'lumina');
            return $wk;
        });

        $view['scope_type']     = get_config_scope();
        $view['pacs']           = getCONFIG("pacs");
        $view['pacsserver']     = configTYPE("pacs","pacsserver");
        $view['pacsport']       = configTYPE("pacs","port");
        $view['doctor']         = Department::userActive("doctor");
        $view['page_type']      = @$r->type."" == 'register' ? true : false;
        $view['instant_cid']    = @$r->instant_cid."" != "" ? $r->instant_cid : '';
        return view('lumina.worklist.index',$view);
    }

    public function show($id){
    }

    public function store(Request $r){

    }
}
