<?php
require "login.php";
require "QueryList.class.php";
class evaluate{

    private $_listUrl="http://202.115.47.141/jxpgXsAction.do?oper=listWj&page=";//评教列表页面
    private $_formUrl="http://202.115.47.141/jxpgXsAction.do?oper=wjShow&";//评教表单页面
    private $_postUrl="http://202.115.47.141/jxpgXsAction.do?oper=wjpg&";//评教提交地址
    private $_yjpj;
    private $_successNo=0;//成功次数
    private $_failNo=0;//失败次数

    public function __construct($name,$pass)
    {

        $this->_yjpj=new login($name,$pass);
    }

    /**
     * 获取评教列表并且返回列表信息以及页码
     * @author mohuishou<1@lailin.xyz>
     * @return mixed
     *       $list=[
     *           'info'=>[
     *               [
     *                   'param'=>[
     *                       0 => '0000000038',
     *                       1 => '19873162',
     *                       2 => '高苹',
     *                       3 => '学评教',
     *                       4 => '马克思主义基本原理概论',
     *                       5 => '107021030',
     *                   ],
     *                   'status'=>0
     *               ],
     *              ......
     *           ],
     *           'page'=>[
     *               [
     *                   'page'=>'2'
     *               ]
     *           ]
     *       ];
     */

    public function  getList()
    {
        /*--------------获取第一页的评教列表页面---------------*/
        $listContent=$this->_yjpj->show($this->_listUrl."1");

        /*------------获取评教分页信息--------------*/
        $pageReg=[
            'page'=>['script:eq(3)','html','',function($page){
                $pattern="/document\.all\.pageNo\.value>(.)/";
                preg_match($pattern,$page,$no);
                if($no[1]){
                    return $no[1];
                }

            }]
        ];
        $page=QueryList::Query($listContent,$pageReg);
        $page=$page->jsonArr[0]['page'];

        /*---------获取评教老师信息--------------*/
        $listReg=[
            'param'=>['img','name','',function($a){
                if($a){
                    return explode('#@',$a);
                }
            }],
            'status'=>['img','onclick','',function($a){
                if(trim($a)=="evaluation(this)"){
                    return 1;
                }else{
                    return 0;
                }
            }],
        ];

        /*[优化]:减少重复抓取列表页,大大提高效率*/
        $c=QueryList::Query($listContent,$listReg);
        $list[]=$c->jsonArr;
        if($page>1){
            for($i=2;$i<=$page;$i++){
                $listContent=$this->_yjpj->show($this->_listUrl.$i);
                $c=QueryList::Query($listContent,$listReg);
                $list[]=$c->jsonArr;
            }
        }

        return $list;//获取到的评教信息
    }

    public function getPostInfo($list)
    {
//        print_r($list);
        foreach($list as  $listVal){
//            print_r($listVal);
            foreach($listVal as $listkey => $listValue){
                if($listValue['status']==1){
//                    print_r($listValue);
                    $param=[
                        'wjbm'=>$listValue['param'][0],
                        'bpr'=>$listValue['param'][1],
                        'pgnr'=>$listValue['param'][5],
                    ];
                    $param=http_build_query($param);
                    $url=$this->_formUrl.$param;
//                    echo $url;
                    $formContent=$this->_yjpj->show($url);
//                    print_r($formContent);
                    $res=QueryList::Query($formContent,['name'=>['[value=10_1]','name']]);

                    /*--------------将获取的表单信息整理成为即将post的参数-------------*/
                    foreach($res->jsonArr as $postKey => $postValue){
                        $postParam[$postValue['name']]="10_1";
                    }
                    $postParam['wjbm']=$listValue['param'][0];
                    $postParam['bpr']=$listValue['param'][1];
                    $postParam['pgnr']=$listValue['param'][5];
                    $postParam['zgpj']="very good teacher!!";
                    $this->post($postParam);
//                    return 0;
                }
            }
        }
    }

    public function post($param){
        $param=http_build_query($param);
        $url=$this->_postUrl.$param;
//        echo $url;
        $evalContent=$this->_yjpj->show($url);

        /*--------------------抓取提交之后的数据查看是否成功提交--------------------*/
        $res=QueryList::Query($evalContent,['info'=>['script','html','',function($a){
            $pattern='/alert\("(.+)"\)/';
            preg_match_all($pattern,$a,$res);
            $success='评估成功!';
            if($res[1][0]==$success){
                $a=[
                    'message'=>$res[1][0],
                    'status'=>1
                ];
            }else{
                $a=[
                    'message'=>$res[1][0],
                    'status'=>0
                ];
            }
            return $a;
        }]]);
        $status=$res->jsonArr[0]['info']['status'];
        if($status==1){
            $this->_successNo+=1;
        }else{
            $this->_failNo+=1;
        }
//        print_r($res->jsonArr);
    }




    public function action()
    {
        $this->getPostInfo($this->getList());
        $this->count();
      	return ['success'=>$this->_successNo,'fail'=>$this->_failNo];
        //return '评教成功,成功:'.$this->_successNo.'位,'.'失败:'.$this->_failNo."位";
    }
}