<?php
namespace app\admin\controller;

use think\Db;

class Bursting extends Base
{
    protected $userId = 0;
    protected function initialize()
    {
        parent::initialize();

        if (!is_login()) {
            $this->redirect(url('Common/logout'));
        }
        $this->userId = is_login();
    }

    public function index()
    {
        $info = Db::name('burst_record')
            ->where('user_id',$this->userId)
            ->where('status',1)
            ->where('del',0)
            ->find();
        if(!$info){
            $this->redirect(url('Index/index'));
        }
        $binfo =  Db::name('burst')
            ->where('id',$info['burst_id'])
            ->find();
        $info['title'] = $binfo['title'];
        $this->changeStatus($info);

        $info = Db::name('burst_record')
            ->where('id',$info['id'])
            ->where('del',0)
            ->find();
        $info['title'] = $binfo['title'];

        $this->assign('info',$info);
        return $this->fetch();
    }

    public function detail()
    {
        $id = input('id');
        $info = Db::name('burst_record')
            ->where('id',$id)
            ->where('del',0)
            ->find();
        if(!$info){
            $this->error('数据不存在');
            exit;
        }
        $binfo =  Db::name('burst')
            ->where('id',$info['burst_id'])
            ->find();
        $info['title'] = $binfo['title'];
        $this->assign('info',$info);
        return $this->fetch();
    }

    // 更改步骤状态
    private function changeStatus($info){
        if($info['status'] == 2 || $info['status'] == 3){
            return $info;
        }
        $curTime = time();
        $ctime = ($curTime - strtotime($info['create_time']))/60;
        $content = json_decode($info['content'],true);
        $t = 0;
        // 查状态
        foreach ($content as $k=>$v){
            $t1 = $t;
            $t = $t + $v['time'];

            $s = 0; // 都已完成
            foreach ($v['options'] as $kk=>$vv){
                if($vv['status'] == 0){
                    $s = 1;
                }
            }

            if($v['status'] == 0||$v['status'] == 1){
                if($ctime < $t && $ctime >= $t1){
                    $content[$k]['status'] = 1;
                    if($s == 0){
                        $content[$k]['status'] = 2;
                        if($k+1 < count($content)){
                            $content[$k+1]['status'] = 1;
                        }
                    }
                }else if($ctime >= $t){
                    $content[$k]['status'] = 2;
                }
            }
        }

        $status = $info['status'];
        if($ctime < $t){
            $flag = 0;
            foreach ($content as $k=>$v){
                foreach ($v['options'] as $kk=>$vv){
                    if($vv['status'] == 0){
                        $flag = 1;
                        break;
                    }
                }
            }
            if($flag == 0){
                $status = 2;
            }
        }else{
            $status = 3;
        }

//        if($content[count($content) - 1]['status'] == 2 && $ctime >= $t){
//            $status = 3;
//        } else if ($content[count($content) - 1]['status'] == 2 && $ctime < $t){
//            $status = 2;
//        }

        $content = json_encode($content);
        Db::name('burst_record')->where('id',$info['id'])->update(['status'=>$status,'content' => $content,'update_time'=>date('Y-m-d H:i:s')]);
        $info['content'] = $content;
        return $info;
    }

    public function finish(){
        $id = input('id/d',0);
        $index = input('index/d',0);
        $index2 = input('index2/d',0);
        $info = Db::name('burst_record')
            ->where('id',$id)
            ->where('del',0)
            ->find();
        if(!$info){
            $this->error('数据不存在');
        }
        if($info['status'] != 1){
            $this->error('无权限操作');
        }
        $content = json_decode($info['content'],true);
        foreach ($content as $k=>$v){
            if($k == $index){
                foreach ($v['options'] as $kk => $vv){
                    if($kk == $index2){
                        $content[$k]['options'][$kk]['status'] = 1;
                    }
                }
            }
        }
        $content = json_encode($content);
        $ret = Db::name('burst_record')->where('id',$id)->update(['content' => $content,'update_time'=>date('Y-m-d H:i:s')]);
        if($ret){
            $info['content'] = $content;
            $this->changeStatus($info);
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }


}