<!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">

                    <form class="layui-form" action="" lay-filter="component-form-element">
                        <div class="layui-row layui-col-space10">
                            <div class="layui-col-xs12 layui-col-sm4 layui-col-md4">
                                <label>单号：<span id="no"></span></label>
                            </div>
                            <div class="layui-col-xs12 layui-col-sm4 layui-col-md4">
                                <label>下单时间：<span id="create_time"></span></label>
                            </div>
                            <div class="layui-col-xs12 layui-col-sm4 layui-col-md4">
                                <label>需求人：<span id="demend_user_text"></span></label>
                            </div>
                        </div>
                        <div class="layui-row layui-col-space10">

                            <table id="dataTable" lay-filter="dataTable" 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 _data_list = [];
    var _data = [];
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'table'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,table = layui.table
            ,form = layui.form;
        var id = layui.view.getParameterByName('id');

        if (id) {
            var editdata = parent.layui.table.editdata;

            $('#no').html(editdata.no);
            $('#create_time').html(editdata.create_time);
            $('#demend_user_text').html(editdata.demend_user_text);
        }

        admin.req({
            url: '/purchase/purchase/table_totle/?id=' + id,
            done: function(res){
                var plan_details = res.data.plan_details;
                var purchase_users = res.data.purchase_users;
                var purchase_prices = res.data.purchase_prices;

                var getUserPrice = function(user_id,detail_id) {
                    for (var n in purchase_prices) {
                        var purchase_price = purchase_prices[n];

                        if (purchase_price.user_id == user_id && purchase_price.detail_id == detail_id) {
                            return purchase_price;
                        }
                    }
                    return null;
                };

                var title2 = [];
                var title3 = [];
                var title1 = [
                    {title: '序号', type: 'numbers', rowspan: 3},
                    {field: 'name', title: '员工名称', width: 200, rowspan: 3}
                ];

                for (var n in plan_details){
                    var product = plan_details[n];
                    title1.push({align: 'center', title: product.name, colspan: 4});
                    title2.push({align: 'center', title:'代码:'+product.model+'&emsp;&emsp;&emsp;&emsp;&emsp;'+'数量:'+product.count+'&emsp;&emsp;&emsp;&emsp;&emsp;'+'单位:'+product.unit, colspan: 4});
                    title3.push({field: 'price' + product.id, align:'right', title: '价格', width: 80});
                    title3.push({field: 'supplier' + product.id, title: '供应商', width: 120});
                    title3.push({field: 'notes' + product.id, title: '备注', minWidth: 200});
                    title3.push({field:''+product.id,width:60, title: '选择', templet:function(d){
                            return '<input type="radio" user="'+d.id+'" name="product_price'+d.id+'" value="1" title="">';

                        }});
                }
                _data_list=[title1,title2,title3];

                for (var n in plan_details) {
                    var plan_detail = plan_details[n];

                    for (var i in purchase_users) {
                        var user = purchase_users[i];
                        var user_price = getUserPrice(user.id,plan_detail.id);
                        user['price'+plan_detail.id] = '';
                        user['supplier'+plan_detail.id] = '';
                        user['notes'+plan_detail.id] = '';
                        user[plan_detail.id] = '';
                        if (user_price != null) {
                            user['price'+plan_detail.id] = user_price.price;
                            user['supplier'+plan_detail.id] = user_price.supplier;
                            user['notes'+plan_detail.id] = user_price.notes;
                            user[plan_detail.id] = user_price.id;
                        }
                    }
                }
                _data = purchase_users;

                rendTable();


            }
        });
        var layTableId = "dataTable";
        var tableIns = null;

        var rendTable = function (){
            tableIns =table.render({
                elem: '#dataTable',
                id: layTableId,
                data: _data,
                page: false,
                limit: 1000,
                loading: true,
                even: true //不开启隔行背景
                ,cols:_data_list
                ,height: 'full-85'
                ,done:function(res, curr, count) {
                    var oldData = res.data;
                    $("input").each(function () {
                        var product_id = $(this).parent().parent().attr('data-field');
                        var user_id = $(this).attr('user');

                        this.setAttribute('name','product_price'+product_id );

                        for (var n in oldData) {
                            var item = oldData[n];

                            if (item.id == user_id) {
                                if (!item[product_id]) {
                                    $(this).parent().children(1).remove();
                                    $(this).remove();
                                }
                            }

                        }

                    });

                }

            });
        };
        form.on('submit(component-form-element)', function () {
            var oldData = table.cache[layTableId];
            var data = [];
            $("input:checked").each(function () {
                var product_id = $(this).parent().parent().attr('data-field');
                var user_id = $(this).attr('user');
                for (var n in oldData) {
                    var item = oldData[n];
                    if (item.id == user_id) {
                        var product_price = item[product_id];
                        if (product_price){
                            var row={
                                user_price_id: product_price
                            };
                            data.push(row);
                        }
                    }

                }
            });
            if (!data.length) {
                layer.msg('请选择询价结果');
                return false;
            }

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