compilation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. colSpread = TREE_SHEET_HELPER.createNewSpread($('#main-tree-col')[0]);
  235. let billsTemplateTree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1});
  236. billsTemplateTree.loadDatas(JSON.parse(billsTemplateData));
  237. TREE_SHEET_HELPER.loadSheetHeader(JSON.parse(mainTreeCol), colSpread.getActiveSheet());
  238. TREE_SHEET_HELPER.showTreeData(JSON.parse(mainTreeCol), colSpread.getActiveSheet(), billsTemplateTree);
  239. // 标准清单
  240. let html = '';
  241. for(let tmp of billListData) {
  242. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  243. html += tmpHtml;
  244. }
  245. $("select[name='standard_bill']").children("option").first().after(html);
  246. // 定额库
  247. html = '';
  248. for(let tmp of rationLibData) {
  249. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  250. html += tmpHtml;
  251. }
  252. $("select[name='ration_lib']").children("option").first().after(html);
  253. // 工料机库
  254. html = '';
  255. for(let tmp of gljLibData) {
  256. let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
  257. html += tmpHtml;
  258. }
  259. $("select[name='glj_lib']").children("option").first().after(html);
  260. }
  261. /**
  262. * 校验数据
  263. *
  264. * @param {String} model
  265. * @return {Array}
  266. */
  267. function getAndValidData(model) {
  268. let name = $("input[name='compilation_name']").val();
  269. let standardBill = $("select[name='standard_bill']").children("option:selected").val();
  270. let rationLib = $("select[name='ration_lib']").children("option:selected").val();
  271. let gljLib = $("select[name='glj_lib']").children("option:selected").val();
  272. if (name === '' && model === 'all') {
  273. throw '编办名字不能为空';
  274. }
  275. if ( model === 'bill' && (standardBill === '' || standardBill === undefined)) {
  276. throw '请选择标准清单库';
  277. }
  278. if (model === 'ration' && (rationLib === '' || rationLib === undefined)) {
  279. throw '请选择定额库';
  280. }
  281. if (model === 'glj' && (gljLib === '' || gljLib === undefined)) {
  282. throw '请选择工料机库';
  283. }
  284. let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
  285. let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
  286. let gljLibString = $("select[name='glj_lib']").children("option:selected").text();
  287. return [name, standardBill, rationLib, gljLib, standardBillString, rationLibString, gljLibString];
  288. }
  289. /**
  290. * 验证计价规则数据
  291. *
  292. * @return {boolean}
  293. */
  294. function validValuation() {
  295. let result = false;
  296. try {
  297. let valuationName = $("input[name='name']").val();
  298. if (valuationName === '') {
  299. throw '请填写计价规则名称';
  300. }
  301. let engineering = $("select[name='engineering']").val();
  302. if (engineering === '' || engineering <= 0) {
  303. throw '请选择工程专业';
  304. }
  305. if ($("input:hidden[name='bill_lib']").length <= 0) {
  306. throw '请添加标准清单';
  307. }
  308. if ($("input:hidden[name='ration_lib']").length <= 0) {
  309. throw '请添加定额库';
  310. }
  311. result = true;
  312. } catch (error) {
  313. alert(error);
  314. result = false;
  315. }
  316. return result;
  317. }
  318. /**
  319. * 切换switch效果
  320. *
  321. * @param {Object} element
  322. * @return {boolean}
  323. */
  324. function switchChange(element) {
  325. // 第一个元素判断当前的状态
  326. let firstButton = element.children("button").first();
  327. let secondButton = element.children("button").eq(1);
  328. let currentStatus = firstButton.is(":disabled");
  329. if (currentStatus) {
  330. // 当前为true切换到false
  331. firstButton.removeClass('btn-success').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  332. firstButton.text('开启');
  333. secondButton.removeClass("btn-default").addClass("btn-danger").addClass("disabled").attr("disabled", "disabled");
  334. secondButton.text('已禁用');
  335. } else {
  336. // 当前false切换到true
  337. firstButton.removeClass("btn-default").addClass("btn-success").addClass("disabled").attr("disabled", "disabled");
  338. firstButton.text('已开启');
  339. secondButton.removeClass('btn-danger').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
  340. secondButton.text('禁用');
  341. }
  342. return !currentStatus;
  343. }