/**
 * Created by IOAN on 9/29/2015.
 */
$(document).ready(function(){
    /*$('.wysihtml5').removeClass('hidden').wysihtml5({
        toolbar: {
            "font-styles": false, //Font styling, e.g. h1, h2, etc. Default true
            "emphasis": true, //Italics, bold, etc. Default true
            "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true
            "html": false, //Button which allows you to edit the generated HTML. Default false
            "link": false, //Button to insert a link. Default true
            "image": false, //Button to insert an image. Default true,
            "color": false, //Button to change color of font
            "blockquote": true, //Blockquote
            "size": "xs", //default: none, other options are xs, sm, lg
            "fa": true
        },
        "events": {
            "load": 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%',
        plugins:    [ "autoresize" ],
        statusbar:  false,
        menubar:    false,
        toolbar:    "bold italic |  bullist numlist outdent indent"
    });

    $('#form-submit').on('click', function(e){
        $('#job-form').trigger('submit');
        $(this).prop("disabled", true);
        return false;
    });

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

        var form_data = $('#job-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/job_actions.php");
        //console.log(form_data);
        $.ajax({
            url: baseurl + '/proxy.php',
            data: form_data,
            type: 'post',
            dataType: 'json',
            cache: false,
            success: function(r) {
                //console.log(r);
                if(r.result == 1) {
                    if(r.hasOwnProperty('flashm')) {
                        //$.post(baseurl + '/flashm.php', {'flashm': r.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;
                    }, 1000);

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


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

        var $button = $(this);
        var job_jobs_id = $(this).data('jid');
        var job_status = $(this).data('job_status');
        var $parent = $(this).closest('.job-footer');
        var $job_stat = $('.job-stat', $parent);

        var post_data = {
            'csurl': encodeURIComponent(full_uri + "amodules/jobsresumes/ajax/job_actions.php"),
            'job_jobs_id':job_jobs_id,
            'job_status': 1-job_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(job_status == 1) {
                        $job_stat.text('Inactive');
                        $button.text('Activate');
                        $job_stat.removeClass('label-success').addClass('label-danger');
                        $button.data('job_status', 0);
                    }

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



    });

    $('.delete-job').on('click', function(e) {
        e.preventDefault();
        var post_data = {
            'csurl': encodeURIComponent(full_uri + "amodules/jobsresumes/ajax/job_actions.php"),
            'job_jobs_id':$(this).data('jid'),
            'chcAction': 'Delete'
        };

        BootstrapDialog.confirm('Do you really want to delete this job?', function(result){
            if(result) {
                $.ajax({
                    url: baseurl + '/proxy.php',
                    data: post_data,
                    type: 'post',
                    dataType: 'json',
                    cache: false,
                    success: function(r) {

                        if(r.result == 1) {
                            if(r.hasOwnProperty('flashm')) {
                                //$.post(baseurl + '/flashm.php', {'flashm': r.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;
                            }, 400);
                        }
                    }
                });
            }

        });
    });

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

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

    $('#Town').on('change', function(){
        var town = $(this).val();
        var $ot = $('#OtherTown');

        if(town == "other") {
            $ot.prop('disabled', false);
            $ot.css({'text-decoration': 'none'});
        }
        else {
            $ot.prop('disabled', true);
            if($.trim($ot.val()) != "") {
                $ot.css({'text-decoration': 'line-through'});
            }
        }
    });


});