<!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">
    <style type="text/css">
        .layui-form-select dl{max-height: 120px;}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" pad15>
            <form class="layui-form" action="" lay-filter="component-form-element">

                <div class="layui-row layui-col-space10">

                    <div class="layui-col-xs12">
                        <label class="layui-form-label"><font color='red' size="4">*</font>名称：</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" lay-verify="required" autocomplete="off" id="name" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-col-xs12">
                        <label class="layui-form-label">需求人：</label>
                        <div class="layui-input-block">
                            <select id="id_demend_user" name="demend_user" lay-filter="demend_user">
                                <option value=""></option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-col-xs12">
                        <label class="layui-form-label">需求部门：</label>
                        <div class="layui-input-block" id="id_demend_department" style="line-height: 36px;">
                        </div>
                    </div>

                    <div class="layui-col-xs12">
                        <label class="layui-form-label">备注：</label>
                        <div class="layui-input-block">
                            <input type="text" name="notes" autocomplete="off" id="notes" class="layui-input">
                        </div>
                    </div>

                </div>

                <button class="layui-btn" id="purchase-submit" lay-submit lay-filter="component-form-element" style="display: none"></button>
                <button class="layui-btn" type="button" id="purchase-import" style="display: none"></button>
            </form>
        </div>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script>
var _user = [];

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

    admin.req({
            url: '/plan/plan/options/',
            done: function(res){
                var users = res.data.users;
                _user = users;
                var demend_user_node = $('#id_demend_user');
                for (var i in users) {
                    var uid = users[i].id;
                    var value = users[i].name;
                    demend_user_node.append("<option value='"+uid+"'>"+value+"</option>");
                }
                form.render();
            }
        });

    form.on('select(demend_user)', function (data) {
        var department_name = '';
        for (var i in _user) {
            if (_user[i].id == data.value) {
                department_name = _user[i].department_name;
                break;
            }
        }
        $('#id_demend_department').html(department_name);
    });

     var form_data = {field:{}};
     upload.render({
            elem: '#purchase-import',
             url: '/purchase/purchase/import/'
            , accept: 'file'
            , acceptMime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            , exts: 'xlsx'
            , field: 'excel_file'
            , multiple: false
            , data:{main_data: function(){return JSON.stringify(form_data.field);}}
            , done: function (res) {
                if (res.code != 0) {
                    layer.msg(res.msg);
                }else {
                    parent.layui.onSubmitChild(res.data);
                }
            }
            , error: function () {
                layer.msg('导入失败');
            }
        });
    form.on('submit(component-form-element)', function (data) {
        form_data = data;
        $('#purchase-import').click();
        return false;
    });

     parent.layui.submitChild = function () {
      $("#purchase-submit").click();
    };


})
  </script>
</body>
</html>