<!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>
<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="table-overlay">
                            <table id="product_data_table" lay-filter="product_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 = [];
var _user = [];

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

    admin.req({
        url: '/purchase/purchase_price/detail/?id=' + id,
        done: function (res) {
            var data = res.data.data;
            var productData = table.cache[product_data_table];
            for (var i in data) {
                var newRow = {
                    id: data[i].id,
                    name: data[i].name,
                    user_name: data[i].user_name,
                    supplier_name: data[i].supplier_name,
                    price: data[i].price
                };
                productData.push(newRow);

            }

            product_tableIns.reload({
                data: productData
            })
        }
    });
    var product_data_table = "product_data_table";
    var product_tableIns = table.render({
        elem: '#product_data_table',
        id: product_data_table,
        data: [],
        page: false,
        limit: 1000,
        loading: true,
        even: true, //不开启隔行背景
        cols: [[
            {title: '序号', type: 'numbers'},
            {field: 'name', title: '产品名称', width: 100},
            {field: 'user_name', title: '采购员', width: 100},
            {field: 'supplier_name', title: '供应商', width: 100},
            {field: 'price', title: '价格', width: 100, templet: function (d){
            return '<input id="price'+d.id+'" type="text" value="'+d.price+'" lay-verify="required|numberGtZ" autocomplete="off" class="layui-input">';
            }}
        ]]
        ,done: function () {
                layui.index.removeNoPermButtons();
            }
    });
    //监听工具条
    table.on('tool(product_data_table)', function (obj) {
        var data = obj.data, event = obj.event, tr = obj.tr; //获得当前行 tr 的DOM对象;
        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 getproductData = function () {
        var rows = table.cache[product_data_table];
        var data = [];
        for (var n in rows) {
            if (rows[n] == false) {
                continue;
            }
            var price = $('#price'+rows[n].id).val();
            var item = {
                id: rows[n].id,
                user_name: rows[n].user_name,
                supplier_name: rows[n].supplier_name,
                name: rows[n].name,
                price: price
            };
            data.push(item);
        }
        return data;
    };
    form.on('submit(component-form-element)', function (data) {
        var items = getproductData();
        if (items.length == 0) {
            layer.msg('没有上报询价！', {icon: 5});
            return false;
        }
        admin.req({
            url: '/purchase/purchase_price/edit_save/'
            , data: {data: JSON.stringify(items)}
            , type: 'post'
            , done: function (res) {
                parent.layui.onSubmitChild();
            }
        });
        return false;
    });

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