<?php
/**
 * Contract Sections Management View
 *
 * @package Freelance_Studio
 */

if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="wrap fs-wrap">
    <h1><?php esc_html_e('Contract Sections', 'freelance-studio'); ?></h1>
    <p class="description"><?php esc_html_e('Edit the pre-written sections that appear in your contracts. These sections are based on standard design contract terms and can be customized for your business.', 'freelance-studio'); ?></p>
    
    <?php if ($show_success): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php esc_html_e('Section saved successfully.', 'freelance-studio'); ?></p>
        </div>
    <?php endif; ?>
    
    <div class="fs-contract-sections-container">
        <?php if ($editing_section): ?>
            <!-- Edit Form -->
            <div class="fs-section-editor">
                <?php /* translators: 1. date/time */ ?>
                <h2><?php echo wp_kses_post( sprintf( __('Edit Section: %s', 'freelance-studio'), esc_html($editing_section->title))); ?></h2>
                
                <form method="post" action="">
                    <?php wp_nonce_field('fs_save_contract_section', 'fs_section_nonce'); ?>
                    <input type="hidden" name="section_id" value="<?php echo esc_attr($editing_section->id); ?>">
                    
                    <table class="form-table">
                        <tr>
                            <th scope="row">
                                <label for="section_title"><?php esc_html_e('Section Title', 'freelance-studio'); ?></label>
                            </th>
                            <td>
                                <input type="text" name="section_title" id="section_title" class="regular-text" 
                                       value="<?php echo esc_attr($editing_section->title); ?>" required>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label><?php esc_html_e('Section Key', 'freelance-studio'); ?></label>
                            </th>
                            <td>
                                <code><?php echo esc_html($editing_section->section_key); ?></code>
                                <p class="description"><?php esc_html_e('This identifier cannot be changed.', 'freelance-studio'); ?></p>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label><?php esc_html_e('Category', 'freelance-studio'); ?></label>
                            </th>
                            <td>
                                <code><?php echo esc_html(ucfirst($editing_section->category)); ?></code>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label><?php esc_html_e('Required', 'freelance-studio'); ?></label>
                            </th>
                            <td>
                                <?php if ($editing_section->is_required): ?>
                                    <span class="fs-badge fs-badge-info"><?php esc_html_e('Required', 'freelance-studio'); ?></span>
                                    <p class="description"><?php esc_html_e('This section is required and will always be included in contracts.', 'freelance-studio'); ?></p>
                                <?php else: ?>
                                    <span class="fs-badge fs-badge-secondary"><?php esc_html_e('Optional', 'freelance-studio'); ?></span>
                                    <p class="description"><?php esc_html_e('This section can be optionally included in contracts.', 'freelance-studio'); ?></p>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="is_active"><?php esc_html_e('Active', 'freelance-studio'); ?></label>
                            </th>
                            <td>
                                <label>
                                    <input type="checkbox" name="is_active" id="is_active" value="1" 
                                           <?php checked($editing_section->is_active, 1); ?>>
                                    <?php esc_html_e('Section is available for use in contracts', 'freelance-studio'); ?>
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="section_content"><?php esc_html_e('Content', 'freelance-studio'); ?></label>
                            </th>
                            <td>
                                <?php 
                                wp_editor($editing_section->content, 'section_content', array(
                                    'media_buttons' => false,
                                    'textarea_rows' => 15,
                                    'teeny' => false,
                                    'quicktags' => true,
                                ));
                                ?>
                                <p class="description">
                                    <?php esc_html_e('Available placeholders:', 'freelance-studio'); ?>
                                    <code>[Number]</code> - <?php esc_html_e('Notice/review days', 'freelance-studio'); ?>,
                                    <code>[Percentage]</code> - <?php esc_html_e('Late fee %', 'freelance-studio'); ?>,
                                    <code>[Hourly Rate]</code> - <?php esc_html_e('Your hourly rate', 'freelance-studio'); ?>,
                                    <code>[Designer's State/Country]</code> - <?php esc_html_e('Jurisdiction', 'freelance-studio'); ?>
                                </p>
                            </td>
                        </tr>
                    </table>
                    
                    <p class="submit">
                        <input type="submit" name="submit" class="button button-primary" value="<?php esc_attr_e('Save Section', 'freelance-studio'); ?>">
                        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-contract-sections') ); ?>" class="button"><?php esc_html_e('Cancel', 'freelance-studio'); ?></a>
                    </p>
                </form>
            </div>
        <?php else: ?>
            <!-- Sections List -->
            <div class="fs-info-box" style="margin-bottom: 20px; padding: 15px; background: #f0f6fc; border-left: 4px solid #2271b1; border-radius: 4px;">
                <strong><?php esc_html_e('How Contract Sections Work', 'freelance-studio'); ?></strong>
                <p style="margin: 8px 0 0 0;"><?php esc_html_e('When creating a contract, you can choose which sections to include using checkboxes. Required sections are always included. Edit sections below to customize the legal language for your business.', 'freelance-studio'); ?></p>
            </div>
            
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th style="width: 30px;"><?php esc_html_e('#', 'freelance-studio'); ?></th>
                        <th><?php esc_html_e('Title', 'freelance-studio'); ?></th>
                        <th style="width: 120px;"><?php esc_html_e('Category', 'freelance-studio'); ?></th>
                        <th style="width: 100px;"><?php esc_html_e('Required', 'freelance-studio'); ?></th>
                        <th style="width: 100px;"><?php esc_html_e('Status', 'freelance-studio'); ?></th>
                        <th style="width: 100px;"><?php esc_html_e('Actions', 'freelance-studio'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $counter = 1;
                    foreach ($sections as $section): 
                    ?>
                        <tr>
                            <td><?php echo  esc_html( $counter++ ); ?></td>
                            <td>
                                <strong><?php echo esc_html($section->title); ?></strong>
                                <br><code style="font-size: 11px; color: #666;"><?php echo esc_html($section->section_key); ?></code>
                            </td>
                            <td>
                                <?php 
                                $category_labels = array(
                                    'parties' => __('Parties', 'freelance-studio'),
                                    'scope' => __('Scope', 'freelance-studio'),
                                    'terms' => __('Terms & Conditions', 'freelance-studio'),
                                );
                                echo  esc_html( isset($category_labels[$section->category]) ? $category_labels[$section->category] : ucfirst($section->category) );                                ?>
                            </td>
                            <td>
                                <?php if ($section->is_required): ?>
                                    <span class="fs-badge fs-badge-info"><?php esc_html_e('Required', 'freelance-studio'); ?></span>
                                <?php else: ?>
                                    <span class="fs-badge fs-badge-secondary"><?php esc_html_e('Optional', 'freelance-studio'); ?></span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($section->is_active): ?>
                                    <span class="fs-badge fs-badge-success"><?php esc_html_e('Active', 'freelance-studio'); ?></span>
                                <?php else: ?>
                                    <span class="fs-badge fs-badge-warning"><?php esc_html_e('Inactive', 'freelance-studio'); ?></span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-contract-sections&edit=' . $section->id) ); ?>" class="button button-small">
                                    <?php esc_html_e('Edit', 'freelance-studio'); ?>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            
            <div class="fs-info-box" style="margin-top: 20px; padding: 15px; background: #fff8e5; border-left: 4px solid #dba617; border-radius: 4px;">
                <strong><?php esc_html_e('Note', 'freelance-studio'); ?></strong>
                <p style="margin: 8px 0 0 0;"><?php esc_html_e('These sections are pre-populated with standard design contract language. While you can customize them, we recommend having a legal professional review any significant changes to ensure they protect your business properly.', 'freelance-studio'); ?></p>
            </div>
        <?php endif; ?>
    </div>
</div>

<style>
.fs-contract-sections-container {
    margin-top: 20px;
}
.fs-section-editor {
    background: #fff;
    padding: 20px;
    border: 1px solid #c3c4c7;
    border-radius: 4px;
}
.fs-badge {
    display: inline-block;
    padding: 3px 8px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 500;
}
.fs-badge-info {
    background: #e5f3ff;
    color: #0073aa;
}
.fs-badge-secondary {
    background: #f0f0f1;
    color: #50575e;
}
.fs-badge-success {
    background: #d4edda;
    color: #155724;
}
.fs-badge-warning {
    background: #fff3cd;
    color: #856404;
}
</style>
