<?php
/**
 * OTIS Finance Edit View (تعديل السجل المالي)
 * Premium RTL / Soft Design
 */
$csrf = $_SESSION['csrf_token'] ?? '';
$categories = $categories ?? [];
?>
<style>
@import url('https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;800&display=swap');

.finance-edit-container { font-family: 'Tajawal', sans-serif; color: #374151; }
.card-premium { border-radius: 24px; border: none; box-shadow: 0 20px 60px rgba(0,0,0,0.05); overflow: hidden; background: #fff; }
.card-header-premium { background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%); padding: 2.5rem 2rem; border: none; position: relative; }
.card-header-premium h2 { color: white; font-weight: 800; margin: 0; font-size: 1.75rem; letter-spacing: -0.5px; }
.card-header-premium p { color: rgba(255,255,255,0.8); margin: 0.5rem 0 0; font-weight: 500; }

.form-label { font-weight: 700; color: #4b5563; margin-bottom: 0.6rem; font-size: 0.95rem; display: flex; align-items: center; }
.form-label i { width: 24px; color: #6366f1; font-size: 1rem; }
.form-control, .form-select { border-radius: 14px; padding: 0.8rem 1.2rem; border: 2px solid #f3f4f6; background-color: #f9fafb; font-weight: 600; transition: all 0.2s ease; }
.form-control:focus, .form-select:focus { border-color: #6366f1; background-color: #fff; box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1); outline: none; }

.btn-save { background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%); color: white; border: none; border-radius: 16px; padding: 0.9rem 2.5rem; font-weight: 800; transition: all 0.3s; box-shadow: 0 10px 20px rgba(79, 70, 229, 0.2); }
.btn-save:hover { transform: translateY(-2px); box-shadow: 0 15px 30px rgba(79, 70, 229, 0.3); color: white; }
.btn-cancel { border-radius: 16px; padding: 0.9rem 2rem; font-weight: 700; border: 2px solid #e5e7eb; color: #6b7280; transition: all 0.2s; }
.btn-cancel:hover { background-color: #f9fafb; border-color: #d1d5db; color: #374151; }

.footer-action { border-top: 1px solid #f3f4f6; padding-top: 2rem; margin-top: 2rem; }

.input-group-text-premium { background: #f3f4f6; border: 2px solid #f3f4f6; border-left: none; border-radius: 0 14px 14px 0; font-weight: 800; color: #6b7280; padding: 0 1.2rem; }
.has-icon .form-control { border-radius: 14px 0 0 14px; border-right: none; }
</style>

<div class="finance-edit-container py-5 px-3">
    <div class="row justify-content-center">
        <div class="col-lg-7 col-xl-6">
            
            <div class="card card-premium animate-fade-in">
                <div class="card-header-premium text-center">
                    <h2><i class="fa-solid fa-pen-nib me-2"></i> تعديل البيانات المالية</h2>
                    <p>تحديث تفاصيل السجل في جدول: <?= htmlspecialchars($table) ?></p>
                </div>
                
                <div class="card-body p-4 p-md-5">
                    <form action="<?= BASE_URL ?>/finance/update" method="POST" id="editFinanceForm">
                        <?= csrf_field() ?>
                        <input type="hidden" name="table_name" value="<?= htmlspecialchars($table) ?>">
                        <input type="hidden" name="item_id" value="<?= htmlspecialchars($item->id) ?>">
                        
                        <div class="row g-4">
                            <?php if ($table === 'finance_expenses'): ?>
                                <div class="col-12">
                                    <label class="form-label"><i class="fa-solid fa-layer-group"></i> الحساب (الفئة)</label>
                                    <select name="category" class="form-select shadow-sm" required>
                                        <?php foreach($categories as $cat): ?>
                                            <option value="<?= htmlspecialchars($cat->name) ?>" <?= ($item->category == $cat->name) ? 'selected' : '' ?>>
                                                <?= htmlspecialchars($cat->name) ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                
                                <div class="col-12">
                                    <label class="form-label"><i class="fa-solid fa-tag"></i> اسم البند / المصروف</label>
                                    <input type="text" name="item_name" class="form-control" value="<?= htmlspecialchars($item->item_name) ?>" placeholder="مثال: فاتورة الكهرباء، مشتريات السوبر ماركت" required>
                                </div>
                                
                                <div class="col-md-6">
                                    <label class="form-label"><i class="fa-solid fa-money-bill-wave"></i> المبلغ</label>
                                    <div class="input-group has-icon">
                                        <input type="number" step="0.01" name="amount" class="form-control" value="<?= htmlspecialchars($item->amount) ?>" required>
                                        <span class="input-group-text input-group-text-premium">ج.م</span>
                                    </div>
                                </div>
                                
                                <div class="col-md-6">
                                    <label class="form-label"><i class="fa-solid fa-calendar-day"></i> التاريخ</label>
                                    <input type="date" name="expense_date" class="form-control" value="<?= substr($item->expense_date, 0, 10) ?>" required>
                                </div>

                            <?php elseif ($table === 'finance_incomes'): ?>
                                <div class="col-12">
                                    <label class="form-label"><i class="fa-solid fa-briefcase"></i> مصدر الدخل</label>
                                    <input type="text" name="source" class="form-control" value="<?= htmlspecialchars($item->source) ?>" required>
                                </div>
                                
                                <div class="col-md-6">
                                    <label class="form-label"><i class="fa-solid fa-money-bill-wave"></i> المبلغ الكلي</label>
                                    <div class="input-group has-icon">
                                        <input type="number" step="0.01" name="amount" class="form-control" value="<?= htmlspecialchars($item->amount) ?>" required>
                                        <span class="input-group-text input-group-text-premium">ج.م</span>
                                    </div>
                                </div>
                                
                                <div class="col-md-6">
                                    <label class="form-label"><i class="fa-solid fa-calendar-day"></i> تاريخ الاستلام</label>
                                    <input type="date" name="date_received" class="form-control" value="<?= substr($item->date_received, 0, 10) ?>" required>
                                </div>

                            <?php elseif ($table === 'finance_debts'): ?>
                                <div class="col-12">
                                    <label class="form-label"><i class="fa-solid fa-user-tag"></i> اسم الشخص / الجهة</label>
                                    <input type="text" name="person_name" class="form-control" value="<?= htmlspecialchars($item->person_name) ?>" required>
                                </div>
                                
                                <div class="col-md-6">
                                    <label class="form-label"><i class="fa-solid fa-hand-holding-dollar"></i> المبلغ</label>
                                    <div class="input-group has-icon">
                                        <input type="number" step="0.01" name="amount" class="form-control" value="<?= htmlspecialchars($item->amount) ?>" required>
                                        <span class="input-group-text input-group-text-premium">ج.م</span>
                                    </div>
                                </div>
                                
                                <div class="col-md-6">
                                    <label class="form-label"><i class="fa-solid fa-arrows-left-right"></i> نوع الدين</label>
                                    <select name="type" class="form-select">
                                        <option value="owed_to_me" <?= $item->type == 'owed_to_me' ? 'selected' : '' ?>>لي (مستحق لي)</option>
                                        <option value="owed_by_me" <?= $item->type == 'owed_by_me' ? 'selected' : '' ?>>علي (دين علي)</option>
                                    </select>
                                </div>

                            <?php else: ?>
                                <!-- Generic Fallback for simple tables -->
                                <?php 
                                $skipFields = ['id', 'user_id', 'created_at', 'updated_at', 'deleted_at', 'dist_personal', 'dist_business', 'dist_savings', 'dist_charity', 'paid_months_json', 'icon'];
                                $itemArr = (array) $item;
                                foreach($itemArr as $key => $val): 
                                    if(in_array($key, $skipFields)) continue;
                                ?>
                                <div class="col-12">
                                    <label class="form-label"><?= htmlspecialchars(ucwords(str_replace('_', ' ', $key))) ?></label>
                                    <input type="text" name="<?= htmlspecialchars($key) ?>" class="form-control" value="<?= htmlspecialchars($val) ?>">
                                </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                            
                            <?php if (property_exists($item, 'notes')): ?>
                                <div class="col-12">
                                    <label class="form-label"><i class="fa-solid fa-comment-dots"></i> ملاحظات إضافية</label>
                                    <textarea name="notes" class="form-control" rows="3" placeholder="أضف أي تفاصيل أخرى هنا..."><?= htmlspecialchars($item->notes ?? '') ?></textarea>
                                </div>
                            <?php endif; ?>
                        </div>

                        <div class="footer-action d-flex flex-column flex-md-row justify-content-between gap-3 text-center">
                            <a href="<?= BASE_URL ?>/finance?tab=<?= htmlspecialchars($returnTab ?? 'income') ?>" class="btn btn-cancel">
                                <i class="fa-solid fa-arrow-right me-2"></i> إلغاء والعودة
                            </a>
                            <button type="submit" class="btn btn-save px-5">
                                <i class="fa-solid fa-check-double me-2"></i> حفظ التعديلات الآن
                            </button>
                        </div>
                    </form>
                </div>
            </div>
            
            <div class="text-center mt-4 mb-5 text-muted fw-bold">
                <i class="fa-solid fa-shield-halved me-1"></i> جميع البيانات مشفرة وآمنة ضمن نظام OTIS
            </div>

        </div>
    </div>
</div>
