compilation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**
  2. * 编办管理相关js
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/28
  6. * @version
  7. */
  8. $(document).ready(function() {
  9. let isAdding = false;
  10. let model = '';
  11. let section = 'bill';
  12. let id = $("#compilation-id").val();
  13. // 计价规则页面初始化数据
  14. if ($("#section").length > 0) {
  15. initCompilation();
  16. }
  17. // 计价类型选择
  18. $(".nav-tabs li > a").click(function() {
  19. section = $(this).attr("aria-controls");
  20. });
  21. // 新增编办
  22. $("#add-compilation").click(function() {
  23. try {
  24. let [name, standardBill, rationLib, standardBillString, rationLibString] = getAndValidData(model);
  25. let url = '/compilation/add';
  26. if (model === 'all') {
  27. // 新增编办操作
  28. $.ajax({
  29. url: url,
  30. type: 'post',
  31. data: {name: name},
  32. error: function() {
  33. isAdding = false;
  34. },
  35. beforeSend: function() {
  36. isAdding = true;
  37. },
  38. success: function(response) {
  39. isAdding = false;
  40. if (response.err === 0) {
  41. window.location.reload();
  42. } else {
  43. let msg = response.msg === undefined ? '未知错误' : response.msg;
  44. alert(msg);
  45. }
  46. }
  47. });
  48. } else {
  49. // 新增标准清单/定额库
  50. let addLib = {
  51. name: '',
  52. id: ''
  53. };
  54. switch (model) {
  55. case 'bill':
  56. addLib.name = standardBillString;
  57. addLib.id = standardBill;
  58. break;
  59. case 'ration':
  60. addLib.name = rationLibString;
  61. addLib.id = rationLib;
  62. break;
  63. }
  64. // 判断是否有重复的数据
  65. if ($("input:hidden[name='"+ model +"_lib'][data-id='"+ addLib.id +"']").length > 0) {
  66. alert('重复添加数据!');
  67. return false;
  68. }
  69. let removeHtml = '<a class="pull-right text-danger remove-lib" data-model="bill" ' +
  70. 'title="移除"><span class="glyphicon glyphicon-remove"></span></a>';
  71. let tmpHtml = '<p class="form-control-static">' + removeHtml + addLib.name +
  72. '<input type="hidden" data-id="'+ addLib.id +'" name=\'' + model + '_lib\' value=\'' + JSON.stringify(addLib) + '\'>' + '</p>';
  73. $("." + model + "-list").append(tmpHtml);
  74. $('#addcompilation').modal('hide');
  75. }
  76. } catch (error) {
  77. alert(error);
  78. }
  79. });
  80. // 新增计价规则
  81. $("#add-valuation").click(function() {
  82. try {
  83. if (id === '') {
  84. throw '页面数据有误';
  85. }
  86. let name = $("input[name='valuation_name']").val();
  87. if (name === '') {
  88. throw '请填写计价规则名称';
  89. }
  90. $.ajax({
  91. url: '/compilation/add-valuation',
  92. type: 'post',
  93. data: {name: name, id: id, section: section},
  94. error: function() {
  95. isAdding = false;
  96. },
  97. beforeSend: function() {
  98. isAdding = true;
  99. },
  100. success: function(response) {
  101. isAdding = false;
  102. if (response.err === 0) {
  103. window.location.reload();
  104. } else {
  105. let msg = response.msg === undefined ? '未知错误' : response.msg;
  106. alert(msg);
  107. }
  108. }
  109. });
  110. } catch (error) {
  111. alert(error);
  112. return false;
  113. }
  114. });
  115. // 添加
  116. $(".add-compilation").click(function() {
  117. model = $(this).data('model');
  118. switch (model) {
  119. case 'all':
  120. $("#name-area").show();
  121. $("#bill-area").hide();
  122. $("#ration-area").hide();
  123. $("#add-compilation-title").text('添加新编办');
  124. break;
  125. case 'bill':
  126. $("#name-area").hide();
  127. $("#bill-area").show();
  128. $("#ration-area").hide();
  129. $("#add-compilation-title").text('添加标准清单');
  130. break;
  131. case 'ration':
  132. $("#name-area").hide();
  133. $("#bill-area").hide();
  134. $("#ration-area").show();
  135. $("#add-compilation-title").text('添加定额库');
  136. break;
  137. }
  138. $("#addcompilation").modal('show');
  139. });
  140. // 保存计价规则
  141. $("#save-valuation").click(function() {
  142. if (validValuation()) {
  143. $("form").submit();
  144. }
  145. });
  146. // 移除操作
  147. $(".bill-list, .ration-list").on("click", ".remove-lib", function() {
  148. $(this).parent().remove();
  149. });
  150. // 计价规则启用/禁止
  151. $(".enable").click(function() {
  152. let goingChangeStatus = switchChange($(this));
  153. let id = $(this).data('id');
  154. if (id === undefined || id === '' || isAdding) {
  155. return false;
  156. }
  157. $.ajax({
  158. url: '/compilation/valuation/' + section + '/enable',
  159. type: 'post',
  160. dataType: "json",
  161. data: {id: id, enable: goingChangeStatus},
  162. error: function() {
  163. isAdding = false;
  164. switchChange($(this));
  165. },
  166. beforeSend: function() {
  167. isAdding = true;
  168. },
  169. success: function(response) {
  170. isAdding = false;
  171. if (response.err !== 0) {
  172. switchChange($(this));
  173. alert('更改失败');
  174. }
  175. }
  176. });
  177. });
  178. // 发布编办
  179. $("#release").click(function() {
  180. let id = $(this).data("id");
  181. let status = $(this).data("status");
  182. status = parseInt(status);
  183. if (isAdding || id === '' || isNaN(status)) {
  184. return false;
  185. }
  186. $.ajax({
  187. url: '/compilation/release',
  188. type: 'post',
  189. data: {id: id, status: status},
  190. dataType: "json",
  191. error: function() {
  192. isAdding = false;
  193. },
  194. beforeSend: function() {
  195. isAdding = true;
  196. },
  197. success: function(response) {
  198. isAdding = false;
  199. if (response.err === 0) {
  200. window.location.reload();
  201. } else {
  202. let msg = response.msg === undefined ? "未知错误" : response.msg;
  203. alert(msg);
  204. }
  205. }
  206. });
  207. });
  208. });
  209. /**
  210. * 初始化
  211. *
  212. * @return {void|boolean}
  213. */
  214. function initCompilation() {
  215. let billListData = billList === undefined ? [] : JSON.parse(billList);
  216. let rationLibData = rationList === undefined ? [] : JSON.parse(rationList);
  217. // 初始化 造价书列设置
  218. colSpread = SheetDataHelper.createNewSpread($('#main-tree-col')[0]);
  219. SheetDataHelper.loadSheetHeader(JSON.parse(mainTreeCol), colSpread.getActiveSheet());
  220. if (billListData.length <= 0 || rationLibData.length <= 0) {
  221. return false;
  222. }
  223. // 标准清单
  224. let html = '';
  225. for(let tmp of billListData) {
  226. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  227. html += tmpHtml;
  228. }
  229. $("select[name='standard_bill']").children("option").first().after(html);
  230. // 定额库
  231. html = '';
  232. for(let tmp of rationLibData) {
  233. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  234. html += tmpHtml;
  235. }
  236. $("select[name='ration_lib']").children("option").first().after(html);
  237. }
  238. /**
  239. * 校验数据
  240. *
  241. * @param {String} model
  242. * @return {Array}
  243. */
  244. function getAndValidData(model) {
  245. let name = $("input[name='compilation_name']").val();
  246. let standardBill = $("select[name='standard_bill']").children("option:selected").val();
  247. let rationLib = $("select[name='ration_lib']").children("option:selected").val();
  248. if (name === '' && model === 'all') {
  249. throw '编办名字不能为空';
  250. }
  251. if ( model === 'bill' && (standardBill === '' || standardBill === undefined)) {
  252. throw '请选择标准清单库';
  253. }
  254. if (model === 'ration' && (rationLib === '' || rationLib === undefined)) {
  255. throw '请选择定额库';
  256. }
  257. let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
  258. let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
  259. return [name, standardBill, rationLib, standardBillString, rationLibString];
  260. }
  261. /**
  262. * 验证计价规则数据
  263. *
  264. * @return {boolean}
  265. */
  266. function validValuation() {
  267. let result = false;
  268. try {
  269. let valuationName = $("input[name='name']").val();
  270. if (valuationName === '') {
  271. throw '请填写计价规则名称';
  272. }
  273. let engineering = $("select[name='engineering']").val();
  274. if (engineering === '' || engineering <= 0) {
  275. throw '请选择工程专业';
  276. }
  277. if ($("input:hidden[name='bill_lib']").length <= 0) {
  278. throw '请添加标准清单';
  279. }
  280. if ($("input:hidden[name='ration_lib']").length <= 0) {
  281. throw '请添加定额库';
  282. }
  283. result = true;
  284. } catch (error) {
  285. alert(error);
  286. result = false;
  287. }
  288. return result;
  289. }
  290. /**
  291. * 切换switch效果
  292. *
  293. * @param {Object} element
  294. * @return {boolean}
  295. */
  296. function switchChange(element) {
  297. // 第一个元素判断当前的状态
  298. let firstButton = element.children("button").first();
  299. let secondButton = element.children("button").eq(1);
  300. let currentStatus = firstButton.is(":disabled");
  301. if (currentStatus) {
  302. // 当前为true切换到false
  303. firstButton.removeClass('btn-success').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  304. firstButton.text('开启');
  305. secondButton.removeClass("btn-default").addClass("btn-danger").addClass("disabled").attr("disabled", "disabled");
  306. secondButton.text('已禁用');
  307. } else {
  308. // 当前false切换到true
  309. firstButton.removeClass("btn-default").addClass("btn-success").addClass("disabled").attr("disabled", "disabled");
  310. firstButton.text('已开启');
  311. secondButton.removeClass('btn-danger').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  312. secondButton.text('禁用');
  313. }
  314. return !currentStatus;
  315. }