<?php


namespace App\Community\Controllers;


use App\Models\Organization;
use App\Models\SchedulePeriod;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;

class ScheduleStatController extends Admin
{
    public function schedule()
    {
        $org_id = Admin::user()->org_id;
        if(empty($org_id)){
            $org_id = 0;
        }
        $scheduleType = request('t',1);

        $weekStartTime = strtotime('this week');
        for ($i=1;$i<=7;$i++){
            $time = $weekStartTime + ($i-1)*86400;
            $date = date('Y-m-d',$time);
            $amCount = SchedulePeriod::where(['organization_id'=>$org_id,'schedule_date'=>$date,'type'=>1,'schedule_type'=>$scheduleType])->count();
            $pmCount = SchedulePeriod::where(['organization_id'=>$org_id,'schedule_date'=>$date,'type'=>2,'schedule_type'=>$scheduleType])->count();
            $evCount = SchedulePeriod::where(['organization_id'=>$org_id,'schedule_date'=>$date,'type'=>3,'schedule_type'=>$scheduleType])->count();
            $week[$i] = [$amCount,$pmCount,$evCount];
        }

        $data['week'] = $week;
        $data['timeType'] = ['上午','下午','晚上'];
        $data['name'] = Organization::where(['id'=>$org_id])->value('name');
        $content = new Content();
        $types = [1=>'门诊',2=>'计免','3'=>'儿保'];
        $content->title($data['name']);
        $content->description($types[$scheduleType].'排班统计');
        $content->view('cdms.totalSchedule',['data'=>$data]);

        return $content;
    }

}
