<?php

namespace app\admin\controller\student;

use app\admin\model\Report;
use app\common\controller\Backend;
use app\common\model\Config as ConfigModel;

/**
 * sim-练习集合管理
 *
 * @icon fa fa-circle-o
 */
class Practice extends Backend
{

    /**
     * Collection模型对象
     * @var \app\admin\model\teacher\Collection
     */
    protected $model = null;
    protected $exam_model = null;

    protected $whereExtend = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\teacher\Collection;
        $this->exam_model = new \app\admin\model\teacher\Exams;
        $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
        $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());

        $this->whereExtend['exam_collection_type'] = 1;//练习
        $this->whereExtend['exam_collection_state'] = 2;
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {
        $isset = $this->model->alias('z')
            ->join('real_exam r','z.exam_collection_id=r.exam_collection_id','left')
            ->where('z.exam_collection_type=1 and z.exam_collection_state=2 and r.endtime=0 and r.user_id='.$this->auth->id)->find();
        
        if(empty($isset)){
            //提示页面，没有开始的考试
            return $this->view->fetch('tishi');
        }else{
//             $this->redirect('/ZQOtIMLKud.php/student/collection/examing/ids/'.$isset['exam_id'].'?ref=addtabs');
            echo "<script>location.href='/ZQOtIMLKud.php/student/practice/examing/ids/".$isset['exam_id']."';</script>";
            //$this->redirect('/hotelmanage/room/displayindex/ids/1');
            exit();
        }
        die();

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)->where($this->whereExtend)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as $k => $v){
            $exam_isset =  $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$v['exam_collection_id'],'exam_collection_type'=>1])->find();
            if(isset($exam_isset)){
                $v->is_user_examed = 1;
            }else{
                $v->is_user_examed = 0;
            }
        }
        unset($v);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    //进入考试
    public function  into($ids = null)
    {
        $row = $this->model->get($ids);
        if(!$row){
            $this->error('未找到记录');
        }
        $info = $this->exam_model->where(['user_id'=>$this->auth->id,'exam_collection_id'=>$ids])->find();
        if(empty($info)){
            $arr = [
                'exam_collection_id' => $ids,
                'user_id' => $this->auth->id,
                'user_username' => $this->auth->username,
                'user_nickname' => $this->auth->nickname,
                'user_depart_id' => $this->auth->depart_id,
                'exam_collection_name' => $row->exam_collection_name,
                'exam_collection_type' =>1,
                'sim_type' => $row->sim_type,
                'seat_id' => 1,
                'sim_id' => 12,
            ];
            $this->exam_model->save($arr);
        }
        if ($this->request->isPost()) {
            $exam_collection_id = $this->request->post('exam_collection_id');
            if(empty($info->starttime)){
                $info->start_time = date('Y-m-d H:i:s');
                $info->starttime = time();
            }
            $info->exam_status = 4;
            $info->save();
            $this->success('开始成功','/ZQOtIMLKud.php/student/practice/examing/ids/'.$info['exam_id']);
        }
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }

    public  function  examing($ids = null)
    {
        $row = $this->exam_model->get($ids);
        if(!$row){
            $this->error('未找到记录');
        }
        $this->assignConfig('timer', 60*$this->model->where(['exam_collection_id'=>$row['exam_collection_id']])->value('limit_duration'));
        $this->view->assign('row', $row);

        $departmentdata = [];
        $departmentdata = Report::where(['level'=>4,'is_replace'=>1])->select();
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $row->end_time = date('Y-m-d H:i:s');
            $row->endtime = time();
            $row->exam_status = 5;
            $row->save();
            $this->success('交卷成功','/ZQOtIMLKud.php/student/practice/analysis/ids/'.$row['exam_id']);
        }
        $this->view->assign('departmentdata', $departmentdata);
        return $this->view->fetch();
    }


    public function analysis($ids = null)
    {
        $row = $this->exam_model->get($ids);
        if(!$row){
            $this->error('未找到记录');
        }
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            //计算总分到学员考试表
            $this->success('操作成功','/ZQOtIMLKud.php/student/practice/index');
        }
        return $this->view->fetch();
    }

}
