<!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">
<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">
                    <form class="layui-form" action="" lay-filter="component-form-element">

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

                        <button class="layui-btn" id="submit_btn" lay-submit lay-filter="component-form-element" style="display: none">确定</button>

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

<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    var _data_list = [];
    var _data = [];
layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'form', 'table'], function() {
    var $ = layui.$;
    var admin = layui.admin;
    var form = layui.form;
    var table = layui.table;
    var id = layui.view.getParameterByName('id');
    admin.req({
            url: '/purchase/search_user/?id=' + id,
            done: function(res){
                var datas = res.data;
                var title1 = [];

                title1.push({field:'id',width:60, title: '选择', templet:function(d){
                    return '<input type="checkbox" id="user_check'+d.id+'" title="" lay-skin="primary">';

                    }});
                title1.push({field: 'name', title: '姓名', width: 150});
                title1.push({field: 'department', title: '部门', minWidth: 120})

                _data_list=[title1];
                _data = datas;

                rendTable();
            }
        });
    var layTableId = "dataTable";
    var rendTable = function (){
        table.render({
        elem: '#dataTable',
        id:layTableId,
        data: _data,
        page: false,
        limit: 1000,
        loading: true,
        even: true //不开启隔行背景
        ,cols:_data_list
        ,height: 'full-60'
         ,done:function(res, curr, count) {
                var oldData = res.data;
                for (var n in oldData) {
                    if (oldData[n]['check']==1) {
                        $("#user_check"+oldData[n].id).prop('checked', true);
                    }
                    if (oldData[n]['is_report']==1){
                        $("#user_check"+oldData[n].id).prop('disabled', true);
                    }
                }
                form.render();
            }
        });
    };
    form.on('submit(component-form-element)', function () {
        var oldData = table.cache[layTableId];
        var data = [];
        for (var n in oldData) {
            var is_checked = $('#user_check'+oldData[n].id).prop('checked');
            if (is_checked) {
                 data.push({id:oldData[n].id})
            }
        }

        admin.req({
            url: '/purchase/purchase_user/save/?id=' + id
            , data: {users: JSON.stringify(data)}
            , type: 'post'
            , done: function (res) {
                parent.layui.onSubmitChild();
            }
        });
        return false;
    });
    parent.layui.submitChild = function () {
      $("#submit_btn").click();
    };
})
  </script>
</body>
</html>