<!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" class="layui-input" autocomplete="off" id="repair-keyword" placeholder="请输入名称、代码搜索"/>
                        </div>
                        <div class="layui-col-sm6">
                            <button type="button" class="layui-btn" data-type="onSearchMaterial">
                                <i class="layui-icon layui-icon-search"></i> 搜索
                            </button>
                            <button type="button" class="layui-btn layui-btn-warm" id="btn_create" data-permission="material.add_consumable">
                                <i class="layui-icon layui-icon-add-circle"></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 lay-submit id="btn_search_consumable_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;
      var warehouse = layui.view.getParameterByName('warehouse');
      var supplier = layui.view.getParameterByName('supplier');

      $('#btn_search_consumable_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);
      });
      var tbWidth = $("#tableRes").width();
		var layTableId = "layTable";
		var tableIns = table.render({
			elem: '#dataTable',
			id: layTableId,
			data: [],
			width: tbWidth,
			page: true,
			loading: true,
			even: true, //不开启隔行背景
			cols: [[
				{title: '序号', type: 'numbers'},
				{title: 'ck', type: 'checkbox'},
                {field: 'name', title: '名称', width: 150},
				{field: 'model', title: '代码', width: 150},
                {field: 'option_type', title: '类别', minWidth: 150},
                {field: 'unit', title: '单位', minWidth: 150},
                {field: 'standard', title: '规格', minWidth: 150},
                {field: 'warehouse_place', title: '库位', width: 150},
			]]
            , done: function () {
                layui.index.removeNoPermButtons();
            }
		});

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

              admin.req({
                  url: '/material/consumable/select/?param=' + value + '&warehouse='+warehouse +'&supplier=' + supplier,
                  done: function (res) {
                      var data = res.data;
                      var oldData = [];
                      for (var i in data) {
                        var item = data[i];
                        var newRow = {
                          name: item.name,
                          model: item.model,
                          option_type: item.option_type,
                          unit: item.unit,
                          warehouse_place: item.warehouse_place,
                          id: item.id,
                          product_id: item.product_id,
                          record_data: item.record_data,
                          standard: item.standard
                        };
				        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.onSearchMaterial();

      var oldOnSubmitChild = null;
      var oldSubmitChild = null;
      $(document).on('click', '#btn_create', function () {
          oldOnSubmitChild = parent.layui.onSubmitChild;
          oldSubmitChild = parent.layui.submitChild;
          parent.layer.open({
              type: 2,
              title: '添加原耗材',
              area: ['500px', '98%'],
              btn: ['保存', '取消'],
              end: function() {
                  parent.layui.onSubmitChild = oldOnSubmitChild;
                  parent.layui.submitChild = oldSubmitChild;
              },
              yes: function (index, dom) {
                  parent.layui.onSubmitChild = function (data) {
                      parent.layer.close(index);
                  };
                  parent.layui.submitChild();
              },
              btn2: function (index, layero) {
                  parent.layer.close(index);//关闭当前按钮
              },
              content: '/views/material/consumable_edit.html'
          });
      });
      parent.layui.submitChild = function () {
      $("#btn_search_consumable_ok").click();
    };
 });

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