<?php

namespace app\admin\controller\student;

use app\admin\model\Fault;
use app\admin\model\Report;
use app\common\controller\Backend;
use app\common\model\Config as ConfigModel;
use think\Db;
use app\admin\model\department\Department;

/**
 * sim-考试表/成绩总分
 *
 * @icon fa fa-circle-o
 */
class Exam extends Backend
{

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

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\teacher\Exams;
        $groupIds = $this->auth->getGroupIds();
        //学员查看自己的
        if(in_array(8, $groupIds)){
            $this->whereExtend['user_id'] = $this->auth->id;
        }
        $this->assignconfig("groupIds", $groupIds[0]);
        $this->whereExtend['exam_collection_type'] = 3;
        $this->whereExtend['endtime'] = ['>',0];
    }



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


    public function view($ids = null)
    {
        $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
        if (!$row) {
            $this->error(__('未交卷'));
        }
        $rows = $this->model->get($ids);
        $row['seat_id'] = $rows->seat_id;
        $row['user_nickname'] = $rows->user_nickname;
        $row['user_username'] = $rows->user_username;
        $row['user_depart_id'] = $rows->user_depart_id;
        $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
        $row['start_time'] = $rows->start_time;
        $row['end_time'] = $rows->end_time;

        $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
        $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
        $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
        $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');

        $row['xianxian_content_name'] = '';
        if(!empty($row['xianxian_content']))
        {
            $row['xianxian_content_name'] = json_decode($row['xianxian_content'],true);
        }
        $row['yuanyin_content_name'] = '';
        if(!empty($row['yuanyin_content']))
        {
            $row['yuanyin_content_name'] = json_decode($row['yuanyin_content'],true);
        }
        $row['buwei_content_name'] = '';
        if(!empty($row['buwei_content']))
        {
            $row['buwei_content_name'] = json_decode($row['buwei_content'],true);
        }
        $row['fangfa_content_name'] = '';
        if(!empty($row['fangfa_content']))
        {
            $row['fangfa_content_name'] = json_decode($row['fangfa_content'],true);
        }
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
}
