<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('任务列表')"/>
    <link th:href="@{/ruoyi/css/ry-ui-custom.css?v=4.7.3}" rel="stylesheet"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row" style="height: auto !important;">
        <div class="col-sm-12 pr0 pl0 shadow">
            <div class="ibox float-e-margins ">
                <div class="ibox-title">
                    <div class="col-sm-8  pl0 pr0">
                        <h8>任务列表</h8>
                    </div>
                </div>
                <div class="ibox-content ibox-content-1" style="margin: 0; height: 350px">
                    <div class="btn-group-sm" id="toolbar" role="group">
                        <form id="formId">
                            <div class="select-list">
                                <ul>
                                    <li>
                                        <label>任务号：</label>
                                        <input type="text" name="taskId"/>
                                    </li>
                                    <li>
                                        <a class="btn btn-primary btn-rounded btn-sm"
                                           onclick="$.table.search('formId')"><i
                                                class="fa fa-search"></i>&nbsp;搜索</a>
                                        <a class="btn btn-warning btn-rounded btn-sm"
                                           onclick="$.form.reset('formId')"><i
                                                class="fa fa-refresh"></i>&nbsp;重置</a>
                                    </li>
                                </ul>
                            </div>
                        </form>
                    </div>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table" data-resizable="true"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var prefix = ctx + "business/taskMonitor";
    var inStatus = [[${@dict.getType('in_status')}]];
    var outStatus = [[${@dict.getType('out_status')}]];

    /**
     * 表格初始化
     */
    $(function () {
        var options = {
            url: prefix + "/list",
            modalName: "任务列表",
            showSearch: false,
            showToggle: false,
            showRefresh: false,
            showColumns: false,
            search: false,
            // TODO
            columns: [
                {
                    field: 'taskNo',
                    title: '任务编号'
                },
                {
                    field: 'taskId',
                    title: '任务号'
                },
                {
                    field: 'goodsId',
                    title: '物料编码'
                },
                {
                    field: 'opNumber',
                    title: '数量'
                },
                {
                    field: 'startLocation',
                    title: '起始货位'
                },
                {
                    field: 'targetLocation',
                    title: '目标货位'
                },
                {
                    field: 'ctl',
                    title: '托盘号'
                },
                {
                    field: 'taskMode',
                    title: '任务模式',
                    formatter: function (value, row, index) {
                        return value === "1" ? "入库" : "出库";
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if (row.taskMode === "1") {
                            return $.table.selectDictLabel(inStatus, value);
                        } else {
                            return $.table.selectDictLabel(outStatus, value);
                        }
                    },
                },
                {
                    field: 'taskTime',
                    title: '任务时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.taskMode === "1") {
                            actions.push('<button id="solve" class="btn btn-default btn-xs" onclick="agvComplete(\'' + row.taskNo + '\',\'' + row.taskMode + '\')">AGV完成</button>');
                        }
                        if (row.status !== "2") {
                            actions.push('<button id="solve" class="btn btn-default btn-xs" onclick="solveTask(\'' + row.taskNo + '\',\'' + row.taskMode + '\')">已处理</button>');
                            actions.push('<button id="del" class="btn btn-default btn-xs" onclick="delTask(\'' + row.taskNo + '\',\'' + row.taskMode + '\')">删除</button>');
                            if (row.status !== "0") {
                                actions.push('<button id="restart" class="btn btn-default btn-xs" onclick="reboot(\'' + row.taskNo + '\',\'' + row.taskMode + '\')">初始化</button>');
                            }
                        }
                        return actions.join('');
                    }
                },
            ]
        };
        $.table.init(options);
    });

    setInterval(function () {
        console.log("刷新任务列表");
        $.table.refresh();
    }, 2000);

    // 删除任务
    function delTask(taskNo, taskMode) {
        $.ajax({
            type: "post",
            url: prefix + "/delTask",
            data: {
                "taskNo": taskNo,
                "taskMode": taskMode
            },
            success: function (data) {
                console.log(data)
                if (data.code === 0) {
                    if (data.msg === "1") {
                        $.modal.msgSuccess('任务已删除')
                        $.table.refresh();
                    } else {
                        $.modal.msgError('错误')
                    }
                } else {
                    $.modal.msgError('错误')
                }
            },
            error: function () {
                $.modal.msgError('错误')
            }
        });
    }

    // 更改任务状态为已处理
    function solveTask(taskNo, taskMode) {
        $.ajax({
            type: "post",
            url: prefix + "/solveTask",
            data: {
                "taskNo": taskNo,
                "taskMode": taskMode
            },
            success: function (data) {
                console.log(data)
                if (data.code === 0) {
                    if (data.msg === "1") {
                        $.modal.msgSuccess('任务已处理')
                        $.table.refresh();
                    } else {
                        $.modal.msgError('错误')
                    }
                } else {
                    $.modal.msgError('错误')
                }
            },
            error: function () {
                $.modal.msgError('错误')
            }
        });
    }

    // AGV已完成
    function agvComplete(taskNo, taskMode) {
        $.ajax({
            type: "post",
            url: prefix + "/agvComplete",
            data: {
                "taskNo": taskNo,
                "taskMode": taskMode
            },
            success: function (data) {
                console.log(data)
                if (data.code === 0) {
                    if (data.msg === "1") {
                        $.modal.msgSuccess('agv任务已完成')
                        $.table.refresh();
                    } else {
                        $.modal.msgError('错误')
                    }
                } else {
                    $.modal.msgError('错误')
                }
            },
            error: function () {
                $.modal.msgError('错误')
            }
        });
    }

    // 重置任务状态
    function reboot(taskNo, taskMode) {
        $.ajax({
            type: "post",
            url: prefix + "/rebootTask",
            data: {
                "taskNo": taskNo,
                "taskMode": taskMode
            },
            success: function (data) {
                console.log(data)
                if (data.code === 0) {
                    if (data.msg === "1") {
                        $.modal.msgSuccess('任务已重置')
                        $.table.refresh();
                    } else {
                        $.modal.msgError('错误')
                    }
                } else {
                    $.modal.msgError('错误')
                }
            },
            error: function () {
                $.modal.msgError('错误')
            }
        });
    }
</script>
</body>
</html>
<style>
    .inner_row_style {
        margin-left: 0px;
        margin-right: 0px;
        padding-top: 5px;
    }
</style>