<?php

namespace app\admin\controller\student;

use app\admin\model\Report;
use app\common\controller\Backend;
use app\common\model\Config as ConfigModel;
use think\Db;
use think\exception\PDOException;

/**
 * sim-自主练习集合管理
 *
 * @icon fa fa-circle-o
 */
class Exercise 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->assign('sim_sim_type', ConfigModel::getSimTypeList());
    }



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

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $count = 0;
            Db::startTrans();
            try {
                $params['exam_collection_type'] = 2;
                $params['exam_collection_state'] = 2;
                $params['question_setting_method'] = 1;
                $params['exam_collection_name'] = $this->auth->nickname.'自主练习';
                $params['limit_duration'] = 30;
                $params['start_time'] = date('Y-m-d');
                $params['end_time'] = date('Y-m-d');
                $exam_id = $this->model->insertGetId($params);

                $data = [
                    'exam_collection_id' => $exam_id,
                    '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' => $params['exam_collection_name'],
                    'exam_collection_type' =>$params['exam_collection_type'],
                    'sim_type' => $params['sim_type'],
                    'seat_id' =>1,
                    'seat_id' =>12,
                    'exam_status'=>4,
                    'start_time'=>date('Y-m-d H:i:s'),
                    'starttime'=>time(),
                    'create_time'=>date('Y-m-d H:i:s'),
                    'createtime'=>time(),
                    'update_time'=>date('Y-m-d H:i:s'),
                    'updatetime'=>time(),
                ];
                $examid = $this->exam_model->insertGetId($data);
                $count = $examid;
                Db::commit();
            } catch (PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success('开始成功','/ZQOtIMLKud.php/student/exercise/examing/ids/'.$count);
            }
            $this->error(__('No rows were updated'));
        }
    }

    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/exercise/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/exercise/index');
        }
        return $this->view->fetch();
    }

}
