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

    // For edit: $edocman_lite_main_category_id and $edocman_lite_selected_additional_categories are set by the controller.
    // For add:  default to 0 / empty.
    if ( ! isset( $edocman_lite_main_category_id ) ) {
        $edocman_lite_main_category_id = 0;
    }
    if ( ! isset( $edocman_lite_selected_additional_categories ) ) {
        $edocman_lite_selected_additional_categories = array();
    }
    ?>

    <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_document">
        <?php wp_nonce_field( 'edocman_lite_document_action', 'edocman_lite_document_nonce' ); ?>
        <?php if ( $edocman_lite_is_edit ) : ?>
            <input type="hidden" name="document_id" value="<?php echo absint( $document->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( $document->title ) : ''; ?>"
                           required>
                </td>
            </tr>

            <!-- Main Category (single select) -->
            <tr>
                <th scope="row">
                    <label for="main_category_id"><?php esc_html_e( 'Main Category', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <select name="main_category_id" id="main_category_id" class="edocman-select-wide">
                        <option value="0"><?php esc_html_e( '-- None --', 'edocman-lite' ); ?></option>
                        <?php if ( ! empty( $categories ) ) : ?>
                            <?php foreach ( $categories as $cat ) : ?>
                                <option value="<?php echo absint( $cat->id ); ?>"
                                        <?php selected( $edocman_lite_main_category_id, $cat->id ); ?>>
                                    <?php echo esc_html( isset( $cat->title_display ) ? $cat->title_display : $cat->title ); ?>
                                </option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                    <p class="description">
                        <?php esc_html_e( "The primary category. The document file will be stored in this category's folder.", 'edocman-lite' ); ?>
                    </p>
                </td>
            </tr>

            <!-- Additional Categories (multi-select) -->
            <tr>
                <th scope="row">
                    <label for="additional_category_ids"><?php esc_html_e( 'Additional Categories', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <select name="additional_category_ids[]" id="additional_category_ids" multiple size="6" class="edocman-select-wide">
                        <?php if ( ! empty( $categories ) ) : ?>
                            <?php foreach ( $categories as $cat ) : ?>
                                <option value="<?php echo absint( $cat->id ); ?>"
                                        <?php selected( in_array( $cat->id, $edocman_lite_selected_additional_categories, true ), true ); ?>>
                                    <?php echo esc_html( isset( $cat->title_display ) ? $cat->title_display : $cat->title ); ?>
                                </option>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </select>
                    <p class="description">
                        <?php esc_html_e( 'Hold Ctrl (or Cmd on Mac) to select multiple categories. The main category above is excluded automatically.', 'edocman-lite' ); ?>
                    </p>
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="document_file">
                        <?php esc_html_e( 'Document File', 'edocman-lite' ); ?>
                        <?php if ( ! $edocman_lite_is_edit ) : ?>
                            <span class="required">*</span>
                        <?php endif; ?>
                    </label>
                </th>
                <td>
                    <input type="file" name="document_file" id="document_file" <?php if ( ! $edocman_lite_is_edit ) echo 'required'; ?>>
                    <?php if ( $edocman_lite_is_edit && ! empty( $document->original_filename ) ) : ?>
                        <p class="description">
                            <?php esc_html_e( 'Current file:', 'edocman-lite' ); ?>
                            <strong><?php echo esc_html( $document->original_filename ); ?></strong>
                            (<?php echo esc_html( $document->file_size ); ?>)
                            <br>
                            <?php esc_html_e( 'Upload a new file to replace it.', 'edocman-lite' ); ?>
                        </p>
                    <?php endif; ?>
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="short_description"><?php esc_html_e( 'Short Description', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <textarea name="short_description"
                              id="short_description"
                              rows="3"
                              class="large-text"><?php echo $edocman_lite_is_edit ? esc_textarea( $document->short_description ) : ''; ?></textarea>
                </td>
            </tr>

            <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 ? $document->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( 'Thumbnail Image', 'edocman-lite' ); ?></label>
                </th>
                <td>
                    <input type="text"
                           name="image"
                           id="document_image"
                           class="regular-text"
                           value="<?php echo $edocman_lite_is_edit ? esc_attr( $document->image ) : ''; ?>">
                    <button type="button" class="button" id="upload_document_image_button">
                        <?php esc_html_e( 'Select Image', 'edocman-lite' ); ?>
                    </button>
                    <div id="document_image_preview" class="edocman-image-preview">
                        <?php if ( $edocman_lite_is_edit && ! empty( $document->image ) ) : ?>
                            <img src="<?php echo esc_url( $document->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( $document->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( $document->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-documents' ) ); ?>" class="button">
                <?php esc_html_e( 'Cancel', 'edocman-lite' ); ?>
            </a>
        </p>
    </form>
</div>
