<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>Right</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;
        }
    </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="providerid" id="select_providerid">
                    <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="goodsname" id="goodsname" class="layui-input input-radius"
                       placeholder="请输入商品名称">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">生产批号</label>
            <div class="layui-input-inline">
                <input type="text" name="productcode" id="productcode" class="layui-input input-radius" placeholder="请输入生产批号">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">批准文号</label>
            <div class="layui-input-inline">
                <input type="text" name="promitcode" id="promitcode" 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-inline">
                <input type="text" name="description" id="description" class="layui-input input-radius"
                       placeholder="请输入商品描述">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">商品规格</label>
            <div class="layui-input-inline">
                <input type="text" name="size" id="size" class="layui-input input-radius" placeholder="请输入商品规格">
            </div>
        </div>
        <div class="layui-inline" style="padding-left: 3%">
            <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="goodsTable" lay-filter="goodsTable"></table>
    <div id="goodsToolBar" style="display: none">
        <button type="button" lay-event="add" shiro:hasPermission="goods: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="goodsRowBar" style="display: none;">
        <button type="button" lay-event="update" shiro:hasPermission="goods: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="goods:delete" class="layui-btn layui-btn-sm layui-btn-danger 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-col-md12 layui-col-xs12">
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md9 layui-col-xs7">
                    <div class="layui-form-item magt3">
                        <label class="layui-form-label">供应商</label>
                        <div class="layui-input-block">
                            <select name="providerid" id="providerid">
                                <option value="0">请选择供应商</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">商品名称</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="id">
                            <input type="text" class="layui-input input-radius" name="goodsname" lay-verify="required" placeholder="请输入商品名称">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">商品描述</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input input-radius" name="description" lay-verify="required" placeholder="请输入商品描述">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3 layui-col-xs5">
                    <div class="layui-upload-list thumbBox mag0 magt3">
                        <input type="hidden" name="goodsimg" id="goodsimg" value="images/noDefaultImage.jpg">
                        <img class="layui-upload-img thumbImg" src="/file/showImageByPath?path=images/noDefaultImage.jpg">
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <div class="layui-inline">
                    <label class="layui-form-label">产地</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input input-radius" name="produceplace" lay-verify="required" placeholder="请输入商品产地">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">包装</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input input-radius" name="goodspackage" lay-verify="required" placeholder="请输入商品包装">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">规格</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input input-radius" name="size" lay-verify="required" placeholder="请输入商品规格">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">生产批号</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input input-radius" name="productcode" lay-verify="required" placeholder="请输入商品生产批号">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">批准文号</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input input-radius" name="promitcode" lay-verify="required" placeholder="请输入商品批准文号">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">销售价格</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input input-radius" name="price" lay-verify="required|number" placeholder="请输入商品销售价格">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">库存量</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input input-radius" name="number" lay-verify="required|number" placeholder="请输入商品库存量">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">预警值</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input input-radius" name="dangernum" lay-verify="required|number" placeholder="请输入商品预警值">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">是否可用</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="available" value="1" title="可用" checked="">
                        <input type="radio" name="available" value="0" title="不可用" >
                    </div>
                </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>

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

<script type="text/javascript">

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

    layui.use(['jquery', 'form', 'layer', 'table','upload'], function () {
        var $ = layui.jquery; // 引入jQuery模块
        var form = layui.form; // 引入表单模块
        var layer = layui.layer; // 引入弹出层模块
        var table = layui.table; // 引入表格模块
        var upload = layui.upload; // 引入文件上传模块

        //初始化表格 加载数据
        tableIns = table.render({
            elem: "#goodsTable", // 绑定元素ID
            title: "商品数据表格", // 表格标题
            url: "/goods/loadAllGoods", // 数据接口
            toolbar: "#goodsToolBar", // 工具栏模板选择器
            page: true, // 开启分页
            height: "full-180", // 设置表格高度
            cols: [[ // 定义表头
                {field: 'id', title: 'ID', align: 'center', width: '50'},
                {field: 'goodsname', title: '商品名称', align: 'center', width: '150'},
                {field: 'providername', title: '供应商', align: 'center', width: '150'},
                {field: 'produceplace', title: '商品产地', align: 'center', width: '150'},
                {field: 'description', title: '商品描述', align: 'center', width: '150'},
                {field: 'price', title: '销售价格', align: 'center', width: '90'},
                {field: 'number', title: '数量', align: 'center', width: '90'},
                {field: 'dangernum', title: '库存预警值', align: 'center', width: '100'},
                {
                    field: 'goodsimg', title: '商品图片', align: 'center', width: '150', templet: function (d) {
                        return '<img width=40 height=40 src=/file/showImageByPath?path=' + d.goodsimg + ' />';
                    }
                },
                {field: 'size', title: '规格', align: 'center', width: '120'},
                {field: 'goodspackage', title: '包装', align: 'center', width: '120'},
                {field: 'productcode', title: '生产批号', align: 'center', width: '130'},
                {field: 'promitcode', title: '批准文号', align: 'center', width: '130'},
                {
                    field: 'available', title: '是否可用', align: 'center', width: '100', templet: function (d) {
                        return d.available == 1 ? '<font color="blue">可用</font>' : '<font color="red">不可用</font>';
                    }
                },
                {fixed: 'right', title: '操作', toolbar: '#goodsRowBar', align: 'center', width: '180'}
            ]],
            done: function (data, curr, count) {
                //不是第一页时，如果当前返回的数据为0那么就返回上一页
                if (data.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: curr - 1
                        }
                    })
                }
            }
        });

        //初始化查询条件的下拉列表
        $.get("/provider/loadAllProviderForSelect", function (res) {
            var data = res.data; // 获取返回的数据
            var dom = $("#select_providerid"); // 获取下拉列表的DOM元素
            var html = '<option value="0">请选择供应商</option>'; // 初始化下拉列表的HTML内容
            $.each(data, function (index, item) {
                html += '<option value="' + item.id + '">' + item.providername + '</option>'; // 动态添加选项
            })
            dom.html(html); // 将生成的HTML内容添加到下拉列表中
            form.render("select"); // 重新渲染下拉列表
        });


        //监控模糊查询按钮事件
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field, // 设置查询条件
                page: {
                    curr: 1 // 重置到第一页
                }
            });
            return false; // 阻止表单默认提交行为
        });

        // 监控工具条事件
        table.on("toolbar(goodsTable)", function (obj) {
            switch (obj.event) {
                case 'add': // 添加按钮事件
                    openAddLayer();
                    break;
                case 'deleteBatch': // 批量删除按钮事件
                    batchDeleteGoods();
                    break;
            }
        });

        //监控行工具条事件
        table.on("tool(goodsTable)", function (obj) {
            var data = obj.data; // 获取当前行数据
            switch (obj.event) {
                case 'delete': // 删除按钮事件
                    deleteGoods(data);
                    break;
                case 'update': // 更新按钮事件
                    updateGoods(data);
                    break;
            }
        });

        //文件上传
        upload.render({
            elem: '.thumbBox', // 绑定上传元素
            url: '/file/uploadFile', // 上传接口地址
            acceptMime: 'image/*', // 接受的文件类型
            field: 'mf', // 上传字段名
            method: "post", // 请求方式
            done: function (res, index, upload) {
                var path = res.path;
                $('.thumbImg').attr('src', '/file/showImageByPath?path=' + path);
                $('.thumbBox').css("background", "#fff");
                //给隐藏域赋值
                $("#goodsimg").val(path);
            }
        });

        var mainIndex; // 用于存储弹出层的索引
        var url; // 用于存储请求的URL地址

// 打开添加弹出层
        function openAddLayer() {
            mainIndex = layer.open({
                type: 1, // 弹出层类型为页面层
                content: $("#addOrUpdateDiv"), // 弹出层内容为id为addOrUpdateDiv的元素
                area: ['700px', '500px'], // 设置弹出层大小
                title: '添加商品', // 设置弹出层标题
                success: function () {
                    $("#dataFrm")[0].reset(); // 重置表单
                    url = "/goods/addGoods"; // 设置提交表单的URL
                    $.get("/provider/loadAllProviderForSelect", function (res) {
                        var data = res.data; // 获取供应商数据
                        var dom = $("#providerid"); // 获取供应商下拉列表元素
                        var html = '<option value="0">请选择供应商</option>'; // 初始化下拉列表选项
                        $.each(data, function (index, item) {
                            html += '<option value="' + item.id + '">' + item.providername + '</option>'; // 动态生成下拉列表选项
                        });
                        dom.html(html); // 将生成的选项添加到下拉列表中
                        form.render("select"); // 重新渲染下拉列表
                    });
                    // 设置默认图片
                    $(".thumbImg").attr("src", '/file/showImageByPath?path=images/noDefaultImage.jpg');
                    $("#goodsimg").val('/images/noDefaultImage.jpg'); // 设置默认图片路径到隐藏域
                }
            });
        }

// 打开修改的弹出层
        function updateGoods(data) {
            mainIndex = layer.open({
                type: 1, // 弹出层类型为页面层
                content: $("#addOrUpdateDiv"), // 弹出层内容为id为addOrUpdateDiv的元素
                area: ['700px', '500px'], // 设置弹出层大小
                title: '修改商品', // 设置弹出层标题
                success: function () {
                    $("#dataFrm")[0].reset(); // 重置表单
                    form.val("dataFrm", data); // 填充表单数据
                    $(".thumbImg").attr("src", '/file/showImageByPath?path=' + data.goodsimg); // 显示商品图片
                    $.get("/provider/loadAllProviderForSelect", function (res) {
                        var redata = res.data; // 获取供应商数据
                        var dom = $("#providerid"); // 获取供应商下拉列表元素
                        var html = '<option value="0">请选择供应商</option>'; // 初始化下拉列表选项
                        $.each(redata, function (index, item) {
                            if (data.providerid === item.id) {
                                html += '<option value="' + item.id + '" selected>' + item.providername + '</option>'; // 选中当前供应商
                            } else {
                                html += '<option value="' + item.id + '">' + item.providername + '</option>'; // 其他供应商选项
                            }
                        });
                        dom.html(html); // 将生成的选项添加到下拉列表中
                        form.render("select"); // 重新渲染下拉列表
                    });
                    url = "/goods/updateGoods"; // 设置提交表单的URL
                }
            });
        }

// 表单提交事件处理函数
        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 deleteGoods(data) {
            layer.confirm('你确定要删除【' + data.goodsname + '】这个商品吗？删除该商品，其商品进货、商品退货、商品销售、商品销售退货将会同步删除！', {
                icon: 3,
                title: '提示'
            }, function (index) {
                $.post("/goods/deleteGoods", {id: data.id, goodsimg: data.goodsimg}, function (res) {
                    if (res.code == 200) {
                        tableIns.reload({where: ""}); // 刷新表格数据
                    }
                    layer.msg(res.msg); // 显示操作结果消息
                });
                layer.close(index); // 关闭确认对话框
            });
        }
    })




</script>

</body>
</html>