jQuery(document).ready(function($) {
    // 克隆仓库
    $('#git-clone-form').on('submit', function(e) {
        e.preventDefault();
        const repoUrl = $('#repo-url').val();
        const targetDir = $('#target-dir').val();
        
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'code_editor_git_clone',
                repo_url: repoUrl,
                target_dir: targetDir,
                security: codeEditorVars.nonce
            },
            beforeSend: () => {
                $('#git-output').html('<p>正在克隆仓库...</p>');
            },
            success: (response) => {
                if (response.success) {
                    $('#git-output').html('<p class="success">' + response.data.message + '</p><pre>' + response.data.output + '</pre>');
                } else {
                    $('#git-output').html('<p class="error">' + response.data + '</p>');
                }
            }
        });
    });

    // 拉取更新
    $('#git-pull-form').on('submit', function(e) {
        e.preventDefault();
        const repoPath = $('#pull-repo-path').val();
        
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'code_editor_git_pull',
                repo_path: repoPath,
                security: codeEditorVars.nonce
            },
            beforeSend: () => {
                $('#git-output').html('<p>正在拉取更新...</p>');
            },
            success: (response) => {
                if (response.success) {
                    $('#git-output').html('<p class="success">' + response.data.message + '</p><pre>' + response.data.output + '</pre>');
                } else {
                    $('#git-output').html('<p class="error">' + response.data + '</p>');
                }
            }
        });
    });

    // 推送更改
    $('#git-push-form').on('submit', function(e) {
        e.preventDefault();
        const repoPath = $('#push-repo-path').val();
        const commitMessage = $('#commit-message').val();
        
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'code_editor_git_push',
                repo_path: repoPath,
                commit_message: commitMessage,
                security: codeEditorVars.nonce
            },
            beforeSend: () => {
                $('#git-output').html('<p>正在推送更改...</p>');
            },
            success: (response) => {
                if (response.success) {
                    $('#git-output').html('<p class="success">' + response.data.message + '</p><pre>' + response.data.output + '</pre>');
                } else {
                    $('#git-output').html('<p class="error">' + response.data + '</p>');
                }
            }
        });
    });
});