123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- /**
- * 编办管理相关js
- *
- * @author CaiAoLin
- * @date 2017/7/28
- * @version
- */
- $(document).ready(function() {
- let isAdding = false;
- let model = '';
- let section = 'bill';
- let id = $("#compilation-id").val();
- // 计价规则页面初始化数据
- if ($("#section").length > 0) {
- initCompilation();
- }
- // 计价类型选择
- $(".nav-tabs li > a").click(function() {
- section = $(this).attr("aria-controls");
- });
- // 新增编办
- $("#add-compilation").click(function() {
- try {
- let [name, standardBill, rationLib, gljLib, standardBillString, rationLibString, gljLibString] = getAndValidData(model);
- let url = '/compilation/add';
- if (model === 'all') {
- // 新增编办操作
- $.ajax({
- url: url,
- type: 'post',
- data: {name: name},
- error: function() {
- isAdding = false;
- },
- beforeSend: function() {
- isAdding = true;
- },
- success: function(response) {
- isAdding = false;
- if (response.err === 0) {
- window.location.reload();
- } else {
- let msg = response.msg === undefined ? '未知错误' : response.msg;
- alert(msg);
- }
- }
- });
- } else {
- // 新增标准清单/定额库
- let addLib = {
- name: '',
- id: ''
- };
- switch (model) {
- case 'bill':
- addLib.name = standardBillString;
- addLib.id = standardBill;
- break;
- case 'ration':
- addLib.name = rationLibString;
- addLib.id = rationLib;
- break;
- case 'glj':
- addLib.name = gljLibString;
- addLib.id = gljLib;
- }
- // 判断是否有重复的数据
- if ($("input:hidden[name='"+ model +"_lib'][data-id='"+ addLib.id +"']").length > 0) {
- alert('重复添加数据!');
- return false;
- }
- let removeHtml = '<a class="pull-right text-danger remove-lib" data-model="bill" ' +
- 'title="移除"><span class="glyphicon glyphicon-remove"></span></a>';
- let tmpHtml = '<p class="form-control-static">' + removeHtml + addLib.name +
- '<input type="hidden" data-id="'+ addLib.id +'" name=\'' + model + '_lib\' value=\'' + JSON.stringify(addLib) + '\'>' + '</p>';
- $("." + model + "-list").append(tmpHtml);
- $('#addcompilation').modal('hide');
- }
- } catch (error) {
- alert(error);
- }
- });
- // 新增计价规则
- $("#add-valuation").click(function() {
- try {
- if (id === '') {
- throw '页面数据有误';
- }
- let name = $("input[name='valuation_name']").val();
- if (name === '') {
- throw '请填写计价规则名称';
- }
- $.ajax({
- url: '/compilation/add-valuation',
- type: 'post',
- data: {name: name, id: id, section: section},
- error: function() {
- isAdding = false;
- },
- beforeSend: function() {
- isAdding = true;
- },
- success: function(response) {
- isAdding = false;
- if (response.err === 0) {
- window.location.reload();
- } else {
- let msg = response.msg === undefined ? '未知错误' : response.msg;
- alert(msg);
- }
- }
- });
- } catch (error) {
- alert(error);
- return false;
- }
- });
- // 添加
- $(".add-compilation").click(function() {
- model = $(this).data('model');
- switch (model) {
- case 'all':
- $("#name-area").show();
- $("#bill-area").hide();
- $("#ration-area").hide();
- $("#glj-area").hide();
- $("#add-compilation-title").text('添加新编办');
- break;
- case 'bill':
- $("#name-area").hide();
- $("#bill-area").show();
- $("#ration-area").hide();
- $("#glj-area").hide();
- $("#add-compilation-title").text('添加标准清单');
- break;
- case 'ration':
- $("#name-area").hide();
- $("#bill-area").hide();
- $("#ration-area").show();
- $("#glj-area").hide();
- $("#add-compilation-title").text('添加定额库');
- break;
- case 'glj':
- $("#name-area").hide();
- $("#bill-area").hide();
- $("#ration-area").hide();
- $("#glj-area").show();
- $("#add-compilation-title").text('添加定额库');
- }
- $("#addcompilation").modal('show');
- });
- // 保存计价规则
- $("#save-valuation").click(function() {
- if (validValuation()) {
- $("form").submit();
- }
- });
- // 移除操作
- $(".bill-list, .ration-list").on("click", ".remove-lib", function() {
- $(this).parent().remove();
- });
- // 计价规则启用/禁止
- $(".enable").click(function() {
- let goingChangeStatus = switchChange($(this));
- let id = $(this).data('id');
- if (id === undefined || id === '' || isAdding) {
- return false;
- }
- $.ajax({
- url: '/compilation/valuation/' + section + '/enable',
- type: 'post',
- dataType: "json",
- data: {id: id, enable: goingChangeStatus},
- error: function() {
- isAdding = false;
- switchChange($(this));
- },
- beforeSend: function() {
- isAdding = true;
- },
- success: function(response) {
- isAdding = false;
- if (response.err !== 0) {
- switchChange($(this));
- alert('更改失败');
- }
- }
- });
- });
- // 发布编办
- $("#release").click(function() {
- let id = $(this).data("id");
- let status = $(this).data("status");
- status = parseInt(status);
- if (isAdding || id === '' || isNaN(status)) {
- return false;
- }
- $.ajax({
- url: '/compilation/release',
- type: 'post',
- data: {id: id, status: status},
- dataType: "json",
- error: function() {
- isAdding = false;
- },
- beforeSend: function() {
- isAdding = true;
- },
- success: function(response) {
- isAdding = false;
- if (response.err === 0) {
- window.location.reload();
- } else {
- let msg = response.msg === undefined ? "未知错误" : response.msg;
- alert(msg);
- }
- }
- });
- });
- });
- /**
- * 初始化
- *
- * @return {void|boolean}
- */
- function initCompilation() {
- let billListData = billList === undefined ? [] : JSON.parse(billList);
- let rationLibData = rationList === undefined ? [] : JSON.parse(rationList);
- let gljLibData = gljList === undefined ? [] : JSON.parse(gljList);
- if (billListData.length <= 0 || rationLibData.length <= 0 || gljLibData.length <= 0) {
- return false;
- }
- // 标准清单
- let html = '';
- for(let tmp of billListData) {
- let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
- html += tmpHtml;
- }
- $("select[name='standard_bill']").children("option").first().after(html);
- // 定额库
- html = '';
- for(let tmp of rationLibData) {
- let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
- html += tmpHtml;
- }
- $("select[name='ration_lib']").children("option").first().after(html);
- // 工料机库
- html = '';
- for(let tmp of gljLibData) {
- let tmpHtml = '<option value="' + tmp.id + '">' + tmp.name + '</option>';
- html += tmpHtml;
- }
- $("select[name='glj_lib']").children("option").first().after(html);
- }
- /**
- * 校验数据
- *
- * @param {String} model
- * @return {Array}
- */
- function getAndValidData(model) {
- let name = $("input[name='compilation_name']").val();
- let standardBill = $("select[name='standard_bill']").children("option:selected").val();
- let rationLib = $("select[name='ration_lib']").children("option:selected").val();
- let gljLib = $("select[name='glj_lib']").children("option:selected").val();
- if (name === '' && model === 'all') {
- throw '编办名字不能为空';
- }
- if ( model === 'bill' && (standardBill === '' || standardBill === undefined)) {
- throw '请选择标准清单库';
- }
- if (model === 'ration' && (rationLib === '' || rationLib === undefined)) {
- throw '请选择定额库';
- }
- if (model === 'glj' && (gljLib === '' || gljLib === undefined)) {
- throw '请选择工料机库';
- }
- let standardBillString = $("select[name='standard_bill']").children("option:selected").text();
- let rationLibString = $("select[name='ration_lib']").children("option:selected").text();
- let gljLibString = $("select[name='glj_lib']").children("option:selected").text();
- return [name, standardBill, rationLib, gljLib, standardBillString, rationLibString, gljLibString];
- }
- /**
- * 验证计价规则数据
- *
- * @return {boolean}
- */
- function validValuation() {
- let result = false;
- try {
- let valuationName = $("input[name='name']").val();
- if (valuationName === '') {
- throw '请填写计价规则名称';
- }
- let engineering = $("select[name='engineering']").val();
- if (engineering === '' || engineering <= 0) {
- throw '请选择工程专业';
- }
- if ($("input:hidden[name='bill_lib']").length <= 0) {
- throw '请添加标准清单';
- }
- if ($("input:hidden[name='ration_lib']").length <= 0) {
- throw '请添加定额库';
- }
- result = true;
- } catch (error) {
- alert(error);
- result = false;
- }
- return result;
- }
- /**
- * 切换switch效果
- *
- * @param {Object} element
- * @return {boolean}
- */
- function switchChange(element) {
- // 第一个元素判断当前的状态
- let firstButton = element.children("button").first();
- let secondButton = element.children("button").eq(1);
- let currentStatus = firstButton.is(":disabled");
- if (currentStatus) {
- // 当前为true切换到false
- firstButton.removeClass('btn-success').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
- firstButton.text('开启');
- secondButton.removeClass("btn-default").addClass("btn-danger").addClass("disabled").attr("disabled", "disabled");
- secondButton.text('已禁用');
- } else {
- // 当前false切换到true
- firstButton.removeClass("btn-default").addClass("btn-success").addClass("disabled").attr("disabled", "disabled");
- firstButton.text('已开启');
- secondButton.removeClass('btn-danger').removeClass('disabled').addClass('btn-default').removeAttr("disabled");
- secondButton.text('禁用');
- }
- return !currentStatus;
- }
|