<?php
ini_set('display_errors', 1);
session_start(); // starts new or resumes existing session
Doo::loadModelAt('auser', 'admin');
Doo::loadModelAt('ausers', 'admin');
Doo::loadModelAt('ameasure', 'admin');
Doo::loadModelAt('fileup', 'admin');
Doo::loadModelAt('measureaudit', 'admin');
Doo::loadModelAt('numofper', 'admin');
Doo::loadModel('users');
Doo::loadClass('profile');
Doo::loadClass('contractact');
Doo::loadClass('PasswordHash');
Doo::loadClass('mailer');
Doo::loadClass('project');
Doo::loadClass('attfile');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// 列表停用 编辑 重置密码
// 管理员权限管理
// 管理员修改密码
class ProController extends DooController
{

    private $data, $users, $user, $profile, $ph, $userz, $mailer, $project, $am, $cc, $an, $attfile, $ama, $statusArray = array('uncheck' => '<span class = "colGray">未上报</span>', 'checking' => '<span class = "colOrange">审核中</span>', 'checked' => '<span class = "colGreen">完成</span>', 'checkno' => '<span class = "colRed">不通过</span>'), $statusArray2 = array('uncheck' => '', 'checking' => '<span class = "colOrange">审批中</span>', 'checked' => '<span class = "colGreen">审批通过</span>', 'checkno' => '<span class = "colRed">审批不通过</span>');

    public function beforeRun($resource, $action)
    {
        if (!isset($_SESSION['auid'])) {
            return Doo::conf()->APP_URL . 'manage';
        }
    }

    public function __construct()
    {
        $this->users = new AUsers();
        $this->user = new AUser();
        $this->userz = new Users();
        $this->profile = new Profile();
        $this->mailer = new Mailer();
        $this->ph = new PasswordHash(8, FALSE);
        $this->am = new Ameasure();
        $this->ama = new AmeasureAudit();
        $this->af = new Afileup();
        $this->an = new Anumofper();
        $this->project = new Project();
        $this->cc = new Contractact();
        $this->attfile = new attFile();
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
    }

    public function index()
    {
        if(isset($_POST['type']) && isset($_POST['pid'])){
            if($_POST['type'] == 'del'){
                $num = $this->cc->getNumRow($_POST['pid']);
                if($num > 0){
                    die(json_encode(array('status' => 1)));
                }else{
                    die(json_encode(array('status' => 2)));
                }
            }
            if($_POST['type'] == "delpro"){
                $this->project->del($_POST['pid']);
                die(json_encode(array('status' => 'ok')));
            }
        }
        $pArray = $this->project->getAll();
        if(!empty($pArray)){
            foreach($pArray as $k => $v){
                $pArray[$k]['mnum'] = $this->cc->getNumRow($v['pid']);
                $userfile = $this->profile->getProWithUid($v['uid']);
                $company = !empty($userfile['company']) ? '-'.$userfile['company'] : '';
                $pArray[$k]['uname'] = $userfile['name'].$company;
            }
        }
        $this->data['pArray'] = $pArray;
        $this->data['menu'] = 5;
        $this->render('admin-project', $this->data, TRUE);
    }

    public function section()
    {
        if(isset($_POST['type'])){
            if($_POST['type'] == 'del' && isset($_POST['stid'])){
                $num = $this->am->getRowNum($_POST['stid']);
                if($num > 0){
                    die(json_encode(array('status' => 1)));
                }else{
                    die(json_encode(array('status' => 2)));
                }
            }

            if($_POST['type'] == 'delpro' && isset($_POST['stid'])){
                $this->cc->del($_POST['stid']);
                die(json_encode(array('status' => 'ok')));
            }

            if($_POST['type'] == 'delmcheck' && isset($_POST['pmid'])){
                $num = $this->an->getLastNew2($_POST['pmid']);
                if(isset($num['numpname']) && !empty($num['numpname']) && $num['numpname'] != 1 && $num['times'] != 1){
                    die(json_encode(array('status' => 1)));
                }else{
                    die(json_encode(array('status' => 2)));
                }
            }

            if($_POST['type'] == 'delmpro' && isset($_POST['pmid'])){
                $this->am->del($_POST['pmid']);
                //删除该标段下第1期#1所有审批人列表
                $anmsg = $this->an->getLastNew2($_POST['pmid']);
                if(!empty($anmsg)){
                    $muaudits = $this->ama->getAuditsByMpid($anmsg['mpid']);
                    if(!empty($muaudits)){
                        foreach($muaudits as $k => $v){
                            $this->ama->del($v['maid']);
                        }
                    }
                    $this->an->del($anmsg['mpid']);

                    //文件列也要删除，不然软件报错
                    $this->attfile->delAllTimesFile2($anmsg['mpid']);
                }
                die(json_encode(array('status' => 'ok')));
            }

        }


        $typeArray = $this->cc->getRowByPid($this->params['pid']);
        if(!empty($typeArray)){
            foreach($typeArray as $k => $v){
                $datarows = $this->am->getRowswith($v['stid']);
                if(!empty($datarows)){
                    foreach($datarows as $dk => $dv){
                        $userfile = $this->profile->getProWithUid($dv['uid']);
                        $datarows[$dk]['name'] = $userfile['name'];
                        $company = !empty($userfile['company']) ? '-'.$userfile['company'] : '';
                        $datarows[$dk]['company'] = $company;

                        //获取第几期和审核状态
                        $countNum = $this->an->getLastNew2($dv['pmid'])['numpname'];
                        if (!isset($countNum)) {
                            $countNum = 0;
                        }
                        $lastStatusArray = $this->an->getMaxStatusTimes($dv['pmid'], $countNum);

                        $datarows[$dk]['num'] = $countNum;
                        $datarows[$dk]['status'] =  $countNum != 0 ? $this->statusArray[$lastStatusArray['currstatus']] : '';

                    }
                }
                $typeArray[$k]['datarows'] = $datarows;
            }
        }

        $this->data['pid'] = $this->params['pid'];
        $this->data['typeArray'] = $typeArray;
        $this->data['menu'] = 5;
        $this->render('admin-section', $this->data, TRUE);
    }

    public function period(){
        if(isset($_POST['op'])){
            if($_POST['op'] == 'pass' && isset($_POST['maid'])){
                $mamsg = $this->ama->getRowData($_POST['maid']);
                if(!empty($mamsg)){
                    $mumsg = $this->an->getRowByMpid($mamsg['mpid']);
                    //修改期数状态
                    if(!empty($mumsg)){
                        $nextmumsg = $this->ama->getAuditByMpidStatus($mamsg['mpid'],'uncheck');
                        if(!empty($nextmumsg)){
                            $this->ama->updateStatus($nextmumsg['maid'],'checking',2,0);
                        }else{
                            $this->an->updateAuditStatus($mamsg['mpid'],'checked');
                        }
                    }
                    //修改状态为checked
                    $this->ama->updateStatus($_POST['maid'],'checked');
                    $this->attfile->updateHash($mumsg['pmid'], $mumsg['numpname'], $mumsg['times']);
                }
                die(json_encode(array('status' => 'ok')));
            }

            if($_POST['op'] == 'unpass' && isset($_POST['maid'])){
                $mamsg = $this->ama->getRowData($_POST['maid']);
                if(!empty($mamsg)){
                    $mumsg = $this->an->getRowByMpid($mamsg['mpid']);
                    //修改期数状态
                    if(!empty($mumsg)){
                        $this->an->updateAuditStatus($mamsg['mpid'],'checkno');
                    }
                    //修改状态为checkno
                    $this->ama->updateStatus($_POST['maid'],'checkno');
                    $this->attfile->updateHash($mumsg['pmid'], $mumsg['numpname'], $mumsg['times']);
                }
                die(json_encode(array('status' => 'ok')));
            }

            if($_POST['op'] == 'checking' && isset($_POST['maid'])){
                $mamsg = $this->ama->getRowData($_POST['maid']);
                if(!empty($mamsg)){
                    $mumsg = $this->an->getRowByMpid($mamsg['mpid']);
                    //修改期数状态
                    if(!empty($mumsg) && $mumsg['currstatus'] != 'checking'){
                        $this->an->updateAuditStatus($mamsg['mpid'],'checking');
                    }
                    //修改上一审状态为uncheck，下一审状态为checking
                    $lastmamsg = $this->ama->getAuditByMpidStatus($mamsg['mpid'],'checking');
                    $this->ama->updateStatus($lastmamsg['maid'],'uncheck',2,0);
                    $this->ama->updateStatus($_POST['maid'],'checking',2,0);

                    //查看当前审批人附件是否存在，存在则删除
                    $attfile = $this->attfile->getMyAttFile($mumsg['pmid'], $mumsg['numpname'], $mumsg['times'],$mamsg['auditoruid']);
                    if(!empty($attfile)){
                        $this->attfile->delMyAttFile($mumsg['pmid'], $mumsg['numpname'], $mumsg['times'],$mamsg['auditoruid']);

                        //更新最后一个附件为最新 isnew=2
                        $lastaid = $this->attfile->getLastFileAid($mumsg['pmid'], $mumsg['numpname'], $mumsg['times']);
                        if(!empty($lastaid)){
                            $this->attfile->updateIsNew2($lastaid);
                        }
                    }
//                    $this->attfile->updateHash($mumsg['pmid'], $mumsg['numpname'], $mumsg['times']);
                }
                die(json_encode(array('status' => 'ok')));
            }

            if($_POST['op'] == 'delete' && isset($_POST['numpname']) && isset($_POST['times'])){
                //删除该期审批人列表
                $muaudits = $this->ama->getAuditsByPmidNumTimes($this->params['pmid'],$_POST['numpname'],$_POST['times']);
                if(!empty($muaudits)){
                    foreach($muaudits as $k => $v){
                        $this->ama->del($v['maid']);
                    }
                }
                $mumsg = $this->an->getMsgByPmidNumTimes($this->params['pmid'],$_POST['numpname'],$_POST['times']);
                if(!empty($mumsg)){
                    $this->an->del($mumsg['mpid']);
                    //文件列也要删除，不然软件报错
                    $this->attfile->delAllTimesFile2($mumsg['mpid']);
                }
                die(json_encode(array('status' => 'ok')));
            }
        }

        $pmArray = $this->an->getGroupByLastOne2($this->params['pmid']);
        //判断是否有获取未上报的数据，如果有则置顶；
        $nopm = $this->ama->getLastAuditMsgByPmid($this->params['pmid']);
        $this->data['pmid'] = $this->params['pmid'];
        if(!empty($pmArray)){
            $this->data['pid'] = $pmArray[0]['pid'];
            $this->data['keymax'] = $pmArray[0]['numpname'];
            if($nopm['numpname'] == $pmArray[0]['numpname'] && $nopm['times'] != $pmArray[0]['times']){
                $nopmArray = array();
                $nopmArray['mpid'] = $nopm['mpid'];
                $nopmArray['numpname'] = $nopm['numpname'];
                $nopmArray['times'] = $nopm['times']+1;
                $audits2 = $this->ama->getAuditsByPmidNumTimes($nopm['pmid'],$nopm['numpname'],$nopm['times']);
                $numaudits2 = $this->ama->getNumAuditsByPmidNumTimes($nopm['pmid'],$nopm['numpname'],$nopm['times']);
                if(!empty($audits2)){
                    foreach($audits2 as $ak => $av){
                        $audits2[$ak]['numaudit'] = $numaudits2-$ak;
                        $audits2[$ak]['status'] = $this->statusArray2[$av['mastatus']];
                        $audits2[$ak]['auditcontent'] = !empty($av['auditcontent']) ? $av['auditcontent'] : '';
                        $userfile = $this->profile->getProWithUid($av['auditoruid']);
                        $company = !empty($userfile['company']) ? '-'.$userfile['company'] : '';
                        $audits2[$ak]['name'] = $userfile['name'];
                        $audits2[$ak]['company'] = $company;
                        $audits2[$ak]['enable'] = 3;
                    }
                }
                $nopmArray['audits'] = $audits2;
                $this->data['nopmArray'] = $nopmArray;
            }
            foreach($pmArray as $k => $v){
                $pmArray[$k]['times'] = $v['times']+1;
                $audits = $this->ama->getAuditsByPmidNumTimes($v['pmid'],$v['numpname'],$v['times']);
                $numaudits = $this->ama->getNumAuditsByPmidNumTimes($v['pmid'],$v['numpname'],$v['times']);
                if(!empty($audits)){
                    foreach($audits as $ak => $av){
                        $audits[$ak]['numaudit'] = $numaudits-$ak;
                        $audits[$ak]['status'] = $this->statusArray2[$av['mastatus']];
                        $audits[$ak]['auditcontent'] = !empty($av['auditcontent']) ? $av['auditcontent'] : '';
                        $userfile = $this->profile->getProWithUid($av['auditoruid']);
                        $company = !empty($userfile['company']) ? '-'.$userfile['company'] : '';
                        $audits[$ak]['name'] = $userfile['name'];
                        $audits[$ak]['company'] = $company;
                        $audits[$ak]['enable'] = 3;
                        if($audits[0]['mastatus'] == 'checked'){
                            $audits[0]['enable'] = 2;
                        }else{
                            if($av['mastatus'] == "checking"){
                                $audits[$ak]['enable'] = 1;
                            }
                            if(($ak > 0 && $audits[$ak-1]['mastatus'] == "checking") || $av['mastatus'] == "checkno"){
                                $audits[$ak]['enable'] = 2;
                            }
                        }
                    }
                }
                $pmArray[$k]['audits'] = $audits;
            }
        }

        $this->data['isnopm'] = isset($nopmArray) && !empty($nopmArray) ? 1 : 0;
        $this->data['pmArray'] = $pmArray;
        $this->data['menu'] = 5;
        $this->render('admin-period', $this->data, TRUE);
    }
}
