contract_setting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. 'use strict';
  2. /**
  3. * 项目信息js
  4. *
  5. * @author EllisRan.
  6. * @date 2019/3/19
  7. * @version
  8. */
  9. function setTypeTable(types, type, fun = 'type') {
  10. $('#'+ type + '-'+ fun +'-table').empty();
  11. if (fun === 'used') {
  12. $('#' + type + '-' + fun + '-table').append(`<tr>
  13. <td>合同</td>
  14. <td></td>
  15. </tr>`);
  16. }
  17. types.forEach(t => {
  18. const typeRow = `<tr>
  19. <td><input class="form-control form-control-sm" name="value" placeholder="请输入值" value="${t}"></td>
  20. <td>
  21. <a href="javascript:void(0);" class="btn btn-sm text-danger remove-${fun}-btn"><i class="fa fa-remove"></i></a>
  22. </td>
  23. </tr>`;
  24. $('#'+ type + '-'+ fun +'-table').append(typeRow);
  25. });
  26. }
  27. $(document).ready(() => {
  28. autoFlashHeight();
  29. const funs = ['type', 'used'];
  30. for (const fun of funs) {
  31. $('#add-' + fun + '-btn').click(function(){
  32. const type = $('#' + fun + '-tabs a.active').attr('type');
  33. const newType = `<tr>
  34. <td><input class="form-control form-control-sm" name="value" placeholder="请输入值" value=""></td>
  35. <td>
  36. <a href="javascript:void(0);" class="btn btn-sm text-danger remove-${fun}-btn"><i class="fa fa-remove"></i></a>
  37. </td>
  38. </tr>`;
  39. $('#'+ type + '-' + fun + '-table').append(newType);
  40. });
  41. $('body').on('click', '.remove-' + fun + '-btn', function() {
  42. const input = $(this).parents('td').siblings('td').eq(0).children('input');
  43. input.attr('disabled', true);
  44. // 文字加删除线并移除foucs
  45. if (input.val() === '') {
  46. input.removeAttr('placeholder');
  47. }
  48. input.css('text-decoration', 'line-through');
  49. input.blur();
  50. $(this).remove();
  51. checkAndShowTypesBtn(fun);
  52. });
  53. $('body').on('blur', '#contract-' + fun + '-set input[name="value"]', function() {
  54. checkAndShowTypesBtn(fun);
  55. });
  56. $('#set-' + fun + '-btn').click(function() {
  57. const { new_subProject_types, new_tender_types } = checkAndShowTypesBtn(fun, true);
  58. const data = {
  59. type: 'set-contract-' + fun,
  60. }
  61. if (fun === 'type') {
  62. data.contract_type = {
  63. contract_type: new_subProject_types,
  64. tender_contract_type: new_tender_types
  65. };
  66. } else if (fun === 'used') {
  67. data.contract_used = {
  68. contract_used: new_subProject_types,
  69. tender_contract_used: new_tender_types
  70. }
  71. }
  72. postData(`/sp/${spid}/contract/audit/save`, data, function (res) {
  73. toastr.success('设置成功');
  74. if (fun === 'type') {
  75. tender_type = new_tender_types;
  76. subProject_type = new_subProject_types;
  77. } else if (fun === 'used') {
  78. tender_used = new_tender_types;
  79. subProject_used = new_subProject_types;
  80. }
  81. setTypeTable(new_tender_types, 'tender', fun);
  82. setTypeTable(new_subProject_types, 'subProject', fun);
  83. checkAndShowTypesBtn(fun);
  84. });
  85. });
  86. $('#cancel-' + fun + '-btn').click(function() {
  87. toastr.warning('已取消改动');
  88. if (fun === 'type') {
  89. setTypeTable(tender_type, 'tender', fun);
  90. setTypeTable(subProject_type, 'subProject', fun);
  91. } else if (fun === 'used') {
  92. setTypeTable(tender_used, 'tender', fun);
  93. setTypeTable(subProject_used, 'subProject', fun);
  94. }
  95. checkAndShowTypesBtn(fun);
  96. });
  97. }
  98. function checkAndShowTypesBtn(fun, return_type = false) {
  99. const new_subProject_types = [];
  100. const new_tender_types = [];
  101. const type = ['subProject', 'tender'];
  102. for (const t of type) {
  103. $('#'+ t + '-' + fun + '-table tr').each(function () {
  104. const input = $(this).find('input[name="value"]');
  105. console.log(input.val());
  106. if (input && input.val() !== undefined && !input.prop('disabled')) {
  107. const value = input.val().trim();
  108. if (value) {
  109. if (t === 'subProject') {
  110. new_subProject_types.push(value);
  111. } else {
  112. new_tender_types.push(value);
  113. }
  114. }
  115. }
  116. });
  117. }
  118. if (fun === 'type') {
  119. // 判断数组是否相同,但别改变原数组顺序
  120. if (_.isEqual(_.sortBy(new_tender_types), _.sortBy(tender_type)) && _.isEqual(_.sortBy(new_subProject_types), _.sortBy(subProject_type))) {
  121. $('#show-'+ fun +'-btn').hide();
  122. } else {
  123. $('#show-' + fun + '-btn').show();
  124. }
  125. } else if (fun === 'used') {
  126. if (_.isEqual(_.sortBy(new_tender_types), _.sortBy(tender_used)) && _.isEqual(_.sortBy(new_subProject_types), _.sortBy(subProject_used))) {
  127. $('#show-'+ fun +'-btn').hide();
  128. } else {
  129. $('#show-' + fun + '-btn').show();
  130. }
  131. }
  132. if (return_type) {
  133. return {
  134. new_subProject_types,
  135. new_tender_types
  136. };
  137. }
  138. }
  139. // 审批设置
  140. $('#contract-shenpi-set input[type="checkbox"]').change(function() {
  141. postData(`/sp/${spid}/contract/setting/update`, { type: 'page_show',
  142. openContractSubProjectShenpi: $('#openContractSubProjectShenpi')[0].checked,
  143. openContractPaySubProjectShenpi: $('#openContractPaySubProjectShenpi')[0].checked,
  144. openContractTenderShenpi: $('#openContractTenderShenpi')[0].checked,
  145. openContractPayTenderShenpi: $('#openContractPayTenderShenpi')[0].checked,
  146. }, function (result) {
  147. });
  148. });
  149. // 附加属性设置
  150. // 立即获取表格 tbody 并绑定事件(确保脚本加载时即可初始化)
  151. const $tbody = $('#attribute-set-table');
  152. // 保存初始 HTML,在关闭时恢复(保证再次打开时为初始状态)
  153. let _initialColSetTbodyHtml = $tbody.html();
  154. // 更新上下移链接显示状态
  155. function updateMoveButtons() {
  156. const $currentRows = $tbody.find('tr');
  157. const totalRows = $currentRows.length;
  158. $currentRows.each(function(index) {
  159. const $row = $(this);
  160. const $upLink = $row.find('.move-up');
  161. const $downLink = $row.find('.move-down');
  162. if (totalRows === 1) {
  163. $upLink.hide();
  164. $downLink.hide();
  165. } else if (index === 0) {
  166. $upLink.hide();
  167. $downLink.show();
  168. } else if (index === totalRows - 1) {
  169. $upLink.show();
  170. $downLink.hide();
  171. } else {
  172. $upLink.show();
  173. $downLink.show();
  174. }
  175. });
  176. checkAttribute();
  177. }
  178. function checkAttribute() {
  179. const $currentRows = $('#attribute-set-table').find('tr');
  180. const attribute_set = [];
  181. $currentRows.each(function() {
  182. const $row = $(this);
  183. let alias = '';
  184. const $aliasInput = $row.find('input[type="text"]');
  185. if ($aliasInput.length) {
  186. alias = $aliasInput.val().trim();
  187. }
  188. // 仅组装数据库需要的3个字段
  189. const rowData = {
  190. field: $row.attr('code'), // 字段标识(必填)
  191. show: Number($row.find('.form-check-input').is(':checked')), // 是否显示(必填)
  192. alias: alias // 别名(必填,固定列保存"-")
  193. };
  194. attribute_set.push(rowData); // 所有行按顺序存入数组,顺序=表格排序
  195. });
  196. // 和 attributeSet对比部分值和顺序,如果不同则返回true,否则返回false
  197. const defaultAttr = [];
  198. for (const attr of attributeSet) {
  199. defaultAttr.push({
  200. field: attr.field,
  201. show: attr.show,
  202. alias: attr.alias || '' // 确保默认值也有 alias 字段,且空值为 '-'
  203. });
  204. }
  205. console.log(attribute_set, defaultAttr);
  206. if (_.isEqual(attribute_set, defaultAttr)) {
  207. $('#show-attribute-btn').hide();
  208. $('#cancel-attribute-btn').hide();
  209. } else {
  210. $('#show-attribute-btn').show();
  211. $('#cancel-attribute-btn').show();
  212. }
  213. }
  214. // 立即初始化链接状态(页面渲染完成后就计算显示)
  215. updateMoveButtons();
  216. $tbody.on('change', 'input', function () {
  217. checkAttribute();
  218. });
  219. $('#set-attribute-btn').click(function() {
  220. const $currentRows = $('#attribute-set-table').find('tr');
  221. const attribute_set = [];
  222. $currentRows.each(function() {
  223. const $row = $(this);
  224. let alias = '';
  225. const $aliasInput = $row.find('input[type="text"]');
  226. if ($aliasInput.length) {
  227. alias = $aliasInput.val().trim();
  228. }
  229. // 仅组装数据库需要的3个字段
  230. const rowData = {
  231. field: $row.attr('code'), // 字段标识(必填)
  232. show: Number($row.find('.form-check-input').is(':checked')), // 是否显示(必填)
  233. alias: alias // 别名(必填,固定列保存"-")
  234. };
  235. attribute_set.push(rowData); // 所有行按顺序存入数组,顺序=表格排序
  236. });
  237. console.log(attribute_set);
  238. postData(`/sp/${spid}/contract/setting/update`, { type: 'attribute_set',
  239. attribute_set,
  240. }, function (result) {
  241. toastr.success('设置成功');
  242. attributeSet = result;
  243. _initialColSetTbodyHtml = $tbody.html(); // 更新初始 HTML 为当前状态(保存后以当前状态为初始状态)
  244. checkAttribute();
  245. });
  246. });
  247. $('#cancel-attribute-btn').click(function() {
  248. toastr.warning('已取消改动');
  249. $tbody.html(_initialColSetTbodyHtml); // 恢复初始 HTML
  250. updateMoveButtons(); // 恢复后重新计算链接显示状态
  251. });
  252. // 事件委托:从一开始就绑定上/下移事件
  253. $tbody.on('click', '.move-up', function(e) {
  254. e.preventDefault();
  255. const $currentRow = $(this).closest('tr');
  256. const $prevRow = $currentRow.prev('tr');
  257. if ($prevRow.length) {
  258. $currentRow.insertBefore($prevRow);
  259. updateMoveButtons();
  260. }
  261. });
  262. $tbody.on('click', '.move-down', function(e) {
  263. e.preventDefault();
  264. const $currentRow = $(this).closest('tr');
  265. const $nextRow = $currentRow.next('tr');
  266. if ($nextRow.length) {
  267. $currentRow.insertAfter($nextRow);
  268. updateMoveButtons();
  269. }
  270. });
  271. });