function on_confirm_all(){} (function ($) { $(function () { /*_____________________________________________*/ function test_jobs(){ for(var i =0; i<12; i++) { var h = $('#jobtemplate').wrap('
').parent().html(); $('#jobtemplate').unwrap(); var el = $(h); el.attr('id', 'job_' + i); $('#jobtemplate').after(el); } } function set_user_name_at_summary(name){ var title = ''; if ( typeof name != 'undefined' && name != "") title = name + "'s Job"; else title = "Job Arrangement"; $('.jobsummary h2').html(title); } function get_my_jobs(){ $.post(bts().ajax_url, { // POST request _ajax_nonce: bts().nonce, // nonce action: "list_job_by_staff", // action start: get_start_date(), finish: get_finish_date(), }, function(response, status, xhr){ if (response.status == "success"){ pre_process(response); //console.log("%o", response); load_staff_jobs(response); }else{ display_error(response); } }); } // function get_staff_login() // { // //pathname: "/task/a6536a3b-ef22-4a28-8d55-e2a26d4ae227/" // var path = window.location.pathname; // var p = path.substr(6);//remove /task/ // var s = p.substring(0, p.length - 1); //remove last / // //console.log(s); // return s; // } //add extra info to response.jobs function pre_process(response) { var newjobs = []; $.each(response.jobs, function(idx, val){ if (val.ack != 0){ val.readonly = "checked disabled"; val.confirm = "Already Confirmed"; }else{ val.confirm = "Confirm"; val.readonly = ""; } val.start_day = get_weekday_name(val.start); val.finish_day = get_weekday_name(val.finish); newjobs.push(val); }); response.jobs = newjobs; //console.log("%o", response); } function get_weekday_name(dateString) { var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var d = new Date(dateString); var dayName = days[d.getDay()]; return dayName; } function load_staff_jobs(response) { set_user_name_at_summary(response.staff_name); var template = get_staff_job_template(); var html = Mustache.render(template, response); $('#jobtemplate').after(html); } function get_staff_job_template() { var h = get_html_include_tag($('#jobtemplate')); var h = get_html_include_tag($(h).removeAttr('id')); return " {{#jobs}}" + h + " {{/jobs}} "; } function get_html_include_tag(el) { var h = el.wrap('').parent().html(); el.unwrap(); return h; } function display_error() { err_message_box("Network Error", "Your Jobs for this week cannot be loaded, please try again later. For urgent job arrangement please contact Helen directly."); } function format_date(date){ var dd = date.getDate(); var mm = date.getMonth() + 1; //January is 0! var hh = date.getHours(); var ii = date.getMinutes(); var ss = date.getSeconds(); var yyyy = date.getFullYear(); if (dd < 10) { dd = '0' + dd; } if (mm < 10) { mm = '0' + mm; } if (hh< 10){ hh = '0' + hh; } if (ii < 10){ ii = '0' + ii; } if (ss <10 ){ ss = '0' + ss; } return yyyy + '-' + mm + '-' +dd + " " +hh +":" + ii + ":" + ss; } function format_date_only(date){ var dd = date.getDate(); var mm = date.getMonth() + 1; //January is 0! var yyyy = date.getFullYear(); if (dd < 10) { dd = '0' + dd; } if (mm < 10) { mm = '0' + mm; } return yyyy + '-' + mm + '-' +dd; } function get_this_week_start(){ var curr = new Date; // get current date // First day is the day of the month - the day of the week var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first var last = first + 6; // last day is the first day + 6 var firstday = new Date(curr.setDate(first)); //Mon firstday.setHours(0,0,0); return format_date(firstday); } function get_this_week_end(){ var curr = new Date; // get current date // First day is the day of the month - the day of the week var first = curr.getDate() - curr.getDay() + 1; //+1 we want Mon as first var last = first + 6; // last day is the first day + 6 var lastday = new Date(curr.setDate(last)); //Sat lastday.setHours(23,59,59); return format_date(lastday); } function get_start_date() { var b=bts_task1; if (b.bts_job_start != '') return b.bts_job_start + " 00:00:00"; else return get_this_week_start(); } function get_finish_date() { var b=bts_task1; if (b.bts_job_finish != '') return b.bts_job_finish +" 23:59:59"; else return get_this_week_end(); } function do_on_confirm_all() { var job_ids=[]; $('.jobcard input[type="checkbox"]').prop('checked', true); if ( $('.jobcard input[type="checkbox"]:checked').length ==0 ) { err_message_box("Warning", "You have no job to Confirm"); return; } $('.jobcard input[type="checkbox"]:checked').each(function(e){ var ack = $(this).is(':checked'); var lb = $(this).closest('label[name="recordinfo"]'); var id = lb.attr('data-record-id'); if ( id != '{{id}}' ) //the template job_ids.push({id:id, ack:ack}); }); do_update_ack(job_ids); info_message_box("Congratulations", "All Your Jobs has been confirmed. Your payment will be calculated based on them. For urgent job changes please contact Helen directly."); } function info_message_box(title, message) { message_box('.job_ok_box', title, message); } function err_message_box(title, message) { message_box('.job_error_box', title, message); } function message_box(selector, title,message) { set_modal_title(selector, title); set_modal_message(selector, message); $(selector + '_trigger').trigger('click'); } function set_modal_title(selector, title) { var el = selector + ' .ult_modal-title '; $(el).html(title); } function set_modal_message(selector, msg) { var el = selector + ' .ult_modal-body '; $(el).html(msg); } function do_update_ack(job_ids) { $.post(bts().ajax_url, { // POST request _ajax_nonce: bts().nonce, // nonce action: "staff_ack_job", // action jobs: job_ids, }, function(response, status, xhr){ if (response.status == "success"){ var txt = response.ack? 'Confirmed':'Click to Confirm'; span.html(txt); }else{ err_message_box("Network Error", "Try again later"); } }); } function set_prev_next_week_link() { var link = get_previous_week_link(); $('#previousweek a').attr('href', link); var link = get_next_week_link(); $('#nextweek a').attr('href', link); } function get_previous_week_link() { return get_link_by_move_date(-7); } function get_next_week_link() { return get_link_by_move_date(+7); } function get_link_by_move_date(val) { var link = '/task/start-'; //start date; var start = new Date(get_start_from_url()); var date = start.getDate(); start.setDate(date + val); link += format_date_only(start) + "/finish-"; //finish date var finish = new Date(get_finish_from_url()); var date = finish.getDate(); finish.setDate(date + val); link += format_date_only(finish) + "/"; return link; } function get_start_from_url() { //pathname: "/task/start-2017-07-01/finish-2018-09-02/" var path = window.location.pathname; var regex = /start-(.*)\/f/g; var match = regex.exec(path); if ( match != null && match.length ==2) return match[1]; else return get_this_week_start(); } function get_finish_from_url() { //pathname: "/task/start-2017-07-01/finish-2018-09-02/" var path = window.location.pathname; var regex = /finish-(.*)\//g; var match = regex.exec(path); if ( match != null && match.length ==2) return match[1]; else return get_this_week_end(); } //register events $(document).on('change', 'div.confirmfield input', function(){ var ack = $(this).is(':checked'); var lb = $(this).closest('label[name="recordinfo"]'); var id = lb.attr('data-record-id'); var span = lb.find('span'); span.html('reporting ...'); $.post(bts().ajax_url, { // POST request _ajax_nonce: bts().nonce, // nonce action: "staff_ack_job", // action jobs: [{id:id, ack:ack}], }, function(response, status, xhr){ if (response.status == "success"){ var txt = ack? 'Confirmed':'Click to Confirm'; span.html(txt); }else{ err_message_box("Network Error", "Try again later"); } }); }); //init set_user_name_at_summary(); get_my_jobs(); set_prev_next_week_link(); //get_staff_login(); on_confirm_all = do_on_confirm_all; /*_____________________________________________*/ }); })(jQuery);