compilation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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, gljLib, standardBillString, rationLibString, gljLibString] = 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. case 'glj':
  64. addLib.name = gljLibString;
  65. addLib.id = gljLib;
  66. }
  67. // 判断是否有重复的数据
  68. if ($("input:hidden[name='"+ model +"_lib'][data-id='"+ addLib.id +"']").length > 0) {
  69. alert('重复添加数据!');
  70. return false;
  71. }
  72. let removeHtml = '<a class="pull-right text-danger remove-lib" data-model="bill" ' +
  73. 'title="移除"><span class="glyphicon glyphicon-remove"></span></a>';
  74. let tmpHtml = '<p class="form-control-static">' + removeHtml + addLib.name +
  75. '<input type="hidden" data-id="'+ addLib.id +'" name=\'' + model + '_lib\' value=\'' + JSON.stringify(addLib) + '\'>' + '</p>';
  76. $("." + model + "-list").append(tmpHtml);
  77. $('#addcompilation').modal('hide');
  78. }
  79. } catch (error) {
  80. alert(error);
  81. }
  82. });
  83. // 新增计价规则
  84. $("#add-valuation").click(function() {
  85. try {
  86. if (id === '') {
  87. throw '页面数据有误';
  88. }
  89. let name = $("input[name='valuation_name']").val();
  90. if (name === '') {
  91. throw '请填写计价规则名称';
  92. }
  93. $.ajax({
  94. url: '/compilation/add-valuation',
  95. type: 'post',
  96. data: {name: name, id: id, section: section},
  97. error: function() {
  98. isAdding = false;
  99. },
  100. beforeSend: function() {
  101. isAdding = true;
  102. },
  103. success: function(response) {
  104. isAdding = false;
  105. if (response.err === 0) {
  106. window.location.reload();
  107. } else {
  108. let msg = response.msg === undefined ? '未知错误' : response.msg;
  109. alert(msg);
  110. }
  111. }
  112. });
  113. } catch (error) {
  114. alert(error);
  115. return false;
  116. }
  117. });
  118. // 添加
  119. $(".add-compilation").click(function() {
  120. model = $(this).data('model');
  121. switch (model) {
  122. case 'all':
  123. $("#name-area").show();
  124. $("#bill-area").hide();
  125. $("#ration-area").hide();
  126. $("#glj-area").hide();
  127. $("#add-compilation-title").text('添加新编办');
  128. break;
  129. case 'bill':
  130. $("#name-area").hide();
  131. $("#bill-area").show();
  132. $("#ration-area").hide();
  133. $("#glj-area").hide();
  134. $("#add-compilation-title").text('添加标准清单');
  135. break;
  136. case 'ration':
  137. $("#name-area").hide();
  138. $("#bill-area").hide();
  139. $("#ration-area").show();
  140. $("#glj-area").hide();
  141. $("#add-compilation-title").text('添加定额库');
  142. break;
  143. case 'glj':
  144. $("#name-area").hide();
  145. $("#bill-area").hide();
  146. $("#ration-area").hide();
  147. $("#glj-area").show();
  148. $("#add-compilation-title").text('添加定额库');
  149. }
  150. $("#addcompilation").modal('show');
  151. });
  152. // 保存计价规则
  153. $("#save-valuation").click(function() {
  154. if (validValuation()) {
  155. $("form").submit();
  156. }
  157. });
  158. // 移除操作
  159. $(".bill-list, .ration-list").on("click", ".remove-lib", function() {
  160. $(this).parent().remove();
  161. });
  162. // 计价规则启用/禁止
  163. $(".enable").click(function() {
  164. let goingChangeStatus = switchChange($(this));
  165. let id = $(this).data('id');
  166. if (id === undefined || id === '' || isAdding) {
  167. return false;
  168. }
  169. $.ajax({
  170. url: '/compilation/valuation/' + section + '/enable',
  171. type: 'post',
  172. dataType: "json",
  173. data: {id: id, enable: goingChangeStatus},
  174. error: function() {
  175. isAdding = false;
  176. switchChange($(this));
  177. },
  178. beforeSend: function() {
  179. isAdding = true;
  180. },
  181. success: function(response) {
  182. isAdding = false;
  183. if (response.err !== 0) {
  184. switchChange($(this));
  185. alert('更改失败');
  186. }
  187. }
  188. });
  189. });
  190. // 发布编办
  191. $("#release").click(function() {
  192. let id = $(this).data("id");
  193. let status = $(this).data("status");
  194. status = parseInt(status);
  195. if (isAdding || id === '' || isNaN(status)) {
  196. return false;
  197. }
  198. $.ajax({
  199. url: '/compilation/release',
  200. type: 'post',
  201. data: {id: id, status: status},
  202. dataType: "json",
  203. error: function() {
  204. isAdding = false;
  205. },
  206. beforeSend: function() {
  207. isAdding = true;
  208. },
  209. success: function(response) {
  210. isAdding = false;
  211. if (response.err === 0) {
  212. window.location.reload();
  213. } else {
  214. let msg = response.msg === undefined ? "未知错误" : response.msg;
  215. alert(msg);
  216. }
  217. }
  218. });
  219. });
  220. });
  221. /**
  222. * 初始化
  223. *
  224. * @return {void|boolean}
  225. */
  226. function initCompilation() {
  227. let billListData = billList === undefined ? [] : JSON.parse(billList);
  228. let rationLibData = rationList === undefined ? [] : JSON.parse(rationList);
  229. let gljLibData = gljList === undefined ? [] : JSON.parse(gljList);
  230. if (billListData.length <= 0 || rationLibData.length <= 0 || gljLibData.length <= 0) {
  231. return false;
  232. }
  233. // 标准清单
  234. let html = '';
  235. for(let tmp of billListData) {
  236. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  237. html += tmpHtml;
  238. }
  239. $("select[name='standard_bill']").children("option").first().after(html);
  240. // 定额库
  241. html = '';
  242. for(let tmp of rationLibData) {
  243. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  244. html += tmpHtml;
  245. }
  246. $("select[name='ration_lib']").children("option").first().after(html);
  247. // 工料机库
  248. html = '';
  249. for(let tmp of gljLibData) {
  250. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  251. html += tmpHtml;
  252. }
  253. $("select[name='glj_lib']").children("option").first().after(html);
  254. }
  255. /**
  256. * 校验数据
  257. *
  258. * @param {String} model
  259. * @return {Array}
  260. */
  261. function getAndValidData(model) {
  262. let name = $("input[name='compilation_name']").val();
  263. let standardBill = $("select[name='standard_bill']").children("option:selected").val();
  264. let rationLib = $("select[name='ration_lib']").children("option:selected").val();
  265. let gljLib = $("select[name='glj_lib']").children("option:selected").val();
  266. if (name === '' && model === 'all') {
  267. throw '编办名字不能为空';
  268. }
  269. if ( model === 'bill' && (standardBill === '' || standardBill === undefined)) {
  270. throw '请选择标准清单库';
  271. }
  272. if (model === 'ration' && (rationLib === '' || rationLib === undefined)) {
  273. throw '请选择定额库';
  274. }
  275. if (model === 'glj' && (gljLib === '' || gljLib === undefined)) {
  276. throw '请选择工料机库';
  277. }
  278. let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
  279. let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
  280. let gljLibString = $("select[name='glj_lib']").children("option:selected").text();
  281. return [name, standardBill, rationLib, gljLib, standardBillString, rationLibString, gljLibString];
  282. }
  283. /**
  284. * 验证计价规则数据
  285. *
  286. * @return {boolean}
  287. */
  288. function validValuation() {
  289. let result = false;
  290. try {
  291. let valuationName = $("input[name='name']").val();
  292. if (valuationName === '') {
  293. throw '请填写计价规则名称';
  294. }
  295. let engineering = $("select[name='engineering']").val();
  296. if (engineering === '' || engineering <= 0) {
  297. throw '请选择工程专业';
  298. }
  299. if ($("input:hidden[name='bill_lib']").length <= 0) {
  300. throw '请添加标准清单';
  301. }
  302. if ($("input:hidden[name='ration_lib']").length <= 0) {
  303. throw '请添加定额库';
  304. }
  305. result = true;
  306. } catch (error) {
  307. alert(error);
  308. result = false;
  309. }
  310. return result;
  311. }
  312. /**
  313. * 切换switch效果
  314. *
  315. * @param {Object} element
  316. * @return {boolean}
  317. */
  318. function switchChange(element) {
  319. // 第一个元素判断当前的状态
  320. let firstButton = element.children("button").first();
  321. let secondButton = element.children("button").eq(1);
  322. let currentStatus = firstButton.is(":disabled");
  323. if (currentStatus) {
  324. // 当前为true切换到false
  325. firstButton.removeClass('btn-success').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  326. firstButton.text('开启');
  327. secondButton.removeClass("btn-default").addClass("btn-danger").addClass("disabled").attr("disabled", "disabled");
  328. secondButton.text('已禁用');
  329. } else {
  330. // 当前false切换到true
  331. firstButton.removeClass("btn-default").addClass("btn-success").addClass("disabled").attr("disabled", "disabled");
  332. firstButton.text('已开启');
  333. secondButton.removeClass('btn-danger').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  334. secondButton.text('禁用');
  335. }
  336. return !currentStatus;
  337. }