<!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>
<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>
<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" id="btn_add" data-permission="order.add_sale_order"><i
                                class="layui-icon layui-icon-add-circle"></i>添加
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_deliver"
                                data-permission="order.add_goods_deliver"><i
                                class="layui-icon layui-icon-add-circle"></i>转出库
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_loss" data-permission="order.loss_sale_order"><i
                                class="layui-icon layui-icon-layer"></i>扣减
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_receive"><i
                                class="layui-icon layui-icon-ok-circle"></i>完善发货
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_ship" data-permission="order.pay_sale_order"><i
                                class="layui-icon layui-icon-dollar"></i>装运
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_pay" data-permission="order.pay_sale_order"><i
                                class="layui-icon layui-icon-dollar"></i>结算
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_clear" data-permission="order.pay_sale_order"><i
                                class="layui-icon layui-icon-dollar"></i>结清
                        </button>

                        <button class="layui-nav" data-permission="order.export_sale_order">
                            <div class="layui-nav-item">
                                <a href="javascript:;" style="color:#fff;">导出</a>
                                <dl class="layui-nav-child"> <!-- 二级菜单 -->
                                    <dd><a href="#" id="btn_download">导出列表</a></dd>
                                    <dd><a href="#" id="btn_download_detail">导出明细</a></dd>
                                </dl>
                            </div>
                        </button>
                        <button class="layui-nav" data-permission="order.export_sale_order">
                            <div class="layui-nav-item">
                                <a href="javascript:;" style="color:#fff;">导入</a>
                                <dl class="layui-nav-child"> <!-- 二级菜单 -->
                                    <dd><a href="#" id="import_order">执行导入</a></dd>
                                    <dd><a href="#" id="btn_download_tmp">下载模板</a></dd>
                                </dl>
                            </div>
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_print" data-permission="order.print_sale_order">
                            <i class="layui-icon layui-icon-form"></i>打印
                        </button>
                        <button class="layui-btn layui-btn-sm" id="btn_query"><i
                                class="layui-icon layui-icon-search"></i>查询
                        </button>
                    </div>

                    <table class="layui-hide" id="datagrid" lay-filter="datagrid-operate"></table>

                    <script type="text/html" id="datagrid-operate-bar">
                        <div class="layui-btn-group">
                            <a class="layui-btn layui-btn-xs" lay-event="detail"
                               data-permission="order.view_sale_order">查看</a>
                            {{# if(d.status ==0){ }}
                            <a class="layui-btn layui-btn-xs" lay-event="edit"
                               data-permission="order.add_sale_order">修改</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                               data-permission="order.delete_sale_order">删除</a>
                            <a class="layui-btn layui-btn-xs" lay-event="check_pass"
                               data-permission="order.check_sale_order">审核</a>
                            {{# } }}
                            {{# if(d.status ==1){ }}
                            <a class="layui-btn layui-btn-xs" lay-event="check_revoke"
                               data-permission="order.check_sale_order">撤销审核</a>
                            {{# } }}
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="dlg_query" style="display: none">
    <form class="layui-form" lay-filter="query-form-element">
        <div class="layui-card-body" pad15>
            <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-inline">
                        <input type="text" class="layui-input" autocomplete="off" name="create_time" id="create_time"
                               placeholder=" - ">
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">下单人：</label>
                    <div class="layui-input-block">
                        <input type="text" autocomplete="off" name="create_user" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">状态：</label>
                    <div class="layui-input-block">
                        <select id="id_check_status" name="status">
                            <option value="" selected></option>
                            <option value="0">待审核</option>
                            <option value="1">已审核</option>
                        </select>
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">订单号：</label>
                    <div class="layui-input-block">
                        <input type="text" autocomplete="off" name="no" 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" autocomplete="off" name="customer_name" 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" autocomplete="off" name="customer_tel" class="layui-input">
                    </div>
                </div>
                <div class="layui-col-xs12 layui-col-sm12">
                    <label class="layui-form-label">结清：</label>
                    <div class="layui-input-block">
                        <select name="cleared">
                            <option value="" selected>全部</option>
                            <option value="0">未结清</option>
                            <option value="1">已结清</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="display: none">
                <button id="query_search" class="layui-btn" lay-submit lay-filter="query-form-element">查询</button>
            </div>
        </div>
    </form>
</div>

<script src="../../layuiadmin/layui/layui.js?t=1"></script>
<script>
    var _params = '';
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'laydate', 'form',  'upload', 'element', 'utils'], function () {
        var $ = layui.$;
        var table = layui.table
            , laydate = layui.laydate
            , form = layui.form
            , utils = layui.utils
            , upload = layui.upload;

        table.render({
            elem: '#datagrid'
            , url: '/order/sale_order/data/'
            , cols: [[
                {type: 'radio', width: 50}
                , {field: 'no', title: '单号', width: 150}
                , {field: 'customer_name', title: '客户', width: 120}
                , {field: 'customer_tel', title: '客户电话', width: 150}
                , {field: 'products', title: '产品', width: 120}
                , {field: 'count', title: '合计数量', width: 100, align: 'right'}
                , {field: 'amount', title: '合计金额', width: 130, align: 'right'}
                , {field: 'receive_count', title: '收货数量', width: 100, align: 'right'}
                , {field: 'receive_amount', title: '收货金额', width: 130, align: 'right'}
                , {field: 'loss_amount', title: '扣减金额', width: 130, align: 'right'}
                , {field: 'total_amount', title: '应付金额', width: 130, align: 'right'}
                , {field: 'pay_amount', title: '结算金额', width: 130, align: 'right'}
                , {field: 'put_amount', title: '装车费用', width: 130, align: 'right'}
                , {field: 'fare_amount', title: '运输费用', width: 130, align: 'right'}
                , {field: 'clear_text', title: '结清', width: 60}
                , {field: 'loss_notes', title: '扣减备注', width: 100, align: 'right'}
                , {field: 'create_time', title: '创建时间', width: 150}
                , {field: 'create_user_text', title: '创建人', width: 150}
                , {field: 'check_status_text', title: '审核状态', width: 120}
                , {field: 'check_time', title: '审核时间', width: 150}
                , {field: 'check_user_text', title: '审核人', width: 120}
                , {field: 'notes', title: '备注', minWidth: 200}
                , {width: 200, align: 'left', fixed: 'right', toolbar: '#datagrid-operate-bar'}
            ]]
            , page: true
            , totalRow: true
            , height: 'full-104'
            , parseData: function (res) {
                var cols = this.cols[0];
                for (var i in cols) {
                    if (cols[i].field == 'count') {
                        cols[i].totalRowText = res.more.sum_count;
                    }

                    if (cols[i].field == 'amount') {
                        cols[i].totalRowText = res.more.sum_amount;
                    }
                    if (cols[i].field == 'receive_count') {
                        cols[i].totalRowText = res.more.sum_receive_count;
                    }
                    if (cols[i].field == 'receive_amount') {
                        cols[i].totalRowText = res.more.sum_receive_amount;
                    }
                    if (cols[i].field == 'loss_amount') {
                        cols[i].totalRowText = res.more.sum_loss_amount;
                    }
                    if (cols[i].field == 'total_amount') {
                        cols[i].totalRowText = res.more.sum_total_amount;
                    }
                    if (cols[i].field == 'pay_amount') {
                        cols[i].totalRowText = res.more.sum_pay_amount;
                    }
                    if (cols[i].field == 'put_amount') {
                        cols[i].totalRowText = res.more.sum_put_amount;
                    }
                    if (cols[i].field == 'fare_amount') {
                        cols[i].totalRowText = res.more.sum_fare_amount;
                    }
                }
                return {
                    "code": res.code, //解析接口状态
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            , done: function () {
                layui.index.removeNoPermButtons();
            }
        });

        //监听工具条
        table.on('tool(datagrid-operate)', function (obj) {
            var data = obj.data;
            table.editdata = data;
            if (obj.event === 'detail') {
                layer.open({
                    type: 2,
                    title: '查看',
                    shadeClose: true,
                    area: ['90%', '90%'],
                    content: 'sale_order_detail.html?id=' + data.id
                });
            } else if (obj.event === 'del') {
                if (data.status != 0) {
                    layer.msg('该订单已审核,不允许删除');
                    return;
                }
                layer.confirm('确定要删除吗？', function (index) {
                    layer.close(index);
                    layui.admin.req({
                        url: '/order/sale_order/delete/?id=' + data.id
                        , done: function (res) {
                            table.reload('datagrid', {});
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                if (data.status != 0) {
                    layer.msg('该订单已审核,不允许修改');
                    return;
                }
                table.editdata = data;
                layer.open({
                    type: 2,
                    title: '修改订单[' + data.no + ']',
                    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: 'sale_order_edit.html?id=' + data.id
                });
            } else if (obj.event === 'check_pass') {
                if (data.status != 0) {
                    layer.msg('该出订单已审核, 禁止重复操作');
                    return;
                }
                layer.confirm('确定要通过审核吗？', function (index) {
                    layer.close(index);
                    layui.admin.req({
                        url: '/order/sale_order/check/?id=' + data.id + '&status=1'
                        , done: function (res) {
                            table.reload('datagrid', {});
                        }
                    });
                });
            } else if (obj.event === 'check_revoke') {
                if (data.status == 0) {
                    layer.msg('该出订单尚未审核');
                    return;
                }
                layer.confirm('确定要撤销审核吗？', function (index) {
                    layer.close(index);
                    layui.admin.req({
                        url: '/order/sale_order/check/?id=' + data.id + '&status=0'
                        , done: function (res) {
                            table.reload('datagrid', {});
                        }
                    });
                });
            }
        });

        $('#btn_add').on('click', function () {
            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: 'sale_order_edit.html'
            });
        });
        $('#btn_download').on('click', function () {
            layui.admin.req({
                url: '/order/sale_order/export/',
                data: _params,
                done: function (res) {
                    layui.view.download(res.data.filename);
                }
            });
        });

        $('#btn_download_detail').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }

            layui.admin.req({
                url: '/order/sale_order/export_detail/?id=' + data.id,
                done: function (res) {
                    layui.view.download(res.data.filename);
                }
            });
        });
        $('#btn_download_tmp').on('click', function () {
            layui.view.download("/static/xls/销售订单导入模板.xlsx");
        });
        upload.render({
            elem: '#import_order'
            , url: '/order/sale_order/import/'
            , accept: 'file'
            , acceptMime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            , exts: 'xlsx'
            , field: 'excel_file'
            , multiple: false
            , done: function (res) {
                if (res.code == 0) {
                    table.reload('datagrid', {});
                } else {
                    layer.msg(res.msg);
                }
            }
            , error: function () {
                layer.msg('导入失败');
            }
        });
        $('#btn_print').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }

            table.editdata = data;

            layer.open({
                type: 2,
                title: '打印',
                shadeClose: true,
                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: 'sale_order_print.html?id=' + data.id
            });
        });
        $('#btn_deliver').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }

            if (data.status == 0) {
                layer.msg('该订单未审核,不允许出库');
                return;
            }

            table.editdata = data;

            layer.open({
                type: 2,
                title: '转出库',
                shadeClose: false,
                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_edit.html?order_id=' + data.id
            });
        });

        $('#btn_loss').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }

            if (data.status == 0) {
                layer.msg('该订单未审核,不允许扣减');
                return;
            }

            table.editdata = data;

            layer.open({
                type: 2,
                title: '扣减[' + data.no + ']',
                shadeClose: false,
                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: 'sale_order_loss.html?order_id=' + data.id
            });
        });

        $('#btn_pay').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }

            if (data.status == 0) {
                layer.msg('该订单未审核，不允许收银');
                return;
            }

            table.editdata = data;

            layer.open({
                type: 2,
                title: '付款[' + data.no + ']',
                shadeClose: false,
                area: ['40%', '80%'],
                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: 'sale_order_pay.html?order_id=' + data.id
            });
        });

        $('#btn_ship').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }

            table.editdata = data;

            layer.open({
                type: 2,
                title: '添加装运费[' + data.no + ']',
                shadeClose: false,
                area: ['80%', '70%'],
                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: 'sale_order_fare.html?order_id=' + data.id
            });
        });


        $('#btn_receive').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }

            if (data.status == 0) {
                layer.msg('该订单未审核');
                return;
            }

            table.editdata = data;

            layer.open({
                type: 2,
                title: '完善发货信息[' + data.no + ']',
                shadeClose: false,
                area: ['40%', '80%'],
                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: 'sale_order_message.html?order_id=' + data.id
            });
        });

        $('#btn_clear').on('click', function () {
            var data = utils.getSelectedRow('datagrid');
            if (data == false) {
                return;
            }

            if (data.status === 0) {
                layer.msg('该订单未审核');
                return;
            }
            if (data.cleared) {
                layer.msg('该订单已结清');
                return;
            }
            layer.confirm('确定要结清吗？', function (index) {
                layer.close(index);
                layui.admin.req({
                    url: '/order/sale_order/clear/?id=' + data.id
                    , done: function (res) {
                        table.reload('datagrid', {});
                    }
                });
            });

        });

        $('#btn_query').on('click', function () {
            layer.open({
                type: 1,
                shadeClose: true,
                area: ['500px', '450px'],
                title: '查询',
                btn: ['查询'],
                yes: function (index, dom) {
                    $('#query_search').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
        });

        laydate.render({
            elem: '#create_time'
            , range: true
        });
    });
</script>
</body>
</html>

