<?php

namespace admin\action;

class uAction  extends \action
{
    private $person;
    private $userid;
    private $userRow;
	public function __construct()
    {
		$this->userid = \mvc::$req['u'];
		$this->person = new \model\doctor\person();
		$this->userRow = $this->person->getUserRow($this->userid);
		$_SESSION['tmpUserID'] = $this->userid;
	}
	
    public function viewSet($u='')
    {
        if($u!=''){
            $this->userid = $u;
    		$this->userRow = $this->person->getUserRow($this->userid);
        }
	    \view::set('USERID',$this->userid);
	    \view::set('user',$this->userRow);
	    \view::set('headerTitle',$this->userRow['au_name'].' '.($this->userRow['au_sex']=='男'?'先生':'女士'));
	    \view::set('req',\mvc::$req);
    }    
    
    public function p($u)
    {
	    $this->viewSet();
	    \view::put();
    }    
    
    public function y()
    {
	    \view::put();
    }    
    
    public function my()
    {
	    $this->viewSet();
	    \view::put();
    }    
    
    public function record()
    {
	    $this->viewSet();
	    \view::put();
    }    
    
    public function report()
    {
	    $this->viewSet();
	    \view::put();
    }    
    
    public function weekMonthArea($day)
    {
        $result = [];
        $result['day'] = $day;
        $objDay = new \DateTime();
        $objDay->setTimestamp(strtotime($day));
        $firstDay = clone $objDay;
        $firstDay->modify('this week');
        $lastDay = clone $objDay;
        $lastDay->modify('this sunday');        
        $result['weekFirst'] = $firstDay->format('Y-m-d');
        $result['weekLast'] = $lastDay->format('Y-m-d');
        
        $firstDay->modify('first day of this month');
        $lastDay->modify('last day of this month');        
        $result['monthFirst'] = $firstDay->format('Y-m-d');
        $result['monthLast'] = $lastDay->format('Y-m-d');
        return $result;
    }    

    public function bloodRecord()
    {
        $maxDay = $this->person->getBloodMaxDayByUser($this->userid,'');
        $day = new \DateTime();
        $days = $this->weekMonthArea($maxDay);
	    \view::set('days',$days);
	    $this->viewSet();
	    \view::put();
    }    
    
    public function bloodReport()
    {
        $reportRows = $this->person->getReportRows($this->userid);
	    \view::set('reportRows',$reportRows);
	    $this->viewSet();
	    \view::put();
    } 
    
    public function bloodSumReport()
    {
        $req = \mvc::$req;
        $maxDay = $this->person->getBloodMaxDayByUser($this->userid,'');
        $days['beginDate'] = date('Y-m',strtotime("-5 month",strtotime($maxDay)));
        $days['endDate'] = date('Y-m',strtotime($maxDay));
        $days['sunBeginTime'] = '07:30';
        $days['sunEndTime'] = '23:30';
	    \view::set('days',$days);
	    $this->viewSet();
	    \view::put();
    } 
    
    public function getBloodSumReport()
    {
        $req = \mvc::$req;
        $data = $this->person->getSumReport($this->userid,$req);
        $this->successJson($data);
    } 
    
    public function timeBeginEnd($type,$day1,$day2,$value1,$value2)
    {
        $result = [
            'day1'=>$day1,
            'day2'=>$day2,
            'beginTime'=>'',
            'endTime'=>'',
        ];
        if($type=='1'){
            $result['day1'] = date('Y-m-d',strtotime($day1.' 00:00:00') + $value1 * 24 * 60 * 60);
            $result['beginTime'] = date('Y-m-d H:i:s',strtotime($result['day1'].' 00:00:00'));
            $result['endTime'] = date('Y-m-d H:i:s',strtotime($result['day1']) + 24 * 60 * 60 - 1);
        }else if($type=='2'){
            $result['day1'] = date('Y-m-d',strtotime($day1.' 00:00:00') + $value1 * 7 * 24 * 60 * 60);
            $result['day2'] = date('Y-m-d',strtotime($day2.' 00:00:00') + $value1 * 7 * 24 * 60 * 60);
            $result['beginTime'] = date('Y-m-d H:i:s',strtotime($result['day1'].' 00:00:00'));
            $result['endTime'] = date('Y-m-d H:i:s',strtotime($result['day2']) + 24 * 60 * 60 - 1);
        }else if($type=='3'){
            if($value1==0){
                $result['day1'] = date('Y-m-d',strtotime('first day of this month', strtotime($result['day1'])));
            }else{
                $rule = ($value1>0) ? "+1 month" : "-1 month";
                $result['day1'] = date('Y-m-d',strtotime($rule,strtotime($day1)));
            }
            $result['day2'] = date('Y-m-d',strtotime('last day of this month', strtotime($result['day1'])));
            $result['beginTime'] = date('Y-m-d H:i:s',strtotime($result['day1'].' 00:00:00'));
            $result['endTime'] = date('Y-m-d H:i:s',strtotime($result['day2']) + 24 * 60 * 60 - 1);
        }else if($type=='4' && $value1 && $value2){
            $result['beginTime'] = date('Y-m-d H:i:s',strtotime($value1.' 00:00:00'));
            $result['endTime'] = date('Y-m-d H:i:s',strtotime($value2.' 00:00:00') + 24 * 60 * 60 - 1);
        }
        //if($day1 == '2024-01-03') dd($type,$day1,$day2,$value1,$value2,$result);
        return $result;
    }    
    
    
    public function getBloodRecord()
    {
        $req = \mvc::$req;
        $beArr = $this->timeBeginEnd($req['type'],$req['day1'],$req['day2'],$req['value1'],$req['value2']);
        $rows = $this->person->getBloodRecord($this->userid,$beArr['beginTime'],$beArr['endTime']);
        //$blood = new \model\doctor\blood();
        
        //$sumArr = $blood->sumInfo($rows);
        foreach($rows as $k=>$row){
		    $time = date('H:i',strtotime('2000-01-01 '.$row['bd_time'].':00'));
		    $sbp = $row['bd_sbp']*1;
		    $dbp = $row['bd_dbp']*1;
		    $heart = $row['bd_heart_rate']*1;
		    $pulse = $sbp - $dbp;
		    $map = floor(($sbp - $dbp) / 3 + $dbp);

		    $sumArr['sbp']['list'][$k] = $sbp;
		    $sumArr['dbp']['list'][$k] = $dbp;
		    $sumArr['heart']['list'][$k] = $heart;
		    $sumArr['pulse']['list'][$k] = $pulse;
		    $sumArr['map']['list'][$k] = $map;
		    if($row['bd_hand']=='L'){
    		    $sumLeftArr['sbp']['list'][$k] = $sbp;
    		    $sumLeftArr['dbp']['list'][$k] = $dbp;
    		    $sumLeftArr['heart']['list'][$k] = $heart;
    		    $sumLeftArr['pulse']['list'][$k] = $pulse;
    		    $sumLeftArr['map']['list'][$k] = $map;
		    }else if($row['bd_hand']=='R'){
    		    $sumRightArr['sbp']['list'][$k] = $sbp;
    		    $sumRightArr['dbp']['list'][$k] = $dbp;
    		    $sumRightArr['heart']['list'][$k] = $heart;
    		    $sumRightArr['pulse']['list'][$k] = $pulse;
    		    $sumRightArr['map']['list'][$k] = $map;
		    }
        }
        if($sumArr){
    		$sumArr['sbp']['average'] = round(array_sum($sumArr['sbp']['list']) / count($sumArr['sbp']['list']));
    		$sumArr['dbp']['average'] = round(array_sum($sumArr['dbp']['list']) / count($sumArr['dbp']['list']));
    		$sumArr['heart']['average'] = round(array_sum($sumArr['heart']['list']) / count($sumArr['heart']['list']));
    		$sumArr['pulse']['average'] = round(array_sum($sumArr['pulse']['list']) / count($sumArr['pulse']['list']));
    		$sumArr['map']['average'] = round(array_sum($sumArr['map']['list']) / count($sumArr['map']['list']));
            $sumArr['count'] = count($sumArr['sbp']['list']);
        }
        if($sumLeftArr){
    		$sumLeftArr['sbp']['average'] = round(array_sum($sumLeftArr['sbp']['list']) / count($sumLeftArr['sbp']['list']));
    		$sumLeftArr['dbp']['average'] = round(array_sum($sumLeftArr['dbp']['list']) / count($sumLeftArr['dbp']['list']));
    		$sumLeftArr['heart']['average'] = round(array_sum($sumLeftArr['heart']['list']) / count($sumLeftArr['heart']['list']));
    		$sumLeftArr['pulse']['average'] = round(array_sum($sumLeftArr['pulse']['list']) / count($sumLeftArr['pulse']['list']));
    		$sumLeftArr['map']['average'] = round(array_sum($sumLeftArr['map']['list']) / count($sumLeftArr['map']['list']));
            $sumLeftArr['count'] = count($sumLeftArr['sbp']['list']);
        }
        
        if($sumRightArr){
    		$sumRightArr['sbp']['average'] = round(array_sum($sumRightArr['sbp']['list']) / count($sumRightArr['sbp']['list']));
    		$sumRightArr['dbp']['average'] = round(array_sum($sumRightArr['dbp']['list']) / count($sumRightArr['dbp']['list']));
    		$sumRightArr['heart']['average'] = round(array_sum($sumRightArr['heart']['list']) / count($sumRightArr['heart']['list']));
    		$sumRightArr['pulse']['average'] = round(array_sum($sumRightArr['pulse']['list']) / count($sumRightArr['pulse']['list']));
    		$sumRightArr['map']['average'] = round(array_sum($sumRightArr['map']['list']) / count($sumRightArr['map']['list']));
            $sumRightArr['count'] = count($sumRightArr['sbp']['list']);
        }

        $this->successJson([
            'day1'=>$beArr['day1'],
            'day2'=>$beArr['day2'],
            'rows'=>$rows,
            'sumArr'=>$sumArr,
            'sumLeftArr'=>$sumLeftArr,
            'sumRightArr'=>$sumRightArr,
        ]);
    }    

    public function dataRecordDate()
    {
        $req = \mvc::$req;
        $rows = $this->person->getDataRecordDate($this->userid);
	    \view::set('rows',$rows);
	    $this->viewSet();
	    \view::put();
    }    

    public function dataRecord()
    {
        $req = \mvc::$req;
        
        $rule = $this->person->getDataRecordById($req['did']);
        //$rows = $this->person->getDataRecordByTime($this->userid,$rule['ddate'],$rule['dhour'],$rule['dremark']);
        $rows = $this->person->getDataRecordByTime($this->userid,$rule['ddate'],'',$rule['dremark']);
        $userRow = $this->person->getUserRow($this->userid);
        $rows = $this->person->checkDataRecord($this->userid,$rows,$userRow);
        $bigArr = [];
        foreach ($rows as $row){
            if(!in_array($row['cbig'],$bigArr)) $bigArr[] = $row['cbig'];
        }
	    \view::set('rule',$rule);
	    \view::set('rows',$rows);
	    \view::set('bigArr',$bigArr);
	    $this->viewSet();
	    \view::put();
    }    
    
    public function dataRecordLine()
    {
        $req = \mvc::$req;
        $cate = $this->person->getCateById($this->userid,$req['cid']);
        $rows = $this->person->getDataRecordByCid($this->userid,$req['cid']);
        $userRow = $this->person->getUserRow($this->userid);
        $rows = $this->person->checkDataRecord($this->userid,$rows,$userRow);
        $rule = $this->person->getRefeRule($cate['crefetxt2'] ? $cate['crefetxt2']: $cate['crefe'],$req['cid'],$userRow);
	    \view::set('cate',$cate);
	    \view::set('rule',$rule);
	    \view::set('rows',$rows);
	    $this->viewSet();
	    \view::put();
    }  
    
    public function dataRecordSum()
    {
        $req = \mvc::$req;
        $res = $this->person->getDataRecordSum($this->userid);
        $userRow = $this->person->getUserRow($this->userid);
        foreach($res['data'] as $k=>$rows ){
            $res['data'][$k] = $this->person->checkDataRecord($this->userid,$rows,$userRow);
        }
	    \view::set('targets',$res['targets']);
	    \view::set('rows',$res['data']);
	    $this->viewSet();
	    \view::put();
    }  
    
    
    public function medicine()
    {
        $req = \mvc::$req;
        $rows = $this->person->medicine($this->userid);
        $userRow = $this->person->getUserRow($this->userid);
	    \view::set('rows',$rows);
	    \view::set('userRow',$userRow);
	    $this->viewSet();
	    \view::put();
    }  
    
    
    public function examine()
    {
        $req = \mvc::$req;
        $beginDate='';
        $endDate='';
        $keyword = $req['keyword'] ?:'';
        switch ($req['rang']) {
            case 'week':
                $endDate = date('Y-m-d');
                $beginDate = date("Y-m-d", strtotime("-7 days", strtotime($endDate)));
                break;
            case 'month':
                $endDate = date('Y-m-d');
                $beginDate = date("Y-m-d", strtotime("-31 days", strtotime($endDate)));
                break;
            case 'year':
                $endDate = date('Y-m-d');
                $beginDate = date("Y-m-d", strtotime("-365 days", strtotime($endDate)));
                break;
        }
        $rows = $this->person->examine($this->userid,$beginDate,$endDate,$keyword);
        $userRow = $this->person->getUserRow($this->userid);
	    \view::set('rows',$rows);
	    \view::set('userRow',$userRow);
	    $this->viewSet();
	    \view::put();
    }  
    
    public function pdf()
    {
        $req = \mvc::$req;
	    $this->viewSet();
	    \view::put();
    }  
    
    public function measureList()
    {
        $req = \mvc::$req;
        $cates = (new \model\element('dt_measure_type'))->getRowsAll();
        $bigCates = [];
        foreach($cates as $k=>$cate){
            $bigCates[] = $cate['meat_name'];
        }
        $bigCates = array_unique($bigCates);
		\view::set('bigCates',$bigCates);
	    $this->viewSet();
	    \view::put();
    }  
    public function measureView()
    {
        $req = \mvc::$req;
        $bigCate = $req['bigCate'];
        $beginDate='';
        $endDate='';
        switch ($req['rang']) {
            case 'week':
                $endDate = date('Y-m-d');
                $beginDate = date("Y-m-d", strtotime("-7 days", strtotime($endDate)));
                break;
            case 'month':
                $endDate = date('Y-m-d');
                $beginDate = date("Y-m-d", strtotime("-31 days", strtotime($endDate)));
                break;
            case 'year':
                $endDate = date('Y-m-d');
                $beginDate = date("Y-m-d", strtotime("-365 days", strtotime($endDate)));
                break;
        }
        $rows = $this->person->getMeasure($this->userid,$bigCate,$beginDate,$endDate);
        $crefetxt = $this->person->getMeasureRefetxt($bigCate);
	    \view::set('rows',$rows);
	    \view::set('crefetxt',$crefetxt);
	    $this->viewSet();
	    \view::put();
    }  
    
    
    public function edicaldoc()
    {
        $req = \mvc::$req;
        $cates = $this->person->getEdicaldocCates();
        $rows = $this->person->getEdicaldoc();
	    \view::set('cates',$cates);
	    \view::set('rows',$rows);
	    $this->viewSet();
	    \view::put();
    }  
}