(function($){$.fn.lwsCaretPosDT=function(pos){let value=this.val();if(undefined==pos)
pos=this.get(0).selectionStart;let caret={pos:pos,start:value.lastIndexOf(':',pos-1),end:value.indexOf(':',pos)};caret.start=(-1==caret.start?0:(caret.start+1));caret.end=(-1==caret.end?value.length:caret.end);return caret}
$.fn.lwsReplaceAndSelectDT=function(value,pos){this.val(value);let caret=this.lwsCaretPosDT(pos);this.get(0).setSelectionRange(caret.start,caret.end)}})(jQuery)
jQuery(function($){$('body').on('change','input.lws_adm_datetime.master',function(e,opt){if('lws_adm_datetime'==opt)
return;let name=$(e.target).attr('name');let subs=$(`input.lws_adm_datetime.sub[data-for="${name}"]`).removeClass('err');let value=$(e.target).val().trim();if(value.length){let pattern=/^(\d{4})-(\d?\d)-(\d?\d)(?:(?:\s+|T)(\d?\d):(\d?\d):(\d?\d)(?:\.\d*)?\s*([-+]\d+(?::\d+)?)?)?/i
let dt=pattern.exec(value);if(dt){let utc=subs.filter('.utc');if(utc.length)
utc.prop('checked',!0);if(dt.length>7&&undefined!=dt[7]){let p=dt[7].split(':');if(p.length>1){dt[7]=(60*(parseInt(p[1])+(60*parseInt(p[0]))))}else if(dt[7].length>2){dt[7]=(60*(parseInt(dt[7].substring(2,4))+(60*parseInt(dt[7].substring(0,2)))))}else{dt[7]=(3600*parseInt(dt[7]))}
dt[7]=parseInt(dt[7]);if(!isNaN(dt[7])&&0!=dt[7]){let offset=utc.length?parseInt(subs.filter('.offset').val()):0;if(isNaN(offset))
offset=0;if(offset!=0)
utc.prop('checked',!1);if(offset!=dt[7]){let d=new Date(parseInt(dt[1]),parseInt(dt[2])-1,parseInt(dt[3]),parseInt(dt[4]),parseInt(dt[5]),parseInt(dt[6]),0);d.setTime(d.getTime()+(1000*(offset-dt[7])));dt=[dt[0],d.getFullYear().toString(),(d.getMonth()+1).toString(),d.getDate().toString(),d.getHours().toString(),d.getMinutes().toString(),d.getSeconds().toString()]}}}
subs.filter('.date').val([dt[1],dt[2].padStart(2,'0'),dt[3].padStart(2,'0')].join('-'));if(dt.length>4&&undefined!=dt[4]){subs.filter('.time').val([dt[4].padStart(2,'0'),dt[5].padStart(2,'0'),dt[6].padStart(2,'0')].join(':'))}}else{subs.filter('.date').val('');subs.filter('.time').val('')}}else{subs.filter('.date').val('');subs.filter('.time').val('')}});$('body').on('change','input.lws_adm_datetime.sub',function(e){let name=$(e.target).data('for');let master=$(`input.lws_adm_datetime.master[name="${name}"]`);let subs=$(`input.lws_adm_datetime.sub[data-for="${name}"]`).removeClass('err');let d=subs.filter('.date').val().trim();if(d.length){if(!d.match(/\d{4}-(0?\d|1[0-2])-([0-2]\d|3[01])/)){subs.filter('.date').addClass('err');d=''}}
let t=subs.filter('.time').val().trim();if(t.length){if(!t.match(/^([01]?\d|2[0-3]):[0-5]?\d(:[0-5]?\d)?$/)){subs.filter('.time').addClass('err');t=''}else{let parts=t.split(':');for(let i=0;i<3;i++){if(parts.length>i)
parts[i]=parts[i].padStart(2,'0');else parts.push('00')}
t=parts.join(':');subs.filter('.time').val(t)}}else if(d.length){t='00:00:00'}
let val='';if(d.length&&t.length){val=(d+'T'+t);if(!subs.filter('.utc').prop('checked')){let tz=parseInt(subs.filter('.offset').val().trim());let minutes=Math.round(tz/60);let hours=Math.floor(minutes/60);val+=((tz>=0?'+':'-')+hours.toString().padStart(2,'0')+":"+(minutes%60).toString().padStart(2,'0'))}}
if(val!=master.val()){master.val(val);master.trigger('change',['lws_adm_datetime'])}});$('body').on('click','input.lws_adm_datetime.sub.time',function(e){let value=$(e.target).val();if(!value.length)
return;if(undefined!=e.target.selectionStart&&undefined!=e.target.selectionEnd&&e.target.selectionStart!=e.target.selectionEnd)
return;let caret=$(e.target).lwsCaretPosDT();e.target.setSelectionRange(caret.start,caret.end)});$('body').on('keyup','input.lws_adm_datetime.sub.time',function(e){let value=$(e.target).val();if(!value.length)
return;let caret=$(e.target).lwsCaretPosDT();if(38==event.which){let part=parseInt(value.substring(caret.start,caret.end));if(!isNaN(part)){part=((part+1)%(-1==value.lastIndexOf(':',caret.end-1)?24:60));value=(value.substring(0,caret.start)+part.toString().padStart(2,'0')+value.substring(caret.end));$(e.target).lwsReplaceAndSelectDT(value,caret.pos)}}else if(40==event.which){let part=parseInt(value.substring(caret.start,caret.end));if(!isNaN(part)){part=((part-1)%(-1==value.lastIndexOf(':',caret.end-1)?24:60));value=(value.substring(0,caret.start)+part.toString().padStart(2,'0')+value.substring(caret.end));$(e.target).lwsReplaceAndSelectDT(value,caret.pos)}}else if(37==event.which){caret=$(e.target).lwsCaretPosDT(caret.start-1);e.target.setSelectionRange(caret.start,caret.end)}else if(39==event.which){caret=$(e.target).lwsCaretPosDT(caret.end+1);e.target.setSelectionRange(caret.start,caret.end)}else if((48<=event.which&&event.which<=57)||(96<=event.which&&event.which<=105)){let parts=value.split(':').slice(0,3);while(parts.length<3)
parts.push('');let max=24;let part=0;let change=!1;for(let i=0;i<3;i++){part=parseInt(parts[i]);while(!isNaN(part)&&(part>max||parts[i].length>2)){change=!0;if(i<2){parts[i+1]=(parts[i].substring(parts[i].length-1)+parts[i+1])}
parts[i]=parts[i].substring(0,parts[i].length-1);part=parseInt(parts[i])}
max=60}
if(change){$(e.target).val(parts.filter(word=>word.length).join(':'));e.target.setSelectionRange(caret.pos+1,caret.pos+1)}}else{value=value.replace(/[^0-9]+/g,':');$(e.target).val(value.split(':').slice(0,3).join(':'));if(value!=$(e.target).val())
e.target.setSelectionRange(caret.pos,caret.pos);}});$('body').on('change','input.lws_adm_datetime.sub.utc',function(e){let name=$(e.target).data('for');let subs=$(`input.lws_adm_datetime.sub[data-for="${name}"]`);let fields={shift:subs.filter('.offset'),date:subs.filter('.date:not(.err)'),time:subs.filter('.time:not(.err)'),};if(fields.shift.length&&fields.date.length&&fields.time.length){if(fields.date.val().trim().length&&fields.time.val().trim().length){let offset=parseInt(fields.shift.val());if(offset!=0){let date=fields.date.val().trim().split('-');let time=fields.time.val().trim().split(':');let d=new Date(date[0],parseInt(date[1])-1,date[2],time[0],time[1],time[2],0);if($(e.target).prop('checked'))
d.setTime(d.getTime()-(1000*offset));else d.setTime(d.getTime()+(1000*offset));fields.date.val([d.getFullYear(),(d.getMonth()+1).toString().padStart(2,'0'),d.getDate().toString().padStart(2,'0')].join('-'));fields.time.val([d.getHours().toString().padStart(2,'0'),d.getMinutes().toString().padStart(2,'0'),d.getSeconds().toString().padStart(2,'0')].join(':'))}}}})})