{{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="#"><i class="fa fa-dashboard"></i> 任务列表</a></li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="box">
                <div class="box-body">

                    <div id="status-div" class="col-xs-1" style="width: auto;float: right">
                        <label for="statusSelect">状态：
                            <select class="form-control selectpicker" id="statusSelect">
                                <option value="-1">全部</option>
                                <option value="未开始">未开始</option>
                                <option value="进行中">进行中</option>
                                <option value="已完成">已完成</option>
                                <option value="已关闭">已关闭</option>
                            </select></label>
                        </span>
                        <span class="input-group date date-picker" id="starttime" data-provide="datepicker">
                                <input type="text" class="form-control form-filter input-sm" readonly name="starttime" placeholder="开始日期" />
                                 <span class="input-group-addon">
                                     <i class="fa fa-calendar"></i>
                                  </span>
                            </span>
                        <span class="input-group date date-picker" id="endtime" data-provide="datepicker">
                                <input type="text" class="form-control form-filter input-sm" readonly name="endtime" placeholder="结束日期" />
                                 <span class="input-group-addon">
                                     <i class="fa fa-calendar"></i>
                                  </span>
                            </span>
                        <input type="button" class="btn btn-sm btn-primary" onclick="exportTask()" value="导出">
                        <!--                            <a type="button" class="btn btn-sm btn-primary" href="/front/group/task/export">导出</a>-->
                    </div>

                    <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>
                            <th>完成进度</th>
                            <th>任务时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                    </table>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
    </section>
</div>

{{include "com/footer.html"}}
<script>
    menuActive("/front/group/admin/task/list");

    $(function () {
        ttable = $('#dataTable').dataTable({
            "paging": true,
            "lengthChange": false,
            "searching": true,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "serverSide": true,
            "ajax": {
                "url": "/front/group/admin/task/list",
                "type": "post"
            },
            "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.api().page(redirectpage).draw(false);
                });
                this.api().column(0).nodes().each(function (cell, i) {
                    cell.innerHTML = i + 1;
                });
            },
            "columns": [
                {"data": null, width: "1%"},
                {"data": "s_entname", width: "4%"},
                {"data": "s_departname", width: "4%"},
                {"data": "s_rulename", width: "4%"},
                {"data": "s_projectname", width: "4%"},
                {"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/group/user/task/list?grouptaskid=' + val + '&s_sourceinfo='+row.s_sourceinfo+'">查看</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-primary" onclick="retrieveTask(\'' + val + '\',\''+row.s_sourceinfo+'\',\'' + row.s_status + '\')">收回</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-primary" href="/front/user/check/data?pid='+row.s_projectid+'&tid=' + val + '&s_sourceinfo='+row.s_sourceinfo+'">质检</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-primary" onclick="closeTask(\'' + val + '\',\''+row.s_sourceinfo+'\',\'' + row.s_status + '\')">关闭</a>&nbsp;&nbsp;' +
                            '</div>';
                        return tmp
                    }
                }
            ],
            "initComplete": function () {
                $("#dataTable_filter").append($('#status-div'))
                $("#dataTable_wrapper .col-sm-6").css({width:"100%"});
            },
            "fnServerParams": function (e) {
                e.s_status = $("#statusSelect option:selected").val()
                let st = $('#starttime').datepicker('getDate')
                let et = $('#endtime').datepicker('getDate')
                if (st != null) {
                    let s = st.toLocaleDateString()
                    let sc = Date.parse(new Date(s).toString())/1000
                    e.starttime = sc
                }
                if (et != null) {
                    let es = et.toLocaleDateString()
                    let etc = Date.parse(new Date(es).toString())/1000
                    e.endtime = etc
                }
            }
        });

        $('.date-picker').datepicker({
            language: 'zh-CN',
            autoclose: true,
            clearBtn: true, //清除按钮
            todayBtn: false, //今日按钮
            format: "yyyy-mm-dd"
        });
        $('#starttime').datepicker().on('changeDate', function (e) {
            $('#endtime').datepicker('setStartDate', e.dates[0])
            ttable.api().ajax.reload();
        })
        $('#endtime').datepicker().on('changeDate', function () {
            ttable.api().ajax.reload();
        })
        $('#statusSelect').on('changed.bs.select', function () {
            ttable.api().ajax.reload();
        })
    });

    // 收回
    function retrieveTask(id, sourceinfo, status) {
        if (status === "未开始" || status === "进行中") {
            $.ajax({
                url: "/front/project/task/retrieve",
                type: 'POST',
                data: {"s_status": status, "taskid": id, "s_sourceinfo": sourceinfo},
                success: function (r) {
                    if (r.success) {
                        let msg = r.msg+"<br>"+"共收回"+r.count+"数据。"
                        showTip(msg, 1000);
                        ttable.api().ajax.reload()
                    } else {
                        showTip(r.msg);
                    }
                }
            })
        }else {
            showTip("操作不允许")
        }
    }
    function closeTask(id, sourceinfo,status) {
        if (status === "未开始") {
            showConfirm("确认要关闭当前任务吗？", function () {
                $.ajax({
                    url: "/front/project/task/close",
                    type: 'POST',
                    data: {"s_sourceinfo": sourceinfo, "s_status": status, "taskid": id},
                    success: function (r) {
                        if (r.success) {
                            let msg = r.msg+"<br>"+"共收回"+r.count+"数据。"
                            showTip(msg, 1000);
                            ttable.api().ajax.reload()
                        } else {
                            showTip(r.msg);
                        }
                    }
                })
            })
        }else {
            showTip("操作不允许")
        }
    }

    function exportTask() {

        let search = $("[type=search]").val();
        let status = $("#statusSelect option:selected").val()
        let st = $('#starttime').datepicker('getDate')
        let et = $('#endtime').datepicker('getDate')
        if (st != null) {
            st = st.toLocaleDateString()
            st = Date.parse(new Date(st).toString())/1000
        }else {
            st = 0
        }
        if (et != null) {
            et = et.toLocaleDateString()
            et = Date.parse(new Date(et).toString())/1000
        }else {
            st = 0
        }
        window.location.href = "/front/group/task/export?"+"i_starttime="+st+"&i_completetime="+et+"&s_search="+search+"&s_status="+status
    }

</script>
