<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加项目</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">
  <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" pad15>
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">

                    <div class="layui-col-md12">
                        <div class="layui-col-sm6">
                            <input type="text" autocomplete="off" id="keyword" placeholder="请输入名称搜索" class="layui-input"/>
                        </div>
                        <div class="layui-col-sm6">
                            <button type="button" class="layui-btn" data-type="onSearchPurchase">
                                <i class="layui-icon layui-icon-search"></i> 搜索
                            </button>
                        </div>
                        <div style="clear:both;height: 5px;"></div>
                    </div>

                    <div class="layui-col-md12">
                        <table id="dataTable" lay-filter="dataTable" class="layui-hide"></table>
                    </div>

                    <button class="layui-btn" lay-submit id="btn_search_option_ok" style="display: none">确定</button>

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

  <script src="../../layuiadmin/layui/layui.js"></script>
  <script>

  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
      var $ = layui.$
      ,table = layui.table;

      $('#btn_search_option_ok').on('click', function () {
          var oldData = table.cache[layTableId];
          var data = [];
          for (var i in oldData){
              if(oldData[i].LAY_CHECKED){
                  data.push(oldData[i])
              }
          }
          parent.layui.onSubmitChild(data);

      });
      parent.layui.submitChild = function () {
          $("#btn_search_option_ok").click();
      };

      var tbWidth = $("#tableRes").width();
		var layTableId = "layTable";
		var tableIns = table.render({
			elem: '#dataTable',
			id: layTableId,
			data: [],
			width: tbWidth,
			loading: true,
			even: true, //不开启隔行背景
			cols: [[
				{title: 'ck', type: 'checkbox'},
                {field: 'name', title: '名称', width: 150},
                {field: 'notes', title: '备注', minWidth: 150}
			]]
		});

		//定义事件集合
		var active = {
            onSearchPurchase: function(){
              var $ = layui.$;
              var keyword = $('#keyword').val();
              var admin = layui.admin;

              admin.req({
                  url: '/tenant/option/search/?type=1',
                  data: {keyword: keyword},
                  done: function (res) {
                      var data = res.data;
                      var oldData = [];
                      for (var i in data) {
                        var item = data[i];
                        var newRow = {
                          id: item.value,
                          name: item.name,
                          notes: item.notes
                        };
				        oldData.push(newRow);
                      }
                      tableIns.reload({
                          data : oldData
                      });
                  }
              });
            },
		};

		//激活事件
		var activeByType = function (type, arg) {
			if(arguments.length === 2){
				active[type] ? active[type].call(this, arg) : '';
			}else{
				active[type] ? active[type].call(this) : '';
			}
		}

		//注册按钮事件
		$('.layui-btn[data-type]').on('click', function () {
			var type = $(this).data('type');
			activeByType(type);
		});
      active.onSearchPurchase();
 });

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