<?php
$type = $_GET['type'] ?? 'supplier';
$tableName = $type == 'supplier' ? 'supplier' : 'customer';
$title = $type == 'supplier' ? '供应商' : '客户';

// 删除处理
if (isset($_GET['del_id']) && intval($_GET['del_id']) > 0) {
    $delId = intval($_GET['del_id']);
    mysqli_query($conn, "DELETE FROM $tableName WHERE id = $delId");
    echo "<div class='alert alert-success'>{$title}删除成功！</div>";
}

// 保存处理
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['save_data'])) {
    $id = intval($_POST['id']);
    $name = trim($_POST['name']);
    $contact = trim($_POST['contact'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $status = intval($_POST['status'] ?? 1);
    
    if (empty($name)) {
        echo "<div class='alert alert-error'>请填写{$title}名称！</div>";
    } else {
        if ($id > 0) {
            mysqli_query($conn, "UPDATE $tableName SET name='$name', contact='$contact', phone='$phone', status=$status WHERE id=$id");
        } else {
            mysqli_query($conn, "INSERT INTO $tableName (name, contact, phone, status, create_time) VALUES ('$name', '$contact', '$phone', $status, NOW())");
        }
        echo "<div class='alert alert-success'>{$title}保存成功！</div>";
    }
}

// 查询数据
$list = mysqli_query($conn, "SELECT * FROM $tableName ORDER BY create_time DESC");
$editData = [];
if (isset($_GET['edit_id']) && intval($_GET['edit_id']) > 0) {
    $editId = intval($_GET['edit_id']);
    $res = mysqli_query($conn, "SELECT * FROM $tableName WHERE id = $editId");
    $editData = mysqli_fetch_assoc($res);
}
?>

<!-- 切换标签 -->
<div style="margin-bottom: 20px;">
    <a href="index.php?page=supplier_manage" class="btn <?= $type == 'supplier' ? 'btn-blue' : '' ?>">供应商管理</a>
    <a href="index.php?page=customer_manage" class="btn <?= $type == 'customer' ? 'btn-blue' : '' ?>">客户管理</a>
</div>

<!-- 新增/编辑表单 -->
<form method="post">
    <input type="hidden" name="save_data" value="1">
    <input type="hidden" name="id" value="<?= $editData['id'] ?? 0 ?>">
    
    <div class="form-group">
        <label><?= $title ?>名称 <span style="color: red;">*</span></label>
        <input type="text" name="name" class="form-control" value="<?= $editData['name'] ?? '' ?>" required>
    </div>
    
    <div class="form-group">
        <label>联系人</label>
        <input type="text" name="contact" class="form-control" value="<?= $editData['contact'] ?? '' ?>">
    </div>
    
    <div class="form-group">
        <label>联系电话</label>
        <input type="text" name="phone" class="form-control" value="<?= $editData['phone'] ?? '' ?>">
    </div>
    
    <div class="form-group">
        <label>状态</label>
        <select name="status" class="form-control">
            <option value="1" <?= ($editData['status'] ?? 1) == 1 ? 'selected' : '' ?>>启用</option>
            <option value="0" <?= ($editData['status'] ?? 1) == 0 ? 'selected' : '' ?>>禁用</option>
        </select>
    </div>
    
    <div class="form-group">
        <button type="submit" class="btn btn-blue">保存</button>
        <?php if ($editData): ?>
            <a href="index.php?page=<?= $type == 'supplier' ? 'supplier_manage' : 'customer_manage' ?>" class="btn btn-green">取消编辑</a>
        <?php endif; ?>
    </div>
</form>

<!-- 列表展示 -->
<div style="margin-top: 30px;">
    <h4><?= $title ?>列表</h4>
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th><?= $title ?>名称</th>
                <th>联系人</th>
                <th>联系电话</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <?php if (mysqli_num_rows($list) > 0): ?>
                <?php while ($row = mysqli_fetch_assoc($list)): ?>
                    <tr>
                        <td><?= $row['id'] ?></td>
                        <td><?= $row['name'] ?></td>
                        <td><?= $row['contact'] ?? '-' ?></td>
                        <td><?= $row['phone'] ?? '-' ?></td>
                        <td><?= $row['status'] == 1 ? '启用' : '禁用' ?></td>
                        <td>
                            <a href="index.php?page=<?= $type == 'supplier' ? 'supplier_manage' : 'customer_manage' ?>&edit_id=<?= $row['id'] ?>" style="color: #3498db; text-decoration: none; margin-right: 10px;">编辑</a>
                            <a href="index.php?page=<?= $type == 'supplier' ? 'supplier_manage' : 'customer_manage' ?>&del_id=<?= $row['id'] ?>" style="color: #e74c3c; text-decoration: none;" onclick="return confirm('确定删除【<?= $row['name'] ?>】吗？')">删除</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr>
                    <td colspan="6" style="text-align: center; color: #666;">暂无<?= $title ?>数据，请先添加</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>