<!DOCTYPE html>
<html>
<head>
    <:include file="../common/css.html" title="供应商管理"/>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar" lay-filter="supplierTbSearchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">名&emsp;称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入供应商名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">联系方式:</label>
                        <div class="layui-input-inline">
                            <input name="contact" class="layui-input" placeholder="输入联系方式"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="supplierTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="supplierTable" lay-filter="supplierTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="supplierTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs"
       data-dropdown="#supplierTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide"
         id="supplierTbDelDrop{{d.LAY_INDEX}}"
         style="max-width: 200px;white-space: normal;min-width: auto;margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除{{d.name}}吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal" lay-event="del">确定</a>
        </div>
    </div>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="supplierEditDialog">
    <form id="supplierEditForm" lay-filter="supplierEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入供应商名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式:</label>
            <div class="layui-input-block">
                <input name="contact" placeholder="请输入联系方式" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">余额:</label>
            <div class="layui-input-block">
                <input name="balance" placeholder="请输入余额" class="layui-input" value="0"/>
            </div>
        </div>
        <div class="layui-form-item" id="usernameItem">
            <label class="layui-form-label layui-form-required">登录账号:</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入登录账号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" id="passwordItem">
            <label class="layui-form-label layui-form-required">登录密码:</label>
            <div class="layui-input-block">
                <input name="password" type="password" placeholder="请输入登录密码" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户昵称:</label>
            <div class="layui-input-block">
                <input name="nickName" placeholder="默认使用供应商名称" class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="supplierEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<:include file="../common/js.html"/>
<script>
    layui.use(['layer', 'form', 'table', 'tableX', 'util', 'admin', 'formX', 'dropdown'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var util = layui.util;
        var admin = layui.admin;

        /* 渲染表格 */
        var insTb = tableX.render({
            elem: '#supplierTable',
            url: 'supplier/page',
            page: true,
            height: 'full-100',
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'name', title: '供应商名称', sort: true},
                {field: 'username', title: '登录账号', sort: true, width: 150},
                {field: 'contact', title: '联系方式', sort: true},
                {field: 'balance', title: '余额', sort: true, width: 120},
                {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true, width: 180
                },
                {title: '操作', toolbar: '#supplierTbBar', align: 'center', minWidth: 220, fixed: 'right'}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(supplierTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(supplierTable)', function (obj) {
            if ('edit' === obj.event) {
                showEditModel(obj.data);
            } else if ('del' === obj.event) {
                doDel(obj);
            } else if ('reset' === obj.event) {
                admin.prompt({formType: 1, title: '请输入新密码'}, function (value, i) {
                    layer.close(i);
                    var loadIndex = layer.load(2);
                    $.post('supplier/psw/reset', {id: obj.data.id, password: value}, function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 2, anim: 6});
                        }
                    });
                });
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(supplierTable)', function (obj) {
            if ('add' === obj.event) {
                showEditModel();
            } else if ('del' === obj.event) {
                var checkRows = table.checkStatus('supplierTable');
                if (!checkRows || !checkRows.data || 0 === checkRows.data.length) {
                    return layer.msg('请选择要删除的数据', {icon: 2, anim: 6});
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '供应商',
                content: $('#supplierEditDialog').html(),
                success: function (layero, dIndex) {
                    // 编辑模式隐藏账号和密码字段
                    if (mData) {
                        $(layero).find('#usernameItem').remove();
                        $(layero).find('#passwordItem').remove();
                    }
                    form.val('supplierEditForm', mData);
                    form.on('submit(supplierEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(mData ? 'supplier/update' : 'supplier/save', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2, anim: 6});
                            }
                        });
                        return false;
                    });
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            if (obj.ids) {
                admin.confirm('确定要删除选中数据吗？', function (i) {
                    layer.close(i);
                    var loadIndex = layer.load(2);
                    admin.req('supplier/removeBatch', JSON.stringify(obj.ids), function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2, anim: 6});
                        }
                    }, 'post');
                });
            } else {
                var loadIndex = layer.load(2);
                $.get('supplier/remove', {id: obj.data.id}, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2, anim: 6});
                    }
                });
            }
        }

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