<?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');
Doo::loadClass('itemmeasurenum');
Doo::loadClass('itemfile');

/* * proDetail
 * MainController
 * Feel free to delete the methods and replace them with your own code.
 *
 * @author darkredz
 */

class ProjectController extends DooController
{

    private $data, $auth, $attfile, $profile, $project, $contractact, $actmeasure, $measureauditact, $numofperact, $statusArray = array('uncheck' => '<span class = "colGray">未审批</span>', 'checking' => '<span class = "colOrange">审批中</span>', 'checked' => '<span class = "colGreen">审批通过</span>', 'checkno' => '<span class = "colRed">审批不通过</span>'), $itemmeasurenum, $itemfle;

    public function beforeRun($resource, $action)
    {
        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->itemmeasurenum = new ItemMeasureNumpofper();
        $this->itemfle = new ItemFile();
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
        $this->data['currChannle'] = 'p';
        $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;
        }
    }

    /**
     *
     * @return type
     */
    public function index()
    {
        if (isset($_POST['pname']) && $_POST['pname']) {
            $this->project->insertProject($this->auth->getUid(), $_POST['pname']);
            return Doo::conf()->APP_URL . 'project/index';
        }
        if (isset($_POST['target_pname']) && $_POST['target_pname'] && isset($_POST['ppid']) && $_POST['ppid']) {
            $this->project->updateProName($_POST['ppid'], $_POST['target_pname']);
            return Doo::conf()->APP_URL . 'project/index';
        }
        $this->data['pros'] = $this->project->getAll($this->auth->getUid());
        if (isset($this->data['pros'][0]['pid'])) {
            foreach ($this->data['pros'] as $key => $value) {
                $this->data['pros'][$key]['numMeasure'] = $this->actmeasure->getMearsureCount($value['pid']);
                $this->data['pros'][$key]['contracttotal'] = $this->actmeasure->getTotalWithPid($value['pid']);
                $this->data['pros'][$key]['changTotal'] = $this->numofperact->getChangTotal($value['pid']);
//		$this->data['pros'][$key]['contracttotal'] += $this->data['pros'][$key]['changTotal'];
                $this->data['pros'][$key]['currdone'] = $this->numofperact->getCurrDoneNow($value['pid']);
                if ($this->data['pros'][$key]['contracttotal']) {
                    $this->data['pros'][$key]['pcurrdone'] = round(($this->data['pros'][$key]['currdone'] / $this->data['pros'][$key]['contracttotal']) * 100) . '%';
                } else {
                    $this->data['pros'][$key]['pcurrdone'] = '0%';
                }
                $this->data['pros'][$key]['bcontracttotal'] = number_format($this->data['pros'][$key]['contracttotal'], 2, '.', ',');
                $this->data['pros'][$key]['bchangTotal'] = number_format($this->data['pros'][$key]['changTotal'], 2, '.', ',');
            }
        } else {
            $this->data['pros'] = [];
        }
        $this->render('w-project', $this->data, TRUE);
    }

    public function proOverview()
    {
        $conArray = $this->contractact->getRowByPid($this->params['pid']);
        $this->data['projectDate'] = '';
        $this->data['conArray'][] = array();
        $this->getFav();
//	$this->data['measureArray'] = $this->actmeasure->getRowByPids($this->params['pid']);
//	foreach ($this->data['measureArray'] as $key => $value) {
//	    $this->data['measureArray'][$key]['stopNow'] = $this->numofperact->getStopNow($value['pmid'])['sntotal'];
//	    $this->data['measureArray'][$key]['pstopnow'] = round(($this->data['measureArray'][$key]['stopNow'] / $value['contracttotal']) * 100);
//	}
        $this->data['pid'] = $this->params['pid'];
        $this->data['conArray'] = $conArray;

        $contracttotal = 0;
        // 不同标段和值
//	$list = $this->numofperact->getStopNowStMonth($v['stid']); //print_r($list);
//	if (empty($list))
//	    $startMonth = date("Y-m");
//	else
//	    $startMonth = $list[0]['month'];
//	//$list=Array ( 0 => Array ( 'sntotal' => 20000 ,'month' => '2014-09' ),1 => Array ( 'sntotal' => 40000, 'month' => '2016-10' ) );
//	$d = 0;
//	$mDList = $mList = array();
//	$toMD = date("Y-m");
//	foreach ($list as $value) {
//	    $flag = true;
//	    do {
//		$MD = date("Y-m", strtotime($startMonth . " " . $d . " month")); //echo $MD."/".$value['month']."<br/>";
//		array_push($mDList, "'" . $MD . "'");
//		array_push($mList, $MD);
//
//		if ($MD == $toMD)//if(strtotime($MD)>=strtotime($toMD))
//		    $flag = false;
//		if ($d > 0 && ($MD == $value['month'] || $toMD == $value['month']))
//		    $flag = false;
//		$d++;
////		  		if ($d==10)
////		  			$flag=false;
//	    } while ($flag);
//	}
//	$this->data['projectDate'] = implode(',', $mDList);
//
//	$dataList = array();
//	$sntotalTA = 0;
//	foreach ($mList as $va) {
//	    $falg = true;
//	    foreach ($list as $value) {
//		if ($va == $value['month']) {
//		    $sntotalTA+=$value['sntotal'];
//		    $PER = round($sntotalTA / $contracttotal, 3) * 100;
//		    array_push($dataList, $PER);
//		    $falg = false;
//		    break;
//		}
//	    }
//	    if ($falg)
//		array_push($dataList, 0);
//	}
//	$this->data['conArray'][$k]['monthStopnow'] = $dataList;
//
//	$list = $this->numofperact->getCurrDoneSMontht($v['stid']);
//	$dataList = array();
//	$currdoneTA = 0;
//	foreach ($mList as $va) {
//	    $falg = true;
//	    foreach ($list as $value) {
//		if ($va == $value['month']) {
//		    $currdoneTA+=$value['currdone'];
//		    $PER = round($currdoneTA / $contracttotal, 3) * 100;
//		    array_push($dataList, $PER);
//		    $falg = false;
//		    break;
//		}
//	    }
//	    if ($falg)
//		array_push($dataList, 0);
//	}
//	$this->data['conArray'][$k]['monthCurrdone'] = $dataList;
//
        $totalstid = 0;
        $monthArray = [];
        foreach ($this->data['conArray'] as $k => $v) {
            $this->data['conArray'][$k]['pstopnow'] = $this->data['conArray'][$k]['pscurrdone'] = 0;
            $retval = $this->actmeasure->getAllbyStidSum($v['stid']);
            $contracttotal += $retval[0]['contracttotal'];
            $totalstid += $this->actmeasure->getTotal($v['stid'])['total'];
            $total = $this->actmeasure->getTotal($v['stid'])['total'];
            if ($total) {
                $this->data['conArray'][$k]['total'] = round($total, 2);
            } else {
                $this->data['conArray'][$k]['total'] = 0;
            }
            $sntotal = $this->numofperact->getCountTotalSTID2($v['stid'])['totalplus'];
            if ($sntotal) {
                $this->data['conArray'][$k]['stopnow'] = round($sntotal, 2);
            } else {
                $this->data['conArray'][$k]['stopnow'] = 0;
            }
            $this->data['conArray'][$k]['currdone'] = $this->numofperact->getCurrDoneSt($v['stid'])['currdone'];
            if ($this->data['conArray'][$k]['total'] > 0) {
                $this->data['conArray'][$k]['pstopnow'] = round(($this->data['conArray'][$k]['stopnow'] / $this->data['conArray'][$k]['total']) * 100);
                $this->data['conArray'][$k]['pscurrdone'] = round(($this->data['conArray'][$k]['currdone'] / $this->data['conArray'][$k]['total']) * 100);
            }
            // 分组出年月 每个月份的独立相加
            $month = $this->numofperact->getStopNowStMonth($v['stid']);
            foreach ($month as $key => $value) {
                $allmonth[] = $value;
                if (!in_array($value['month'], $monthArray))
                    $monthArray = $value['month'];
            }

            // 分组出年月 每个月份的独立相加
            $month1 = $this->numofperact->getCurrDoneSMontht($v['stid']);
            foreach ($month1 as $key => $value) {
                $allmonth1[] = $value;
            }
        }
        // 中文表述年月
        foreach ($monthArray as $kk => $vv) {
            foreach ($allmonth as $keyk => $valuev) {
                if ($valuev['month'] == $vv)
                    $total[$kk] += $valuev['sntotal'];
            }
            foreach ($allmonth1 as $k1 => $v1) {
                if ($v1['month'] == $vv)
                    $total1[$kk] += $v1['currdone'];
            }
            $ymarray = split('-', $vv);
            $cnym = '\'' . $ymarray[1] . '月' . '\'';
            if ($kk == 0)
                $cnym = '\'' . $ymarray[0] . '年' . $ymarray[1] . '月' . '\'';
            $cnymArray[] = $cnym;
        }
        foreach ($total as $value2) {
            $total3[] = round(($value2 / $totalstid) * 100);
        }
        foreach ($total1 as $value11) {
            $total4[] = round(($value11 / $totalstid) * 100);
        }
        $this->data['projectDate'] = implode(',', $cnymArray);
        $this->data['t1'] = implode(',', $total3);
        $this->data['t2'] = implode(',', $total4);
        $this->render('w-project-detail', $this->data, TRUE);
    }

    public function proSection()
    {
        if (isset($_POST['stid']) && isset($_POST['proname'])) {
            $this->contractact->updateStName($_POST['stid'], $_POST['proname']);
            return Doo::conf()->APP_URL . 'project/' . $this->params['pid'] . '/section';
        }
        $this->getFav();
        $contractArray = $this->contractact->getRowByPid($this->params['pid']); // 获取合同段
        $htmlstr = NULL;
        foreach ($contractArray as $k => $v) {// 获取所有合同段
            $retval = $this->actmeasure->getAllbyStid($v['stid']); // 获取标段
            $bdhtmlstr = NULL;
            $bdhtmlstr = '<thead>
				    <tr><th class="taC taCble header"  width="225">标段名</th><th class="taC" width="140">计量期数</th><th width="115" class="taC">总价</th><th class="taC" width="">截止上期完成/本期完成/未完成</th></tr></thead>
				<tbody>';
            $biaoduantotalmoney = 0;
            $currTotal = 0;
            $progress = $otherprogress = '0%';
            $currTotal = $this->numofperact->getCountTotalSTID2($v['stid']);
            foreach ($retval as $key => $value) {
                $displayPercentSnTotalProgress = '0%';
                $displayPercentCurrTotalProgress = '0%';
                $displayPercentLessTotalProgress = '0%';
                $snTotalProgress = $currTotalProgress = $lessTotalProgress = '0%';
                $nTotalProgress = $ncurrTotalProgress = $nlessTotalProgress = number_format(0, 2, '.', ',');
                $numCount = $this->numofperact->getCountTotalnum2($value['pmid']); // 获取标段已有期数
                if (isset($numCount['numpname'])) {
                    $lastStatusArray = $this->numofperact->getMaxStatusTimes($value['pmid'], $numCount['numpname']);
                } else {
                    $lastStatusArray['currstatus'] = 'uncheck';
                    $numCount['numpname'] = 0;
                }
                $totalplus = $this->numofperact->getCountTotalplus($value['pmid']);
                $snTotal = $this->numofperact->getStopNowTotal($value['pmid']); //根据标段ID获取最新一期（包括不通过）
                $currcomTotal = $this->numofperact->getCurrdoneNew($value['pmid']); //截止目前
                $numStatusArray = $this->numofperact->getLastStatus2($this->params['pid'], $value['pmid']);
                if ($numCount > 0) {
                    $nTotalProgress = number_format($snTotal['sntotal'], 2, '.', ',');
                    $ncurrTotalProgress = number_format($currcomTotal['currtotal'], 2, '.', ',');
                    $nlessTotalProgress = number_format($value['contracttotal'] - $snTotal['sntotal'] - $currcomTotal['currtotal'], 2, '.', ',');
                    if (isset($value['contracttotal']) && ($value['contracttotal'] > 0)) {
                        $snTotalProgress = round(($snTotal['sntotal'] / $value['contracttotal']) * 100);
                        $currTotalProgress = round(($currcomTotal['currtotal'] / $value['contracttotal']) * 100);
//			$lessTotalProgress = round((($value['contracttotal'] - ($currcomTotal['currtotal'] + $snTotal['sntotal'])) / $value['contracttotal']) * 100);
                        $lessTotalProgress = 100 - $snTotalProgress - $currTotalProgress;
                        $displayPercentSnTotalProgress = $snTotalProgress . '%';
                        $displayPercentCurrTotalProgress = $currTotalProgress . '%';
                        $displayPercentLessTotalProgress = $lessTotalProgress . '%';
                    }
                }
                $biaoduantotalmoney += $value['contracttotal'];
                $totalmoney = number_format($value['contracttotal'], 2, '.', ',');
                if (isset($numStatusArray['currstatus'])) {
                    $statusStr = '（' . $this->statusArray[$lastStatusArray['currstatus']] . '）';
                } else {
                    $statusStr = '';
                }
                $bdhtmlstr .= '
				<tr>
					<td><a href="/project/' . $this->params['pid'] . '/section/' . $value['pmid'] . '/detail">' . $value['pmname'] . '</a></td>
					<td class="">第 ' . $numCount['numpname'] . ' 期' . $statusStr . '</td>
					<td class="taR">￥' . $totalmoney . '</td>
					<td>
					    <div class="progress">
						<div class="bar bar-success" style="width: ' . $displayPercentSnTotalProgress . ';" data-placement="bottom" data-toggle="tooltip" data-original-title="截止上期完成：￥' . $nTotalProgress . '">' . $displayPercentSnTotalProgress . '</div>
						<div class="bar" style="width:' . $displayPercentCurrTotalProgress . ';" data-placement="bottom" data-toggle="tooltip" data-original-title="本期完成：￥' . $ncurrTotalProgress . '">' . $displayPercentCurrTotalProgress . '</div>
						<div class="bar bar-gary" style="width:' . $displayPercentLessTotalProgress . ';" data-placement="bottom" data-toggle="tooltip" data-original-title="未完成：￥' . $nlessTotalProgress . '">' . $displayPercentLessTotalProgress . '</div>
					    </div>
					</td>
				    </tr>';
            }
            $nprogress = $notherprogress = NULL;
//	    $currTotal = $this->numofperact->getCurrdoneSTID($v['stid']);
            if ($biaoduantotalmoney > 0) {
                $nprogress = number_format($currTotal['totalplus'], 2, '.', ',');
                $notherprogress = number_format($biaoduantotalmoney - $currTotal['totalplus'], 2, '.', ',');
                $progress = round(($currTotal['totalplus'] / $biaoduantotalmoney) * 100) . '%'; // 累计完成计量 所有标段合计
                $otherprogress = round((($biaoduantotalmoney - $currTotal['totalplus']) / $biaoduantotalmoney) * 100) . '%';
                $biaoduantotalmoney = number_format($biaoduantotalmoney, 2, '.', ',');
            }
            $htmlstr .= '<div class="project">
			<div class="proSection">
			    <table class="table">
				<thead>
				    <tr>
					<td width="150"><span aria-hidden="true" data-icon="u"></span> ' . $v['stname'] . '<a id="idt_' . $v['stid'] . '" href="#secoption" data-toggle="modal" title="编辑/查看KEY" stid="' . $v['stid'] . '" proname="' . $v['stname'] . '" key="' . $v['stkey'] . '"><span data-icon="S" aria-hidden="true" class="closePanel" data-placement="bottom" data-toggle="tooltip" data-original-title="查看KEY" style="font-size:18px"></span></a></td>
					<td width="90">总价：</td><td width="165">￥<b style="font-size:16px">' . $biaoduantotalmoney . '</b></td>
					<td width="60">完成进度：</td><td><div class="progress">
						<div class="bar bar-success" style="width:' . $progress . ';" data-placement="bottom" data-toggle="tooltip" data-original-title="累计完成：￥' . $nprogress . '">' . $progress . '</div>
						<div class="bar bar-danger" style="width:' . $otherprogress . ';" data-placement="bottom" data-toggle="tooltip" data-original-title="未完成：￥' . $notherprogress . '">' . $otherprogress . '</div>
					    </div></td>
				    </tr>
				</thead>
			    </table>
			    <table class="table table-striped table-hover tablelist">
				' . $bdhtmlstr . '
				</tbody>
			    </table>
			</div>
		    </div>';
        }
        $this->data['htmlstr'] = $htmlstr;
        if (isset($_POST['secname']) && $_POST['secname']) {
            $this->contractact->insertContract($this->params['pid'], $this->auth->getUid(), $_POST['secname']);
            return Doo::conf()->APP_URL . 'project/' . $this->params['pid'] . '/section';
        }
        $this->data['pid'] = $this->params['pid'];
        $this->render('w-project-section', $this->data, TRUE);
    }

    function proSectionFiles()
    {
        // 面包屑导航项目
        $this->data['currproArray'] = $this->data['allproArray'] = NULL;
        $this->data['pmid'] = $this->params['pmid'];
        $allproArray = $this->project->getAll();
        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['itemMeasureArray'] = $this->itemmeasurenum->getItemMeasureNum($this->params['pmid']);
        foreach ($this->data['itemMeasureArray'] as $k => $v) {
            $fileArray = $this->itemfle->getItemFile($v['iaid']);
            $this->data['itemMeasureArray'][$k]['realname'] = $this->profile->getProWithUid($v['ownerid'])['name'];
            $this->data['itemMeasureArray'][$k]['pmname'] = $this->actmeasure->getRowByPmid($v['pmid'])['pmname'];
            $this->data['itemMeasureArray'][$k]['filename'] = $fileArray['filename'];
            $this->data['itemMeasureArray'][$k]['fileext'] = $fileArray['fileext'];
            $this->data['itemMeasureArray'][$k]['tips'] = $v['tips'];
        }
        $this->data['pid'] = $this->params['pid'];
        $this->data['pmid'] = $this->params['pmid'];
        $this->data['mpid'] = $this->params['mpid'];
        $this->render('w-project-section-files', $this->data, TRUE);
    }

    function proSectionFilesRecover()
    {
        // 面包屑导航项目
        $this->data['currproArray'] = $this->data['allproArray'] = NULL;
        $this->data['pmid'] = $this->params['pmid'];
        $allproArray = $this->project->getAll();
        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['itematta'] = $this->itemfle->getFilesAll($this->params['pmid']);
        foreach ($this->data['itematta'] as $k => $v) {
            $this->data['itematta'][$k]['realname'] = $this->profile->getProWithUid($v['ownerid'])['name'];
            $this->data['itematta'][$k]['pmname'] = $this->actmeasure->getRowByPmid($v['pmid'])['pmname'];
        }
        $this->data['pid'] = $this->params['pid'];
        $this->data['pmid'] = $this->params['pmid'];
        $this->data['mpid'] = $this->params['mpid'];
        $this->render('w-project-section-files-recover', $this->data, TRUE);
    }

    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;
    }

    private function getFav()
    {
        $proArray = $this->project->getAll($this->auth->getUid());
        $this->data['othrPro'] = [];
        foreach ($proArray as $key => $value) {
            if ($value['pid'] == $this->params['pid']) {
                $this->data['currProName'] = $value['pname'];
                $this->data['currProID'] = $value['pid'];
            } else {
                $this->data['othrPro'][] = $proArray[$key];
            }
        }
    }

    public function proSectionMeasure()
    {
//此处未做更改，JSON文件已经固定名称
        $jsonpath = pathinfo($this->attfile->getMaxRow()['filepath']);
        if (isset($jsonpath['dirname'])) {
            $extPathdir = Doo::conf()->SITE_PATH . $jsonpath['dirname'] . '/' . $jsonpath['filename'];
            if ($handle = opendir($extPathdir)) {
                while (false !== ($file = readdir($handle))) {
                    $filename = NULL;
                    $filename = pathinfo($file);
                    if ($filename['extension'] == 'json')
                        $proArray = json_decode(file_get_contents($extPathdir . '/' . $file), TRUE, JSON_UNESCAPED_UNICODE);
                }
                closedir($handle);
            }
            $this->data['proArray'] = $proArray['Bills'];
        } else {
            $this->data['proArray'] = [];
        }
        $this->data['mpid'] = $this->params['mpid'];
        $this->data['pid'] = $this->params['pid'];
        $this->render('w-project-section-measure', $this->data, TRUE);
    }

    public function substr_replace_cn($string, $repalce = '*', $start = 0, $len = 0)
    {
        $count = mb_strlen($string, 'UTF-8'); //此处传入编码，建议使用utf-8。此处编码要与下面mb_substr()所使用的一致
        if (!$count) {
            return $string;
        }
        if ($len == 0) {
            $end = $count;  //传入0则替换到最后
        } else {
            $end = $start + $len;       //传入指定长度则为开始长度+指定长度
        }
        $i = 0;
        $returnString = '';
        while ($i < $count) { //循环该字符串
            $tmpString = mb_substr($string, $i, 1, 'UTF-8'); // 与mb_strlen编码一致
            if ($start <= $i && $i < $end) {
                $returnString .= $repalce;
            } else {
                $returnString .= $tmpString;
            }
            $i++;
        }
        return $returnString;
    }

    function unicode_encode($name)
    {//to Unicode
        $name = iconv('UTF-8', 'UCS-2', $name);
        $len = strlen($name);
        $str = '';
        for ($i = 0; $i < $len - 1; $i = $i + 2) {
            $c = $name[$i];
            $c2 = $name[$i + 1];
            if (ord($c) > 0) {// 两个字节的字
                $str .= '\\' . base_convert(ord($c), 10, 16) . base_convert(ord($c2), 10, 16);
            } else {
                $str .= $c2;
            }
        }
        $str = strtoupper($str);
        return $str;
    }

    function unicode_decode($name)
    {//Unicode to
        $pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
        preg_match_all($pattern, $name, $matches);
        if (!empty($matches)) {
            $name = '';
            for ($j = 0; $j < count($matches[0]); $j++) {
                $str = $matches[0][$j];
                if (strpos($str, '\\u') === 0) {
                    $code = base_convert(substr($str, 2, 2), 16, 10);
                    $code2 = base_convert(substr($str, 4), 16, 10);
                    $c = chr($code) . chr($code2);
                    $c = iconv('UCS-2', 'UTF-8', $c);
                    $name .= $c;
                } else {
                    $name .= $str;
                }
            }
        }
        return $name;
    }

    public function proDetail()
    {
// 面包屑导航项目
        $this->data['currproArray'] = $this->data['allproArray'] = NULL;
        $this->data['pmid'] = $this->params['pmid'];
        $allproArray = $this->project->getAll();
        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'] && $value['currdone'] > 0) {
                $this->data['MeasureArray'][$key]['lessTotal'] = round(($value['currdone'] / $measureArray['contracttotal']) * 100);
            } else {
                $this->data['MeasureArray'][$key]['lessTotal'] = 0;
            }
        }
        $this->data['pmid'] = $this->params['pmid'];
        $this->data['pid'] = $this->params['pid'];
        $this->render('w-project-section-detail', $this->data, TRUE);
    }

    public function welcome()
    {
//	if (!$this->auth->isLoggedIn())
//	    return Doo::conf()->APP_URL;
        if ($this->profile->getProWithUid($this->auth->getUid())['userid'])
            return Doo::conf()->APP_URL . 'project/list';
        if (isset($_POST['welform'])) {
            $profileUserArray = $_POST;
            $profileUserArray['userid'] = $this->auth->getUid();
            $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);
    }

// ajax提取密码名称
    public function getAjaxSection()
    {
        if (!$this->isAjax())
            return;
        echo json_encode($_POST);
    }

}

?>