<?php

Doo::loadClass('auth');
Doo::loadClass('attfile');
Doo::loadClass('profile');
Doo::loadClass('project');
Doo::loadClass('contractact');
Doo::loadClass('actmeasure');
Doo::loadClass('numofperact');
Doo::loadClass('measureauditact');

/**
 * MainController
 * Feel free to delete the methods and replace them with your own code.
 *
 * @author darkredz
 */
class SProjectController extends DooController {

    private $data, $auth, $attfile, $profile, $project, $contractact, $actmeasure, $numofperact, $measureauditact;

    public function beforeRun($resource, $action) {
//	$uGroups = $this->profile->getProWithUid($this->auth->getUid());
//	$falg = Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
//	if (!$falg)
//	    return Doo::acl()->defaultFailedRoute;
        if ($this->auth->getUid()) {
            $uGroups = $this->profile->getProWithUid($this->auth->getUid());
            $falg = Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
            if (!$falg)
                return Doo::acl()->defaultFailedRoute;
        }else {
            return Doo::acl()->defaultFailedRoute;
        }
    }

    public function __construct() {
        $this->auth = new Auth();
        $this->attfile = new attFile();
        $this->profile = new Profile();
        $this->project = new Project();
        $this->contractact = new Contractact();
        $this->actmeasure = new actMeasure();
        $this->numofperact = new NumofperAct();
        $this->measureauditact = new MeasureauditAct();
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
        $this->data['currChannle'] = 's';
        $this->data['user'] = $this->profile->getProWithUid($this->auth->getUid());
        $mpidArray = $this->measureauditact->getAuditProject2($this->auth->getUid());
        if (isset($mpidArray[0]['pid'])) {
            foreach ($mpidArray as $key => $value) {
                // 审批操作按照时间排序多标段
                $this->data['numofchecking'] += $this->measureauditact->getCountMyChecking($value['pid'], $this->auth->getUid());
            }
        }
        if ($this->data['numofchecking'] == 0) {
            $this->data['numofchecking'] = null;
        }
    }

    public function index() {
        $this->data['conArray'] = NULL;
        $conArray = $this->actmeasure->getGroupProject($this->auth->getUid());
        if (isset($conArray)) {
            foreach ($conArray as $k => $v) {
                $proArray = $this->project->getRowByPid($v['pid']);
                $this->data['conArray'][] = $proArray;
                $profileArray = $this->profile->getProWithUid($proArray['uid']);
                $this->data['conArray'][$k]['company'] = $profileArray['company'];
                $countMeasureInt = $this->actmeasure->getCountMeasure($v['stid']);
                $countTotalMeasureInt = $this->actmeasure->getMearsureCount($v['pid']);
                $this->data['conArray'][$k]['countmeasure'] = $countMeasureInt;
                $this->data['conArray'][$k]['countTotalMeasureInt'] = $countTotalMeasureInt;
                $this->data['conArray'][$k]['changTotal'] = $this->numofperact->getChangTotal($v['pid']);
                $this->data['conArray'][$k]['currdone'] = $this->numofperact->getCurrDoneNow($v['pid']);
                $this->data['conArray'][$k]['contracttotal'] = $this->actmeasure->getTotalWithPid($v['pid']);
                if ($this->data['conArray'][$k]['contracttotal']) {
                    $this->data['conArray'][$k]['pcurrdone'] = round(($this->data['conArray'][$k]['currdone'] / $this->data['conArray'][$k]['contracttotal']) * 100) . '%';
                } else {
                    $this->data['conArray'][$k]['pcurrdone'] = '0%';
                }
                $this->data['conArray'][$k]['bcontracttotal'] = number_format($this->data['conArray'][$k]['contracttotal'], 2, '.', ',');
                $this->data['conArray'][$k]['bchangTotal'] = number_format($this->data['conArray'][$k]['changTotal'], 2, '.', ',');
            }
        }
        $this->render('s-project', $this->data, TRUE);
    }

    public function proOverview() {
//	$this->data['pros'] = $this->project->getAll();
//	if (isset($_POST['pname']) && $_POST['pname']) {
//	    $this->project->insertProject($_POST['pname']);
//	    return Doo::conf()->APP_URL . 'project/index';
//	}
        $this->data['pid'] = $this->params['pid'];
        $this->render('w-project-detail', $this->data);
    }

    public function proSection() {
        $proArray = $this->actmeasure->getGroupProject($this->auth->getUid());
        foreach ($proArray as $v) {
            $pidArray[] = $v['pid'];
        }
        $pidstr = implode(',', $pidArray);
        // 项目名称
        $this->data['allproArray'] = $this->project->getFewRow($pidstr);
        $this->data['currproArray'] = $this->project->getRowByPid($this->params['pid']);
        // 标段按月进度
        for ($i = 1; $i <= 7; $i++) {
            $monthz[] = date('n月', strtotime('+' . $i . ' months', $this->data['currproArray']['intime']));
            if ($i == 1) {
                $ii = date('Y年', $this->data['currproArray']['intime']) . date('n月', strtotime('+' . $i . ' months', $this->data['currproArray']['intime']));
            } else {
                $ii = date('n月', strtotime('+' . $i . ' months', $this->data['currproArray']['intime']));
            }
            $this->data['months'][$i]['month'] = $ii;
        }
        // 标段
        // 搜索有没有相应的月份，有就把实际赋值，没有就为零
        $this->data['MeasureArray'] = $this->actmeasure->getRowsByPid($this->params['pid']);
        foreach ($this->data['MeasureArray'] as $key => $value) {
            $this->data['MeasureArray'][$key]['pmid'] = $value['pmid'];
            $this->data['MeasureArray'][$key]['type'] = $this->contractact->getRowByStid($value['stid'])['stname'];
            $numArray = $this->numofperact->getRowByPmid2($value['pmid']);
            $num = $num1 = 0;
            $this->data['MeasureArray'][$key]['num'] = $this->data['MeasureArray'][$key]['num1'] = NULL;
            foreach ($numArray as $k => $v) {
                if ($v['currstatus'] == 'checked') {
                    $monthInt = date('n月', $v['audittime']);
                    $retval = in_array($monthInt, $monthz);
                    if ($retval) {
                        $num = round(($v['currdone'] / $value['contracttotal']) * 100);
                        $num1 = round(($v['curralltotal'] / $value['contracttotal']) * 100);
                    }
                }
                $this->data['MeasureArray'][$key]['num'] .= $num . ',';
                $this->data['MeasureArray'][$key]['num1'] .= $num1 . ',';
            }
            $this->data['MeasureArray'][$key]['totalnum'] = $this->numofperact->getCountTotalnum($value['pmid']);
            $this->data['MeasureArray'][$key]['totalplus'] = number_format($this->actmeasure->getRowByPmid($value['pmid'])['contracttotal'], 2, '.', ',');
            $this->data['MeasureArray'][$key]['stopnow'] = $this->numofperact->getStopNowTotal($value['pmid'])['sntotal'];
            $this->data['MeasureArray'][$key]['nstopnow'] = number_format($this->numofperact->getStopNowTotal($value['pmid'])['sntotal'], 2, '.', ',');
            $this->data['MeasureArray'][$key]['currdone'] = $this->numofperact->getCurrdoneNew($value['pmid'])['currtotal'];
            $this->data['MeasureArray'][$key]['ncurrdone'] = number_format($this->data['MeasureArray'][$key]['currdone'], 2, '.', ',');
            $this->data['MeasureArray'][$key]['pstopnow'] = '0%';
            $this->data['MeasureArray'][$key]['pcurrdone'] = '0%';
            $this->data['MeasureArray'][$key]['pless'] = '0%';
            if ($value['contracttotal'] > 0) {
                $this->data['MeasureArray'][$key]['pstopnow'] = round(($this->data['MeasureArray'][$key]['stopnow'] / $value['contracttotal']) * 100);
                $this->data['MeasureArray'][$key]['dispstopnow'] = $this->data['MeasureArray'][$key]['pstopnow'] . '%';
                $this->data['MeasureArray'][$key]['pcurrdone'] = round(($this->data['MeasureArray'][$key]['currdone'] / $value['contracttotal']) * 100);
                $this->data['MeasureArray'][$key]['dispcurrdone'] = $this->data['MeasureArray'][$key]['pcurrdone'] . '%';
                $this->data['MeasureArray'][$key]['pless'] = (100 - $this->data['MeasureArray'][$key]['pstopnow'] - $this->data['MeasureArray'][$key]['pcurrdone']) . '%';
            }
            $this->data['MeasureArray'][$key]['nless'] = number_format($value['contracttotal'] - $this->data['MeasureArray'][$key]['stopnow'] - $this->data['MeasureArray'][$key]['currdone'], 2, '.', ',');
            $tenderArray = $this->numofperact->getRowTender2($value['pmid']);
            $this->data['MeasureArray'][$key]['ownstatus'] = '审批中';
            $ownerStatusArray = $this->measureauditact->getStatusTen($tenderArray['pmid'], $tenderArray['numpname']);
            if (isset($ownerStatusArray[0]['last']) && isset($ownerStatusArray[0]['mastatus']))
                if (($ownerStatusArray[0]['last'] == 1) && ($ownerStatusArray[0]['mastatus'] == 'checked')) {
                    $this->data['MeasureArray'][$key]['ownstatus'] = '已审批';
                } else {
                    $this->data['MeasureArray'][$key]['ownstatus'] = '审批中';
                }
        }
        $this->render('s-project-section', $this->data, TRUE);
    }

    public function proDetail() {
        $this->data['currproArray'] = $this->data['allproArray'] = NULL;
        $proArray = $this->actmeasure->getGroupProject($this->auth->getUid());
        foreach ($proArray as $v) {
            $pidArray[] = $v['pid'];
        }
        $pidstr = implode(',', $pidArray);
        // 项目名称
        $allproArray = $this->project->getFewRow($pidstr);
        // 面包屑导航项目
        foreach ($allproArray as $kk => $vv) {
            if ($vv['pid'] == $this->params['pid']) {
                $this->data['currproArray'] = $vv;
            } else {
                $this->data['allproArray'][] = $vv;
            }
        }
        // 面包屑导航合同
        $conArray = $this->contractact->getAll();
        $measureArray = $this->actmeasure->getRowByPmid($this->params['pmid']);
        if (isset($measureArray['stid'])) {
            foreach ($conArray as $kkk => $vvv) {
                if ($vvv['stid'] == $measureArray['stid']) {
                    $this->data['currconArray'] = $vvv;
                } else {
                    if ($vvv['pid'] == $this->params['pid'])
                        $this->data['allconArray'][] = $vvv;
                }
            }
        } else {
            $this->data['currconArray'] = $this->data['allconArray'] = NULL;
        }
        // 面包屑导航标段
        $this->data['curractmeasureArray'] = NULL;
        $this->data['allactmeasureArray'] = [];
        $actmeasureArray = $this->actmeasure->getAll();
        foreach ($actmeasureArray as $kkkk => $vvvv) {
            if ($vvvv['stid'] == $measureArray['stid']) {
                if (($vvvv['pmid'] == $this->params['pmid'])) {
                    $this->data['curractmeasureArray'] = $vvvv;
                } else {
                    $this->data['allactmeasureArray'][] = $vvvv;
                }
            }
        }
        // 图标
        $this->data['MeasureArray'] = $this->numofperact->getGroupByLastOne($this->params['pmid']);
        $measureArray = $this->actmeasure->getRowByPmid($this->params['pmid']);
        foreach ($this->data['MeasureArray'] as $key => $value) {
            if ($measureArray['contracttotal'] > ($value['currdone'] > 0)) {
                $this->data['MeasureArray'][$key]['lessTotal'] = round(($value['currdone'] / $measureArray['contracttotal']) * 100);
            } else {
                $this->data['MeasureArray'][$key]['lessTotal'] = 0;
            }
        }
        $this->data['pid'] = $this->params['pid'];
        // 获取当前合同段的期数
//	$this->data['allNumArray'] = $this->numofperact->getNumByStid(1);
        $this->render('s-project-section-detail', $this->data, TRUE);
    }

    public function proMeasure() {
        $MpidArray = $this->measureauditact->getRowByMpid(1);
        foreach ($MpidArray as $key => $value) {
            $uidArray = $value['uid'];
        }
        $this->data['pid'] = $this->params['pid'];
        $this->render('s-project-section-measure', $this->data);
    }

    Function fNumber($number) {
        if ($number == '')
            Return "-";
        $nlen = strlen($number);
        while ($nlen > 3) {
            $fNumber = "," . substr($number, $nlen - 3, 3);
            $number = substr($number, 0, -3);
            $nlen = strlen($number);
        }

        if ($nlen <= 3) {
            $fNumber = $number . $fNumber;
        }
        Return $fNumber;
    }

    public function welcome() {
//	if (!$this->auth->isLoggedIn())
//	    return Doo::conf()->APP_URL;
        if ($this->profile->getProWithUid($_SESSION['uid'])['userid'])
            return Doo::conf()->APP_URL . 'project/list';
        if (isset($_POST['welform'])) {
            $profileUserArray = $_POST;
            $profileUserArray['userid'] = $_SESSION['uid'];
            $this->profile->insertProfile($profileUserArray); // 无自增字段返回0
            return Doo::conf()->APP_URL . 'project/list';
        }
        $this->render('welcome', $this->data);
    }

    public function prolist() {
//	if (!$this->auth->isLoggedIn())
//	    return Doo::conf()->APP_URL;
//	$proArray = new stdClass();
//	$jsonpath = pathinfo($this->attfile->getMaxRow()['filepath']);
//	$extPathdir = Doo::conf()->SITE_PATH . $jsonpath['dirname'] . '/' . $jsonpath['filename'];
//	if ($handle = opendir($extPathdir)) {
//	    while (false !== ($file = readdir($handle))) {
//		$filename = pathinfo($file);
//		if ($filename['extension'] == 'json')
//		    $proArray = json_decode(file_get_contents($extPathdir . '/' . $file));
//	    }
//	    closedir($handle);
//	}
//	$this->data['proArray'] = null;
        $this->render('s-project', $this->data);
    }

}

?>