<?php
// 删除发票
if (isset($_GET['del_id']) && intval($_GET['del_id']) > 0) {
    $delId = intval($_GET['del_id']);
    // 删除附件（可选）
    $attachPath = mysqli_fetch_assoc(mysqli_query($conn, "SELECT attach_path FROM invoice WHERE id = $delId"))['attach_path'];
    if ($attachPath && file_exists($attachPath)) unlink($attachPath);
    // 删除数据
    mysqli_query($conn, "DELETE FROM invoice WHERE id = $delId");
    echo "<div class='alert alert-success'>发票删除成功！</div>";
}

// 查询发票列表
$where = '';
if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
    $keyword = trim($_GET['keyword']);
    $where = "WHERE i.invoice_no LIKE '%$keyword%' OR s.name LIKE '%$keyword%' OR c.name LIKE '%$keyword%'";
}

$sql = "SELECT i.*, s.name as supplier_name, c.name as customer_name 
        FROM invoice i 
        LEFT JOIN supplier s ON i.supplier_id = s.id 
        LEFT JOIN customer c ON i.customer_id = c.id 
        $where 
        ORDER BY i.create_time DESC";
$invoiceList = mysqli_query($conn, $sql);
?>

<!-- 搜索框 -->
<form method="get" style="margin-bottom: 20px;">
    <input type="hidden" name="page" value="invoice_list">
    <div style="display: flex; gap: 10px; align-items: center;">
        <input type="text" name="keyword" class="form-control" placeholder="输入发票号码/供应商/客户搜索" value="<?= $_GET['keyword'] ?? '' ?>">
        <button type="submit" class="btn btn-blue">搜索</button>
        <a href="index.php?page=invoice_add" class="btn btn-green">新增发票</a>
    </div>
</form>

<!-- 发票列表 -->
<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>发票号码</th>
            <th>开票日期</th>
            <th>金额（元）</th>
            <th>供应商</th>
            <th>客户</th>
            <th>添加时间</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <?php if (mysqli_num_rows($invoiceList) > 0): ?>
            <?php while ($row = mysqli_fetch_assoc($invoiceList)): ?>
                <tr>
                    <td><?= $row['id'] ?></td>
                    <td><?= $row['invoice_no'] ?></td>
                    <td><?= $row['invoice_date'] ?></td>
                    <td><?= $row['amount'] ?></td>
                    <td><?= $row['supplier_name'] ?? '-' ?></td>
                    <td><?= $row['customer_name'] ?? '-' ?></td>
                    <td><?= $row['create_time'] ?></td>
                    <td>
                        <?php if ($row['attach_path']): ?>
                            <a href="<?= $row['attach_path'] ?>" target="_blank" style="color: #3498db; text-decoration: none; margin-right: 10px;">查看附件</a>
                        <?php endif; ?>
                        <a href="index.php?page=invoice_list&del_id=<?= $row['id'] ?>" style="color: #e74c3c; text-decoration: none;" onclick="return confirm('确定删除此发票吗？')">删除</a>
                    </td>
                </tr>
            <?php endwhile; ?>
        <?php else: ?>
            <tr>
                <td colspan="8" style="text-align: center; color: #666;">
                    <?= isset($_GET['keyword']) ? '未找到匹配的发票' : '暂无发票数据，请先添加' ?>
                </td>
            </tr>
        <?php endif; ?>
    </tbody>
</table>