<?php
namespace app\common\model;

use think\Db;

class AttendanceClass extends Base{

    public function updates(){
        $data = request()->post();
        $id = $data['id'];
        unset($data['id']);

        $result = validate('AttendanceClass')->check($data,[],'');
        if(true !== $result){
            $this->error = validate('AttendanceClass')->getError();
            return false;
        }

        $curData = $this->checkForm($data);
        if(!$curData){
            $this->error = "参数错误";
            return false;
        }

        $content = [
            'type' => $data['type'],
            'dates' => $curData
        ];

        $sdata = [
            'name' => $data['name'],
            'content' => json_encode($content),
            'org_id'=>cur_org_id()
        ];
        if($id < 1){
            $check = db('attendance_class')->where('name',$data['name'])->where('del',0)->where('org_id',cur_org_id())->find();

            if($check){
                $this->error = '已存在相同内容';
                return false;
            }
        }

        if($id > 0){
            $sdata['update_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($sdata,['id'=>$id]);
        }else{
            $sdata['create_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($sdata);
        }
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }
        return true;

    }

    private function checkFormold($data){
        $type = $data['type'];
        $timeOne = [$data['start1'],$data['end1']];
        $timeTwo = [isset($data['start2'])?$data['start2']:'',isset($data['end2'])?$data['end2']:''];
        $timeThree = [isset($data['start3'])?$data['start3']:'',isset($data['end3'])?$data['end3']:''];

//        $timeOne = explode(',',$data['start1']);
//        $timeTwo = isset($data['start2'])&&!empty($data['start2'])?explode(',',$data['start2']):[];
//        $timeThree = isset($data['start3'])&&!empty($data['start3'])?explode(',',$data['start3']):[];

        if($timeOne[0] == $timeOne[1]){
            $this->error = '上班时间和下班时间不能一样';
            return  false;
        }
        $dates = [];
        if($type == 1){
            $dates1 = [
                'stime' => $timeOne[0],
                'etime' => $timeOne[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($timeOne[0] > $timeOne[1]){
                $dates1['enext'] = 1;
            }
            $dates[] = $dates1;
        }elseif ($type == 2){

            $dates1 = [
                'stime' => $timeOne[0],
                'etime' => $timeOne[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($timeOne[0] > $timeOne[1]){
                $dates1['enext'] = 1;
            }
            $dates[] = $dates1;

            if(!$timeTwo[0] || !$timeTwo[1]){
                $this->error = '第2次上班时间和下班时间不能为空';
                return false;
            }
            if($timeTwo[0]== $timeTwo[1]){
                $this->error = '第2次上班时间和下班时间不能一样';
                return false;
            }
            if($timeTwo[0] < $timeOne[1] && $dates1['enext'] == 1){
                $this->error = '第2次时间段设置错误';
                return false;
            }
            if($timeTwo[0] > $timeTwo[1] && $dates1['enext'] == 1){
                $this->error = '第2次时间段设置错误';
                return false;
            }

            $dates2 = [
                'stime' => $timeTwo[0],
                'etime' => $timeTwo[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($dates1['enext'] == 1){
                $dates2['snext'] = 1;
                $dates2['enext'] = 1;
            }else{
                if($timeTwo[0] < $timeOne[1]){
                    $dates2['snext'] = 1;
                    $dates2['enext'] = 1;
                }else{
                    $dates2['snext'] = 0;
                    if($timeTwo[0] > $timeTwo[1]){
                        $dates2['enext'] = 0;
                    }
                }
            }
            $dates[] = $dates2;
        }elseif ($type == 3){

            $dates1 = [
                'stime' => $timeOne[0],
                'etime' => $timeOne[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($timeOne[0] > $timeOne[1]){
                $dates1['enext'] = 1;
            }

            $dates[] = $dates1;

            if(!$timeTwo[0] || !$timeTwo[1]){
                $this->error = '第2次上班时间和下班时间不能为空';
                return false;
            }
            if($timeTwo[0]== $timeTwo[1]){
                $this->error = '第2次上班时间和下班时间不能一样';
                return false;
            }
            if($timeTwo[0] < $timeOne[1] && $dates1['enext'] == 1){
                $this->error = '第2次时间段设置错误';
                return false;
            }
            if($timeTwo[0] > $timeTwo[1] && $dates1['enext'] == 1){
                $this->error = '第2次时间段设置错误';
                return false;
            }


            $dates2 = [
                'stime' => $timeTwo[0],
                'etime' => $timeTwo[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($dates1['enext'] == 1){
                $dates2['snext'] = 1;
                $dates2['enext'] = 1;
            }else{
                if($timeTwo[0] < $timeOne[1]){
                    $dates2['snext'] = 1;
                    $dates2['enext'] = 1;
                }else{
                    $dates2['snext'] = 0;
                    if($timeTwo[0] > $timeTwo[1]){
                        $dates2['enext'] = 0;
                    }
                }
            }

            $dates[] = $dates2;

            if(!$timeThree[0] || !$timeThree[1]) {
                $this->error = '第3次上班时间和下班时间不能为空';
                return false;
            }

            if($timeThree[0] == $timeThree[1]){
                $this->error = '第3次上班时间和下班时间不能一样';
                return false;
            }

            if($timeThree[0] < $timeThree[1] && $dates2['enext'] == 1){
                $this->error = '第3次上班时间和下班时间不能一样';
                return false;
            }

            if($timeThree[0] > $timeThree[1] && $dates2['enext'] == 1){
                $this->error = '第3次时间段设置错误';
                return false;
            }


            $dates3 = [
                'stime' => $timeThree[0],
                'etime' => $timeThree[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($dates2['enext'] == 1){
                $dates3['snext'] = 1;
                $dates3['enext'] = 1;
            }else{
                if($timeThree[0] < $timeTwo[1]){
                    $dates3['snext'] = 1;
                    $dates3['enext'] = 1;
                }else{
                    $dates3['snext'] = 0;
                    if($timeThree[0] > $timeThree[1]){
                        $dates3['enext'] = 0;
                    }
                }
            }
            $dates[] = $dates3;
        }

        return $dates;
    }

    private function checkForm($data){

        $type = $data['type'];
        $timeOne = [$data['start1'],$data['end1']];
        $timeTwo = [isset($data['start2'])?$data['start2']:'',isset($data['end2'])?$data['end2']:''];
        $timeThree = [isset($data['start3'])?$data['start3']:'',isset($data['end3'])?$data['end3']:''];

//        $type = $data['type'];
//        $timeOne = explode(',',$data['start1']);
//        $timeTwo = isset($data['start2'])&&!empty($data['start2'])?explode(',',$data['start2']):[];
//        $timeThree = isset($data['start3'])&&!empty($data['start3'])?explode(',',$data['start3']):[];

        if($timeOne[0] == $timeOne[1]){
            $this->error = '上班时间和下班时间不能一样';
            return  false;
        }
        $dates = [];
        if($type == 1){
            $dates1 = [
                'stime' => $timeOne[0],
                'etime' => $timeOne[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($timeOne[0] > $timeOne[1]){
                $dates1['enext'] = 1;
            }
            $dates[] = $dates1;
        }elseif ($type == 2){

            $dates1 = [
                'stime' => $timeOne[0],
                'etime' => $timeOne[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($timeOne[0] > $timeOne[1]){
                $dates1['enext'] = 1;
            }
            $dates[] = $dates1;

            if(!$timeTwo[0] || !$timeTwo[1]){
                $this->error = '第2次上班时间和下班时间不能为空';
                return false;
            }
            if($timeTwo[0]== $timeTwo[1]){
                $this->error = '第2次上班时间和下班时间不能一样';
                return false;
            }
//            if($timeTwo[0] < $timeOne[1] && $dates1['enext'] == 1){
//                $this->error = '第2次时间段设置错误';
//                return false;
//            }
//            if($timeTwo[0] > $timeTwo[1] && $dates1['enext'] == 1){
//                $this->error = '第2次时间段设置错误';
//                return false;
//            }
            if($dates1['enext'] == 1 && $timeTwo[0] > $timeTwo[1]){
                $this->error = '班次时间不能跨越2天';
                return false;
            }

            $dates2 = [
                'stime' => $timeTwo[0],
                'etime' => $timeTwo[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($dates1['enext'] == 1){
                $dates2['snext'] = 1;
                $dates2['enext'] = 1;
            }else{
                if($timeTwo[0] < $timeOne[1]){
                    $dates2['snext'] = 1;
                    $dates2['enext'] = 1;
                }else{
                    $dates2['snext'] = 0;
                    if($timeTwo[0] > $timeTwo[1]){
                        $dates2['enext'] = 1;
                    }
                }
            }
            $dates[] = $dates2;
        }elseif ($type == 3){

            $dates1 = [
                'stime' => $timeOne[0],
                'etime' => $timeOne[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($timeOne[0] > $timeOne[1]){
                $dates1['enext'] = 1;
            }

            $dates[] = $dates1;

            if(!$timeTwo[0] || !$timeTwo[1]){
                $this->error = '第2次上班时间和下班时间不能为空';
                return false;
            }
            if($timeTwo[0]== $timeTwo[1]){
                $this->error = '第2次上班时间和下班时间不能一样';
                return false;
            }
//            if($timeTwo[0] < $timeOne[1] && $dates1['enext'] == 1){
//                $this->error = '第2次时间段设置错误';
//                return false;
//            }
//            if($timeTwo[0] > $timeTwo[1] && $dates1['enext'] == 1){
//                $this->error = '第2次时间段设置错误';
//                return false;
//            }
            if($dates1['enext'] == 1 && $timeTwo[0] > $timeTwo[1]){
                $this->error = '班次时间不能跨越2天';
                return false;
            }


            $dates2 = [
                'stime' => $timeTwo[0],
                'etime' => $timeTwo[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($dates1['enext'] == 1){
                $dates2['snext'] = 1;
                $dates2['enext'] = 1;
            }else{
                if($timeTwo[0] < $timeOne[1]){
                    $dates2['snext'] = 1;
                    $dates2['enext'] = 1;
                }else{
                    $dates2['snext'] = 0;
                    if($timeTwo[0] > $timeTwo[1]){
                        $dates2['enext'] = 1;
                    }
                }
            }

            $dates[] = $dates2;

            if(!$timeThree[0] || !$timeThree[1]) {
                $this->error = '第3次上班时间和下班时间不能为空';
                return false;
            }

            if($timeThree[0] == $timeThree[1]){
                $this->error = '第3次上班时间和下班时间不能一样';
                return false;
            }

//            if($timeThree[0] < $timeThree[1] && $dates2['enext'] == 1){
//                $this->error = '第3次上班时间和下班时间不能一样';
//                return false;
//            }
//
//            if($timeThree[0] > $timeThree[1] && $dates2['enext'] == 1){
//                $this->error = '第3次时间段设置错误';
//                return false;
//            }

            if($dates2['enext'] == 1 && $timeThree[0] > $timeThree[1]){
                $this->error = '班次时间不能跨越2天';
                return false;
            }


            $dates3 = [
                'stime' => $timeThree[0],
                'etime' => $timeThree[1],
                'snext' => 0,
                'enext' => 0
            ];
            if($dates2['enext'] == 1){
                $dates3['snext'] = 1;
                $dates3['enext'] = 1;
            }else{
                if($timeThree[0] < $timeTwo[1]){
                    $dates3['snext'] = 1;
                    $dates3['enext'] = 1;
                }else{
                    $dates3['snext'] = 0;
                    if($timeThree[0] > $timeThree[1]){
                        $dates3['enext'] = 1;
                    }
                }
            }
            $dates[] = $dates3;
        }
        return $dates;
    }

}