<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('RFID标签列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>标签编码：</label>
                                <input type="text" name="tagCode"/>
                            </li>
                            <li>
                                <label>EPC编码：</label>
                                <input type="text" name="epcCode"/>
                            </li>
                            <li>
                                <label>标签状态：</label>
                                <select name="tagStatus" th:with="type=${@dict.getType('ams_tag_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>绑定状态：</label>
                                <select name="bindStatus" th:with="type=${@dict.getType('ams_tag_bind_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>绑定资产编码：</label>
                                <input type="text" name="assetCode"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="asset:tag:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="asset:tag:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-success single disabled" onclick="bindSelectedTag()" shiro:hasPermission="asset:tag:bind">
                    <i class="fa fa-link"></i> 绑定
                </a>
                <a class="btn btn-warning single disabled" onclick="voidSelectedTag()" shiro:hasPermission="asset:tag:edit">
                    <i class="fa fa-ban"></i> 作废
                </a>
                <a class="btn btn-info single disabled" onclick="unbindSelectedTag()" shiro:hasPermission="asset:tag:edit">
                    <i class="fa fa-unlink"></i> 解绑
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="asset:tag:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="asset:tag:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('asset:tag:edit')}]];
        var bindFlag = [[${@permission.hasPermi('asset:tag:bind')}]];
        var removeFlag = [[${@permission.hasPermi('asset:tag:remove')}]];
        var tagStatusDatas = [[${@dict.getType('ams_tag_status')}]];
        var bindStatusDatas = [[${@dict.getType('ams_tag_bind_status')}]];
        var prefix = ctx + "asset/tag";

        $(function() {
            var options = {
                url: prefix + "/list",
                viewUrl: prefix + "/view/{id}",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "RFID标签",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'tagId',
                    title: '标签ID',
                    visible: false
                },
                {
                    field: 'tagCode',
                    title: '标签编码'
                },
                {
                    field: 'epcCode',
                    title: 'EPC编码'
                },
                {
                    field: 'tagStatus',
                    title: '标签状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(tagStatusDatas, value);
                    }
                },
                {
                    field: 'bindStatus',
                    title: '绑定状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(bindStatusDatas, value);
                    }
                },
                {
                    field: 'assetCode',
                    title: '绑定资产编码'
                },
                {
                    field: 'assetName',
                    title: '绑定资产名称'
                },
                {
                    field: 'bindTime',
                    title: '绑定时间'
                },
                {
                    field: 'bindUserName',
                    title: '绑定人'
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="$.operate.view(\'' + row.tagId + '\')"><i class="fa fa-eye"></i>查看</a> ');
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.tagId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        if (row.tagStatus === 'NORMAL' && row.bindStatus === 'UNBOUND') {
                            actions.push('<a class="btn btn-success btn-xs ' + bindFlag + '" href="javascript:void(0)" onclick="bindTag(\'' + row.tagId + '\')"><i class="fa fa-link"></i>绑定</a> ');
                        }
                        if (row.tagStatus !== 'VOID' && row.bindStatus !== 'BOUND') {
                            actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="voidTag(\'' + row.tagId + '\')"><i class="fa fa-ban"></i>作废</a> ');
                        }
                        if (row.bindStatus === 'BOUND') {
                            actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="unbindTag(\'' + row.tagId + '\')"><i class="fa fa-unlink"></i>解绑</a> ');
                        }
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.tagId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function voidSelectedTag() {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if (row) {
                voidTag(row.tagId);
            }
        }

        function bindSelectedTag() {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if (!row) {
                return;
            }
            if (row.tagStatus !== 'NORMAL' || row.bindStatus !== 'UNBOUND') {
                $.modal.alertWarning("只有正常且未绑定的RFID标签可以绑定资产");
                return;
            }
            bindTag(row.tagId);
        }

        function bindTag(tagId) {
            $.modal.open("绑定RFID标签", prefix + "/bind/" + tagId, "800", "620");
        }

        function unbindSelectedTag() {
            var row = $("#bootstrap-table").bootstrapTable('getSelections')[0];
            if (row) {
                unbindTag(row.tagId);
            }
        }

        function voidTag(tagId) {
            $.modal.confirm("确认作废该RFID标签吗？作废后的标签不能再用于绑定资产。", function() {
                $.operate.post(prefix + "/void/" + tagId, {});
            });
        }

        function unbindTag(tagId) {
            $.modal.confirm("确认解绑该RFID标签吗？解绑会同步清空资产台账中的RFID信息。", function() {
                $.operate.post(prefix + "/unbind/" + tagId, {});
            });
        }
    </script>
</body>
</html>
