<!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">
        .table-overlay .layui-table-view,
        .table-overlay .layui-table-header,
        .table-overlay .layui-table-box,
        .table-overlay .layui-table-body{overflow: visible;}
        .table-overlay .layui-table-cell{height: auto; overflow: visible;}
    </style>
</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">

                    <form class="layui-form" action="" lay-filter="component-form-element">

                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-sm6">
                                <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>

                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-sm10">
                                <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>

                        <fieldset class="layui-elem-field layui-field-title">
                            <legend>产品明细&nbsp;&nbsp;<button type="button" id="goods-btn-add" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon"></i></button></legend>
                        </fieldset>

                        <div class="table-overlay">
                            <table id="goods_data_table" lay-filter="goods_data_table" class="layui-hide"></table>
                        </div>

                        <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  _Quality_request = []
    ,_Department = []
    ,_Product_user = [];

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

    var id = layui.view.getParameterByName('id');
     admin.req({
      url: '/foundation/search_options/?type=4',
      done: function (res) {
        _Quality_request = res.data;
        options_isload = true;
        load_list()
      }
    });
    admin.req({
            url: '/plan/plan/options/',
            done: function(res){
                var department = res.data.departments;
                var product_user = res.data.users;
                _Department = department;
                _Product_user = product_user;
                other_isload = true;
                load_list()
            }
        });
  var load_list = function(){
      if (!(options_isload && other_isload)){
        return;
      }
      if (data_isload) {
            return;
        }
      data_isload = true;
      if (id) {
        admin.req({
            url: '/plan/production/detail/?id=' + id,
            done: function (res) {
                var production_items = res.data.production;
                var goods_items = res.data.goods_items;
                for (var p in production_items) {
                    $('#name').val(production_items[p].name);
                    $('#notes').val(production_items[p].notes);
                }
                var goodsData = table.cache[goods_data_table];
                for (var i in goods_items) {
                    var newRow = {
                        id: goods_items[i].goods_id,
                        name: goods_items[i].name,
                        model: goods_items[i].model,
                        unit: goods_items[i].unit,
                        quality_request: goods_items[i].quality_request_id,
                        count: goods_items[i].count,
                        product_user: goods_items[i].product_user_id,
                        p_department: goods_items[i].p_department_id,
                        product_time: goods_items[i].product_time,
                        notes: goods_items[i].notes
                    };
                    goodsData.push(newRow);

                }

                goods_tableIns.reload({
                    data: goodsData
                })
            }
        })
    };
        var sale_id = layui.view.getParameterByName('sale_id');
        if (sale_id) {
                    admin.req({
                        url: '/plan/sale/detail/?id=' + sale_id,
                        done: function (res) {
                            var goods_items = res.data.goods_items;
                            var goodsData = table.cache[goods_data_table];
                            for (var i in goods_items) {
                                var newRow = {
                                    id: goods_items[i].goods_id,
                                    name: goods_items[i].name,
                                    model: goods_items[i].model,
                                    unit: goods_items[i].unit,
                                    quality_request: goods_items[i].quality_request,
                                    count: goods_items[i].require_product_count,
                                    product_user: '',
                                    p_department: '',
                                    product_time: '',
                                    notes: ''
                                };
                                goodsData.push(newRow);

                            }

                            goods_tableIns.reload({
                                data: goodsData
                            })
                        }
                    })
             }
  };


    var goods_data_table = "goods_data_table";
    var goods_tableIns = table.render({
        elem: '#goods_data_table',
        id: goods_data_table,
        data: [],
        page: false,
        limit: 1000,
        loading: true,
        even: true, //不开启隔行背景
        cols: [[
            {title: '序号', type: 'numbers'},
            {field: 'name', title: '产品名称', width: 150},
            {field: 'model', title: '产品代码', width: 150},
            {field: 'unit', title: '单位', width: 120},
            {field: 'quality_request', title: '质量要求', width: 150, templet: function (d) {
                    var options = utils.renderSelectOptions(_Quality_request, {
                        valueField: "id",
                        textField: "name",
                        selectedValue: d.quality_request
                    });
                    return '<select id="quality_request'+d.id+'" lay-filter="type_select"><option value="">请选择</option>' + options + '</select>';
                }
            },
            {field: 'count', title: '数量', width: 100, templet: function (d){
                return '<input id="count'+d.id+'" type="text" value="'+d.count+'" lay-verify="required|numberGtZ" autocomplete="off" class="layui-input">';
            }},
            {field: 'p_department', title: '生产车间', width: 150,templet: function (d) {
                    var options = utils.renderSelectOptions(_Department, {
                        valueField: "id",
                        textField: "name",
                        selectedValue: d.p_department
                    });
                    return '<select id="p_department'+d.id+'" lay-filter="type_select"><option value="">请选择车间</option>' + options + '</select>';
                }
            },
            {field: 'product_time', title: '生产时间', width: 180,event:'chooseDate',templet:function(d){
                return '<input id="product_time'+d.id+'" type="text" lay-verify="datetime" autocomplete="off" class="layui-input" value="'+d.product_time+'">';
            }},
            {field: 'product_user', title: '负责人', width: 150,templet: function (d) {
                    var options = utils.renderSelectOptions(_Product_user, {
                        valueField: "id",
                        textField: "name",
                        selectedValue: d.product_user
                    });
                    return '<select id="product_user'+d.id+'" lay-filter="type_select"><option value="">请选择人员</option>' + options + '</select>';
                }
            },
            {field: 'notes', title: '备注', minWidth: 150, templet: function (d){
                return '<input id="notes'+ d.id +'" type="text" autocomplete="off" value="'+d.notes+'" class="layui-input" autocomplete="off">';
            }},
            {
                field: 'id', title: '操作', minWidth:120,templet: function (d) {
                    return '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                }
            }
        ]]
        ,done: function (res, curr, count) {
        }
    });

    //监听工具条
    table.on('tool(goods_data_table)', function (obj) {
        var data = obj.data;
        var event = obj.event;

        switch (event) {
            case "chooseDate":
                laydate.render({
                    elem: '#product_time'+data.id,
                    trigger:"click",
                    type:'datetime',
                    show:true,
                    closeStop:'#product_time'+data.id
                });
                break;
            case "del":
                obj.del();
                layer.msg('删除成功', {icon: 6});
                break;
        }
    });
    var getgoodsData = function () {
        var rows = table.cache[goods_data_table];
        var data = [];
        for (var n in rows) {
            if (rows[n] == false) {
                continue;
            }

            var notes = $('#notes'+rows[n].id).val();
            var quality_request = $('#quality_request'+rows[n].id).val();
            var count = $('#count'+rows[n].id).val();
            var p_department = $('#p_department'+rows[n].id).val();
            var product_user = $('#product_user'+rows[n].id).val();
            var product_time = $('#product_time'+rows[n].id).val();

            var item = {
                id: rows[n].id,
                name: rows[n].name,
                model: rows[n].model,
                unit: rows[n].unit,
                quality_request:quality_request,
                count:count,
                p_department:p_department,
                product_user:product_user,
                product_time:product_time,
                notes:notes
            };
            data.push(item);
        }
        return data;
    };
    form.on('submit(component-form-element)', function (data) {
        var items = getgoodsData();
        if (items.length == 0) {
            layer.msg('请添加产品！', {icon: 5});
            return false;
        }
        admin.req({
            url: '/plan/production/save/?id=' + id
            , data: {production: JSON.stringify(data.field), items: JSON.stringify(items)}
            , type: 'post'
            , done: function (res) {
                parent.layui.onSubmitChild(res.data);
            }
        });
        return false;
    });

    $('#goods-btn-add').on('click', function () {
        layer.open({
            type: 2,
            title: '添加成品',
            area: ['90%', '98%'],
            btn: ['确定', '取消'],
            yes: function (index, dom) {
                layui.onSubmitChild = function (data) {
                    var oldData = getgoodsData();
                    if (data.length > 0) {
                        layer.msg('添加成功！', {icon: 6});
                    } else {
                        layer.msg('请先选择记录！');
                    }
                    for (var i in data) {

                        var is_exit = false;
                        for (var m in oldData) {
                            if (oldData[m].id == data[i].id) {
                                is_exit = true;
                                break;
                            }
                        }

                        if (is_exit) {
                            continue;
                        }
                        var newRow = {
                            id: data[i].id,
                            name: data[i].name,
                            model: data[i].model,
                            unit: data[i].unit,
                            quality_request: '',
                            count: '',
                            product_user: '',
                            p_department: '',
                            product_time: '',
                            notes: ''
                        };
                        oldData.push(newRow);
                    }
                    goods_tableIns.reload({
                        data: oldData
                    });
                };
                layui.submitChild();
            },
            btn2: function (index, layero) {
                layer.close(index);//关闭当前按钮
            },
            content: '../goods/search_goods_item.html'
        });
    });

    parent.layui.submitChild = function () {
      $("#submit_btn").click();
    };
})
  </script>
</body>
</html>