<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="wrap">
    <?php
    $edocman_lite_is_edit    = isset( $category ) && isset( $category->id ) && $category->id > 0;
    $edocman_lite_page_title = $edocman_lite_is_edit
        ? __( 'Edit Category', 'edocman-lite' )
        : __( 'Add New Category', 'edocman-lite' );
    ?>

    <h1><?php echo esc_html( $edocman_lite_page_title ); ?></h1>

    <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" enctype="multipart/form-data">
        <input type="hidden" name="action" value="edocman_lite_save_category">
        <?php wp_nonce_field( 'edocman_lite_category_action', 'edocman_lite_category_nonce' ); ?>
        <?php if ( $edocman_lite_is_edit ) : ?>
            <input type="hidden" name="category_id" value="<?php echo absint( $category->id ); ?>">
        <?php endif; ?>

        <table class="form-table">
            <tr>
                <th scope="row">
                    <label for="title"><?php esc_html_e( 'Title', 'edocman-lite' ); ?> <span class="required">*</span></label>
                </th>
                <td>
                    <input type="text"
                           name="title"
                           id="title"
                           class="regular-text"
                           value="<?php echo $edocman_lite_is_edit ? esc_attr( $category->title ) : ''; ?>"
                           required>
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="parent_id"><?php esc_html_e( 'Parent Category', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <select name="parent_id" id="parent_id">
                        <option value="0" data-path=""><?php esc_html_e( '-- None (Root Category) --', 'edocman-lite' ); ?></option>
                        <?php if ( ! empty( $parent_categories ) ) : ?>
                            <?php foreach ( $parent_categories as $edocman_lite_parent ) : ?>
                                <option value="<?php echo absint( $edocman_lite_parent->id ); ?>"
                                        data-path="<?php echo esc_attr( isset( $edocman_lite_parent->path ) ? $edocman_lite_parent->path : '' ); ?>"
                                        <?php if ( $edocman_lite_is_edit ) selected( $category->parent_id, $edocman_lite_parent->id ); ?>>
                                    <?php echo esc_html( isset( $edocman_lite_parent->title_display ) ? $edocman_lite_parent->title_display : $edocman_lite_parent->title ); ?>
                                </option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                </td>
            </tr>

            <?php if ( Edocman_Lite_Helper::get_config( 'hierarchical_folders' ) === '1' ) : ?>
            <tr>
                <th scope="row">
                    <label for="folder_name">
                        <?php esc_html_e( 'Folder Name', 'edocman-lite' ); ?>
                        <span class="description">(<?php esc_html_e( 'required', 'edocman-lite' ); ?>)</span>
                    </label>
                </th>
                <td>
                    <div class="folder-path-container">

                        <!-- Display full path. -->
                        <div class="edocman-path-display">
                            <strong><?php esc_html_e( 'Full Path:', 'edocman-lite' ); ?></strong>
                            <span id="full_path_display">
                                <?php
                                if ( $edocman_lite_is_edit && ! empty( $category->path ) ) {
                                    echo '/' . esc_html( $category->path );
                                } else {
                                    echo '/';
                                }
                                ?>
                            </span>
                        </div>

                        <!-- Parent path (readonly) -->
                        <div class="edocman-parent-path-section">
                            <label class="edocman-field-label"><?php esc_html_e( 'Parent Path:', 'edocman-lite' ); ?></label>
                            <input type="text" id="parent_path_display" class="regular-text edocman-readonly-field" readonly
                                value="<?php
                                if ( $edocman_lite_is_edit && $category->parent_id > 0 ) {
                                    $edocman_lite_parent_path = '';
                                    foreach ( $parent_categories as $edocman_lite_parent ) {
                                        // ✅ Strict comparison — cast both sides to int.
                                        if ( (int) $edocman_lite_parent->id === (int) $category->parent_id && isset( $edocman_lite_parent->path ) ) {
                                            $edocman_lite_parent_path = $edocman_lite_parent->path;
                                            break;
                                        }
                                    }
                                    echo $edocman_lite_parent_path ? '/' . esc_attr( $edocman_lite_parent_path ) : '/';
                                } else {
                                    echo '/';
                                }
                                ?>">
                        </div>

                        <!-- Folder name input -->
                        <div class="edocman-folder-name-section">
                            <label class="edocman-field-label"><?php esc_html_e( 'Folder Name:', 'edocman-lite' ); ?></label>
                            <input type="text" name="folder_name" id="folder_name" class="regular-text"
                                value="<?php
                                if ( $edocman_lite_is_edit && ! empty( $category->path ) ) {
                                    $edocman_lite_path_parts = explode( '/', $category->path );
                                    echo esc_attr( end( $edocman_lite_path_parts ) );
                                }
                                ?>"
                                pattern="[a-zA-Z0-9_-]+"
                                title="<?php esc_attr_e( 'Only letters, numbers, hyphens and underscores allowed', 'edocman-lite' ); ?>">

                            <!-- Hidden field storing the full path. -->
                            <input type="hidden" name="path" id="path" value="<?php echo $edocman_lite_is_edit ? esc_attr( $category->path ) : ''; ?>">
                        </div>
                    </div>

                    <p class="description">
                        <?php esc_html_e( 'Physical folder name for storing documents. Only letters, numbers, hyphens and underscores allowed.', 'edocman-lite' ); ?><br>
                        <strong><?php esc_html_e( 'Note:', 'edocman-lite' ); ?></strong>
                        <?php esc_html_e( 'Changing this will affect the file storage location.', 'edocman-lite' ); ?>
                    </p>
                </td>
            </tr>
            <?php endif; ?>

            <tr>
                <th scope="row">
                    <label for="description"><?php esc_html_e( 'Description', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <?php
                    $edocman_lite_content = $edocman_lite_is_edit ? $category->description : '';
                    wp_editor( $edocman_lite_content, 'description', array(
                        'textarea_name' => 'description',
                        'textarea_rows' => 10,
                        'media_buttons' => true,
                    ) );
                    ?>
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="image"><?php esc_html_e( 'Image', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <input type="text"
                           name="image"
                           id="category_image"
                           class="regular-text"
                           value="<?php echo $edocman_lite_is_edit ? esc_attr( $category->image ) : ''; ?>">
                    <button type="button" class="button" id="upload_category_image_button">
                        <?php esc_html_e( 'Select Image', 'edocman-lite' ); ?>
                    </button>
                    <div id="category_image_preview" class="edocman-image-preview">
                        <?php if ( $edocman_lite_is_edit && ! empty( $category->image ) ) : ?>
                            <img src="<?php echo esc_url( $category->image ); ?>" class="edocman-preview-img" alt="">
                        <?php endif; ?>
                    </div>
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="ordering"><?php esc_html_e( 'Ordering', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <input type="number"
                           name="ordering"
                           id="ordering"
                           value="<?php echo $edocman_lite_is_edit ? esc_attr( $category->ordering ) : '0'; ?>"
                           min="0">
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="published"><?php esc_html_e( 'Published', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <input type="checkbox"
                           name="published"
                           id="published"
                           value="1"
                           <?php if ( $edocman_lite_is_edit ) checked( $category->published, 1 ); else echo 'checked'; ?>>
                </td>
            </tr>
        </table>

        <p class="submit">
            <input type="hidden" name="task" id="task" value="save">
            <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_attr_e( 'Save & Close', 'edocman-lite' ); ?>">
            <input type="submit" name="apply" id="apply" class="button" value="<?php esc_attr_e( 'Apply', 'edocman-lite' ); ?>" onclick="document.getElementById('task').value='apply';">
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=edocman-lite-categories' ) ); ?>" class="button">
                <?php esc_html_e( 'Cancel', 'edocman-lite' ); ?>
            </a>
        </p>
    </form>
</div>

<?php if ( Edocman_Lite_Helper::get_config( 'hierarchical_folders' ) === '1' ) : ?>
<script>
jQuery(document).ready(function($) {
    function buildPath(parentPath, folderName) {
        if (parentPath && folderName) {
            return parentPath.replace(/\/$/, '') + '/' + folderName;
        }
        return folderName || '';
    }

    function updateFolderPath() {
        var parentSelect   = $('#parent_id');
        var selectedOption = parentSelect.find('option:selected');
        var parentPath     = selectedOption.data('path') || '';
        var folderName     = $('#folder_name').val().trim();

        $('#parent_path_display').val( parentPath ? '/' + parentPath : '/' );

        var pathValue = buildPath(parentPath, folderName);
        $('#full_path_display').text( pathValue ? '/' + pathValue : '/' );
        $('#path').val(pathValue);
    }

    $('#parent_id').on('change', updateFolderPath);
    $('#folder_name').on('input', updateFolderPath);
});
</script>
<?php endif; ?>
