
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
  <title>能辉</title>
  <meta name="decorator" content="base" />
  <link rel="stylesheet" href="${ctxPlugin}/static/css/reset.css" media="all">
  <style>
    .layui-card{box-shadow: none;}
    .layui-btn + .layui-btn {
      margin-left: 35px;
    }
  </style>
</head>
<body>

<div class="layui-fluid">
  <input type="hidden" id="parentId" name="parentId" value="${parentId}">
  <input type="hidden" id="capitalId" name="capitalId" value="${capitalId}">
  <input type="hidden" id="dictKey" name="dictKey" value="${dictKey}">
  <div class="layui-form layui-card">
    <div class="layui-tab  layui-tab-brief">
      <ul class="layui-tab-title">
<%--        <sfTags:pagePermission authFlag="XTSZ_XDX_T" html='<li class="layui-this sf-tab"><a href="${ctx}/capitalDict/index">字典子类</a></li>'/>--%>
        <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " type="reset"  onclick="reset()" >
          <i class="layui-icon fs-12">&#xe9aa;</i>
          重置
        </button>
        <button class="layui-btn f-r mt-10 mr-15 pl-10 pr-10 " lay-submit lay-filter="LAY-user-back-search" data-type="reload">
          <i class="layui-icon fs-12">&#xe615;</i>
          查询
        </button>
      </ul>
    </div>
    <div class="layui-card-body" id="searchForm">
      <div class="layui-form-item">
        <div class="layui-col-lg2 layui-col-sm3">
          <label class="layui-form-label">字典类型：</label>
          <div class="layui-input-block">
            <input type="text" id="dictValue" name="dictValue" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-col-lg2 layui-col-sm3">
          <label class="layui-form-label">状态：</label>
          <div class="layui-input-block">
            <select name="status" id="status" lay-search>
              <option value="">请选择</option>
              <option value="1">正常</option>
              <option value="0">禁用</option>
            </select>
          </div>
        </div>
      </div>

      </div>
    </div>
  </div>

  <div class='layui-card'>
    <div class="layui-card-body">
      <table class="layui-hide" lay-skin="row" id="test" lay-filter="test"></table>
      <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
          <sfTags:pagePermission authFlag="XDX_XZ_B" html=' <button class="layui-btn layui-btn-sm" id="add" lay-event="addDict"><i class="layui-icon layui-icon-add-1"></i>新增</button>'/>
        </div>
      </script>
    </div>
  </div>

</div>


<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script type="text/html" id="table-toolbar">
</script>

<script type="text/javascript">
  var table;
  var $;

  var deleteBtn = '${fns:checkPagePermission(request, "XDX_SC_B")}';
  var editBtn = '${fns:checkPagePermission(request, "XDX_BJ_B")}';
  layui.config({
    base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'tableExt', 'jquery', 'layer','laydate','form'], function(){
    $ = layui.jquery;
    tableExt = layui.tableExt;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var  form = layui.form;
    table = tableExt.getLayTable();

    var parentId = $("#parentId").val();
    var capitalId = $("#capitalId").val();
    var dictKey = $("#dictKey").val();

    tableExt.render({
      elem: '#test'
      ,toolbar : '#toolbarDemo'
      , height: 'full-160'
      ,defaultToolbar : false
      , limits : [ 20, 50, 100 ]
      , limit:20
      , page : true //开启分页
      ,url: '${ctx}/capitalDict/findCapitalDictPage?parentId='+parentId+'&capitalId='+capitalId+'&dictKey='+dictKey
      ,cols: [[
        {type: 'numbers',title:'序号',fixed:'left',exportable:true}
        ,{field:'id', title: '操作',align: 'center',fixed:'left'
          ,templet:function(d){
            var htm="";
            if(d.status == 1){
              if(editBtn =='0'){
                htm+= '<a class="layui-table-opt-link" lay-event="edit">编辑</a>';
              }
              if(deleteBtn =='0'){
                htm+= '&nbsp;&nbsp;<a class="layui-table-opt-link" lay-event="delete">删除</a>';
              }
              htm+= '&nbsp;&nbsp;<a class="layui-table-opt-link" lay-event="enableDict">禁用</a>';
              return htm;
            }else{
              htm+= '<a class="layui-table-opt-link" lay-event="disableDict">启用</a>';
              return htm;
            }
          }

        }
        ,{field:'code', title: '字典码',align: 'center',fixed:'left',exportable:true}
        ,{field:'dict_value', title: '字典类型',align: 'center',fixed:'left',exportable:true}
        ,{field:'name', title: '资方名称',align: 'center',fixed:'left',exportable:true,templet:function(d){
            var html = "";
            if(d.name!=null){
              html += d.name;
            }else{
              html += "通用"
            }
            return html;
          }}
        ,{field:'capital_id', title: '资方id',align: 'center',fixed:'left',exportable:true,hide:true}
        ,{field:'status', title: '状态',align: 'center',fixed:'left',exportable:true
          ,templet:function(d){
            if(d.status == 1){
              return "正常";
            } else if(d.status == 0){
              return "禁用";
            }
            return "";
          },
          expFormat: function (d) {  //格式导出的化字段
            if(d.status == 1){
              return "正常";
            }else if(d.status == 0){
              return "禁用";
            }
            return "";
          }
        }

      ]]
      ,done: function(res, curr, count){

      },exportOpt:{                           //导出选项配置
        ele:'#exportBtn',                   //导出的Dom元素
        headerTitle: '合同模板',             //excel表格的标题
        pageData:false,                     //true:导出的数据为页面数据，false:最大导出maxAmount条数据
        maxAmount: 1000,                    //当pageData为false生效
        fileName: '合同模板',
        where: function (){                 //当pageData为false时生效，过滤的条件
          return _serializeJson($("#searchForm"));
        }
      }
    });

    //头工具栏事件
    table.on('toolbar(test)', function (obj) {
      var  capitalId = $("#capitalId").val();
      switch (obj.event){
        case 'addDict':
          layer.open({
            title:'新增字典项',
            type: 2,
            area: ["60%", "65%"],
            content: '${ctx}/capitalDict/addChildDict?parentId='+parentId+'&capitalId='+capitalId
          })
          break;
      }
    })

    //监听行工具事件
    table.on('tool(test)', function(obj){
      var data = obj.data;
      var id = data.id;
      var  capitalId = $("#capitalId").val();
      if(obj.event === 'edit'){
        layer.open({
          type: 2,
          title:"编辑",
          resize:false,
          area: ['50%', '65%'],
          content: '${ctx}/capitalDict/editChildDict?id='+id+"&parentId="+parentId+'&capitalId='+capitalId
        });
      }else if(obj.event === 'delete'){
        var id=data.id;
        layer.confirm('确认删除该数据？',{
          btn: ['确定', '取消'],
          success: function (layero, index) {
            this.enterEsc = function (event) {
              if (event.keyCode == 13 ) {
                //确认  执行的代码
                deleteDict(id);
                //关闭弹窗
                layer.close(index);
                console.log("回车确认时事件")
                //取消连续回车键，导致不断弹出询问框
                $(':focus').blur();
              }
            };
            //监听键盘事件
            $(document).on('keydown', this.enterEsc);
            //esc键触发取消
            this.escQuit = (ev) => {
              let code_val = ev.which;
              if(code_val == 0x1B){
                $('.layui-layer-btn1').click();
                return false;
              }
            };
            $(document).on('keydown', this.escQuit);
          },
          end: function () {
            //取消键盘监听事件
            $(document).off('keydown', this.enterEsc);
            $(document).off('keydown', this.escQuit);
          },
          yes: function () {
            //确认执行的代码
            deleteDict(id);
          },
          no: function () {

          }
        });
      }else if(obj.event === 'enableDict'){
        var status = 0;
        var id=data.id;
        layer.confirm('确认禁用该数据？',{
          btn: ['确定', '取消'],
          success: function (layero, index) {
            this.enterEsc = function (event) {
              if (event.keyCode == 13 ) {
                //确认  执行的代码
                updateCapitalDictStatus(id,status);
                //关闭弹窗
                layer.close(index);
                console.log("回车确认时事件")
                //取消连续回车键，导致不断弹出询问框
                $(':focus').blur();
              }
            };
            //监听键盘事件
            $(document).on('keydown', this.enterEsc);
            //esc键触发取消
            this.escQuit = (ev) => {
              let code_val = ev.which;
              if(code_val == 0x1B){
                $('.layui-layer-btn1').click();
                return false;
              }
            };
            $(document).on('keydown', this.escQuit);
          },
          end: function () {
            //取消键盘监听事件
            $(document).off('keydown', this.enterEsc);
            $(document).off('keydown', this.escQuit);
          },
          yes: function () {
            //确认执行的代码
            updateCapitalDictStatus(id,status);
          },
          no: function () {

          }
        });
      }else if(obj.event === 'disableDict'){
        var status = 1;
        var id=data.id;
        layer.confirm('确认启用该数据？',{
          btn: ['确定', '取消'],
          success: function (layero, index) {
            this.enterEsc = function (event) {
              if (event.keyCode == 13 ) {
                //确认  执行的代码
                updateCapitalDictStatus(id,status);
                //关闭弹窗
                layer.close(index);
                console.log("回车确认时事件")
                //取消连续回车键，导致不断弹出询问框
                $(':focus').blur();
              }
            };
            //监听键盘事件
            $(document).on('keydown', this.enterEsc);
            //esc键触发取消
            this.escQuit = (ev) => {
              let code_val = ev.which;
              if(code_val == 0x1B){
                $('.layui-layer-btn1').click();
                return false;
              }
            };
            $(document).on('keydown', this.escQuit);
          },
          end: function () {
            //取消键盘监听事件
            $(document).off('keydown', this.enterEsc);
            $(document).off('keydown', this.escQuit);
          },
          yes: function () {
            //确认执行的代码
            updateCapitalDictStatus(id,status);
          },
          no: function () {

          }
        });
      }
    });

    //监听搜索
    form.on('submit(LAY-user-back-search)', function(data){
      var field = data.field;
      //执行重载
      table.reload('test', {
        where: field,
        page:{
          curr:1
        }
      });
    });



  });

  function deleteDict(id){
    $.post('${ctx}/capitalDict/delCapitalDict', {id:id}, function (result) {
      if (result.code === '200') {
        layer.msg('操作成功', {time: 2000}, function () {
          reload();
        });
      }else {
        layer.msg('操作失败！' + result.errMsg);
      }
    });
  }

  function updateCapitalDictStatus(id,status){
    $.post('${ctx}/capitalDict/updateCapitalDictStatus', {id:id,status:status}, function (result) {
      if (result.code === '200') {
        layer.msg('操作成功', {time: 2000}, function () {
          reload();
        });
      }else {
        layer.msg('操作失败！' + result.errMsg);
      }
    });
  }

  function getChildPage(id){
    window.location.href="${ctx}/capitalDict/childIndex?id="+id;
  }

  function lookDetail(id){
    $.ajax({
      type: 'post',
      url: '${ctx}/contractTemplate/lookContractTemplateDetail',
      data: {
        docTemplateId: id
      },
      success: function (data) {
        if('200' ==data.code){
          var docTemplateEditUrl=data.data;
          window.open(docTemplateEditUrl);
        }else{
          layer.msg(data.errMsg, {icon: 2});
        }
      },
    });
  }

  function reload(){
    table.reload('test', {
      page:{
        curr:1
      }
    });
  }

  function reset(){
    layui.form.render();
    $("#searchForm").find("select").val("");
    $("#searchForm").find("input").val("");
  }

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

