subscription/templates/admin/bundle-list.php

298 lines
13 KiB
PHP

<?php
/**
* 混装产品列表管理页面模板
*
* @package Yoone_Subscriptions
* @version 1.0.0
*/
if (!defined('ABSPATH')) {
exit; // 防止直接访问
}
global $wpdb;
// 处理批量操作
if (isset($_POST['action']) && $_POST['action'] === 'delete' && isset($_POST['bundle_ids'])) {
if (wp_verify_nonce($_POST['_wpnonce'], 'bulk_delete_bundles')) {
$bundle_ids = array_map('intval', $_POST['bundle_ids']);
foreach ($bundle_ids as $bundle_id) {
// 删除混装项目
$wpdb->delete(
$wpdb->prefix . 'yoone_bundle_items',
array('bundle_id' => $bundle_id)
);
// 删除混装产品
$wpdb->delete(
$wpdb->prefix . 'yoone_bundles',
array('id' => $bundle_id)
);
}
echo '<div class="notice notice-success"><p>' . sprintf(__('已删除 %d 个混装产品', 'yoone-subscriptions'), count($bundle_ids)) . '</p></div>';
}
}
// 获取混装产品列表
$per_page = 20;
$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$offset = ($current_page - 1) * $per_page;
$search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : '';
$status_filter = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : '';
$where_conditions = array('1=1');
$where_values = array();
if ($search) {
$where_conditions[] = "(name LIKE %s OR description LIKE %s)";
$where_values[] = '%' . $wpdb->esc_like($search) . '%';
$where_values[] = '%' . $wpdb->esc_like($search) . '%';
}
if ($status_filter) {
$where_conditions[] = "status = %s";
$where_values[] = $status_filter;
}
$where_clause = implode(' AND ', $where_conditions);
// 获取总数
$total_query = "SELECT COUNT(*) FROM {$wpdb->prefix}yoone_bundles WHERE {$where_clause}";
if (!empty($where_values)) {
$total = $wpdb->get_var($wpdb->prepare($total_query, $where_values));
} else {
$total = $wpdb->get_var($total_query);
}
// 获取混装产品数据
$query = "SELECT * FROM {$wpdb->prefix}yoone_bundles
WHERE {$where_clause}
ORDER BY created_at DESC
LIMIT %d OFFSET %d";
$query_values = array_merge($where_values, array($per_page, $offset));
$bundles = $wpdb->get_results($wpdb->prepare($query, $query_values));
$total_pages = ceil($total / $per_page);
// 获取状态统计
$status_counts = $wpdb->get_results(
"SELECT status, COUNT(*) as count FROM {$wpdb->prefix}yoone_bundles GROUP BY status"
);
?>
<div class="wrap">
<h1 class="wp-heading-inline"><?php _e('混装产品管理', 'yoone-subscriptions'); ?></h1>
<a href="<?php echo admin_url('admin.php?page=yoone-bundles&action=add'); ?>" class="page-title-action">
<?php _e('添加新混装产品', 'yoone-subscriptions'); ?>
</a>
<?php if (isset($_GET['saved'])): ?>
<div class="notice notice-success is-dismissible">
<p><?php _e('混装产品已保存', 'yoone-subscriptions'); ?></p>
</div>
<?php endif; ?>
<?php if (isset($_GET['deleted'])): ?>
<div class="notice notice-success is-dismissible">
<p><?php _e('混装产品已删除', 'yoone-subscriptions'); ?></p>
</div>
<?php endif; ?>
<!-- 搜索和筛选 -->
<form method="get" class="search-form">
<input type="hidden" name="page" value="yoone-bundles">
<p class="search-box">
<label class="screen-reader-text" for="bundle-search-input"><?php _e('搜索混装产品', 'yoone-subscriptions'); ?>:</label>
<input type="search" id="bundle-search-input" name="s" value="<?php echo esc_attr($search); ?>" placeholder="<?php _e('搜索混装产品名称或描述', 'yoone-subscriptions'); ?>">
<select name="status">
<option value=""><?php _e('所有状态', 'yoone-subscriptions'); ?></option>
<option value="active" <?php selected($status_filter, 'active'); ?>><?php _e('启用', 'yoone-subscriptions'); ?></option>
<option value="inactive" <?php selected($status_filter, 'inactive'); ?>><?php _e('禁用', 'yoone-subscriptions'); ?></option>
</select>
<?php submit_button(__('搜索', 'yoone-subscriptions'), '', '', false, array('id' => 'search-submit')); ?>
</p>
</form>
<!-- 状态统计 -->
<ul class="subsubsub">
<li class="all">
<a href="<?php echo admin_url('admin.php?page=yoone-bundles'); ?>" <?php echo empty($status_filter) ? 'class="current"' : ''; ?>>
<?php _e('全部', 'yoone-subscriptions'); ?> <span class="count">(<?php echo $total; ?>)</span>
</a>
</li>
<?php foreach ($status_counts as $status_count): ?>
<li class="<?php echo esc_attr($status_count->status); ?>">
| <a href="<?php echo admin_url('admin.php?page=yoone-bundles&status=' . $status_count->status); ?>" <?php echo $status_filter === $status_count->status ? 'class="current"' : ''; ?>>
<?php echo $status_count->status === 'active' ? __('启用', 'yoone-subscriptions') : __('禁用', 'yoone-subscriptions'); ?> <span class="count">(<?php echo $status_count->count; ?>)</span>
</a>
</li>
<?php endforeach; ?>
</ul>
<!-- 混装产品列表表格 -->
<form method="post" id="bundles-filter">
<?php wp_nonce_field('bulk_delete_bundles'); ?>
<div class="tablenav top">
<div class="alignleft actions bulkactions">
<select name="action" id="bulk-action-selector-top">
<option value="-1"><?php _e('批量操作', 'yoone-subscriptions'); ?></option>
<option value="delete"><?php _e('删除', 'yoone-subscriptions'); ?></option>
</select>
<input type="submit" id="doaction" class="button action" value="<?php _e('应用', 'yoone-subscriptions'); ?>">
</div>
</div>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<td id="cb" class="manage-column column-cb check-column">
<label class="screen-reader-text" for="cb-select-all-1"><?php _e('全选', 'yoone-subscriptions'); ?></label>
<input id="cb-select-all-1" type="checkbox">
</td>
<th scope="col" class="manage-column column-id"><?php _e('ID', 'yoone-subscriptions'); ?></th>
<th scope="col" class="manage-column column-name"><?php _e('名称', 'yoone-subscriptions'); ?></th>
<th scope="col" class="manage-column column-description"><?php _e('描述', 'yoone-subscriptions'); ?></th>
<th scope="col" class="manage-column column-discount"><?php _e('折扣', 'yoone-subscriptions'); ?></th>
<th scope="col" class="manage-column column-items"><?php _e('产品数量', 'yoone-subscriptions'); ?></th>
<th scope="col" class="manage-column column-status"><?php _e('状态', 'yoone-subscriptions'); ?></th>
<th scope="col" class="manage-column column-created"><?php _e('创建时间', 'yoone-subscriptions'); ?></th>
<th scope="col" class="manage-column column-actions"><?php _e('操作', 'yoone-subscriptions'); ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($bundles)): ?>
<tr class="no-items">
<td class="colspanchange" colspan="9"><?php _e('未找到混装产品', 'yoone-subscriptions'); ?></td>
</tr>
<?php else: ?>
<?php foreach ($bundles as $bundle): ?>
<?php
// 获取混装产品项目数量
$item_count = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->prefix}yoone_bundle_items WHERE bundle_id = %d",
$bundle->id
));
?>
<tr>
<th scope="row" class="check-column">
<input type="checkbox" name="bundle_ids[]" value="<?php echo $bundle->id; ?>">
</th>
<td class="column-id">
<strong>#<?php echo $bundle->id; ?></strong>
</td>
<td class="column-name">
<strong>
<a href="<?php echo admin_url('admin.php?page=yoone-bundles&action=edit&id=' . $bundle->id); ?>">
<?php echo esc_html($bundle->name); ?>
</a>
</strong>
<div class="row-actions">
<span class="edit">
<a href="<?php echo admin_url('admin.php?page=yoone-bundles&action=edit&id=' . $bundle->id); ?>">
<?php _e('编辑', 'yoone-subscriptions'); ?>
</a> |
</span>
<span class="delete">
<a href="<?php echo wp_nonce_url(admin_url('admin.php?page=yoone-bundles&action=delete&id=' . $bundle->id), 'delete_bundle_' . $bundle->id); ?>"
onclick="return confirm('<?php _e('确定要删除这个混装产品吗?', 'yoone-subscriptions'); ?>')">
<?php _e('删除', 'yoone-subscriptions'); ?>
</a>
</span>
</div>
</td>
<td class="column-description">
<?php echo esc_html(wp_trim_words($bundle->description, 10)); ?>
</td>
<td class="column-discount">
<?php if ($bundle->discount_type === 'percentage'): ?>
<?php echo $bundle->discount_value; ?>%
<?php elseif ($bundle->discount_type === 'fixed'): ?>
<?php echo Yoone_Helper::format_price($bundle->discount_value); ?>
<?php else: ?>
<?php _e('无', 'yoone-subscriptions'); ?>
<?php endif; ?>
</td>
<td class="column-items">
<?php echo $item_count; ?> <?php _e('个产品', 'yoone-subscriptions'); ?>
</td>
<td class="column-status">
<span class="bundle-status status-<?php echo esc_attr($bundle->status); ?>">
<?php echo $bundle->status === 'active' ? __('启用', 'yoone-subscriptions') : __('禁用', 'yoone-subscriptions'); ?>
</span>
</td>
<td class="column-created">
<?php echo date_i18n(get_option('date_format'), strtotime($bundle->created_at)); ?>
</td>
<td class="column-actions">
<a href="<?php echo admin_url('admin.php?page=yoone-bundles&action=edit&id=' . $bundle->id); ?>" class="button button-small">
<?php _e('编辑', 'yoone-subscriptions'); ?>
</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</form>
<!-- 分页 -->
<?php if ($total_pages > 1): ?>
<div class="tablenav bottom">
<div class="tablenav-pages">
<?php
$pagination_args = array(
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => __('&laquo;'),
'next_text' => __('&raquo;'),
'total' => $total_pages,
'current' => $current_page
);
echo paginate_links($pagination_args);
?>
</div>
</div>
<?php endif; ?>
</div>
<style>
.bundle-status {
padding: 3px 8px;
border-radius: 3px;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
}
.status-active { background: #46b450; color: white; }
.status-inactive { background: #dc3232; color: white; }
</style>
<script>
jQuery(document).ready(function($) {
// 全选/取消全选
$('#cb-select-all-1').on('click', function() {
$('input[name="bundle_ids[]"]').prop('checked', this.checked);
});
// 批量操作确认
$('#bundles-filter').on('submit', function(e) {
var action = $('#bulk-action-selector-top').val();
var checked = $('input[name="bundle_ids[]"]:checked').length;
if (action === 'delete' && checked > 0) {
if (!confirm('<?php _e('确定要删除选中的混装产品吗?', 'yoone-subscriptions'); ?>')) {
e.preventDefault();
}
} else if (action !== '-1' && checked === 0) {
alert('<?php _e('请选择要操作的混装产品', 'yoone-subscriptions'); ?>');
e.preventDefault();
}
});
});
</script>