<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>商品销售管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/resources/favicon.ico">
    <link rel="stylesheet" href="resources/layui/css/layui.css" th:href="@{/resources/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/css/public.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/layui_ext/dtree/dtree.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/resources/layui_ext/dtree/font/dtreefont.css}" media="all"/>
    <style>
        input#search_provideridTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input#provideridTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input#leaderprovideridTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input.layui-input.layui-unselect {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input#search_goodsidTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input#goodsidTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input#leadergoodsidTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input.layui-input.layui-unselect {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
    </style>
</head>
<body>
<!--查询条件-->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 15px;">
    <legend>搜索条件</legend>
</fieldset>
<form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">客户名称</label>
            <div class="layui-input-inline">
                <select name="customerid" id="select_customerid">
                    <option value="0">请选择客户</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-inline">
                <select name="goodsid" id="select_goodsid">
                    <option value="0">请选择商品</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-inline">
                <input type="text" name="startTime" id="startTime" readonly="readonly" placeholder="yyyy-MM-dd"
                       class="layui-input input-radius">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-inline">
                <input type="text" name="endTime" id="endTime" readonly="readonly" placeholder="yyyy-MM-dd"
                       class="layui-input input-radius">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block" style="text-align: center;padding-right: 15%;">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" lay-submit="" lay-filter="doSearch"><i
                    class="layui-icon layui-icon-search layui-icon-normal"></i>查询
            </button>
            <button type="reset" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-warm"><i
                    class="layui-icon layui-icon-refresh"></i><span>重置</span>
            </button>
        </div>
    </div>
</form>

<!--数据表格-->
<div>
    <table class="layui-hide" id="salesTable" lay-filter="salesTable"></table>
    <div id="salesToolBar" style="display: none">
        <button type="button" lay-event="add" shiro:hasPermission="sales:create" class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius">
            <i class="layui-icon layui-icon-add-1"></i>添加销售
        </button>
    </div>
    <div id="salesRowBar" style="display: none;">
        <button type="button" lay-event="update" shiro:hasPermission="sales:update" class="layui-btn layui-btn-sm layui-btn-radius"><i
                class="layui-icon layui-icon-edit"></i>编辑
        </button>
        <button type="button" lay-event="delete" shiro:hasPermission="sales:delete" class="layui-btn layui-btn-sm layui-btn-danger layui-btn-radius"><i
                class="layui-icon layui-icon-delete"></i>删除
        </button>
        <button type="button" lay-event="back" shiro:hasPermission="sales:back" class="layui-btn layui-btn-sm layui-btn-warm layui-btn-radius"><i
                class="layui-icon layui-icon-delete"></i>退货
        </button>
    </div>
</div>

<!--添加和修改弹出层-->
<div id="addOrUpdateDiv" style="display: none;padding: 10px;padding-right: 5%">
    <form action="" method="post" class="layui-form" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户名称</label>
                <div class="layui-input-inline">
                    <select name="customerid" id="customerid" lay-filter="customerid">
                        <option value="0">请选择客户</option>
                    </select>
                    <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="goodsid">
                        <option value="0">请选择商品</option>
                    </select>
                </div>
                <div class="mydiv" title="不可修改" style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"> </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">销售数量</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id">
                    <input type="text" name="number" lay-verify="required|number" autocomplete="off" class="layui-input input-radius" placeholder="请输入销售数量">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">销售价格</label>
                <div class="layui-input-block">
                    <input type="text" name="saleprice" lay-verify="required|number" autocomplete="off" class="layui-input input-radius" placeholder="请输入销售价格">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">支付类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="paytype" value="微信" title="微信">
                    <input type="radio" name="paytype" value="支付宝" title="支付宝" checked>
                    <input type="radio" name="paytype" value="银联" title="银联">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">销售备注</label>
                <div class="layui-input-block">
                    <textarea name="remark" placeholder="请输入销售备注" class="layui-textarea" cols="200" rows="8"></textarea>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 7%">
                <button type="button" class="layui-btn layui-btn-radius" lay-submit="" lay-filter="doSubmit" id="doSubmit"><i
                        class="layui-icon layui-icon-search layui-icon-normal"></i>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-radius layui-btn-warm"><i
                        class="layui-icon layui-icon-refresh"></i><span>重置</span>
                </button>
            </div>
        </div>
    </form>
</div>

<!--退货的弹出层-->
<div id="backGoodsDiv" style="display: none;padding-right: 3%">
    <form action="" method="post" class="layui-form" id="dataFrmBack" lay-filter="dataFrmBack">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">退货数量</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id">  <!-- 销售单ID  -->
                    <input type="hidden" name="currentNumber" id="currentNumber">  <!-- 销售单中销售数量  -->
                    <input type="text" name="number" lay-verify="required|checkNumber" autocomplete="off" class="layui-input input-radius" placeholder="请输入退货数量">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">退货备注</label>
                <div class="layui-input-block">
                    <textarea name="remark" placeholder="请输入退货备注" class="layui-textarea" cols="200" rows="8"></textarea>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 7%">
                <button type="button" class="layui-btn layui-btn-radius" lay-submit="" lay-filter="doBackSubmit" id="doBackSubmit"><i
                        class="layui-icon layui-icon-search layui-icon-normal"></i>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-radius layui-btn-warm"><i
                        class="layui-icon layui-icon-refresh"></i><span>重置</span>
                </button>
            </div>
        </div>
    </form>
</div>


<script type="text/javascript" src="/resources/layui/layui.js"></script>

<script type="text/javascript">

    //提升数据表格的作用域，因为底下还有一个reloadTable方法
    var tableIns; // 定义表格实例变量

    layui.use(['jquery', 'form', 'layer', 'table', 'laydate'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var table = layui.table;
        var laydate = layui.laydate;

        // 初始化时间选择器
        laydate.render({
            elem: '#startTime',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime'
        });

        // 初始化表格并加载数据
        tableIns = table.render({
            elem: "#salesTable",
            title: "销售数据表格",
            url: "/sales/loadAllSales",
            toolbar: "#salesToolBar",
            page: true,
            height: "full-180",
            cols: [[
                {field: 'id', title: 'ID', align: 'center', width: '50'},
                {field: 'customername', title: '客户名称', align: 'center', width: '100'},
                {field: 'goodsname', title: '商品名称', align: 'center', width: '150'},
                {field: 'paytype', title: '支付类型', align: 'center', width: '90'},
                {field: 'salestime', title: '销售时间', align: 'center', width: '170'},
                {field: 'operateperson', title: '操作员', align: 'center', width: '130'},
                {field: 'number', title: '销售数量', align: 'center', width: '100'},
                {field: 'size', title: '商品规格', align: 'center', width: '100'},
                {field: 'saleprice', title: '销售价格', align: 'center', width: '120'},
                {field: 'remark', title: '备注', align: 'center', width: '120'},
                {fixed: 'right', title: '操作', toolbar: '#salesRowBar', align: 'center', width: '260'}
            ]],
            done: function (data, curr, count) {
                // 如果不是第一页且当前返回的数据为空，则返回上一页
                if (data.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: curr - 1
                        }
                    });
                }
            }
        });

        // 监控模糊查询按钮事件
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false; // 阻止表单提交
        });

        // 监控工具条事件
        table.on("toolbar(salesTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddLayer(); // 打开添加弹出层
                    break;
            }
            ;
        });

        // 监控行工具条事件
        table.on("tool(salesTable)", function (obj) {
            var data = obj.data; // 获取当前行数据
            switch (obj.event) {
                case 'delete':
                    deleteSales(data); // 删除销售记录
                    break;
                case 'update':
                    updateSales(data); // 打开修改弹出层
                    break;
                case 'back':
                    backSales(data); // 回滚销售记录
                    break;
            }
            ;
        });

        // 初始化客户名称的下拉列表
        $.get("/customer/loadAllCustomerForSelect", function (res) {
            var data = res.data;
            var dom = $("#select_customerid");
            var html = '<option value="0">请选择客户</option>';
            $.each(data, function (index, item) {
                html += '<option value="' + item.id + '">' + item.customername + '</option>';
            });
            dom.html(html);
            form.render("select"); // 重新渲染下拉列表
        });

        // 初始化商品名称的下拉列表
        $.get("/goods/loadAllGoodsForSelect", function (res) {
            var data = res.data;
            var dom = $("#select_goodsid");
            var html = '<option value="0">请选择商品</option>';
            $.each(data, function (index, item) {
                html += '<option value="' + item.id + '">' + item.goodsname + '-[' + item.size + ']-' + '[' + item.providername + ']' + '</option>';
            });
            dom.html(html);
            form.render("select"); // 重新渲染下拉列表
        });

        var mainIndex; // 主窗口索引
        var url; // URL地址变量

        // 打开添加弹出层函数
        function openAddLayer() {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['700px', '500px'],
                title: '添加销售',
                success: function () {
                    $("#dataFrm")[0].reset(); // 重置表单数据
                    initCustomerSelect(); // 初始化客户名称下拉列表
                    $(".mydiv").hide(); // 隐藏特定元素
                    url = "/sales/addSales"; // 设置URL地址为添加销售接口
                }
            });
        }

        // 打开修改弹出层函数
        function updateSales(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['700px', '500px'],
                title: '修改商品销售',
                success: function () {
                    $("#dataFrm")[0].reset(); // 清空原有数据
                    form.val("dataFrm", data); // 装载新的数据到表单中
                    initCustomerSelect(data.customerid); // 初始化客户名称下拉列表并选中当前客户
                    $.get("/goods/loadAllGoodsForSelect", function (res) {
                        var redata = res.data;
                        var dom = $("#goodsid");
                        var html = '<option value="0">请选择供应商</option>';
                        $.each(redata, function (index, item) {
                            if (data.goodsid === item.id) {
                                html += '<option value="' + item.id + '" selected>' + item.goodsname + '</option>';
                            } else {
                                html += '<option value="' + item.id + '">' + item.goodsname + '</option>';
                            }
                        });
                        dom.html(html); // 更新商品下拉列表选项
                        form.render("select"); // 重新渲染下拉列表
                    });
                    $(".mydiv").show(); // 显示特定元素
                    url = "/sales/updateSales"; // 设置URL地址为更新销售接口
                }
            });
        }

        // 初始化添加和修改页面的下拉列表
        function initCustomerSelect(customerid) {
            // 重置客户的下拉列表
            var dom = $("#customerid");
            dom.html("");
            $.get("/customer/loadAllCustomerForSelect", function (res) {
                var data = res.data;
                var html = '<option value="0">请选择客户</option>';
                $.each(data, function (index, item) {
                    html += '<option value="' + item.id + '">' + item.customername + '</option>';
                });
                dom.html(html);
                // 如果 customerid 有值就进行反选
                if (customerid != undefined) {
                    dom.val(customerid);
                }
                // 重新渲染下拉列表
                form.render("select");
            });
        };

// 监听客户的下拉列表的事件
        form.on('select(customerid)', function (data) {
            var customerid = data.value;
            initGoodsSelect(customerid);
        });

// 加载商品的下拉列表
        function initGoodsSelect(goodsid) {
            $.get("/goods/loadAllGoodsForSelect", function (res) {
                var data = res.data;
                var dom = $("#goodsid");
                var html = '<option value="0">请选择商品</option>';
                $.each(data, function (index, item) {
                    html += '<option value="' + item.id + '">' + item.goodsname + '-[' + item.size + ']-' + '[' + item.providername + ']' + '</option>';
                });
                dom.html(html);
                // 如果 goodsid 有值就进行反选
                if (goodsid != undefined) {
                    dom.val(goodsid);
                }
                // 重新渲染下拉列表
                form.render("select");
            });
        }

// 提交表单事件
        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (res) {
                if (res.code == 200) {
                    tableIns.reload(); // 重新加载表格数据
                }
                layer.msg(res.msg); // 显示操作结果消息
                layer.close(mainIndex); // 关闭当前弹出层
            });
            return false; // 阻止表单默认提交行为
        });

// 删除销售信息
        function deleteSales(data) {
            layer.confirm('你确定要删除这条销售信息吗？', {icon: 3, title: '提示'}, function (index) {
                $.post("/sales/deleteSales", {id: data.id}, function (res) {
                    if (res.code == 200) {
                        tableIns.reload({where: "",}); // 重新加载表格数据
                    }
                    layer.msg(res.msg); // 显示操作结果消息
                });
                layer.close(index); // 关闭确认框
            });
        }

// 打开退货的弹出层
        function backSales(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#backGoodsDiv"),
                area: ['600px', '400px'],
                title: '商品销售退货',
                success: function () {
                    $("#dataFrmBack")[0].reset(); // 清空原有的数据
                    // 装载新的数据，获得销售的ID以及当前销售单的销售数量
                    form.val("dataFrmBack", {id: data.id, currentNumber: data.number});
                }
            });
        }

// 自定义验证规则
        form.verify({
            checkNumber: function (value) {
                var currentNumber = $("#currentNumber").val();
                // value为退货的数量，currentNumber为当前销售单的数量
                if (parseInt(value) > currentNumber || parseInt(value) < 1) {
                    return '退货数量只能在【0】-【' + currentNumber + '】之间';
                }
            }
        });

// 退货提交事件
        form.on("submit(doBackSubmit)", function (data) {
            $.post("/salesback/addSalesback", data.field, function (res) {
                if (res.code == 200) {
                    tableIns.reload(); // 重新加载表格数据
                    layer.close(mainIndex); // 关闭退货弹出层
                }
                layer.msg(res.msg); // 显示操作结果消息
            });
            return false; // 阻止表单默认提交行为
        })
    });



</script>

</body>
</html>