/**
 * Created by IOAN on 9/29/2015.
 */
$(document).ready(function(){

    tinymce.baseURL = full_uri + 'assets/plugins/tinymce/js/tinymce';

    tinymce.init({
        selector: "textarea.wysihtml5",
        //theme_url: full_uri + '/assets/plugins/tinymce/js/tinymce/themes/modern/theme.min.js',
        //skin_url: full_uri + '/assets/plugins/tinymce/js/tinymce/skins/lightgray',
        width:      '100%',
        height:     '100px',
        plugins:    [ "autoresize" ],
        statusbar:  false,
        menubar:    false,
        toolbar:    "bold italic |  bullist numlist outdent indent"
    });

    $('#dob-picker').datetimepicker({
        format: 'YYYY-MM-DD',
        viewMode: 'years',
        maxDate: moment(),
        ignoreReadonly: true
    });


    $('#form-submit').on('click', function(e){
        $('#resume-form').trigger('submit');
        return false;
    });

    $('#resume-form').on('submit', function(e){
        //e.preventDefault();

        var form_data = $('#resume-form').serializeObject();

        $('textarea.wysihtml5').each(function(i){
            var el_name = $(this).prop('name');
            var tiny_el = tinyMCE.get(el_name);
            var data = tiny_el.getContent();
            form_data[el_name] = data;
        });

        form_data['csurl'] = encodeURIComponent(full_uri + "amodules/jobsresumes/ajax/resume_actions.php");
        //console.log(form_data);
        $.ajax({
            url: baseurl + '/proxy.php',
            data: form_data,
            type: 'post',
            dataType: 'json',
            cache: false,
            success: function(r) {
                if(r.result == 1) {
                    if(r.hasOwnProperty('flashm')) {
                        $.ajax({
                            type: 'POST',
                            url: baseurl + '/flashm.php',
                            data: {'flashm': r.flashm},
                            success: function(r) {},
                            dataType: 'json',
                            async:false
                        });
                    }
                    window.redirectLink = r.link;
                    window.setTimeout(function(){
                        var rlink = window.redirectLink;
                        delete(window.redirectLink);
                        document.location.href = rlink;
                    }, 200);

                }
                else {
                    BootstrapDialog.show({
                        title: 'Errors Occured',
                        message: $('<div></div>').html(r.error),
                        type: BootstrapDialog.TYPE_DANGER,
                        buttons: [
                            {
                                label: 'Ok',
                                action: function(dlg) {
                                    dlg.close();
                                }
                            }
                        ]
                    });
                }
            }
        });

        return false;
    });

    $('.status-action').removeClass('hidden');
    $('.resume-status').on('click', function(e){
        e.preventDefault();

        var $button = $(this);
        var job_cv_id = $(this).data('rid');
        var resume_status = $(this).data('resume_status');
        var $parent = $(this).closest('.resume-footer');
        var $resume_stat = $('.resume-stat', $parent);

        var post_data = {
            'csurl': encodeURIComponent(full_uri + "amodules/jobsresumes/ajax/resume_actions.php"),
            'job_cv_id':job_cv_id,
            'resume_status': 1-resume_status,
            'chcAction': 'Status'
        };

        $.ajax({
            url: baseurl + '/proxy.php',
            data: post_data,
            type: 'post',
            dataType: 'json',
            cache: false,
            success: function(r) {
                if(r.result == 1) {
                    if(resume_status == 1) {
                        $resume_stat.text('Inactive');
                        $button.text('Activate');
                        $resume_stat.removeClass('label-success').addClass('label-danger');
                        $button.data('resume_status', 0);
                    }

                    if(resume_status == 0) {
                        $resume_stat.text('Active');
                        $button.text('Inactivate');
                        $resume_stat.removeClass('label-danger').addClass('label-success');
                        $button.data('resume_status', 1);
                    }
                }
            }
        });



    });

    $('#radio-delete').on('click', function(e) {
        e.preventDefault();

        BootstrapDialog.confirm('Do you really want to delete your resume?', function(result){
            if(result) {
                $('#radio-delete').prop('checked', 'checked');
            }
            else {
                $('#radio-edit').prop('checked', 'checked');
                return false;
            }
        });
    });


});