<?php
// admin/salary-settings.php - تنظیمات حق التدریس

session_start();
require_once '../config/database.php';
require_once '../includes/db.php';
require_once '../includes/auth.php';
require_once '../includes/functions.php';

if (!isLoggedIn()) {
    header('Location: ../login.php');
    exit;
}

$user = getCurrentUser();
if ($user['role_level'] != 1) {
    header('Location: dashboard.php');
    exit;
}

$pageTitle = 'تنظیمات حق التدریس';
$pageSubtitle = 'مدیریت مبالغ حق التدریس بر اساس مقطع و نوع درس';

$message = '';
$messageType = '';

// ===== دریافت تنظیمات مالیات و بیمه =====
$taxSettings = getTaxSettings();
$taxPercent = $taxSettings['tax_percent'] ?? 0;
$insurancePercent = $taxSettings['insurance_percent'] ?? 0;

// ===== دریافت تنظیمات حق التدریس =====
$salarySettings = fetchAll("SELECT * FROM salary_settings ORDER BY degree, course_type");

// ===== پردازش فرم =====
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    // ===== ذخیره تنظیمات حق التدریس =====
    if (isset($_POST['action']) && $_POST['action'] === 'save_salary') {
        $salaryData = $_POST['salary'] ?? [];
        $success = true;
        
        foreach ($salaryData as $id => $data) {
            $amountPerHour = isset($data['amount_per_hour']) ? (float)str_replace(',', '', $data['amount_per_hour']) : 0;
            $amountPerStudent = isset($data['amount_per_student']) ? (float)str_replace(',', '', $data['amount_per_student']) : 0;
            
            $updateData = [
                'amount_per_hour' => $amountPerHour,
                'amount_per_student' => $amountPerStudent
            ];
            
            $result = updateData('salary_settings', $updateData, 'id = :id', ['id' => $id]);
            if ($result === false) {
                $success = false;
            }
        }
        
        if ($success) {
            $message = '✅ تنظیمات حق التدریس با موفقیت ذخیره شد.';
            $messageType = 'success';
            // به‌روزرسانی لیست
            $salarySettings = fetchAll("SELECT * FROM salary_settings ORDER BY degree, course_type");
        } else {
            $message = '❌ خطا در ذخیره تنظیمات حق التدریس.';
            $messageType = 'danger';
        }
    }
    
    // ===== ذخیره تنظیمات مالیات و بیمه =====
    if (isset($_POST['action']) && $_POST['action'] === 'save_tax') {
        $taxPercent = isset($_POST['tax_percent']) ? (float)$_POST['tax_percent'] : 0;
        $insurancePercent = isset($_POST['insurance_percent']) ? (float)$_POST['insurance_percent'] : 0;
        
        $updateData = [
            'tax_percent' => $taxPercent,
            'insurance_percent' => $insurancePercent,
            'updated_by' => $user['id']
        ];
        
        $result = updateData('tax_settings', $updateData, 'id = :id', ['id' => 1]);
        
        if ($result !== false) {
            $message = '✅ تنظیمات مالیات و بیمه با موفقیت ذخیره شد.';
            $messageType = 'success';
            $taxSettings = getTaxSettings();
            $taxPercent = $taxSettings['tax_percent'] ?? 0;
            $insurancePercent = $taxSettings['insurance_percent'] ?? 0;
        } else {
            $message = '❌ خطا در ذخیره تنظیمات مالیات و بیمه.';
            $messageType = 'danger';
        }
    }
}

$content = 'salary-settings-content.php';
require_once '../includes/layout.php';
?>