/**
 * Sifency CPT Meta Modal Script
 * Handles quick edit meta modal via AJAX.
 *
 * @package Sifency
 * @since 1.0.4
 */

jQuery(document).ready(function($) {
    'use strict';

    const $modal = $('#sifency-meta-modal');
    const $form = $('#sifency-meta-form');
    const $loading = $('.sifency-modal-loading');
    const $container = $('#sifency-meta-fields-container');

    if (!$modal.length) return;

    // Open Modal
    $(document).on('click', '.sifency-meta-trigger', function(e) {
        e.preventDefault();
        let postId = $(this).data('id');
        
        $modal.fadeIn(200);
        $loading.show();
        $form.hide();
        $('#sifency-meta-post-id').val(postId);
        $container.empty();

        $.ajax({
            url: sifency_cpt_meta.ajax_url, 
            type: 'POST',
            data: { 
                action: 'sifency_get_post_details_for_modal', 
                post_id: postId, 
                nonce: sifency_cpt_meta.nonce 
            },
            success: function(res) {
                if(res.success) {
                    $loading.hide(); 
                    $form.fadeIn();
                    $('#sifency-post-title').val(res.data.title);
                    $('#sifency-content').val(res.data.content);
                    $('#sifency-excerpt').val(res.data.excerpt);
                    $container.html(res.data.html);
                }
            }
        });
    });

    $('.sifency-modal-close').on('click', function() { 
        $modal.fadeOut(200); 
    });

    $(document).on('click', '.sifency-upload-btn', function(e) {
        e.preventDefault();
        var btn = $(this);
        var targetInput = btn.siblings('.sifency-img-input');
        var targetPreview = btn.siblings('.sifency-media-preview');

        var frame = wp.media({
            title: 'Select Image',
            multiple: false,
            library: { type: 'image' },
            button: { text: 'Use Image' }
        });

        frame.on('select', function() {
            var attachment = frame.state().get('selection').first().toJSON();
            targetInput.val(attachment.id);
            targetPreview.attr('src', attachment.url).removeClass('empty').show();
        });

        frame.open();
    });

    $(document).on('click', '.sifency-remove-img', function(e) {
        e.preventDefault();
        $(this).siblings('.sifency-img-input').val('');
        $(this).siblings('.sifency-media-preview').attr('src', '').hide();
    });

    $('#sifency-save-meta').on('click', function() {
        let btn = $(this);
        btn.text('Saving...').prop('disabled', true);
        $.ajax({
            url: sifency_cpt_meta.ajax_url, 
            type: 'POST',
            data: $form.serialize() + '&action=sifency_save_post_details_from_modal&nonce=' + sifency_cpt_meta.nonce,
            success: function(res) {
                btn.text('Save Changes').prop('disabled', false);
                if(res.success) { 
                    $modal.fadeOut(200); 
                    let postId = $('#sifency-meta-post-id').val();
                    let newTitle = $('#sifency-post-title').val();
                    if(newTitle) {
                        $('#post-card-' + postId + ' .sifency-template-title-display').text(newTitle);
                        $('#post-card-' + postId + ' .sifency-template-title-input').val(newTitle);
                    }
                } else { 
                    alert('Failed to save.'); 
                }
            },
            error: function() { 
                btn.text('Save Changes').prop('disabled', false); 
            }
        });
    });
});
