<?php
include_once('./rsa.php');
session_start();
$sid=session_id();
$fid=isset($_COOKIE['fid'])?$_COOKIE['fid']:'';

$fid=Rsa::publicDecrypt($fid);
$guid=isset($_COOKIE['guid'])?$_COOKIE['guid']:'';
$guid=Rsa::publicDecrypt($guid);


//授权文件不存在，跳转
if(!file_exists('/auth/certs/'.$fid.'.key')){
	echo '授权失败，正在跳转';
	echo '<script>setTimeout(function(){window.location=\'auth.php\';},1500);</script>';
	die();			
}

if(!file_exists('/auth/tmp/'.$guid)){

	echo '该客户端未授权,请授权后访问';
	echo '<script>setTimeout(function(){window.location=\'auth.php\';},1500);</script>';
	die();

}else{

	$lines=file_get_contents('/auth/tmp/'.$guid);
	$lines=explode("\n",$lines);
	$flag=false;

	foreach($lines as $line){
		
		if($line!=""){
			$d=Rsa::privDecrypt(trim($line));
			if(empty($d)){ continue;}
			$d=explode('::',trim($d));
			//相等表明之前授权过，并且没有被踢掉
			if($d[1]==$sid){
				$flag=true;
				break;
			}
		}
		
	}

	if($flag==false){
		echo '该客户端未授权,请授权后访问';
		echo '<script>setTimeout(function(){window.location=\'auth.php\';},1500);</script>';
		die();
	}
}

date_default_timezone_set("PRC");

   require_once('common/base.php');
 
   $mode=$config['data_from_scada'];
    if ($mode=='true') {
        $mode="SCADA";
    } else {
        $mode="NORMAL";
    }
   $wcids_right=$config['workcenter_of_device'];
   $wcnames_right=array();
   $sql='select CenterName from WorkCenter where CenterId IN ('.implode(',', $wcids_right).')';
   $stmt=sqlsrv_query($conn, $sql);
   if ($stmt) {
       while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
           if ($row!=null) {
               $wcnames_right[]=$row['CenterName'];
           }
       }
       sqlsrv_free_stmt($stmt);
   }
   $wcnames_right=implode(',', $wcnames_right);
   $wcnames_left=array();
   $wcids_left=$config['workcenters'];
   $sql='select CenterName from WorkCenter where CenterId IN ('.implode(',', $wcids_left).')';
   $stmt=sqlsrv_query($conn, $sql);
   if ($stmt) {
       while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
           if ($row!=null) {
               $wcnames_left[]=$row['CenterName'];
           }
       }
       sqlsrv_free_stmt($stmt);
   }
   $wcnames_left=implode(',', $wcnames_left);

   $starttime=null;
//    $sql='select  top 1 * from TimeSchedule order by CONVERT(VARCHAR(255),AttStartTime,108)  asc';
//    $stmt=sqlsrv_query($conn, $sql);
//    if ($stmt) {
//        $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
//        $starttime=$row['AttStartTime']->format('H:i');
//        sqlsrv_free_stmt($stmt);
//    }
//    $starttime=date('Y-m-d '.$starttime, time());
   $starttime=date('Y-m-d 00:00', time());
   $endtime=date('Y-m-d 23:59', time());
   
   if (isset($_GET['starttime'])) {
       $starttime=$_GET['starttime'];
   }
   
   if (isset($_GET['endtime'])) {
       $endtime=$_GET['endtime'];
   }
   
?>
<!doctype html>
<html>
<head>
    <script src="static/js/vue.min.js" ></script>
    <script src="static/js/axios.min.js" ></script>   
    <script src="static/js/echarts.min.js" ></script> 
    <script src="static/js/moment-with-locales.js" ></script>     
    <script src="static/js/util.js" ></script>     
    <link rel="stylesheet" href="static/css/base.css">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="static/element-ui/index.css">
    <!-- 引入组件库 -->
    <script src="static/element-ui/index.js"></script>    
	<link rel="stylesheet" href="static/iconfont/iconfont.css">
    <link rel="stylesheet" href="static/css/app.css">

</head>

<body>
<div id="app">
    <?php include('app.header.php') ?>
    <?php include('app.normal.php') ?>
    <?php include('app.fullsrn.php') ?> 
    <?php include('app.cnc.php') ?> 
    <?php include('app.histdata.php') ?> 
    <?php include('app.normalequ.php') ?> 
</div>

   
    


<script>

    axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
    let commonOption={
        stimeBound:new Date('<?php echo  $starttime;?>'),
        etimeBound:new Date('<?php echo  $endtime;?>'),
        starttime:new Date('<?php echo  $starttime;?>'),
        endtime:new Date('<?php echo  $endtime;?>'),
        laststarttime:new Date('<?php echo  $starttime;?>'),
        lastendtime:new Date('<?php echo  $endtime;?>'),  
        wcids_left:[<?php echo implode(',', $wcids_left); ?>],
        wcids_right:[<?php echo implode(',', $wcids_right); ?>], 
        wcnames_left:'<?php  echo $wcnames_left; ?>',
        wcnames_right:'<?php  echo $wcnames_right; ?>',
        config:<?php echo json_encode($config); ?>,
        mode:'<?php echo $mode;?>',          
    }
  
</script>
<script src="static/js/app.header.js"></script>
<script src="static/js/app.normalbody.js"></script>
<script src="static/js/app.fullsrn.js"></script>
<script src="static/js/app.cnc.js"></script>
<script src="static/js/app.histdata.js"></script>
<script src="static/js/app.normalequ.js"></script>
<script>
    let appStack=[]
    appNormalBody.$mount('#body-normal')
    appStack.push(appNormalBody)
    appFullsrn.$mount('#app-fullsrn')
    appCnc.$mount('#app-cnc')
    appHistData.$mount('#app-hist-data')
    appNormalEqu.$mount('#app-normalequ')
</script>
</body>
</html>