Files
MES/app/views/Admin/productModel.php
T
2026-08-08 18:28:49 +08:00

159 lines
6.9 KiB
PHP

<?php
$activeMenu = 'productModel';
include APP_PATH . 'app/views/layouts/admin_header.php';
// 构建类型颜色和列定义(统一从 typeConfigs 或 getDefaultConfigs 获取)
$typeColors = [];
$typeColumns = [];
// 统一数据源:typeConfigs 为空时回退到默认配置
$allConfigs = !empty($typeConfigs) ? $typeConfigs : \app\models\ProductTypeConfig::getDefaultConfigs();
foreach ($allConfigs as $cfg) {
$typeColors[$cfg['type_name']] = $cfg['color'] ?? 'default';
$cols = [];
if (!empty($cfg['display_columns'])) {
$decoded = json_decode($cfg['display_columns'], true);
if (is_array($decoded)) {
$cols = $decoded;
}
}
// 确保有序号列在最前
$hasId = false;
foreach ($cols as $c) {
if (($c['key'] ?? '') === 'id') { $hasId = true; break; }
}
if (!$hasId) {
array_unshift($cols, ['key'=>'id','label'=>'序号','width'=>'60px']);
}
// 补充 width 字段
foreach ($cols as &$c) {
if (!isset($c['width'])) $c['width'] = '';
}
unset($c);
$typeColumns[$cfg['type_name']] = $cols;
}
// 通用 fallback 列(所有类型都可用的字段集合)
$defaultColumns = [
['key'=>'id','label'=>'序号','width'=>'60px'],
['key'=>'type','label'=>'类型','width'=>'80px'],
['key'=>'model_code','label'=>'产品型号','width'=>''],
['key'=>'model_desc','label'=>'产品说明','width'=>''],
];
function renderModelTableRow($model, $columns, $typeName, $idx, $csrfToken) {
$html = '<tr>';
foreach ($columns as $col) {
$key = $col['key'];
$val = '';
if ($key === 'id') {
$val = $idx;
} elseif ($key === 'type') {
$val = htmlspecialchars($typeName);
} elseif (isset($model[$key])) {
$val = htmlspecialchars($model[$key]);
} else {
$val = '-';
}
$html .= '<td>' . $val . '</td>';
}
$html .= '<td style="width:150px">
<a href="' . BASE_URL . '/Admin/productModelEdit/' . $model['id'] . '" class="btn btn-xs btn-info"><i class="fa fa-edit"></i> 编辑</a>
<form method="POST" action="' . BASE_URL . '/Admin/productModelDelete/' . $model['id'] . '" style="display:inline" onsubmit="return confirm(\'确定删除该型号?\')">
<input type="hidden" name="csrf_token" value="' . ($csrfToken ?? '') . '">
<button type="submit" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> 删除</button>
</form>
</td>';
$html .= '</tr>';
return $html;
}
?>
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title"><i class="fa fa-list"></i> 产品型号列表</h3>
<div class="box-tools" style="display:flex; align-items:center; gap:8px;">
<span class="column-adjuster" title="调整类型卡片列数" style="display:inline-flex; gap:4px;">
<button type="button" class="btn btn-default btn-xs col-btn" data-cols="1" title="1列"><i class="fa fa-align-justify"></i></button>
<button type="button" class="btn btn-default btn-xs col-btn active" data-cols="2" title="2列"><i class="fa fa-columns"></i> 2</button>
<button type="button" class="btn btn-default btn-xs col-btn" data-cols="3" title="3列"><i class="fa fa-columns"></i> 3</button>
<button type="button" class="btn btn-default btn-xs col-btn" data-cols="auto" title="自适应"><i class="fa fa-arrows-h"></i> 自动</button>
</span>
<a href="<?php echo BASE_URL; ?>/Admin/productTypeConfig" class="btn btn-warning btn-sm"><i class="fa fa-cogs"></i> 类型配置</a>
<a href="<?php echo BASE_URL; ?>/Admin/productModelAdd" class="btn btn-primary btn-sm"><i class="fa fa-plus"></i> 添加产品型号</a>
</div>
</div>
</div>
<?php if (!empty($groupedModels)): ?>
<div class="type-grid" id="typeGrid">
<?php foreach ($groupedModels as $typeName => $models): ?>
<div class="box box-<?php echo $typeColors[$typeName] ?? 'default'; ?> type-grid-item">
<div class="box-header with-border">
<h3 class="box-title">
<i class="fa fa-tags"></i>
<?php echo htmlspecialchars($typeName); ?>
<span class="badge bg-<?php echo $typeColors[$typeName] ?? 'default'; ?>"><?php echo count($models); ?></span>
</h3>
</div>
<div class="box-body table-responsive no-padding">
<table class="table table-hover table-striped">
<thead>
<tr>
<?php $cols = $typeColumns[$typeName] ?? $defaultColumns; ?>
<?php foreach ($cols as $col): ?>
<th<?php if ($col['width']) echo ' style="width:' . $col['width'] . '"'; ?>><?php echo $col['label']; ?></th>
<?php endforeach; ?>
<th style="width:150px">操作</th>
</tr>
</thead>
<tbody>
<?php $idx = 0; ?>
<?php foreach ($models as $model): ?>
<?php echo renderModelTableRow($model, $cols, $typeName, ++$idx, $csrfToken); ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else: ?>
<div class="box">
<div class="box-body text-center text-muted" style="padding:40px;">
<i class="fa fa-inbox fa-3x"></i>
<p style="margin-top:10px;">暂无产品型号数据</p>
<a href="<?php echo BASE_URL; ?>/Admin/productModelAdd" class="btn btn-primary"><i class="fa fa-plus"></i> 添加产品型号</a>
</div>
</div>
<?php endif; ?>
</div>
</div>
<script>
// ========== 类型卡片列数调节 ==========
$(function() {
var $grid = $('#typeGrid');
if (!$grid.length) return;
var COL_KEY = 'admin_type_grid_cols';
function applyColumns(cols) {
$grid.attr('data-columns', cols);
$('.column-adjuster .col-btn').removeClass('active');
$('.column-adjuster .col-btn[data-cols="' + cols + '"]').addClass('active');
try { localStorage.setItem(COL_KEY, cols); } catch(e) {}
}
var savedCols = localStorage.getItem(COL_KEY) || '2';
applyColumns(savedCols);
$('.column-adjuster .col-btn').click(function() {
applyColumns($(this).data('cols'));
});
});
</script>
<?php include APP_PATH . 'app/views/layouts/admin_footer.php'; ?>