<!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 layui-form-item">

                            <div class="layui-col-md12">
                                <label class="layui-form-label"><font color='red' size="4">*</font>扣减金额：</label>
                                <div class="layui-input-block">
                                    <div class="layui-col-sm8">
                                        <input id="id_loss_amount" type="text" autocomplete="off" name="notes"  class="layui-input">
                                    </div>
                                </div>
                            </div>

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

                        <fieldset class="layui-elem-field layui-field-title">
                            <legend>产品明细&nbsp;&nbsp;</legend>
                        </fieldset>

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

                        <button 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>


  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index'
  }).use(['index', 'form', 'table', 'utils'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,element = layui.element
    ,form = layui.form
    ,table = layui.table
    ,utils = layui.utils;

    var id = layui.view.getParameterByName('order_id');

    admin.req({
        url: '/foundation/search_options/?type=4',
        done: function(res){
            _options = res.data;
        }
    });

    function fillData(data){
        $('#id_loss_amount').val(data.amount);
        $('#id_loss_notes').val(data.notes);
    }

    var part_layTableId = "part_layTable";
	var part_tableIns = table.render({
        elem: '#part_dataTable',
        id: part_layTableId,
        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: 80},
            {field: 'count', title: '数量', width: 80},
            {field: 'price', title: '单价', width: 80},
            {field: 'receive_count', title: '收货数量', width: 100,  templet: function (d){
            return '<input id="receive_count'+d.id+'" type="text" value="'+d.receive_count+'" lay-verify="required|numberGtZ" autocomplete="off" class="layui-input">';
            }},
            {field: 'quality_request_text', title: '质量要求', width: 80},
        ]],
    });

    table.on('tool(part_dataTable)', function (obj) {
        var data = obj.data, event = obj.event, tr = obj.tr; //获得当前行 tr 的DOM对象;
        switch(event){
            case "del":
                obj.del();
                layer.msg('删除成功', {icon: 6});
                break;
        }
    });

    var getItemsData = function () {
        var rows = table.cache[part_layTableId];
        var items = [];
        for(var k in rows){
            if (rows[k] == false) {
                continue;
            }

            var receive_count = $('#receive_count'+rows[k].id).val();
            var item = {
                detail_id:rows[k].id,
                receive_count:receive_count
            };
            items.push(item);
        }
        return items
    };
    form.on('submit(component-form-element)', function(data){
        var items = getItemsData();


        var order_data = {'amount': $('#id_loss_amount').val(), 'notes': $('#id_loss_notes').val()};

        data.field.order_data = order_data;
        data.field.items = items;
        admin.req({
            url: '/order/sale_order/loss_save/?id='+id
            ,data: JSON.stringify(data.field)
            ,type: 'post'
            ,done: function(res){
                customer_id = null;
                parent.layui.onSubmitChild();
            }
        });
        return false;
    });

    if (id) {
        admin.req({
          url: '/order/sale_order/detail/?id='+id,
          done: function(res){
            var data = res.data.items_data;
            var oldData = [];
            for (var i in data) {
                var item = data[i];
                var newRow = {
                    id: item.id,
                    name: item.name,
                    model:item.model,
                    unit:item.unit,
                    price: item.price,
                    count: item.count,
                    receive_count: item.receive_count,
                    quality_request_text: item.quality_request_text,
                    part_data:item
                };
                oldData.push(newRow);
            }
            part_tableIns.reload({
                data: oldData
            });

             var orderData = JSON.parse(JSON.stringify(parent.layui.table.editdata));
             fillData({
                 amount:orderData.loss_amount,
                 notes:orderData.loss_notes
             });
          }
        });
    }

    parent.layui.submitChild = function () {
      $("#submit_btn").click();
    };

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