<!DOCTYPE html>
<html lang="en">
<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">
    .LAY-btns .layui-nav {padding-left:0;padding-right:10px;top:-4px;margin: 0 10px;border: 0;background-color: #009688;}
    .LAY-btns .layui-nav .layui-nav-item{line-height: 30px;}
    .LAY-btns .layui-nav .layui-nav-child{top:34px;}
    .LAY-btns .layui-nav .layui-nav-bar{display: none;}
    .LAY-btns .layui-nav .layui-nav-child dd.layui-this a{color:#333;background-color:#fff;}
    .LAY-btns .layui-nav .layui-nav-child dd.layui-this a:hover {background-color: #f2f2f2;color: #000;}
    </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">

                <div class="LAY-btns" style="margin-bottom: 10px;">
                    <button class="layui-btn layui-btn-sm" data-permission="order.add_goods_deliver_return" id="btn_add"><i class="layui-icon layui-icon-add-1"></i>退库</button>
                    <button class="layui-btn layui-btn-sm" id="btn_print" data-permission="order.print_goods_deliver_return"><i class="layui-icon layui-icon-form"></i>打印</button>
                    <button class="layui-btn layui-btn-sm" id="btn_query" data-permission="order.view_goods_deliver_return"><i class="layui-icon layui-icon-search"></i>查询</button>
                </div>
                <table class="layui-hide" id="datagrid" lay-filter="datagrid-operate"></table>
            </div>
            </div>
        </div>
    </div>
</div>
    <div id="dlg_print" style="display: none">
      <div class="layui-card-body" pad15>
        <table class="layui-hide" id="dg-print"></table>
      </div>
  </div>
<div id="dlg_query" style="display: none">
    <div class="layui-card-body" pad15>
        <form class="layui-form" lay-filter="query-form-element">
            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">出库单号：</label>
                    <div class="layui-input-block">
                        <input type="text" name="no" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">产品备注：</label>
                    <div class="layui-input-block">
                        <input type="text" name="product_notes" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div style="display: none">
                <button id="query-submit" lay-submit lay-filter="query-form-element">查询</button>
            </div>
        </form>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js?t=1"></script>
<script>
    var _params = '';
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index', //主入口模块
        utils: 'utils'
    }).use(['index', 'table', 'laydate', 'form', 'utils'], function () {
        var $ = layui.$;
        var table = layui.table
            , form = layui.form
            , utils = layui.utils;
        var is_has = utils.isHasPermissions('goods.view_goods_cost');
        var cols = [];
        cols.push({type: 'checkbox', width: 50});
        cols.push({field: 'no', title: '出库单号', width: 140});
        cols.push({field: 'customer_name', title: '客户姓名', width: 100});
        cols.push({field: 'customer_tel', title: '客户电话', width: 150});
        cols.push({field: 'warehouse_text', title: '仓别', width: 100});
        cols.push({field: 'total_count', align:'right', title: '合计数量', width: 150});
        if (is_has){
            cols.push({field: 'total_cost', align:'right', title: '合计成本', width: 150});
        }
        cols.push({field: 'total_amount', align:'right', title: '合计销售价', width: 150});
        cols.push({field: 'return_count', align:'right', title: '合计退库数量', width: 200});
        if (is_has){
            cols.push({field: 'return_cost', align:'right', title: '合计退库成本', width: 200});
        }
        cols.push({field: 'create_time', title: '创建时间', width: 180});
        cols.push({field: 'create_user_text', title: '创建人', width: 100});
        cols.push({field: 'notes', title: '备注', minWidth: 200});
        table.render({
            elem: '#datagrid'
            , url: '/order/deliver_return/data/'
            , cols: [cols]
            , page: true
            , height: 'full-104'
            ,totalRow:true
              ,parseData:function(res) {
                var cols = this.cols[0];
                for (var i in cols) {
                    if (cols[i].field == 'total_count') {
                        cols[i].totalRowText = res.more.total_count;
                    }
                    if (cols[i].field == 'total_cost') {
                        cols[i].totalRowText = res.more.total_cost;
                    }
                    if (cols[i].field == 'total_amount') {
                        cols[i].totalRowText = res.more.total_amount;
                    }
                    if (cols[i].field == 'return_count') {
                        cols[i].totalRowText = res.more.return_count;
                    }
                    if (cols[i].field == 'return_cost') {
                        cols[i].totalRowText = res.more.return_cost;
                    }
                }
                return {
                  "code": res.code, //解析接口状态
                  "count": res.count, //解析数据长度
                  "data": res.data //解析数据列表
                };
            }
        });
        //监听工具条
        $('#btn_add').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }
            var return_count = parseFloat(data.return_count);
            var total_count = parseFloat(data.total_count);
            if (return_count >= total_count){
                layer.msg('该出库单已退完');
                return false;
            }
            layer.open({
                type: 2,
                title: '退库',
                area: ['90%', '90%'],
                btn: ['保存', '取消'],
                yes: function (index, dom) {
                    layui.onSubmitChild = function (data) {
                        layer.close(index);
                        table.reload('datagrid', {});
                    };
                    layui.submitChild();
                },
                btn2: function (index, layero) {
                    layer.close(index);//关闭当前按钮
                },
                content: 'goods_deliver_return_edit.html?id=' + data.id
            });
        });
        $('#btn_query').on('click', function () {
            layer.open({
                type: 1,
                shadeClose: true,
                area: ['400px', '45%'],
                title: '查询',
                btn: ['查询'],
                yes: function (index, dom) {
                    $("#query-submit").click();
                },
                content: $('#dlg_query')
            });
        });
        form.on('submit(query-form-element)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            _params = data.field;
            table.reload('datagrid', {
                where: data.field,
                page:{curr:1}
            });
            layer.closeAll();
            return false
        });
        $('#btn_print').on('click', function(){
          var data = utils.getSelectedRow('datagrid');
          if(data == false)
              return;

          table.editdata = data;

          table.render({
              elem: '#dg-print'
              ,url: '/order/deliver_return/select_data/?id=' + data.id
              ,cols: [[
                  {type:'checkbox',width:50},
                  {field:'no', title:'单号', width:200},
                  {field:'create_time', title:'时间', width:150}
              ]]
              ,page: false
          });

          layer.open({
              type: 1,
              shadeClose: true,
              area: ['500px', '80%'],
              title: '选择退库单',
              btn: ['打印', '取消'],
              yes: function(index, dom){
                 var checkStatus = table.checkStatus('dg-print')
                 ,data = checkStatus.data;

                 if (data.length == 0) {
                     layer.msg('请至少选择一个退库单');
                     return false;
                 }
                 var ids = [];
                 for(var n in data){
                     ids.push(data[n].id)
                 }

                 layer.open({
                      type: 2,
                      title: '打印',
                      area: ['90%', '90%'],
                      btn: ['打印', '取消'],
                      yes: function (index, dom) {
                        layui.onSubmitChild = function(data){
                          layer.close(index);
                        };
                        layui.submitChild();
                    },
                      btn2: function(index, layero){
                        layer.close(index);
                      },
                      content: 'goods_deliver_return_print.html?ids=' + ids.join(',') + '&id=' + table.editdata.id
                 });
                 layer.close(index);
              },
              btn2: function(index, layero){
                layer.close(index);
              },
              content: $('#dlg_print')
          });
        });
    })
</script>
</body>
</html>