<?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()->inroles(['administrator','manager'])){
            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::orderTotal();
                $row->column(3,new InfoBox('门诊订单总数(个)', 'users', 'aqua', '/cdms/orders', $data[0]));
                $row->column(3,new InfoBox('儿保订单总数(个)', 'calendar-check-o', 'aqua', '/cdms/nurse_users', $data[1]));
                $row->column(3,new InfoBox('计免订单总数(个)', 'user', 'aqua', '/cdms/order_vaccines', $data[2]));
                $row->column(3,new InfoBox('服务患者次数(次)', 'user', 'aqua', '/cdms/home', $data[3]));
            })
            ->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::clinc();
                $today = 'orderPatient[appoint_start_time][start]='.date('Y-m-d 00:00:00',time()).'&orderPatient[appoint_start_time][end]='.date('Y-m-d 00:00:00', strtotime('+1days'));
                $yersterday = 'orderPatient[appoint_start_time][start]='.date('Y-m-d 00:00:00',strtotime('-1 days')).'&orderPatient[appoint_start_time][end]='.date('Y-m-d 00:00:00', time());
                $row->column(3,new InfoBox('今日预约数量(人)', 'users', 'aqua', '/cdms/orders?'.$today, $data['today']));
//                $row->column(3,new InfoBox('疫苗种类(种)', 'eyedropper', 'aqua', '/cdms/home', $data['sum_num']));
                $row->column(3,new InfoBox('今日排班人数（人)', 'user', 'aqua', '/cdms/total_schedule?t=1', $data['docters']));
                $row->column(3,new InfoBox('昨日预约数量(人)', 'user', 'aqua', '/cdms/orders?'.$yersterday, $data['yesterday']));
                $row->column(3,new InfoBox('昨日接诊人数(人)', 'user', 'aqua', '#', $data['people']));

            })
            ->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();
                $today = 'orderPatient[appoint_start_time][start]='.date('Y-m-d 00:00:00',time()).'&orderPatient[appoint_start_time][end]='.date('Y-m-d 00:00:00', strtotime('+1days'));
                $yersterday = 'orderPatient[appoint_start_time][start]='.date('Y-m-d 00:00:00',strtotime('-1 days'))
                    .'&orderPatient[appoint_start_time][end]='.date('Y-m-d 00:00:00', time());
                $tommrow = 'orderPatient[appoint_start_time][start]='.date('Y-m-d 00:00:00',strtotime('+1 days'))
                    .'&orderPatient[appoint_start_time][end]='.date('Y-m-d 00:00:00', strtotime('+2 days'));


                $row->column(3,new InfoBox('今日预约数量(人)', 'users', 'aqua', '/cdms/order_vaccines?'.$today, $data['today']));
                $row->column(3,new InfoBox('昨日预约数量(人)', 'user', 'aqua', '/cdms/order_vaccines?'.$yersterday, $data['yesterday']));
                $row->column(3,new InfoBox('昨日接种人数(人)', 'user', 'aqua', '/cdms/order_vaccines?status=4&'.$yersterday, $data['people']));
                $row->column(3,new InfoBox('明日预约数量(人)', 'user', 'aqua', '/cdms/order_vaccines?'.$tommrow, $data['tomorrow']));
            })
            ->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();
                $today = 'patients[appoint_start_time][start]='.date('Y-m-d 00:00:00',time()).'&patients[appoint_start_time][end]='.date('Y-m-d 00:00:00', strtotime('+1days'));
                $yersterday = 'patients[appoint_start_time][start]='.date('Y-m-d 00:00:00',strtotime('-1 days'))
                    .'&patients[appoint_start_time][end]='.date('Y-m-d 00:00:00', time());
                $tommrow = 'patients[appoint_start_time][start]='.date('Y-m-d 00:00:00',strtotime('+1 days'))
                    .'&patients[appoint_start_time][end]='.date('Y-m-d 00:00:00', strtotime('+2 days'));

                $row->column(3,new InfoBox('今日预约数量(人)', 'users', 'aqua', '/cdms/order_nurses?'.$today, $data['today']));
                $row->column(3,new InfoBox('昨日预约数量(人)', 'user', 'aqua', '/cdms/order_nurses?'.$yersterday, $data['yesterday']));
                $row->column(3,new InfoBox('昨日接种人数(人)', 'user', 'aqua', '/cdms/order_nurses?orders[order_status]=4&'.$yersterday, $data['people']));
                $row->column(3,new InfoBox('明日预约数量(人)', 'user', 'aqua', '/cdms/order_nurses?'.$tommrow, $data['tomorrow']));
            })
            ->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::vaccineToal();
                $yersterday = 'orderPatient[appoint_start_time][start]='.date('Y-m-d 00:00:00',strtotime('-1 days'))
                    .'&orderPatient[appoint_start_time][end]='.date('Y-m-d 00:00:00', time());

                $row->column(3,new InfoBox('疫苗总库存(支)', 'first-order', 'aqua', '/cdms/vaccines', $data['have_num']));
                $row->column(3,new InfoBox('疫苗库存告急(中)', 'first-order', 'aqua', '/cdms/vaccines', $data['less_num']));
                $row->column(3,new InfoBox('昨日接种消耗(支)', 'first-order', 'aqua', '/cdms/order_vaccines?order_status=4&'.$yersterday, $data['yesterday']));
            });
    }



    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');
    }
}
