<?php

namespace App\Community\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\Schedule;
use App\Models\SchedulePeriod;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;

class HomeController extends Controller
{
    public function index(){
        return redirect('/cdms/home');
    }
    public function home(Content $content)
    {
        if(!Admin::user()->isAdministrator()){
            return redirect('/cdms/notices');
        }
        return $content
            ->title('数据概况')
            ->description('社区段实时数据')
            ->row(function (Row $row){
                   return $row->column(12,'<p style="font-family:Arial,Helvetica,sans-serif; font-size:1.2em; margin-top20px;vertical-align:middle; font-weight:normal ">实时概况</p>');
            })->row(function (Row $row){
                $data = StatController::service();
                $row->column(3,new InfoBox('今日预约(人)', 'users', 'aqua', '/cdms/home', $data['schedule_paitent']));
                $row->column(3,new InfoBox('今日排班(人)', 'calendar-check-o', 'aqua', '/cdms/home', $data['schedule_docter']));
                $row->column(3,new InfoBox('接诊患者(人次)', 'user', 'aqua', '/cdms/home', $data['paitent_num']));
            })
            ->row(function (Row $row){
                return $row->column(12,'<p style="font-family:Arial,Helvetica,sans-serif; font-size:1.2em; margin-top20px;vertical-align:middle; font-weight:normal ">疫苗概况</p>');
            })->row(function (Row $row){
                $data = StatController::vaccine();
                $row->column(3,new InfoBox('疫苗种类(种)', 'eyedropper', 'aqua', '/cdms/home', $data['sum_num']));
                $row->column(3,new InfoBox('库存剩余(支)', 'home', 'aqua', '/cdms/home', $data['have_num']));
                $row->column(3,new InfoBox('疫苗缺苗(种)', 'genderless', 'aqua', '/cdms/home', $data['less_num']));
            })
            ->row(function (Row $row){
                $data = StatController::vaccine();
                $row->column(3,new InfoBox('今日预约(人支)', 'hospital-o', 'aqua', '/cdms/home', $data['today_schedule']));
                $row->column(3,new InfoBox('预计今日消耗(支)', 'calendar', 'aqua', '/cdms/home', $data['today_vaccine']));
            })
            ->row(function (Row $row){
                return $row->column(12,'<p style="font-family:Arial,Helvetica,sans-serif; font-size:1.2em; margin-top20px;vertical-align:middle; font-weight:normal ">订单概况</p>');
            })->row(function (Row $row){
                $data = StatController::nurse();
                $row->column(3,new InfoBox('门诊订单(个)', 'first-order', 'aqua', '/cdms/home', $data[0]));
                $row->column(3,new InfoBox('计免订单(个)', 'first-order', 'aqua', '/cdms/home', $data[1]));
                $row->column(3,new InfoBox('儿保订单(个)', 'first-order', 'aqua', '/cdms/home', $data[2]));
            })
            ->row(function (Row $row){
                return $row->column(12,'<p style="font-family:Arial,Helvetica,sans-serif; font-size:1.2em; margin-top20px;vertical-align:middle; font-weight:normal ">今日排班         </p>');
            })->row(function (Row $row){
                $data = StatController::schedule();
                $row->column(3,new InfoBox('计免医生(人)', 'hospital-o', 'aqua', '/cdms/home', $data['vaccine_num']));
                $row->column(3,new InfoBox('儿保医生(人)', 'hospital-o', 'aqua', '/cdms/home', $data['nurse_num']));
                $row->column(3,new InfoBox('门诊医生(人)', 'hospital-o', 'aqua', '/cdms/home', $data['clinic_num']));
            });
//            ->row($this->real_data())
//            ->row($this->vaccine_data())
//            ->row($this->nurse_data())
//            ->row($this->schedule_data());
    }

    public function real_data()
    {
        $data = StatController::service();
        return view('cdms.real_data',compact('data'));
    }

    public function vaccine_data()
    {
        $data = StatController::vaccine();
        return view('cdms.vaccine_data',compact('data'));
    }

    public function nurse_data()
    {
        $data = StatController::nurse();
        return view('cdms.nurse_data',compact('data'));
    }

    public function schedule_data()
    {
        $data = StatController::schedule();
        return view('cdms.schedule_data',compact('data'));
    }

    public function order_detail()
    {
        $id = request('id');
        if(empty($id)){
            return redirect('/cdms/docter_chats');
        }
        $data = Order::where('id',$id)->with('orderPatient')->first();
        if(!empty($data->orderPatient->medical_imgs)){
            $data->orderPatient->medical_imgs = object_array(json_decode($data->orderPatient->medical_imgs ));
        } else {
            $data->orderPatient->medical_imgs = [];
        }
        $content = new Content();
        $status = Order::getStatus();
        $content->view('cdms.order_detail',compact('data','status'));

        return $content;
    }

    public function schedule(){
        $time = redirect('time');
        $time = '2020-12-07至2020-12-13';

        $data = sechedule_timeperiod();
        $org_id = Admin::user()->org_id;
        $where = [];
        if($org_id){
            $where['organization_id'] = $org_id;
        }

        if($time){

        } else {
            $where['schedule_date']=date('Y-m-d',strtotime('this week'));
        }

        Schedule::where($where)->pluck('id');
    }
}
