<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use app\common\model\Config as ConfigModel;
use fast\Date;
use think\Db;
use think\Env;


class Simulator extends Backend
{

    protected $noNeedRight = ['readfault'];
    /**
     * 查看
     */
    public function index()
    {

        $simNum = [];
        if(Env::get('app.is_fault')){
            $url = config('site.url_type').'/sim/sim/listAllEnable';
            $ret = json_decode(send_get($url),true);
            if($ret['code'] == 200){
                foreach ($ret['data'] as $k => $val){
                    $simNum[] = [
                        'simNum'=>$val['simNum'],
                        'simType'=>$val['simType'],
                    ];
                }
            }
        }
        $this->assign('simNum', $simNum);
        $this->assignConfig('url_type', config('site.url_type'));
        return $this->view->fetch();
    }

    public function readfault(){
        $fault_list = Db::name('debug_fault')->where(['flag'=>1])->select();
        if(!empty($fault_list)){
            foreach ($fault_list as $k =>$t){
                $answer_right = 2;
                if(!empty($t['sim_fault_question_value']) && !empty($t['sim_fault_answer_value'])){
                    if($t['sim_fault_question_value']!=$t['sim_fault_answer_value'] ){
                        //真实故障id 0002GZBW0001 薄膜开关FPC排线
                        if($t['sim_fault_answer_value']=='00000000' && $t['fault_id'] !='0002GZBW0001'){
                            $answer_right=2;
                        }
                        $answer_right=1;
                    }
                }
                Db::name('debug_fault')->where(['ref_id'=>$t['ref_id']])->update(['answer_right'=>$answer_right]);
            }
        }
        $list = Db::name('debug_fault')->where(['flag'=>1,'answer_right'=>['>',0]])->select();
        if(!empty($list)){
            foreach ($list as $k1 =>$t1){
                $fault_name = Db::name('fault')->where(['fault_id'=>$t1['fault_id']])->value('name');
                $list[$k1]['fault_name'] = $fault_name;
                if($t1['answer_right']==1){
                    $list[$k1]['xiufu_name'] = '已修复';
                }else if($t1['answer_right']==2){
                    $list[$k1]['xiufu_name'] = '修复错误';
                }else{
                    $list[$k1]['xiufu_name'] = '未修复';
                }
            }
        }
        $url = '"/admin/simulator/readfault';
        $this->success('读取成功',$url, $list);
    }
}
