You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

420 lines
20 KiB

2 years ago
  1. 'use strict';
  2. var sProgramId = getProgramId(),
  3. sQueryPrgId = getQueryPrgId(),
  4. sAction = getUrlParam('Action') || 'Add',
  5. sDataId = getUrlParam('Guid'),
  6. sCheckId = sDataId,
  7. fnPageInit = function () {
  8. var oCurData = { CostRules: [] },
  9. oForm = $('#form_main'),
  10. oGrid = null,
  11. oValidator = null,
  12. oEditor_CostInstruction = null,
  13. oEditor_ServiceInstruction = null,
  14. oEditor_CostInstruction_EN = null,
  15. oEditor_ServiceInstruction_EN = null,
  16. /**
  17. * 獲取資料
  18. * @return {Object} Ajax 物件
  19. */
  20. fnGet = function () {
  21. if (sDataId) {
  22. return CallAjax(ComFn.W_Com, ComFn.GetOne, {
  23. Type: '',
  24. Params: {
  25. exhibitionrules: {
  26. Guid: sDataId,
  27. OrgID: parent.OrgID
  28. }
  29. }
  30. }, function (res) {
  31. });
  32. }
  33. else {
  34. oCurData.Guid = guid();
  35. oCurData.FileId_EN = guid();
  36. fnUpload(null, oCurData.Guid, 'fileInput');
  37. fnUpload(null, oCurData.FileId_EN, 'fileInput_EN');
  38. return $.Deferred().resolve().promise();
  39. }
  40. },
  41. /**
  42. * 新增資料
  43. * @param {String} sFlag 新增或儲存後新增
  44. */
  45. fnAdd = function (sFlag) {
  46. var data = getFormSerialize(oForm);
  47. data.StoragePrice = 0; //因應變更寫法,預設0 By Mark
  48. data.IsMerge = 'N'; //因應變更寫法,預設N By Mark
  49. data = packParams(data);
  50. data.OrgID = parent.OrgID;
  51. data.Guid = oCurData.Guid;
  52. data.FileId_EN = oCurData.FileId_EN;
  53. data.CostInstruction = oEditor_CostInstruction.getContent();
  54. data.ServiceInstruction = oEditor_ServiceInstruction.getContent();
  55. data.CostInstruction_EN = oEditor_CostInstruction_EN.getContent();
  56. data.ServiceInstruction_EN = oEditor_ServiceInstruction_EN.getContent();
  57. delete data.editorValue;
  58. data.CostRules = JSON.stringify(oCurData.CostRules);
  59. data.PackingPrice = data.PackingPrice === '' ? 0 : data.PackingPrice;
  60. data.FeedingPrice = data.FeedingPrice === '' ? 0 : data.FeedingPrice;
  61. data.StoragePrice = data.StoragePrice === '' ? 0 : data.StoragePrice;
  62. CallAjax(ComFn.W_Com, ComFn.GetAdd, {
  63. Params: {
  64. exhibitionrules: data
  65. }
  66. }, function (res) {
  67. if (res.d > 0) {
  68. bRequestStorage = false;
  69. if (sFlag === 'add') {
  70. showMsgAndGo(i18next.t("message.Save_Success"), sQueryPrgId); // ╠message.Save_Success⇒新增成功╣
  71. }
  72. else {
  73. showMsgAndGo(i18next.t("message.Save_Success"), sProgramId, '?Action=Add'); // ╠message.Save_Success⇒新增成功╣
  74. }
  75. parent.msgs.server.broadcast(data);
  76. }
  77. else {
  78. showMsg(i18next.t("message.Save_Failed"), 'error'); // ╠message.Save_Failed⇒新增失敗╣
  79. }
  80. });
  81. },
  82. /**
  83. * 修改資料
  84. */
  85. fnUpd = function () {
  86. var data = getFormSerialize(oForm);
  87. data = packParams(data, 'upd');
  88. data.CostInstruction = oEditor_CostInstruction.getContent();
  89. data.ServiceInstruction = oEditor_ServiceInstruction.getContent();
  90. data.CostInstruction_EN = oEditor_CostInstruction_EN.getContent();
  91. data.ServiceInstruction_EN = oEditor_ServiceInstruction_EN.getContent();
  92. delete data.editorValue;
  93. data.CostRules = JSON.stringify(oCurData.CostRules);
  94. data.FileId_EN = oCurData.FileId_EN;
  95. CallAjax(ComFn.W_Com, ComFn.GetUpd, {
  96. Params: {
  97. exhibitionrules: {
  98. values: data,
  99. keys: {
  100. Guid: sDataId,
  101. OrgID: parent.OrgID
  102. }
  103. }
  104. }
  105. }, function (res) {
  106. if (res.d > 0) {
  107. bRequestStorage = false;
  108. showMsgAndGo(i18next.t("message.Modify_Success"), sQueryPrgId); //╠message.Modify_Success⇒修改成功╣
  109. }
  110. else {
  111. showMsg(i18next.t("message.Modify_Failed"), 'error');//╠message.Modify_Failed⇒修改失敗╣
  112. }
  113. }, function () {
  114. showMsg(i18next.t("message.Modify_Failed"), 'error');//╠message.Modify_Failed⇒修改失敗╣
  115. });
  116. },
  117. /**
  118. * 資料刪除
  119. */
  120. fnDel = function () {
  121. CallAjax(ComFn.W_Com, ComFn.GetDel, {
  122. Params: {
  123. exhibitionrules: {
  124. Guid: sDataId,
  125. OrgID: parent.OrgID
  126. }
  127. }
  128. }, function (res) {
  129. if (res.d > 0) {
  130. showMsgAndGo(i18next.t("message.Delete_Success"), sQueryPrgId); // ╠message.Delete_Success⇒刪除成功╣
  131. }
  132. else {
  133. showMsg(i18next.t("message.Delete_Failed"), 'error'); // ╠message.Delete_Failed⇒刪除失敗╣
  134. }
  135. }, function () {
  136. showMsg(i18next.t("message.Delete_Failed"), 'error'); // ╠message.Delete_Failed⇒刪除失敗╣
  137. });
  138. },
  139. /**
  140. * 上傳附件
  141. * @param {Array} files 要綁定的資料
  142. * @param {Sring} parentid 要綁定的資料
  143. * @param {Sring} flag 要綁定的資料
  144. */
  145. fnUpload = function (files, parentid, flag) {
  146. var option = {};
  147. option.input = $('#' + flag);
  148. option.theme = 'dragdropbox';
  149. option.folder = 'WebSiteFiles';
  150. option.type = 'list';
  151. option.parentid = parentid;
  152. if (files) {
  153. option.files = files;
  154. }
  155. fnUploadRegister(option);
  156. },
  157. /**
  158. * Grid客戶化計價模式控件
  159. * @param {Sring}flag 要綁定的資料標記
  160. * @return {HTMLElement} DIV 物件
  161. */
  162. fnCreatePriceModeInput = function (flag) {
  163. var $div = $('<div>'),
  164. data = [{ id: 'N', text: i18next.t('common.ByNumber') }, { id: 'T', text: i18next.t('common.ByWeight') }];//╠common.ByNumber⇒按件數計價╣ ╠common.ByWeight⇒按重量計價╣
  165. $div.html(createRadios(data, 'id', 'text', '~PricingMode' + flag, flag, false, false));
  166. return $div;
  167. },
  168. /**
  169. * ToolBar 按鈕事件 function
  170. * @param {Object}inst 按鈕物件對象
  171. * @param {Object} e 事件對象
  172. * @return {Boolean} 是否停止
  173. */
  174. fnButtonHandler = function (inst, e) {
  175. var sId = inst.id;
  176. switch (sId) {
  177. case "Toolbar_Qry":
  178. break;
  179. case "Toolbar_Save":
  180. $('#CostInstruction').val(oEditor_CostInstruction.getContentTxt());
  181. $('#ServiceInstruction').val(oEditor_ServiceInstruction.getContentTxt());
  182. $('#CostInstruction_EN').val(oEditor_CostInstruction_EN.getContentTxt());
  183. $('#ServiceInstruction_EN').val(oEditor_ServiceInstruction_EN.getContentTxt());
  184. if (!$("#form_main").valid()) {
  185. oValidator.focusInvalid();
  186. return false;
  187. }
  188. if (sAction === 'Add') {
  189. fnAdd('add');
  190. }
  191. else {
  192. fnUpd();
  193. }
  194. break;
  195. case "Toolbar_ReAdd":
  196. $('#CostInstruction').val(oEditor_CostInstruction.getContentTxt());
  197. $('#ServiceInstruction').val(oEditor_ServiceInstruction.getContentTxt());
  198. $('#CostInstruction_EN').val(oEditor_CostInstruction_EN.getContentTxt());
  199. $('#ServiceInstruction_EN').val(oEditor_ServiceInstruction_EN.getContentTxt());
  200. if (!$("#form_main").valid()) {
  201. oValidator.focusInvalid();
  202. return false;
  203. }
  204. fnAdd('readd');
  205. break;
  206. case "Toolbar_Clear":
  207. clearPageVal();
  208. break;
  209. case "Toolbar_Leave":
  210. pageLeave();
  211. break;
  212. case "Toolbar_Add":
  213. break;
  214. case "Toolbar_Upd":
  215. break;
  216. case "Toolbar_Copy":
  217. break;
  218. case "Toolbar_Del": // ╠message.ConfirmToDelete⇒確定要刪除嗎 ?╣ ╠common.Tips⇒提示╣
  219. layer.confirm(i18next.t("message.ConfirmToDelete"), { icon: 3, title: i18next.t('common.Tips') }, function (index) {
  220. fnDel();
  221. layer.close(index);
  222. });
  223. break;
  224. default:
  225. alert("No handle '" + sId + "'");
  226. break;
  227. }
  228. },
  229. /**
  230. * 初始化 function
  231. */
  232. init = function () {
  233. commonInit({
  234. PrgId: sProgramId,
  235. ButtonHandler: fnButtonHandler,
  236. GoTop: true
  237. });
  238. oValidator = $("#form_main").validate({
  239. ignore: ''
  240. });
  241. oEditor_CostInstruction = UE.getEditor('EU_CostInstruction');
  242. oEditor_ServiceInstruction = UE.getEditor('EU_ServiceInstruction');
  243. oEditor_CostInstruction_EN = UE.getEditor('EU_CostInstruction_EN');
  244. oEditor_ServiceInstruction_EN = UE.getEditor('EU_ServiceInstruction_EN');
  245. $.whenArray([
  246. fnGet(),
  247. fnSetArgDrop([
  248. {
  249. ArgClassID: 'Currency',
  250. LevelOfArgument: 0,
  251. ShowId: true,
  252. CallBack: function (data) {
  253. $('#Currency').html(createOptions(data, 'id', 'id'))[0].remove(0);
  254. }
  255. }
  256. ])
  257. ]).done(function (res1) {
  258. if (res1 && res1[0].d) {
  259. var oRes = $.parseJSON(res1[0].d);
  260. oCurData = oRes;
  261. oCurData.CostRules = $.parseJSON(oCurData.CostRules);
  262. if (!oCurData.FileId_EN) {
  263. oCurData.FileId_EN = guid();
  264. }
  265. setFormVal(oForm, oRes);
  266. oEditor_CostInstruction.ready(function () {
  267. oEditor_CostInstruction.setContent(oRes.CostInstruction);
  268. });
  269. oEditor_ServiceInstruction.ready(function () {
  270. oEditor_ServiceInstruction.setContent(oRes.ServiceInstruction);
  271. });
  272. oEditor_CostInstruction_EN.ready(function () {
  273. oEditor_CostInstruction_EN.setContent(oRes.CostInstruction_EN || '');
  274. });
  275. oEditor_ServiceInstruction_EN.ready(function () {
  276. oEditor_ServiceInstruction_EN.setContent(oRes.ServiceInstruction_EN || '');
  277. });
  278. $("#jsGrid").jsGrid("loadData");
  279. fnGetUploadFiles(oCurData.Guid, fnUpload, 'fileInput');
  280. fnGetUploadFiles(oCurData.FileId_EN, fnUpload, 'fileInput_EN');
  281. setNameById().done(function () {
  282. getPageVal();//緩存頁面值,用於清除
  283. });
  284. $('#UniqueID').attr('disabled', 'disabled');
  285. }
  286. $(':input[data-type="money"]').each(function () {
  287. moneyInput($(this), 2);
  288. });
  289. });
  290. $("#jsGrid").jsGrid({
  291. width: "100%",
  292. height: "auto",
  293. autoload: true,
  294. filtering: false,
  295. inserting: true,
  296. editing: true,
  297. pageLoading: true,
  298. confirmDeleting: true,
  299. invalidMessage: i18next.t('common.InvalidData'),// ╠common.InvalidData⇒输入的数据无效!╣
  300. deleteConfirm: i18next.t('message.ConfirmToDelete'),// ╠message.ConfirmToDelete⇒確定要刪除嗎 ?╣
  301. pageIndex: 1,
  302. pageSize: 10000,
  303. fields: [
  304. {
  305. name: "Index", title: '#', width: 50, align: 'center'
  306. },
  307. {// ╠common.Weight_Min⇒重量(小)╣
  308. name: "Weight_Min", title: 'common.Weight_Min', width: 120, align: 'center', type: "text", validate: { validator: 'required', message: i18next.t('common.Weight_Min_required') }//╠common.Weight_Min_required⇒請輸入重量(小)╣
  309. },
  310. {// ╠common.Weight_Max⇒重量(大)╣
  311. name: "Weight_Max", title: 'common.Weight_Max', width: 120, align: 'center', type: "text", validate: { validator: 'required', message: i18next.t('common.Weight_Max_required') }//╠common.Weight_Max_required⇒請輸入重量(大)╣
  312. },
  313. {// ╠common.Price⇒價格╣
  314. name: "Price", title: 'common.Price', width: 120, align: 'right', type: "text", validate: { validator: 'required', message: i18next.t('common.Price_required') },//╠common.Price_required⇒請輸入價格╣
  315. itemTemplate: function (val, item) {
  316. return fMoney(val, 2, item.Currency);
  317. },
  318. insertTemplate: function (val, item) {
  319. var oControl = $('<input type="text" class="form-control w100p" data-type="money" data-name="int" />');
  320. moneyInput(oControl, 2);
  321. return this.insertControl = oControl;
  322. },
  323. insertValue: function () {
  324. return this.insertControl.attr('data-value');
  325. },
  326. editTemplate: function (val, item) {
  327. var oControl = $('<input type="text" class="form-control w100p" data-type="money" data-name="int" />').val(val);
  328. moneyInput(oControl, 2);
  329. return this.editControl = oControl;
  330. },
  331. editValue: function () {
  332. return this.editControl.attr('data-value');
  333. }
  334. },
  335. {// ╠common.PricingMode⇒計價模式╣
  336. name: "PricingMode", title: 'common.PricingMode', align: 'center', width: 180, type: "text",
  337. itemTemplate: function (val, item) {
  338. var oControl = fnCreatePriceModeInput(item.Index);
  339. oControl.find(':input[value="' + val + '"]').click();
  340. uniformInit(oControl);
  341. return oControl;
  342. },
  343. insertTemplate: function (val, item) {
  344. var oControl = fnCreatePriceModeInput('add');
  345. setTimeout(function () {
  346. oControl.find('label:first').click();
  347. uniformInit(oControl);
  348. }, 100);
  349. return this.insertControl = oControl;
  350. },
  351. insertValue: function () {
  352. return this.insertControl.find(':input:checked').val();
  353. },
  354. editTemplate: function (val, item) {
  355. var oControl = fnCreatePriceModeInput('edit');
  356. oControl.find(':input[value="' + val + '"]').click();
  357. uniformInit(oControl);
  358. return this.editControl = oControl;
  359. },
  360. editValue: function () {
  361. return this.editControl.find(':input:checked').val();
  362. }
  363. },
  364. {
  365. name: "Memo", title: 'common.Memo', width: 200, type: "textarea"
  366. },
  367. {
  368. type: "control", width: 50
  369. }
  370. ],
  371. controller: {
  372. loadData: function (args) {
  373. return {
  374. data: oCurData.CostRules,
  375. itemsCount: oCurData.CostRules.length //data.length
  376. };
  377. },
  378. insertItem: function (args) {
  379. args.guid = guid();
  380. args.Index = oCurData.CostRules.length + 1;
  381. oCurData.CostRules.push(args);
  382. },
  383. updateItem: function (args) {
  384. },
  385. deleteItem: function (args) {
  386. oCurData.CostRules = Jsonremove(oCurData.CostRules, 'guid', args.guid);
  387. $.each(oCurData.CostRules, function (idx, _data) {
  388. _data.Index = idx + 1;
  389. });
  390. }
  391. },
  392. onInit: function (args) {
  393. oGrid = args.grid;
  394. }
  395. });
  396. };
  397. init();
  398. };
  399. require(['base', 'jsgrid', 'formatnumber', 'filer', 'util'], fnPageInit);