<?php
// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Check if we're in edit mode (client object exists and has a valid ID)
$is_edit_mode = false;
if (isset($client) && $client instanceof \EasyInvoice\Models\Client) {
    $client_id = $client->getId();
    $is_edit_mode = ($client_id > 0);
}

$form_id = $is_edit_mode ? 'edit-client-form' : 'add-client-form';
$submit_text = $is_edit_mode ? __('Save Changes', 'easy-invoice') : __('Save Client', 'easy-invoice');

// Add unique prefixes to prevent ID conflicts
$field_prefix = $is_edit_mode ? 'edit-' : 'add-';
?>

<form id="<?php echo esc_attr($form_id); ?>">
    <?php if ($is_edit_mode): ?>
        <input type="hidden" id="<?php echo $field_prefix; ?>client-id" name="client_id" value="<?php echo esc_attr($client->getId()); ?>">
    <?php endif; ?>
    
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
        <div>
            <label for="<?php echo $field_prefix; ?>client-business-name" class="block text-sm font-medium text-gray-700"><?php _e('Business/Client Name', 'easy-invoice'); ?> *</label>
            <input type="text" id="<?php echo $field_prefix; ?>client-business-name" name="business_client_name" required 
                value="<?php echo $is_edit_mode ? esc_attr($client->getBusinessClientName()) : ''; ?>"
                class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
        </div>
        <div>
            <label for="<?php echo $field_prefix; ?>client-email" class="block text-sm font-medium text-gray-700"><?php _e('Email', 'easy-invoice'); ?> *</label>
            <input type="email" id="<?php echo $field_prefix; ?>client-email" name="email" required 
                value="<?php echo $is_edit_mode ? esc_attr($client->getEmail()) : ''; ?>"
                class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
        </div>
        <div>
            <label for="<?php echo $field_prefix; ?>client-phone" class="block text-sm font-medium text-gray-700"><?php _e('Phone', 'easy-invoice'); ?></label>
            <input type="text" id="<?php echo $field_prefix; ?>client-phone" name="phone"
                value="<?php echo $is_edit_mode ? esc_attr($client->getPhone()) : ''; ?>"
                class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
        </div>
        <div>
            <label for="<?php echo $field_prefix; ?>client-username" class="block text-sm font-medium text-gray-700"><?php _e('Username', 'easy-invoice'); ?> *</label>
            <input type="text" id="<?php echo $field_prefix; ?>client-username" name="username" required 
                value="<?php echo $is_edit_mode ? esc_attr($client->getUsername()) : ''; ?>"
                class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
        </div>
        <div>
            <label for="<?php echo $field_prefix; ?>client-password" class="block text-sm font-medium text-gray-700">
                <?php echo $is_edit_mode ? __('New Password', 'easy-invoice') : __('Password', 'easy-invoice') . ' *'; ?>
            </label>
            <div class="mt-1 relative">
                <input type="password" id="<?php echo $field_prefix; ?>client-password" name="password" 
                    <?php echo $is_edit_mode ? '' : 'required'; ?>
                    placeholder="<?php echo $is_edit_mode ? __('Leave blank to keep current password', 'easy-invoice') : ''; ?>"
                    class="block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm pr-24">
                <div class="absolute inset-y-0 right-0 pr-3 flex items-center space-x-2">
                    <button type="button" id="<?php echo $field_prefix; ?>generate-password" class="text-sm font-medium text-indigo-600 hover:text-indigo-500"><?php _e('Generate', 'easy-invoice'); ?></button>
                    <button type="button" id="<?php echo $field_prefix; ?>show-password" class="text-gray-500 hover:text-gray-700">
                        <i class="fas fa-eye"></i>
                    </button>
                </div>
            </div>
        </div>
        <div>
            <label for="<?php echo $field_prefix; ?>client-first-name" class="block text-sm font-medium text-gray-700"><?php _e('First Name', 'easy-invoice'); ?></label>
            <input type="text" id="<?php echo $field_prefix; ?>client-first-name" name="first_name" 
                value="<?php echo $is_edit_mode ? esc_attr($client->getFirstName()) : ''; ?>"
                class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
        </div>
        <div>
            <label for="<?php echo $field_prefix; ?>client-last-name" class="block text-sm font-medium text-gray-700"><?php _e('Last Name', 'easy-invoice'); ?></label>
            <input type="text" id="<?php echo $field_prefix; ?>client-last-name" name="last_name" 
                value="<?php echo $is_edit_mode ? esc_attr($client->getLastName()) : ''; ?>"
                class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
        </div>
    </div>
    
    <div class="mb-6">
        <label for="<?php echo $field_prefix; ?>client-address" class="block text-sm font-medium text-gray-700"><?php _e('Address', 'easy-invoice'); ?></label>
        <textarea id="<?php echo $field_prefix; ?>client-address" name="address" rows="3"
            class="block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"><?php echo $is_edit_mode ? wp_kses_post($client->getAddress()) : ''; ?></textarea>
    </div>
    
    <div class="mb-6">
        <label for="<?php echo $field_prefix; ?>client-extra-info" class="block text-sm font-medium text-gray-700"><?php _e('Extra Info', 'easy-invoice'); ?></label>
        <textarea id="<?php echo $field_prefix; ?>client-extra-info" name="extra_info" rows="2"
            class="block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"><?php echo $is_edit_mode ? wp_kses_post($client->getExtraInfo()) : ''; ?></textarea>
    </div>
    
    <div class="mb-6">
        <label for="<?php echo $field_prefix; ?>client-website" class="block text-sm font-medium text-gray-700"><?php _e('Website', 'easy-invoice'); ?></label>
        <input type="url" id="<?php echo $field_prefix; ?>client-website" name="website" 
            value="<?php echo $is_edit_mode ? esc_attr($client->getWebsite()) : ''; ?>"
            class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
    </div>
    
    <div class="mt-5 flex justify-end space-x-3">
        <?php if ($is_edit_mode): ?>
            <a href="?page=easy-invoice-clients" class="bg-white py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                <?php _e('Cancel', 'easy-invoice'); ?>
            </a>
        <?php else: ?>
            <button type="button" id="<?php echo $field_prefix; ?>cancel-add-client" class="bg-white py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                <?php _e('Cancel', 'easy-invoice'); ?>
            </button>
        <?php endif; ?>
        <button type="submit" id="<?php echo $field_prefix; ?>save-client-btn" class="bg-indigo-600 py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
            <?php echo esc_html($submit_text); ?>
        </button>
    </div>
</form> 