<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layuiAdmin 后台管理员</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta name="decorator" content="base" />
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
      <div class="layui-tab layui-tab-brief">
          <ul class="layui-tab-title">
              <sfTags:pagePermission authFlag="XTSZ_GDJJ_GDJJ_T" html='<a href="${ctx}/yangguang/orderSetting/toExigencySetPage"><li class="layui-this">工单加急</li></a>'/>
          </ul>
      </div>
      <div class="layui-card-body">
      <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
      <%--分页--%>
      <div id="demo7"></div>
      <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <sfTags:pagePermission authFlag="XTSZ_GDJJ_GDJJ_ADD_B" html='<button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
            <sfTags:pagePermission authFlag="XTSZ_GDJJ_GDJJ_UPDATE_B" html='<button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</button>'/>
            <sfTags:pagePermission authFlag="XTSZ_GDJJ_GDJJ_DEL_B" html='<button class="layui-btn layui-btn-sm" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</button>'/>
        </div>
      </script>
    </div>
  </div>
</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script>
    layui.use(['table','layer','laypage'], function(){
        $ = layui.$;
        table = layui.table;
        layer = layui.layer;
        laypage = layui.laypage;

        table.render({
            elem: '#test',
            height: 'full-150'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: false
            , limits : [ 20, 50, 100 ]
            , limit:20
            , page : true //开启分页
            , url: '${ctx}/yangguang/orderSetting/getExigencySetData'
            , cols: [[
                {type: 'numbers',title:'序号'}
                , {field: 'id', type: 'checkbox'}
                , {field: 'complete_limit_length', title: '工单加急时间(h)', sort: false, align: 'center'}
                , {field: 'complete_extend_cost', title: '加急费用（元）', align: 'center'}
                , {field: 'createTime', title: '创建时间', sort: false, align: 'center'}
            ]]
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2
                        ,title: '新增'
                        ,content: '${ctx}/yangguang/orderSetting/addExigencyPage'
                        ,area: ['755px', '320px']
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submitID = 'sureSubmit'
                                ,submit = layero.find('iframe').contents().find('#'+ submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                var field = data.field; //获取提交的字段
                                var jsonArry = [];
                                var times = "";
                                var checkStatus = false;
                                layero.find('iframe').contents().find(".contentBody").each(function (index) {
                                    var time = $(this).find("input[name='time']").val();
                                    var price = $(this).find("input[name='money']").val();
                                    if (!isBlank(time) && !isBlank(price)) {
                                        if (times.indexOf(time) != -1) {
                                            layer.msg("加急时间重复！", {icon: 5, anim: 6});
                                            checkStatus = true;
                                            return false;
                                        }
                                        times += time + ",";
                                        var data = {
                                            time: time,
                                            price: price
                                        };
                                        jsonArry.push(data);
                                    }
                                });
                                if(checkStatus){
                                    return;
                                }
                                addExigency(jsonArry,index);
                            });
                            submit.trigger('click');
                        }
                        ,
                        success : function(layero) {
                            layero.find('.layui-layer-btn').css('text-align', 'center');
                            layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color","#009688");
                        }
                    });
                    break;
                case 'edit':
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg("请选择您要编辑的数据！",{icon:5,anim:6});
                    }else if(data.length > 1){
                        layer.msg("一次只能编辑一条数据！",{icon:5,anim:6});
                    }else{
                        layer.open({
                            type: 2
                            ,title: '编辑'
                            , content: '${ctx}/yangguang/orderSetting/editExigencyPage?id=' + data[0].id
                            ,area: ['450px', '213px']
                            ,btn: ['确定', '取消']
                            ,yes: function(index, layero){
                                var iframeWindow = window['layui-layer-iframe'+ index]
                                    ,submitID = 'sureSubmit'
                                    ,submit = layero.find('iframe').contents().find('#'+ submitID);

                                //监听提交
                                iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                                    var field = data.field; //获取提交的字段
                                    console.log(field);
                                    editExigency(field,index);
                                });
                                submit.trigger('click');
                            }
                            ,
                            success : function(layero) {
                                layero.find('.layui-layer-btn').css('text-align', 'center');
                                layero.find('.layui-layer-btn').find(".layui-layer-btn0").css("background-color","#009688");
                            }
                        });
                    }
                    break;
                case 'del':
                    var data = checkStatus.data;
                    if (data.length == '0') {
                        layer.msg("请选择您要删除的数据！",{icon:5,anim:6});
                        return;
                    }
                    var ids = "";
                    for(var i=0;i<data.length;i++){
                        ids += data[i].id + ",";
                    }
                    layer.confirm('确认要删除选中数据吗？', {icon:3}, function(index) {
                        delData(ids,index);
                    });
                    break;
            };
        });
    });


    var noRepeatAdd = false;
    function addExigency(data,index){
        if(noRepeatAdd){
            return;
        }
        noRepeatAdd = true;
        $.ajax({
            url: '${ctx}/yangguang/orderSetting/addExigency',
            type: 'post',
            data: {jsonData: JSON.stringify(data)},
            success: function(data) {
                if(data=="200"){
                    layer.msg("添加成功！",{time:2000},function(){
                        table.reload('test'); //数据刷新
                        layer.close(index); //关闭弹层
                    });
                }else if(data=='203'){
                    layer.msg("添加失败,存在已添加过的加急时间！",{icon:5,anim:6});
                }else{
                    layer.msg("添加失败！",{icon:5,anim:6});
                }
            },
            complete:function(){
                noRepeatAdd = false;
            }
        });
    }


    var noRepeat = false;
    function editExigency(data, index) {
        if (noRepeat) {
            return;
        }
        noRepeat = true;

        $.ajax({
            url: '${ctx}/yangguang/orderSetting/editExigency',
            type: 'post',
            data: {
                id: data.id,
                time: data.time,
                price: data.money
            },
            success: function (data) {
                if (data == "200") {
                    layer.msg("编辑成功！", {time: 2000}, function () {
                        table.reload('test'); //数据刷新
                        layer.close(index); //关闭弹层
                    });
                } else if(data=='203'){
                    layer.msg("添加失败,存在已添加过的加急时间！",{icon:5,anim:6});
                } else {
                    layer.msg("编辑失败！");
                }
            },
            complete: function () {
                noRepeat = false;
            }
        });
    }

    function delData(ids,index){
        $.ajax({
            url: '${ctx}/yangguang/orderSetting/delExigencys',
            type: 'post',
            data: {
                ids: ids
            },
            success: function (data) {
                if (data == "200") {
                    layer.msg("删除成功！");
                    table.reload('test'); //数据刷新
                    layer.close(index); //关闭弹层
                } else {
                    layer.msg("删除失败！");
                }
            },
            complete: function () {
                noRepeat = false;
            }
        });
    }
    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }

</script>
</body>
</html>

