<?php
/**
 * Frontend upload document form partial.
 *
 * Variables available:
 *   $categories   - array of category objects
 *   $message      - feedback string (may be empty)
 *   $message_type - 'success' | 'error'
 *
 * @package    Edocman_Lite
 * @subpackage Edocman_Lite/public/partials
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>
<div class="edocman-lite-upload-wrap">

    <?php if ( ! empty( $message ) ) : ?>
        <div class="edocman-lite-notice edocman-lite-notice-<?php echo esc_attr( $message_type ); ?>"
             role="alert">
            <?php echo esc_html( $message ); ?>
        </div>
    <?php endif; ?>

    <form
        method="post"
        enctype="multipart/form-data"
        class="edocman-lite-upload-form"
        novalidate
    >
        <?php wp_nonce_field( 'edocman_lite_upload_document', 'edocman_lite_upload_nonce' ); ?>

        <div class="edocman-lite-field">
            <label for="edl-upload-title">
                <?php esc_html_e( 'Title', 'edocman-lite' ); ?> <span aria-hidden="true">*</span>
            </label>
            <input
                type="text"
                id="edl-upload-title"
                name="edl_title"
                required
                value="<?php echo esc_attr( isset( $_POST['edl_title'] ) ? sanitize_text_field( wp_unslash( $_POST['edl_title'] ) ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing ?>"
            />
        </div>

        <?php if ( ! empty( $categories ) ) : ?>
        <div class="edocman-lite-field">
            <label for="edl-upload-category">
                <?php esc_html_e( 'Category', 'edocman-lite' ); ?>
            </label>
            <select id="edl-upload-category" name="edl_category_id">
                <option value="0"><?php esc_html_e( '— Select Category —', 'edocman-lite' ); ?></option>
                <?php foreach ( $categories as $cat ) : ?>
                    <option
                        value="<?php echo esc_attr( $cat->id ); ?>"
                        <?php selected( isset( $_POST['edl_category_id'] ) ? intval( $_POST['edl_category_id'] ) : 0, $cat->id ); // phpcs:ignore WordPress.Security.NonceVerification.Missing ?>
                    ><?php echo esc_html( $cat->title ); ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php endif; ?>

        <div class="edocman-lite-field">
            <label for="edl-upload-description">
                <?php esc_html_e( 'Description', 'edocman-lite' ); ?>
            </label>
            <textarea
                id="edl-upload-description"
                name="edl_description"
                rows="4"
            ><?php echo esc_textarea( isset( $_POST['edl_description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['edl_description'] ) ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing ?></textarea>
        </div>

        <div class="edocman-lite-field">
            <label for="edl-upload-file">
                <?php esc_html_e( 'File', 'edocman-lite' ); ?> <span aria-hidden="true">*</span>
            </label>
            <input
                type="file"
                id="edl-upload-file"
                name="edl_file"
                required
            />
        </div>

        <div class="edocman-lite-field">
            <button type="submit" name="edl_submit" class="edocman-lite-btn">
                <?php esc_html_e( 'Upload Document', 'edocman-lite' ); ?>
            </button>
        </div>
    </form>
</div>
