{{include "com/inc.html"}}
<!-- Main Header -->
{{include "com/header.html"}}
<!-- Left side column. 权限菜单 -->
{{include "com/menu.html"}}
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            <small></small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="/front/project"><i class="fa fa-dashboard"></i> 项目列表</a></li>
            <li><a href="#"><i class="fa fa-dashboard"></i> 项目清洗</a></li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-body">

                        <div class="form-horizontal">
                            <div class="box-body margin">
                                <h3><i class="glyphicon glyphicon-exclamation-sign" style="margin-right: 6px"></i>数据情况
                                </h3>
                                <div class="form-group" style="margin-left: 15px">
                                    <span class="form-inline panel-body">分发总量/数据总量（条）：
                                        <input type="text" class="form-control" readonly value="{{.T.allGiveDataNum}}/{{.T.allDataNum}}">
                                    </span>
                                </div>
                                <div class="form-group" style="margin-left: 10px">
                                    <div class="col-xs-6" style="width: auto">
                                        <label class="form-inline">数据总量：
                                        <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.allDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">已分发：
                                        <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.allGiveDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">待分发：
                                        <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.allNoGiveDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">已标注：
                                        <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.allIsTagDataNum}}"></label>
                                    </div>
                                    <div class="col-xs-3 form-group">
                                        <label class="form-inline">操作：
                                            <input type="button" class="btn btn-info" onclick="dispatchTask('all')" value="分发">
                                            <input type="button" class="btn btn-primary" value="质检">
                                            <input type="button" class="btn btn-success" value="质检结果">
                                        </label>
                                    </div>
                                </div>
                                <div class="form-group" style="margin-left: 10px">
                                    <div class="col-xs-6" style="width: auto">
                                        <label class="form-inline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;达标：
                                            <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.okAllDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">已分发：
                                            <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.okIsGiveDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">待分发：
                                            <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.okNotGiveDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">已标注：
                                            <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.okIsTagDataNum}}"></label>
                                    </div>
                                    <div class="col-xs-3 form-group">
                                        <label class="form-inline">操作：
                                            <input type="button" class="btn btn-info" onclick="dispatchTask('notag')" value="分发">
                                            <input type="button" class="btn btn-primary" value="质检">
                                            <input type="button" class="btn btn-success" value="质检结果">
                                        </label>
                                    </div>
                                </div>
                                <div class="form-group" style="margin-left: 10px">
                                    <div class="col-xs-6" style="width: auto">
                                        <label class="form-inline">&nbsp;&nbsp;&nbsp;&nbsp;未达标：
                                            <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.IsNoOkAllDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">已分发：
                                            <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.IsNoOkIsGiveDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">待分发：
                                            <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.IsNotOkNotGiveDataNum}}"></label>
                                        <label class="form-inline" style="margin-left: 20px">已标注：
                                            <input type="text" class="form-control" style="width: 80px" readonly value="{{.T.IsNotOkIsTagDataNum}}"></label>
                                    </div>
                                    <div class="col-xs-3 form-group">
                                        <label class="form-inline">操作：
                                            <input type="button" class="btn btn-info" onclick="dispatchTask('tag')" value="分发">
                                            <input type="button" class="btn btn-primary" value="质检">
                                            <input type="button" class="btn btn-success" value="质检结果">
                                        </label>
                                    </div>
                                </div>

                            </div>
                        </div>

                        <hr>
                        <div class="form-horizontal">
                            <div class="box-body margin">
                                <h3><i class="glyphicon glyphicon-tasks" style="margin-right: 6px"></i>数据清洗任务列表
                                </h3>

                                <table id="dataTable" class="table table-bordered table-hover">
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>项目名称</th>
                                        <th>用户组名称</th>
                                        <th>负责人</th>
                                        <th>分发数据量</th>
                                        <th>任务状态</th>
                                        <th>完成进度</th>
                                        <th>任务时间</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                </table>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
        </div>
    </section>
</div>


<div class="modal fade" id="modal-create-task" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width: 60%">
        <div class="modal-content">
            <div class="modal-header">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <div class="edit-info">
                        <span class="glyphicon glyphicon-tasks" aria-hidden="true"></span>
                        <span class="h3">新建任务</span>
                    </div>
                    <div class="edit-form">
                        <form id="modal-form-task" class="form-horizontal">
                            <div class="modal-body modal-task">
                                <div class="form-group margin-bottom" style="margin-left: 15px">
                                    <label class="control-label form-inline h4">待分发数据总量(条)：
                                        <span id="modal-num">0</span>
                                    </label>
                                </div>
                                <hr>
                                <div></div>
                                <div id="TaskDiv">
                                    <div id="itemDiv" class="form-group" style="margin-left: 10px;display: none">
                                        <select id="group-select-clone"></select>
                                        <label class="control-label form-inline" style="margin-left: 20px">数据量(条)：
                                            <input type="number" class="form-control"></label>
                                    </div>
                                    <div class="form-group group-item" style="margin-left: 10px;">
                                        <select class="selectpicker" id="group-select"></select>
                                        <label class="control-label form-inline" style="margin-left: 20px">数据量(条)：
                                            <input type="number" class="form-control"></label>
                                        <input type="button" class="btn btn-info btn-sm" onclick="addTaskSelect()" value="新增">
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <input type="button" onclick="saveTask()" class="btn btn-primary" value="保存">
                <input type="button" onclick="cancelModel()" class="btn btn-default" value="取消">
            </div>
        </div>
    </div><!-- /.modal -->
</div>

{{include "com/footer.html"}}
<script>
    menuActive("project");

    let projectid = {{ .T.s_projectid }}
    let groupList = []
    let tableData = []
    let stype = ""                                  // 分发类型：所有数据:all 达标数据：notag 未达标：tag

    $(function () {
        ttable = $('#dataTable').dataTable({
            "paging": true,
            "lengthChange": false,
            "searching": true,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "language": {
                "url": "/dist/js/dataTables.chinese.lang"
            },
            "fnDrawCallback": function () {
                $("ul.pagination").prepend("&nbsp;&nbsp;&nbsp;转到第 <input type='text' id='changePage'   style='width:20px;'> 页    <a type='text' href='javascript:void(0);' id='dataTable-btn' style='text-align:center'>GO</a>");
                $('#dataTable-btn').click(function (e) {
                    var redirectpage = 0
                    if ($("#changePage").val() && $("#changePage").val() > 0) {
                        var redirectpage = $("#changePage").val() - 1;
                    }
                    ttable.page(redirectpage).draw(false);
                });
                this.api().column(0).nodes().each(function (cell, i) {
                    cell.innerHTML = i + 1;
                });
            },
            "columns": [
                {"data": null, width: "1%"},
                {"data": "s_projectname", width: "5%"},
                {"data": "s_groupname", width: "4%"},
                {"data": "s_personname", width: "4%"},
                {"data": "i_givenum", width: "4%"},
                {"data": "s_status", width: "4%"},
                {"data": "s_progress", width: "4%"},
                {
                    "data": "_id", width: "4%", render: function (val, a, row) {
                        let str = ""
                        if (row['i_starttime'] === undefined) {
                            str = "未开始"
                        }else if (row['i_starttime'] && row['i_completetime'] === undefined) {
                            let dt = new Date()
                            dt.setTime(parseInt(row['i_starttime']) * 1000);
                            str = dt.format("yyyy-MM-dd") + " - 未结束"
                        }else {
                            let dt = new Date()
                            let ds = new Date()
                            dt.setTime(parseInt(row['i_starttime']) * 1000);
                            ds.setTime(parseInt(row["i_completetime"]) * 1000)
                            str = dt.format("yyyy-MM-dd") + " - " + ds.format("yyyy-MM-dd")
                        }
                        return str
                    }
                },
                {
                    "data": "_id", width: "11%", render: function (val, a, row, pos) {
                        tmp = '<div>' +
                            '<a class="btn btn-sm btn-primary" href="/front/user/task/list?grouptaskid=' + val + '&s_sourceinfo='+row.s_sourceinfo+'">查看</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-warning" onclick="retrieveTask(' + row + ')">收回</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-info">质检</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-info" onclick="repulseTask(' + row + ')">打回</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-default" onclick="closeTask(\'' + val + '\',\'' + row.s_status + '\')">关闭</a>&nbsp;&nbsp;' +
                            '</div>';
                        return tmp
                    }
                }
            ]
        });

        $.ajax({
            url: "/front/project/task/list",
            type: "POST",
            data: {"pid": projectid},
            success: function (r) {
                if (r.data.length > 0) {
                    console.log(r.data)
                    ttable.fnClearTable();
                    ttable.fnAddData(r.data);
                }
            }
        })
        $.ajax({
            url: "/front/group/list",
            type: "POST",
            data: {},
            success: function (r) {
                if (r.rep) {
                    groupList = r.data
                    console.log(groupList)
                }
            }
        })
    });

    function dispatchTask(val) {
        console.log(val)
        stype = val
        if (stype === "all") {
            $('#modal-num').val({{.T.allGiveDataNum}})
        }else if (stype === "notag") {
            $('#modal-num').val({{.T.okNotGiveDataNum}})
        }else {
            $('#modal-num').val({{.T.IsNotOkNotGiveDataNum}})
        }
        $('#modal-create-task').modal('show')
        for (var i in groupList) {
            var opt = document.createElement('option');
            opt.innerText = groupList[i]["s_name"];
            opt.value = groupList[i]["_id"];
            $('#group-select')[0].appendChild(opt)
        }
        $("#group-select").selectpicker("refresh");
    }

    function cancelModel() {
        $('#TaskDiv .group-item.clone-template').remove()
        $("#modal-create-task").modal('hide')
        document.getElementById("modal-form-task").reset();
    }

    function addTaskSelect() {
        let tNode = $('#itemDiv').clone().addClass('group-item').addClass('clone-template').show()
        let st = $(tNode).find('select')
        $(st).addClass('selectpicker')
        for (var i in groupList) {
            var opt = document.createElement('option');
            opt.innerText = groupList[i]["s_name"];
            opt.value = groupList[i]["_id"];
            $(st)[0].appendChild(opt)
        }
        $(st).selectpicker("refresh");
        $('#TaskDiv').append($(tNode))
    }

    function saveTask() {
        let arr = []
        $('.group-item').each(function () {
            var selectId = $(this).find("select option:checked").val()
            var inputVal = $(this).find("input").val()
            let tmp = {}
            for (let v in groupList) {
                if (groupList[v]["_id"] === selectId) {
                    tmp["s_groupid"] = selectId
                    tmp["s_groupname"] = groupList[v]["s_name"]
                    tmp["s_personname"] = groupList[v]["s_personname"]
                    tmp["i_givenum"] = inputVal
                    break
                }
            }
            arr.push(tmp)
        })
        console.log(arr)
        $.ajax({
            url: "/front/project/task/save",
            type: 'POST',
                data: {"s_projectid": projectid, "s_group": JSON.stringify(arr), "s_type": stype},
            success: function (r) {
                if (r.success) {
                    location.reload()
                } else {
                    showTip(r.msg);
                }
            }
        })
    }

    // 收回
    function retrieveTask(row) {
        if (row.s_status === "未开始" || row.status === "进行中") {
            $.ajax({
                url: "/front/project/task/retrieve",
                type: 'POST',
                data: {"s_status": row.s_status, "id": row._id, "s_groupid": row.s_groupid, "s_sourceinfo": row.s_sourceinfo,
                    "taskid": row._id},
                success: function (r) {
                    if (r.success) {
                        ttable.api().ajax.reload()
                    } else {
                        showTip(r.msg);
                    }
                }
            })
        }else {
            showTip("操作不允许")
        }
    }

    // 打回
    function repulseTask(row) {
        if (row.s_status === "已完成") {
            $.ajax({
                url: "/front/project/task/repulse",
                type: 'POST',
                data: {"s_status": row.s_status, "id": row._id, "s_groupid": row.s_groupid, "s_sourceinfo": row.s_sourceinfo,
                    "taskid": row._id},
                success: function (r) {
                    if (r.success) {
                        ttable.api().ajax.reload()
                    } else {
                        showTip(r.msg);
                    }
                }
            })
        }else {
            showTip("操作不允许")
        }
    }

    function closeTask(val, status) {
        showConfirm("确认要关闭当前任务吗？", function () {
            $.ajax({
                url: "/front/project/task/close",
                type: 'POST',
                data: {"s_status": status, "id": val},
                success: function (r) {
                    if (r.success) {
                        ttable.api().ajax.reload()
                    } else {
                        showTip(r.msg);
                    }
                }
            })
        })
    }
</script>
