'use strict'; var sProgramId = getProgramId(), isAdmin = parent.UserInfo.roles.toLowerCase().indexOf('admin') > -1, AddedTip = false, oCalendar = null, fnPageInit = function() { var saCalType = [], saOpenMent = ['P', 'G', 'D', 'C'], /** * 表單驗證 * @param {Object}iframe form表單 */ fnValidate = function(iframe) { iframe.find('[name=OpenMent]').click(function() { var sVal = this.value; if (sVal === 'G') { iframe.find('#trGroupMembers').show(); } else { iframe.find('#trGroupMembers').hide(); iframe.find('#GroupMembers').val('').data('value', ''); } }); $.validator.addMethod("compardatetime", function(value, element, parms) { if (new Date(value) <= new Date(iframe.find('#StartDate').val())) { return false; } return true; }); $.validator.addMethod("groupmember", function(value, element, parms) { if (iframe.find('[name=OpenMent]:checked').val() === 'G' && !$(element).data('value')) { return false; } return true; }); return iframe.find("#form_calendar").validate({ showErrors: function() { this.defaultShowErrors(); transLang(iframe); } }); // //表單欄位驗證 }, /** * 修改資料 * @param {Object} event 事件 * @param {Object} newevent 最新的事件 */ fnUpdData = function(event, newevent) { CallAjax(ComFn.W_Com, ComFn.GetUpd, { Params: { calendar: { values: newevent, keys: { NO: event.id } } } }, function(res) { if (res.d > 0) { showMsg(i18next.t("message.Modify_Success"), 'success'); //╠message.Modify_Success⇒修改成功╣ } else { showMsg(i18next.t("message.Modify_Failed"), 'error'); //╠message.Modify_Failed⇒修改失敗╣ } oCalendar.fullCalendar('refetchEvents'); if (parent.Outklook) { outlookAPI(outlook.Calendar_Upd, { NO: event.id }) } }, function() { showMsg(i18next.t("message.Modify_Failed"), 'error'); //╠message.Modify_Failed⇒修改失敗╣ }); }, /** * pop視窗 * @param {Object} iframe 表單 */ fnRegisterEvent = function(iframe, action) { iframe.find('.add-on').click(function() { fnGetSetUserGroups({ Action: action, Callback: function(data) { if (data.length > 0) { var saId = [], saName = []; $.each(data, function(idx, item) { saId.push(item.UserID); saName.push(item.UserName); }); iframe.find('#GroupMembers').data('value', saId.join(',')).val(saName.join(',')); } } }); }); }, /** * pop資料新增 * @param {Object} start 開始時間 * @param {Object} end 結束時間 */ fnAdd = function(start, end) { var oValidator = null; layer.open({ type: 2, title: '新增行程', shade: 0.75, maxmin: true, //开启最大化最小化按钮 area: ['800px', '580px'], content: '/Page/Pop/CalanderPop.html', success: function(layero, index) { var iframe = $('iframe').contents(); fnSetArgDrop([{ ArgClassID: 'CalType', Select: iframe.find('#CalType'), ShowId: true, CallBack: function(data) { var bAllDay = true; iframe.find('#CurrentDate').val(start); iframe.find('#AllDay')[0].checked = bAllDay = typeof start._i === 'string'; iframe.find('#StartDate').val(bAllDay ? start._d.formate('yyyy/MM/dd 00:00') : newDate(start)); iframe.find('#EndDate').val(bAllDay ? end._d.formate('yyyy/MM/dd 00:00') : newDate(end)); oValidator = fnValidate(iframe); fnRegisterEvent(iframe, 'add'); } }]); }, btn: [i18next.t('common.Confirm'), i18next.t('common.Cancel')], //╠common.Confirm⇒確定╣╠common.Cancel⇒取消╣ yes: function(index, layero) { var iframe = $('iframe').contents(); iframe.find('#GroupMembers').removeAttr('readonly'); if (!iframe.find("#form_calendar").valid()) { oValidator.focusInvalid(); iframe.find('#GroupMembers').attr('readonly', 'readonly'); return false; } var data = getFormSerialize(iframe.find('#form_calendar')); data = packParams(data); data.OrgID = parent.OrgID; data.UserID = parent.UserID; data.Color = parent.UserInfo.CalColor; data.AllDay = data.AllDay || '0'; g_api.ConnectLite(sProgramId, ComFn.GetAdd, data, function(res) { if (res.RESULT) { var sNo = res.DATA.rel; showMsg(i18next.t("message.Save_Success"), 'success'); // ╠message.Save_Success⇒新增成功╣ oCalendar.fullCalendar('refetchEvents'); if (parent.Outklook) { outlookAPI(outlook.Calendar_Add, { NO: sNo, ResponseRequested: true }); } } else { showMsg(i18next.t("message.Save_Failed"), 'error'); // ╠message.Save_Failed⇒新增失敗╣ } }); layer.close(index); } }); }, /** * 資料修改 * @param {Object} event 事件 */ fnUpd = function(event) { var oValidator = null, bEdit = (event.UserID === parent.UserID && event.Memo !== 'outlook'), saBtns = [], layerOption = { type: 2, title: '修改日程', shade: 0.75, maxmin: true, //开启最大化最小化按钮 area: ['800px', '580px'], content: '/Page/Pop/CalanderPop.html', success: function(layero, index) { var iframe = $('iframe').contents(); fnSetArgDrop([{ ArgClassID: 'CalType', Select: iframe.find('#CalType'), ShowId: true, CallBack: function(data) { oValidator = fnValidate(iframe); iframe.find('#CalType').val(event.CalType); iframe.find('#Title').val(event.title); iframe.find('#Description').val(event.content); iframe.find('#GroupMembers').data('value', event.GroupMembers); iframe.find('[name=OpenMent][value=' + event.OpenMent + ']').click(); iframe.find('[name=Importment][value=' + event.Importment + ']').click(); iframe.find('#AllDay')[0].checked = event.AllDay; iframe.find('#CurrentDate').val(newDate(event.start, 1)); iframe.find('#StartDate').val(newDate(event.StartDate)); iframe.find('#EndDate').val(newDate(event.EndDate)); if (event.GroupMembers) { fnSetUserDrop([{ UserIDs: event.GroupMembers, CallBack: function(data) { var saList = data, saName = []; $.each(saList, function(idx, item) { saName.push(item.MemberName); }); iframe.find('#GroupMembers').val(saName); } }]); } if (bEdit) { fnRegisterEvent(iframe, 'upd'); } else { disableInput(iframe); } } }]); layero.find('.layui-layer-btn1').css({ 'border-color': 'red', 'background-color': 'red', 'color': '#fff' }); } }; if (bEdit) { saBtns = [i18next.t('common.Confirm'), i18next.t('common.Toolbar_Del'), i18next.t('common.Cancel')]; //╠common.Confirm⇒確定╣ ╠common.Toolbar_Del⇒刪除╣ ╠common.Cancel⇒取消╣ layerOption.yes = function(index, layero) { var iframe = $('iframe').contents(); iframe.find('#GroupMembers').removeAttr('readonly'); if (!iframe.find("#form_calendar").valid()) { oValidator.focusInvalid(); iframe.find('#GroupMembers').attr('readonly', 'readonly'); return false; } var data = getFormSerialize(iframe.find('#form_calendar')); data = packParams(data, 'upd'); data.UserID = parent.UserInfo.MemberID; data.Description = data.Description || ''; data.Color = parent.UserInfo.CalColor; data.AllDay = data.AllDay || '0'; fnUpdData(event, data); layer.close(index); }; layerOption.btn2 = function() { CallAjax(ComFn.W_Com, ComFn.GetDel, { Params: { calendar: { NO: event.id } } }, function(res) { if (res.d > 0) { showMsg(i18next.t("message.Delete_Success"), 'success'); // ╠message.Delete_Success⇒刪除成功╣ oCalendar.fullCalendar('removeEvents', event.id); if (parent.Outklook) { outlookAPI(outlook.Calendar_Del, { OutlookEventId: event.OutlookEventId }); } } else { showMsg(i18next.t("message.Delete_Failed"), 'error'); // ╠message.Delete_Failed⇒刪除失敗╣ } }); }; } else { saBtns = [i18next.t('common.Cancel')]; } layerOption.btn = saBtns; layer.open(layerOption); }, /** * 選擇分組成員 * @param {Object} option 配置 */ fnGetSetUserGroups = function(option) { option = option || {}; var oGrid = null, saItems = [], saProfileGets = [], fnGetSetlectOptions = function(el) { var saSetlectOptions = []; el.find('#lstRight option').each(function() { var sId = this.value, oItems = {}, sEmail = $(this).attr('data-email'); if (sEmail) { oItems = { UserID: sId, UserName: $(this).text(), OutlookEmail: sEmail }; } else { oItems = $.map(saItems, function(item) { if (sId === item.MemberID) { return { UserID: item.MemberID, UserName: item.MemberName, OutlookEmail: item.OutlookAccount }; } })[0]; } saSetlectOptions.push(oItems); }); return saSetlectOptions; }, fnSetProfileDrop = function(handle) { return CallAjax(ComFn.W_Com, ComFn.GetList, { Type: '', Params: { profiles: { ProfileType: 'UserGroups', OrgID: parent.OrgID, UserID: parent.UserID }, sort: { SN: 'asc' } } }, function(res) { if (res.d) { saProfileGets = JSON.parse(res.d); handle.html(createOptions(saProfileGets, 'SN', 'ProfileName')); } }); }, fnAddProfile = function(layero) { var oAddPm = {}; oAddPm.OrgID = parent.OrgID; oAddPm.UserID = parent.UserID; oAddPm.ProfileType = 'UserGroups'; oAddPm.ProfileName = layero.find('#ProfileName').val(); oAddPm.ProfileSet = fnGetSetlectOptions(layero); oAddPm.ProfileSet = JSON.stringify(oAddPm.ProfileSet); oAddPm = packParams(oAddPm); if (!oAddPm.ProfileName) { showMsg(i18next.t("message.ProfileFeesClassName_Required")); //╠message.ProfileFeesClassName_Required⇒請填寫個人化費用類別名稱╣ return false; } CallAjax(ComFn.W_Com, ComFn.GetAdd, { Params: { profiles: oAddPm } }, function(res) { if (res.d > 0) { fnSetProfileDrop(layero.find('#ProfileClass')).done(function() { layero.find('#ProfileClass option').each(function() { if ($(this).text() === oAddPm.ProfileName) { layero.find('#ProfileClass').val(this.value); return false; } }); }); showMsg(i18next.t("message.Save_Success"), 'success'); // ╠message.Save_Success⇒新增成功╣ } else { showMsg(i18next.t("message.Save_Failed"), 'error'); // ╠message.Save_Failed⇒新增失敗╣ } }, function() { showMsg(i18next.t("message.Save_Failed"), 'error'); // ╠message.Save_Failed⇒新增失敗╣ }); }, fnUpdProfile = function(layero) { var oUpdPm = {}, sId = layero.find('#ProfileClass').val(); oUpdPm.ProfileName = layero.find('#ProfileName').val(); oUpdPm.ProfileSet = []; oUpdPm.ProfileSet = fnGetSetlectOptions(layero); oUpdPm.ProfileSet = JSON.stringify(oUpdPm.ProfileSet); oUpdPm = packParams(oUpdPm, 'upd'); if (!oUpdPm.ProfileName) { showMsg(i18next.t("message.ProfileFeesClassName_Required")); // 請填寫個人化費用類別名稱 return false; } CallAjax(ComFn.W_Com, ComFn.GetUpd, { Params: { profiles: { values: oUpdPm, keys: { SN: sId } } } }, function(res) { if (res.d > 0) { fnSetProfileDrop(layero.find('#ProfileClass')).done(function() { layero.find('#ProfileClass').val(sId); }); showMsg(i18next.t("message.Modify_Success"), 'success'); //╠message.Modify_Success⇒修改成功╣ } else { showMsg(i18next.t("message.Modify_Failed"), 'error'); //╠message.Modify_Failed⇒修改失敗╣ } }, function() { showMsg(i18next.t("message.Modify_Failed"), 'error'); //╠message.Modify_Failed⇒修改失敗╣ }); }, fnDelProfile = function(layero) { var sId = layero.find('#ProfileClass').val(); if (!sId) { showMsg(i18next.t("message.DeleteItem_Required")); //╠message.DeleteItem_Required⇒請選擇要刪除的項目╣ return false; } CallAjax(ComFn.W_Com, ComFn.GetDel, { Params: { profiles: { SN: sId } } }, function(res) { if (res.d > 0) { fnSetProfileDrop(layero.find('#ProfileClass')); layero.find('#ProfileName').val(''); layero.find('#lstRight').html(''); layero.find('#lstLeft').html(createOptions(saItems, 'MemberID', 'MemberName', true)); layero.find('#lstLeft').find('option:first').remove(); showMsg(i18next.t("message.Delete_Success"), 'success'); // ╠message.Delete_Success⇒刪除成功╣ } else { showMsg(i18next.t("message.Delete_Failed"), 'error'); // ╠message.Delete_Failed⇒刪除失敗╣ } }, function() { showMsg(i18next.t("message.Delete_Failed"), 'error'); // ╠message.Delete_Failed⇒刪除失敗╣ }); }; getHtmlTmp('/Page/Pop/UserGroups.html').done(function(html) { layer.open({ type: 1, title: i18next.t('common.ProfileFees'), // ╠common.ProfileFees⇒個人化費用項目╣ shadeClose: false, shade: 0.1, maxmin: true, //开启最大化最小化按钮 area: ['600px', '590px'], content: html, success: function(layero, index) { var elIistLeft = layero.find('#lstLeft'), elListRight = layero.find('#lstRight'); fnSetProfileDrop(layero.find('#ProfileClass')); fnSetUserDrop([{ Select: elIistLeft, ShowId: true, Action: option.Action, //傳入action來判斷是新增或是修改 CallBack: function(data) { saItems = data; } }]).done(function() { elIistLeft.find('option:first').remove(); optionListSearch(elIistLeft, elListRight, layero.find('#ProfileFilter')); }); layero.find('#ProfileClass').on('change', function() { var sProfile = this.value, saProfileSet = []; if (sProfile) { var oProfileGet = $.grep(saProfileGets, function(e) { return e.SN.toString() == sProfile; })[0]; saProfileSet = JSON.parse(oProfileGet.ProfileSet || '[]'); layero.find('#ProfileName').val(oProfileGet.ProfileName); } else { layero.find('#ProfileName').val(''); } elListRight.html(''); elIistLeft.html(createOptions(saItems, 'MemberID', 'MemberName', true)).find('option:first').remove(); elIistLeft.find('option').each(function() { var _option = this, sId = $(_option).val(); $.each(saProfileSet, function(index, item) { if (sId === item.UserID) { $(_option).appendTo(elListRight); item.IsMove = true; return false; } }); }); $.each(saProfileSet, function(index, item) { if (!item.IsMove) { $('