
<?php

include_once('common/base.php');

function getProductionLines($pdepid, &$producingLines)
{
    global $conn;
    //读取部门数据
    $sql_departments='SELECT [DepId],[DepCode],[DepName],[PDepId],[Property] FROM [dbo].[Department] where  [PDepId]='.$pdepid.' Order By [Sequence] ';
    $stmt_departments=sqlsrv_query($conn, $sql_departments);
    if ($stmt_departments) {
        while ($row = sqlsrv_fetch_array($stmt_departments, SQLSRV_FETCH_ASSOC)) {
            if ($row!=null) {
                if ($row['Property']==1) {
                    $producingLines[]=$row;
                } else {
                    getProductionLines($row['DepId'], $producingLines);
                }
            }
        }
        
        sqlsrv_free_stmt($stmt_departments);
    }
}

function getWorkCenters($pdepid, &$workcenters)
{
    global $conn;
    if (empty($pdepid)) {
        $sql_workcenters = 'SELECT [CenterId],[CenterNo],[CenterName] FROM [dbo].[WorkCenter]   Order BY [dbo].[WorkCenter].[Sequence] ASC,[dbo].[WorkCenter].[CenterId] ASC';
    } else {
        $sql_workcenters= 'SELECT [dbo].[WorkCenter].[CenterId],[CenterNo],[CenterName] FROM [dbo].[WorkCenter] INNER JOIN [dbo].[DepWorkCenter] ON [dbo].[WorkCenter].[CenterId]=[dbo].[DepWorkCenter].[CenterId] AND [dbo].[DepWorkCenter].[DepId]='.$pdepid.' Order BY [dbo].[WorkCenter].[Sequence] ASC,[dbo].[WorkCenter].[CenterId] ASC';
    }

    $stmt_workcenters=sqlsrv_query($conn, $sql_workcenters);
    if ($stmt_workcenters) {
        while ($row = sqlsrv_fetch_array($stmt_workcenters, SQLSRV_FETCH_ASSOC)) {
            $row['checked']=false;
            $workcenters[]=$row;
        }
        sqlsrv_free_stmt($stmt_workcenters);
    }

    if (empty($pdepid)) {
        return ;
    }
    //读取部门数据
    $sql_departments='SELECT [DepId],[DepCode],[DepName],[PDepId],[Property] FROM [dbo].[Department] where  [PDepId]='.$pdepid.' Order By [Sequence] ';
    $stmt_departments=sqlsrv_query($conn, $sql_departments);
    if ($stmt_departments) {
        while ($row = sqlsrv_fetch_array($stmt_departments, SQLSRV_FETCH_ASSOC)) {
            if ($row!=null) {
                if ($row['Property']!=1) {
                    getWorkCenters($row['DepId'], $workcenters);
                }
            }
        }
        sqlsrv_free_stmt($stmt_departments);
    }
}


if (isset($_GET['cmd'])&&($_GET['cmd']=='getWorkCenters')) {
    $is_producing_line=1;
    if (isset($_GET['is_producing_line'])) {
        $is_producing_line=$_GET['is_producing_line'];
    }
    $depid=$_GET['depid'];
    $producingLines=array();
    $workcenters=array();
    if ($is_producing_line==1) {
        getProductionLines($depid, $producingLines);
        $sql_workcenters=' SELECT [CenterId],[CenterNo],[CenterName] FROM [dbo].[WorkCenter]   Order BY [dbo].[WorkCenter].[Sequence] ASC,[dbo].[WorkCenter].[CenterId] ASC';
        $stmt_workcenters=sqlsrv_query($conn, $sql_workcenters);

        if ($stmt_workcenters) {
            while ($row = sqlsrv_fetch_array($stmt_workcenters, SQLSRV_FETCH_ASSOC)) {
                $row['checked']=false;
                $workcenters[]=$row;
            }
            sqlsrv_free_stmt($stmt_workcenters);
        }
    } else {
        getWorkCenters($depid, $workcenters);
        //工作站
        // if (empty($depid) || $depid==0) {
        //     $sql_workcenters='SELECT [CenterId],[CenterNo],[CenterName] FROM [dbo].[WorkCenter]   Order BY [dbo].[WorkCenter].[Sequence] ASC,[dbo].[WorkCenter].[CenterId] ASC';
        // } else {
        //     $sql_workcenters= 'SELECT [dbo].[WorkCenter].[CenterId],[CenterNo],[CenterName] FROM [dbo].[WorkCenter] INNER JOIN [dbo].[DepWorkCenter] ON [dbo].[WorkCenter].[CenterId]=[dbo].[DepWorkCenter].[CenterId] AND [dbo].[DepWorkCenter].[DepId]='.$depid.' Order BY [dbo].[WorkCenter].[Sequence] ASC,[dbo].[WorkCenter].[CenterId] ASC';
        // }
    }
    

    
    if ($config['work_order']['producing_department']==$depid) {
        foreach ($workcenters as &$workcenter) {
            if (in_array($workcenter['CenterId'], $config['work_order']['workcenters'])) {
                $workcenter['checked']=true ;
            }
        }
    }
    
    $repsonse['producingLines']=$producingLines;
    $repsonse['workcenters']=$workcenters;
    echo json_encode($repsonse);
    exit();
}

if (isset($_POST['save'])) {
    $cols=$config['parameters']['cols'];
    $colwidths=$_POST['colwidths'];
    foreach ($cols as $k=>$v) {
        $cols[$k]=false;
    }
    
    foreach ($_POST['cols'] as $v) {
        $cols[$v]=true;
    }



    $config2=array(
        
        //制令过滤
        "work_order"=>array(
            //绑定生产部门
            "producing_department"=>$_POST['producing_department'],
            "producing_lines"=>isset($_POST['producing_line'])?$_POST['producing_line']:array(),
            "workcenters"=>isset($_POST['workcenter'])?$_POST['workcenter']:array(),
            "is_producing_line" => $_POST['is_producing_line'],
            //1:仅查今日 2:看3日计划 3:查本周计划 4:查本月计划
            "plan_search_mode"=>$_POST['plan_search_mode'],
            //1:仅查今日 2:查本周计划 3:查本月计划
            "defect_search_mode"=>$_POST['defect_search_mode']
        ),
        
        //参数设置
        "parameters"=>array(
            //表窗体显示列选项
            "cols"=>$cols,
            "colwidths"=>$colwidths,
            "colnames"=>$config['parameters']['colnames'],
            //数据滚动和刷新的时间间隔，单位分钟
            "interval"=>$_POST['interval'],
            //效率预警kpi
            "efficient_kpi_boundary"=>$_POST['efficient_kpi'],
            //达成率预警kpi
            //"achieving_rate_boundary"=>$_POST['achieving_rate'],
            //今日目标预警kpi
            "today_target_kpi_boundary"=>$_POST['today_target_kpi'],
			//"today_output_kpi_boundary"=>$_POST['today_output_kpi'],
			"month_outputs_kpi_boundary"=>$_POST['month_outputs_kpi'],
			"time_seq_chart_source"=>$_POST['time_seq_chart_source'],
            //看板名称
            "ekb_name"=>$_POST['ekbname'],
            //看板子名称
            'sub_ekb_name' =>$_POST['sub_ekb_name'],
            "table_interval"=>$_POST['table_interval'],
			'work_order_data_show'=>$_POST['work_order_data_show']
        ),
        //'sp_achieving_rate'=>$_POST['sp_achieving_rate'],
        //'sp_qualified_rate'=>$_POST['sp_qualified_rate'],
        //'sp_current_workcenter'=>$_POST['sp_current_workcenter'],
    );


    file_put_contents('config.json', json_encode($config2));
    $config=array_merge($config, $config2);
    $save_success=true;
}

//读取部门数据
// $departments=array();
// $sql_departments='SELECT [DepId],[DepCode],[DepName] FROM [dbo].[Department] ';
// $stmt_departments=sqlsrv_query($conn, $sql_departments);
// if ($stmt_departments) {
//     while ($row = sqlsrv_fetch_array($stmt_departments, SQLSRV_FETCH_ASSOC)) {
//         $departments[]=$row;
//     }
// }
$departments=[];
function getDepartments($pdepid, $prefix='')
{
    global $departments,$conn;
    //读取部门数据
    $sql_departments='SELECT [DepId],[DepCode],[DepName],[PDepId] FROM [dbo].[Department] where [PDepId]='.$pdepid.' AND [Property]<>1 Order By [Sequence] ';
    $stmt_departments=sqlsrv_query($conn, $sql_departments);
    if ($stmt_departments) {
        while ($row = sqlsrv_fetch_array($stmt_departments, SQLSRV_FETCH_ASSOC)) {
            if ($row!=null) {
                $row['prefix']=$prefix;
                $departments[]=$row;
                getDepartments($row['DepId'], $prefix.'----');
            }
        }
    }
}
getDepartments(0, '');

$producingLines=array();
$workcenters=array();
$depid=$config['work_order']['producing_department'];
//读取产线
if ($config['work_order']['is_producing_line']==1) {
    getProductionLines($depid, $producingLines);

    $sql_workcenters='SELECT [CenterId],[CenterNo],[CenterName] FROM [dbo].[WorkCenter]   Order BY [dbo].[WorkCenter].[Sequence] ASC,[dbo].[WorkCenter].[CenterId] ASC';
    $stmt_workcenters=sqlsrv_query($conn, $sql_workcenters);

    if ($stmt_workcenters) {
        while ($row = sqlsrv_fetch_array($stmt_workcenters, SQLSRV_FETCH_ASSOC)) {
            $workcenters[]=$row;
        }
        
        sqlsrv_free_stmt($stmt_workcenters);
    }
} else {
    getWorkCenters($depid, $workcenters);
    
    // //工作站
//     if (empty($config['work_order']['producing_department'])) {
//         $sql_workcenters='SELECT [CenterId],[CenterNo],[CenterName] FROM [dbo].[WorkCenter]   Order BY [dbo].[WorkCenter].[Sequence] ASC,[dbo].[WorkCenter].[CenterId] ASC';
//     } else {
//         $sql_workcenters= 'SELECT [dbo].[WorkCenter].[CenterId],[CenterNo],[CenterName] FROM [dbo].[WorkCenter] INNER JOIN [dbo].[DepWorkCenter] ON [dbo].[WorkCenter].[CenterId]=[dbo].[DepWorkCenter].[CenterId] AND [dbo].[DepWorkCenter].[DepId]='.$config['work_order']['producing_department'].' Order BY [dbo].[WorkCenter].[Sequence] ASC,[dbo].[WorkCenter].[CenterId] ASC';
//     }
}





?>

<!doctype html>
<html>
	<head>
		<title>看板配置</title>
		<meta charset="utf-8" />
		<link rel="stylesheet" type="text/css" href="static/layui/css/layui.css"/>
		<link rel="stylesheet" type="text/css" href="static/css/config.css"/>	
		<script src="static/layui/layui.js" ></script>
		<script src="static/js/config.js" ></script>		
	</head>	
	<body>
		<div class="layui-container ">
			<div class="layui-row header">
				<div class="layui-col-md12">
					<h1><?php echo $config['company']['name'];?>看板系统配置</h1>
				</div>
			</div>
			<div class="layui-row main">
				<?php if (isset($save_success) && $save_success==true): ?>
					<div class="info">保存成功</div>
				<?php endif; ?>
				<form action="" method="post"  class="layui-form layui-form-pane">
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">生产部门</label>
						<div class="layui-input-block tlt-input-block " style="display:flex;">
							<select name="producing_department" lay-filter="producing_department" >
							  	<option value="0">无</option>
								<?php foreach ($departments as $department):?>
								<option value="<?php echo $department['DepId']?>" <?php if ($department['DepId']==$config['work_order']['producing_department']) {
    echo 'selected';
} ?>><?php echo $department['prefix'].$department['DepName']; ?></option>
								<?php endforeach; ?>
							</select>
						</div>
					  </div>	
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">是否加入产线</label>
						<div class="layui-input-block tlt-input-block "  style="display:flex;">
							<select name="is_producing_line" lay-filter="is_producing_line">
								<option value="1" <?php if ($config['work_order']['is_producing_line']==1) {
    echo 'selected';
} ?>>是</option>
								<option value="0" <?php if ($config['work_order']['is_producing_line']==0) {
    echo 'selected';
} ?>>否</option>
							</select> 
						</div>
					  </div>						  			
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">完工站</label>
						<div class="layui-input-block tlt-input-block " id="workcenters"> 
							<?php if ($config['work_order']['is_producing_line']==1): ?>
								<?php foreach ($producingLines as $producingLine):?>
									<div class="layui-row" style="display:flex;flex-wrap:wrap;justify-content:flex-start">						

											<div class="layui-col-md2" style="display:flex;justify-content:flex-end">
												<input type="checkbox" name="producing_line[<?php echo $producingLine['DepId']; ?>][checked]" value="1"  title="<?php echo $producingLine['DepName']; ?>"  <?php if (isset($config['work_order']['producing_lines'][$producingLine['DepId']]) && isset($config['work_order']['producing_lines'][$producingLine['DepId']]['checked']) && intval($config['work_order']['producing_lines'][$producingLine['DepId']]['checked'])==1) {
    echo 'checked';
}  ?> />
											</div>									
											<div class="layui-col-md2">
												<select name="producing_line[<?php echo $producingLine['DepId']; ?>][CenterId]" >
													<?php foreach ($workcenters as $wc):?>
														<option value="<?php echo $wc['CenterId']; ?>"  <?php if (isset($config['work_order']['producing_lines'][$producingLine['DepId']]) && isset($config['work_order']['producing_lines'][$producingLine['DepId']]['CenterId']) &&$wc['CenterId']==$config['work_order']['producing_lines'][$producingLine['DepId']]['CenterId']) {
    echo 'selected';
}?> ><?php echo $wc['CenterName'];?></option>
													<?php endforeach;?>
												</select>
											</div>
								
									</div>									
								<?php endforeach; ?>
							<?php else:  ?>

								<?php foreach ($workcenters as $workcenter):?>
									<input type="checkbox" name="workcenter[]" value="<?php  echo $workcenter['CenterId']; ?>"  title="<?php echo $workcenter['CenterName']; ?>"  <?php if (in_array($workcenter['CenterId'], $config['work_order']['workcenters'])) {
    echo 'checked';
}  ?> />
								<?php endforeach; ?>
							<?php endif; ?>


						</div>
					  </div>
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">计划查询模式</label>
						<div class="layui-input-block tlt-input-block ">
							<select name="plan_search_mode" lay-verify="">
								<option value="1" <?php if ($config['work_order']['plan_search_mode']==1) {
    echo 'selected';
} ?> >仅查当日</option>
								<option value="2" <?php if ($config['work_order']['plan_search_mode']==2) {
    echo 'selected';
} ?> >3日计划</option>
								<option value="3" <?php if ($config['work_order']['plan_search_mode']==3) {
    echo 'selected';
} ?> >本周计划</option>
								<option value="4" <?php if ($config['work_order']['plan_search_mode']==4) {
    echo 'selected';
} ?> >本月计划</option>
							</select>
						</div>
					  </div>
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">缺陷状况时间设置</label>
						<div class="layui-input-block tlt-input-block ">
							<select name="defect_search_mode" lay-verify="">
								<option value="1" <?php if ($config['work_order']['defect_search_mode']==1) {
    echo 'selected';
} ?> >仅查当日</option>
								<option value="2" <?php if ($config['work_order']['defect_search_mode']==2) {
    echo 'selected';
} ?> >本周计划</option>
								<option value="3" <?php if ($config['work_order']['defect_search_mode']==3) {
    echo 'selected';
} ?> >本月计划</option>
							</select>
						</div>
					  </div>
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">表窗体显示列</label>
						<div class="layui-input-block tlt-input-block ">
							<?php foreach ($config['parameters']['cols'] as  $k=>$col):?>
								<input type="checkbox" name="cols[]"  title="<?php echo $k; ?>"  <?php if ($col==true) {
    echo 'checked' ;
} ?> value="<?php echo $k; ?>" />
							<?php endforeach; ?>
						</div>
					  </div>
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">表窗体显示列宽</label>
						<div class="layui-input-block tlt-input-block " >
							<div class="layui-row" style="margin-left:10px;display:flex;flex-wrap:wrap;">
							<?php foreach ($config['parameters']['colwidths'] as  $k=>$colwidth):?>
								<div class="layui-col-md4" style="display:flex;margin-bottom:5px; flex-shrink:0;">
									<div style="width:30%;padding-left:10px;padding-top:10px;"><?php echo $k; ?></div><input style="width:70%" type="text" name="colwidths[<?php echo $k; ?>]" autocomplete="off" class="layui-input"    value="<?php echo $colwidth; ?>" />
								</div>
							<?php endforeach; ?>															
							</div>

						</div>
					  </div>					  
					  <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">数据刷新周期</label>
						<div class="layui-input-inline tlt-input-inline ">
							<div class="left-text">按</div><input type="text" name="interval" autocomplete="off" class="layui-input" value="<?php echo isset($config['parameters']['interval'])?$config['parameters']['interval']:'2'?>" style="width:30px;" ><div class="right-text">分钟间隔读取</div>
						</div>
					  </div>
					  <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">表格翻页间隔</label>
						<div class="layui-input-inline tlt-input-inline ">
							<div class="left-text">按</div><input type="text" name="table_interval" autocomplete="off" class="layui-input" value="<?php echo isset($config['parameters']['table_interval'])?$config['parameters']['table_interval']:'10' ?>" style="width:30px;" ><div class="right-text">秒间隔读取</div>
						</div>
					  </div>					  
					  <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">效率预警KPI值</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="efficient_kpi" autocomplete="off" class="layui-input" value="<?php echo $config['parameters']['efficient_kpi_boundary']?>"   />
						</div>
					  </div>	
					  <!-- <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">达成率预警KPI值</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="achieving_rate" autocomplete="off" class="layui-input" value="<?php echo $config['parameters']['achieving_rate_boundary']?>"  />
						</div>
					  </div> -->
					  <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">今日目标KPI</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="today_target_kpi" autocomplete="off" class="layui-input" value="<?php if (isset($config['parameters']['today_target_kpi_boundary'])) {
    echo $config['parameters']['today_target_kpi_boundary'];
}?>"   />
						</div>
					  </div>	
					  <!-- <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">日产量KPI</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="today_output_kpi" autocomplete="off" class="layui-input" value="<?php if (isset($config['parameters']['today_output_kpi_boundary'])) {
    echo $config['parameters']['today_output_kpi_boundary'];
}?>"   />
						</div>
					  </div>		 -->
					  <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">月产量</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="month_outputs_kpi" autocomplete="off" class="layui-input" value="<?php if (isset($config['parameters']['month_outputs_kpi_boundary'])) {
    echo $config['parameters']['month_outputs_kpi_boundary'];
}?>"   />
						</div>
					  </div>				  
					  <!-- <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">达成率存储过程</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="sp_achieving_rate" autocomplete="off" class="layui-input" value="<?php echo $config['sp_achieving_rate']?>"  />
						</div>
					  </div>	 -->
					  <!-- <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">良率存储过程</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="sp_qualified_rate" autocomplete="off" class="layui-input" value="<?php echo $config['sp_qualified_rate']?>"  />
						</div>
					  </div> -->
					  <!-- <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">执行站存储过程</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="sp_current_workcenter" autocomplete="off" class="layui-input" value="<?php echo $config['sp_current_workcenter']?>"  />
						</div>
					  </div>						   -->
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">时序图数据来源</label>
						<div class="layui-input-block tlt-input-block ">
							<select name="time_seq_chart_source" lay-verify="">
								<option value="1" <?php if (!isset($config['parameters']['time_seq_chart_source']) || $config['parameters']['time_seq_chart_source']==1) {
    echo 'selected';
} ?> >设备日志</option>
								<option value="2" <?php if ( isset($config['parameters']['time_seq_chart_source'])  && $config['parameters']['time_seq_chart_source']==2) {
    echo 'selected';
} ?> >SCADA</option>

							</select>
						</div>
					  </div>
					  <div class="layui-form-item">
						<label class="layui-form-label tlt-label">工单数据预警</label>
						<div class="layui-input-block tlt-input-block ">
							<select name="work_order_data_show" lay-verify="">
								<option value="0" <?php if (!isset($config['parameters']['work_order_data_show']) || $config['parameters']['work_order_data_show']==0) {
    echo 'selected';
} ?> >不显示</option>
								<option value="1" <?php if ( isset($config['parameters']['work_order_data_show'])  && $config['parameters']['work_order_data_show']==1) {
    echo 'selected';
} ?> >显示</option>

							</select>
						</div>
					  </div>
					  <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">看板名称</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="ekbname" autocomplete="off" class="layui-input" value="<?php echo $config['parameters']['ekb_name']?>" />
						</div>
					  </div>
					  <div class="layui-form-item">					  
						<label class="layui-form-label tlt-label">看板子名称</label>
						<div class="layui-input-block tlt-input-block ">
							<input type="text" name="sub_ekb_name" autocomplete="off" class="layui-input" value="<?php echo isset($config['parameters']['sub_ekb_name'])?$config['parameters']['sub_ekb_name']:''?>" />
						</div>
					  </div>

					  <input class="layui-btn layui-btn-fluid layui-btn-primary" name="save" type="submit" value="保存">
				</form>
			</div>
			
			<div class="layui-row footer">
				<div class="layui-col-md12">
					<!--<h1><?php echo $config['company']['info'];?>看板系统配置</h1>-->
				</div>							
			</div>
		</div>

	</body>



</html>