<?php
namespace Biz\Datetime;
/**
 * basetime
 */
class DateHandles {
	/**
	 * 时间信息获取
	 * @var string
	 */
	/*
	*@params $params type array [类型：pre上一个，cur当前，next下一个；指定日期；日期区间起始时间；天数][日期区间截止时间]
	*return res type array [start:起始时间][end:截止时间]
	*/
	/*
"date_circle":[{"year":{"type":(-1,0,1)},"month":{"type":(-1,0,1),"num":[8,18,28],"range":{"start":"2","end":"29"}},"week":["type":(-1,0,1),,"num":[1,2,3],"range":{"start":"2","end":"7"}],"day":["type":(-1,0,1),,"num":[1,2,3],"range":{"start":"2","end":"7"}]}
*/
    //获取月份相关时间
    function GetDateDetailsBymonth($params=array()){
        /*$params = array(
            'type' => -1,
            'num' =>array(8,18,28),
            'range' => array(['start'=>2,'end'=>29]),
        );*/
        if(empty($params))return -52101;
        $res = array();
        $par = array();
        $mo = C()->get('months');
        $dates = C()->get('dates');
        $areas = C()->get('areas');
        $v = isset($params['type'])?$params['type']:0;
        if($v==-1){
            $par[0] = 'pre';
            $m = date('Y-m',strtotime('-1month'));
        }else if($v==0){
            $par[0] = 'cur';
            $m = date('Y-m',time());
        }else{
            $par[0] = 'next';
            $m = date('Y-m',strtotime('+1month'));
        }
        $par[0] = ($v==-1)?'pre':($v==0?'cur':'next');
        $res['type'][] = $mo->parseTime($par);
        if(isset($params['num']) && count($params['num'])>0){
            foreach ($params['num'] as $nk => $nv) {
                $par[0] = $m.'-'.$nv;
                $res['num'][] = $dates->parseTime($par);
            }
        }
        if(isset($params['range']) && count($params['range'])>0){
            if(isset($params['range']['start'])){
                $par[0] = $m.'-'.$params['range']['start'];
                $par[1] = $m.'-'.$params['range']['end'];
                $res['range'][] = $areas->parseTime($par);
            }else{
                foreach ($params['range'] as $nk => $nv) {
                    $par[0] = $m.'-'.$nv['start'];
                    $par[1] = $m.'-'.$nv['end'];
                    $res['range'][] = $areas->parseTime($par);
                }
            }  
        }
        return $res;
    }

    //获取日期相关时间
    function GetDateDetailsByday($params=array()){
        /*$params = array(
            'type' => 1,
            'num' =>array(8,18,22),
            'range' => array('start'=>2,'end'=>22),
        );*/
        if(empty($params))return -52101;
        $res = array();
        $par = array();
        $days = C()->get('days');
        $hours = C()->get('hours');
        $areah = C()->get('areah');
        $v = isset($params['type'])?$params['type']:0;
        if($v==-1){
            $par[0] = 'pre';
            $d = date('Y-m-d',strtotime('-1day'));
        }else if($v==0){
            $par[0] = 'cur';
            $d = date('Y-m-d',time());
        }else{
            $par[0] = 'next';
            $d = date('Y-m-d',strtotime('+1day'));
        }
        $par[0] = ($v==-1)?'pre':($v==0?'cur':'next');
        $res['type'][] = $days->parseTime($par);
        if(isset($params['num']) && count($params['num'])>0){
            foreach ($params['num'] as $nk => $nv) {
                $par[0] = $d.' '.$nv.':00:00';
                $res['num'][] = $hours->parseTime($par);
            }
        }
        if(isset($params['range']) && count($params['range'])>0){
            if(isset($params['range']['start'])){
                $par[0] = $d.' '.$params['range']['start'].':00:00';
                $par[1] = $d.' '.$params['range']['end'].':00:00';
                $res['range'][] = $areah->parseTime($par);
            }else{
                foreach ($params['range'] as $nk => $nv) {
                    $par[0] = $d.' '.$nv['start'].':00:00';
                    $par[1] = $d.' '.$nv['end'].':00:00';
                    $res['range'][] = $areah->parseTime($par);
                }
            }  
        }
        return $res;
    }

    //获取年相关时间
    function GetDateDetailsByyear($params=array()){
        /*$params = array(
            'type' => 1,
        );*/
        if(empty($params))return -52101;
        $res = array();
        $par = array();
        $years = C()->get('years');
        $v = isset($params['type'])?$params['type']:0;
        $par[0] = ($v==-1)?'pre':($v==0?'cur':'next');
        $res['type'][] = $years->parseTime($par);
        return $res;
    }

    //获取周相关时间
    function GetDateDetailsByweek($params=array()){
        $params = array(
            'type' => 1,
            'num' =>array(2,4,5),
            'range' => array('start'=>2,'end'=>7),
        );
        if(empty($params))return -52101;
        $res = array();
        $par = array();
        $weeks = C()->get('weeks');
        $dates = C()->get('dates');
        $areas = C()->get('areas');
        $w = date('w');
        $w = ($w==0)?7:$w;
        $v = isset($params['type'])?$params['type']:0;
        if($v==-1){
            $par[0] = 'pre';
            $cd = -7;
        }else if($v==0){
            $par[0] = 'cur';
            $cd = 0;
        }else{
            $par[0] = 'next';
            $cd = 7;
        }
        $res['type'][] = $weeks->parseTime($par);
        if(isset($params['num']) && count($params['num'])>0){
            foreach ($params['num'] as $nk => $nv) {
                $cw = $nv-$w+$cd;
                $str = ($cw==0)?'':($cw>0?'+'.$cw.'day':$cw.'day');
                $par[0] = date('Y-m-d',strtotime($str));
                $res['num'][] = $dates->parseTime($par);
            }
        }
        if(isset($params['range']) && count($params['range'])>0){
            if(isset($params['range']['start'])){
                $cw = $params['range']['start']-$w+$cd;
                $str = ($cw==0)?'':($cw>0?'+'.$cw.'day':$cw.'day');
                $par[0] = date('Y-m-d',strtotime($str));
                $cw = $params['range']['end']-$w+$cd;
                $str = ($cw==0)?'':($cw>0?'+'.$cw.'day':$cw.'day');
                $par[1] = date('Y-m-d',strtotime($str));
                $res['range'][] = $areas->parseTime($par);
            }else{
                foreach ($params['range'] as $nk => $nv) {
                    $cw = $nv['start']-$w+$cd;
                    $str = ($cw==0)?'':($cw>0?'+'.$cw.'day':$cw.'day');
                    $par[0] = date('Y-m-d',strtotime($str));
                    $cw = $nv['end']-$w+$cd;
                    $str = ($cw==0)?'':($cw>0?'+'.$cw.'day':$cw.'day');
                    $par[1] = date('Y-m-d',strtotime($str));
                    $res['range'][] = $areas->parseTime($par);
                }
            }  
        }
        return $res;
    }

}