<!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('报废处置管理列表')" />
</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="disposalNo"/>
                            </li>
                            <li>
                                <label>申请人：</label>
                                <input type="text" name="applicantName"/>
                            </li>
                            <li>
                                <label>申请部门：</label>
                                <input type="text" name="applyDeptName"/>
                            </li>
                            <li>
                                <label>处置方式：</label>
                                <input type="text" name="disposalMethod"/>
                            </li>
                            <li>
                                <label>资产编码：</label>
                                <input type="text" name="assetCode"/>
                            </li>
                            <li class="select-time">
                                <label>处置时间：</label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginDisposalTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endDisposalTime]"/>
                            </li>
                            <li>
                                <label>单据状态：</label>
                                <select name="orderStatus" th:with="type=${@dict.getType('ams_disposal_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </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:disposal:add">
                    <i class="fa fa-plus"></i> 新增申请
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="asset:disposal: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:disposal:edit')}]];
        var removeFlag = [[${@permission.hasPermi('asset:disposal:remove')}]];
        var submitFlag = [[${@permission.hasPermi('asset:disposal:submit')}]];
        var confirmFlag = [[${@permission.hasPermi('asset:disposal:confirm')}]];
        var rejectFlag = [[${@permission.hasPermi('asset:disposal:reject')}]];
        var orderStatusDatas = [[${@dict.getType('ams_disposal_status')}]];
        var prefix = ctx + "asset/disposal";

        $(function() {
            var options = {
                url: prefix + "/list",
                viewUrl: prefix + "/view/{id}",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "报废处置单",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'orderId',
                    title: '单据ID',
                    visible: false
                },
                {
                    field: 'disposalNo',
                    title: '报废单号'
                },
                {
                    field: 'applicantName',
                    title: '申请人'
                },
                {
                    field: 'applyDeptName',
                    title: '申请部门'
                },
                {
                    field: 'disposalMethod',
                    title: '处置方式'
                },
                {
                    field: 'createTime',
                    title: '申请时间'
                },
                {
                    field: 'confirmUserName',
                    title: '确认人'
                },
                {
                    field: 'confirmTime',
                    title: '确认时间'
                },
                {
                    field: 'orderStatus',
                    title: '单据状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(orderStatusDatas, value);
                    }
                },
                {
                    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.orderId + '\')"><i class="fa fa-eye"></i>查看</a> ');

                        // 草稿状态允许编辑、提交、删除；已驳回是终态，只允许查看。
                        if (row.orderStatus === 'DRAFT') {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.orderId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-warning btn-xs ' + submitFlag + '" href="javascript:void(0)" onclick="submitOrder(\'' + row.orderId + '\')"><i class="fa fa-send"></i>提交</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.orderId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        }

                        // 待确认状态允许确认报废和驳回
                        if (row.orderStatus === 'PENDING_CONFIRM') {
                            actions.push('<a class="btn btn-success btn-xs ' + confirmFlag + '" href="javascript:void(0)" onclick="confirmOrder(\'' + row.orderId + '\')"><i class="fa fa-check"></i>确认报废</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + rejectFlag + '" href="javascript:void(0)" onclick="rejectOrder(\'' + row.orderId + '\')"><i class="fa fa-reply"></i>驳回</a> ');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function submitOrder(orderId) {
            $.modal.confirm("确认要提交该报废申请吗？", function() {
                $.post(prefix + "/submit/" + orderId, function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.msgSuccess(result.msg);
                        $.table.refresh();
                    } else {
                        $.modal.alertError(result.msg);
                    }
                });
            });
        }

        function confirmOrder(orderId) {
            $.modal.confirm("确认要同意并执行此报废处置吗？此操作将更改对应资产为已报废状态，并且不可回滚！", function() {
                $.post(prefix + "/confirm/" + orderId, function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.msgSuccess(result.msg);
                        $.table.refresh();
                    } else {
                        $.modal.alertError(result.msg);
                    }
                });
            });
        }

        function rejectOrder(orderId) {
            layer.prompt({
                formType: 2,
                title: '请输入驳回原因',
                value: '',
                maxlength: 500
            }, function(value, index, elem) {
                var rejectReason = value.trim();
                if ($.common.isEmpty(rejectReason)) {
                    $.modal.msgWarning("驳回原因不能为空");
                    return false;
                }
                if (rejectReason.length > 500) {
                    $.modal.msgWarning("驳回原因长度不能超过500个字符");
                    return false;
                }
                $.post(prefix + "/reject/" + orderId, { rejectReason: rejectReason }, function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.msgSuccess(result.msg);
                        layer.close(index);
                        $.table.refresh();
                    } else {
                        $.modal.alertError(result.msg);
                    }
                });
            });
        }
    </script>
</body>
</html>
