{{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>
        </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">
                                <div class="form-group" style="margin-left: 20px">
                                    <a class="btn btn-sm btn-success h4" onclick="createPro()"><i
                                            class="fa fa-fw fa-plus fa-lg"></i>新建项目</a>
                                </div>
                            </div>
                        </div>
                        <div class="col-xs-12">
                            <select class="selectpicker" id="pushModelSelect">
                                <option value=-1>全部</option>
                                <option value=0>开启</option>
                                <option value=1>关闭</option>
                            </select>
                        </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>
                            </tr>
                            </thead>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
        </div>
    </section>
</div>

<div class="modal fade" id="modal-create-project" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width: 50%">
        <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-alert" aria-hidden="true"></span>
                        <span class="h4">新建项目</span>
                    </div>
                    <div class="edit-form">
                        <hr>
                        <div class="form-group">
                            <label class="radio-inline">
                                <input type="radio" name="stype" value="coll" checked>数据库导入
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="stype" value="excel">Excel表导入
                            </label>
                        </div>
                        <form id="model-form-project" class="form-horizontal" enctype="multipart/form-data">
                            <div class="box-body">
                                <div class="form-group margin-bottom">
                                    <label class="col-sm-3 control-label"><span style="color:red;">* </span>项目名称</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control" id="project-name" placeholder="项目名称">
                                    </div>
                                </div>
                                <div id="import-coll">
                                    <h5><i class="glyphicon glyphicon-bookmark"
                                           style="color: #00c4ff;margin-right: 6px"></i>数据来源</h5>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"><span
                                                style="color:red;">* </span>数据库名</label>
                                        <div class="col-sm-5">
                                            <input type="text" class="form-control" id="db-name" value="jyqyfw"
                                                   readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"><span
                                                style="color:red;">* </span>数据表名</label>
                                        <div class="col-sm-5">
                                            <input type="text" class="form-control" id="coll-name"
                                                   value="usermail_history" readonly>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"><span
                                                style="color:red;">* </span>数据导出ID</label>
                                        <div class="col-sm-6">
                                            <input type="text" class="form-control" id="data-id" placeholder="数据导出ID">
                                        </div>
                                    </div>
                                </div>

                                <div id="import-excel" style="display: none">
                                    <h5><i class="glyphicon glyphicon-bookmark"
                                           style="color: #00c4ff;margin-right: 6px"></i>数据信息</h5>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label"><span style="color:red;">* </span>公司名称</label>
                                        <ul class="dropdown-menu"> </ul>
                                        <div class="col-sm-6">
                                            <input type="text" class="form-control" id="company-name" autocomplete="off"
                                                   data-provide="typeahead" placeholder="公司名称">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">部门名称</label>
                                        <div class="col-sm-6">
                                            <input type="text" class="form-control" id="dpart-name" placeholder="部门名称">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">规则名称</label>
                                        <div class="col-sm-6">
                                            <input type="text" class="form-control" id="rule-name" placeholder="规则名称">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">选择文件</label>
                                        <div class="col-sm-6">
                                            <input type="file" name="file" id="uploadfile">
                                        </div>
                                    </div>
                                </div>
                                <h5><i class="glyphicon glyphicon-bookmark"
                                       style="color: #00c4ff;margin-right: 6px"></i>数据存储</h5>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label"><span style="color:red;">* </span>数据库名</label>
                                    <div class="col-sm-6">
                                        <input type="text" class="form-control" value="jyqykhfw" readonly>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label"><span style="color:red;">* </span>数据表名</label>
                                    <div class="col-sm-6">
                                        <input type="text" class="form-control" id="coll-save-name" placeholder="数据表名">
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <input type="button" onclick="importData()" class="btn btn-primary" value="导入">
                <input type="button" onclick="cancelModel()" class="btn btn-default" value="取消">
            </div>
        </div>
    </div><!-- /.modal -->
</div>

<div class="modal fade" id="modal-edit-project" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" style="width: 50%">
        <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-alert" aria-hidden="true"></span>
                        <span class="h4">编辑项目</span>
                    </div>
                    <div class="edit-form">
                        <hr>
                        <form class="form-horizontal" enctype="multipart/form-data">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">公司名称</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control" id="company-edit-name" readonly>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">部门名称</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control" id="dpart-edit-name">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">规则名称</label>
                                    <div class="col-sm-5">
                                        <input type="text" class="form-control" id="rule-edit-name">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">售后人员</label>
                                    <div class="col-sm-6">
                                        <input type="text" class="form-control" id="edit-person">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">选择标注字段</label>
                                    <div class="col-sm-6">
                                        <select class="form-control selectpicker" multiple
                                                id="markFieldSelect"></select>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <input type="button" onclick="saveData()" class="btn btn-primary saveBtn" value="保存">
                <input type="button" onclick="cancelModel()" class="btn btn-default" value="取消">
            </div>
        </div>
    </div><!-- /.modal -->
</div>

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

    let stype = "coll";
    let fields = {{ .T.fields }}
    let projectmap = {}

    $(function () {
        ttable = $('#dataTable').dataTable({
            "paging": true,
            "lengthChange": false,
            "searching": true,
            "ordering": false,
            "info": true,
            "autoWidth": false,
            "serverSide": true,
            "ajax": {
                "url": "/front/project",
                "type": "post",
                "data": {"status": "-1"}
            },
            "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: "2%"},
                {"data": "s_entname", width: "5%"},
                {"data": "s_departname", width: "4%"},
                {"data": "s_rulename", width: "4%"},
                {"data": "s_name", width: "4%"},
                {"data": "s_personname", width: "4%", "defaultContent": ""},
                {"data": "i_importnum", width: "4%"},
                {"data": "s_status", 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" onclick="editPro(\'' + pos.row + '\')">编辑</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-warning" onclick="clearPro(\''+ pos.row +'\')">清洗</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-info" onclick="del(\'' + val + '\')">判重</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-default" href="">完成</a>&nbsp;&nbsp;<br>' +
                            '<div style="margin-top: 5px">' +
                            '<a class="btn btn-sm btn-primary" href="">上传</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-primary" href="">下载</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-primary" href="">扣费</a>&nbsp;&nbsp;' +
                            '<a class="btn btn-sm btn-danger" href="">删除</a>&nbsp;&nbsp;' +
                            '</div></div>';
                        return tmp
                    }
                }
            ]
        });

        $('input[type=radio][name=stype]').change(function () {
            if (this.value === 'coll') {
                stype = "coll"
                $("#import-coll").attr("style", "display:block;")
                $("#import-excel").attr("style", "display:none;")
            } else if (this.value === 'excel') {
                stype = "excel"
                $("#import-coll").attr("style", "display:none;")
                $("#import-excel").attr("style", "display:block;")
            }
        });

        for (var i in fields) {
            var opt = document.createElement('option');
            opt.innerText = fields[i]["s_name"];
            opt.value = fields[i]["s_code"];
            $('#markFieldSelect')[0].appendChild(opt)
        }
        $("#markFieldSelect").selectpicker("refresh");

        $("#company-name").typeahead({
            source: function (query, process) {
                return $.ajax({
                    url: '/front/project/getEntnameList',
                    type: 'post',
                    data: {'entname': query},
                    success: function (result) {
                        return process(result.entname);
                    },
                });
            }
        });
    });

    function createPro() {
       $("#modal-create-project").modal('show')
    }

    function cancelModel() {
        document.getElementById("model-form-project").reset();
        $("#modal-create-project").modal('hide')
        $("#modal-edit-project").modal('hide')
    }

    function importData() {
        let sname = $('#project-name').val()
        let dataid = $('#data-id').val()
        let coll = $('#coll-save-name').val()
        if (sname === "") {
            alert("项目名称为必填项")
        }
        if (dataid === "") {
            alert("数据导出ID为必填项")
        }
        if (coll === "") {
            alert("数据导出ID为必填项")
        }
        if (stype === "coll") {
            projectmap["s_name"] = sname
            projectmap["s_historyid"] = dataid
            projectmap["s_sourceinfo"] = coll
            if (projectmap["s_sourceinfo"] === "") {
                alert("数据表名为必填项")
            }
            projectmap["s_type"] = stype
            $.ajax({
                url: "/front/project/save",
                type: 'POST',
                data: projectmap,
                success: function (r) {
                    if (r.success) {
                        $("#modal-create-project").modal('hide')
                        ttable.api().ajax.reload();
                    } else {
                        showTip(r.msg)
                    }
                }
            })
        } else if (stype === "excel") {
            let formData = new FormData();
            formData.append("s_name", sname)
            formData.append("s_sourceinfo", coll)
            formData.append("s_entname", $('#company-name').val())
            formData.append("s_departname", $('#dpart-name').val())
            formData.append("s_rulename", $('#rule-name').val())
            formData.append("s_type", stype)
            let file = $('#uploadfile')[0].files[0]
            if (file) {
                formData.append("xlsx", file)
                $.ajax({
                    url: "/front/project/save",
                    type: 'POST',
                    data: formData,
                    cache: false,
                    processData: false,
                    contentType: false,
                    success: function (r) {
                        if (r.rep) {
                            $("#modal-create-project").modal('hide')
                            ttable.api().ajax.reload();
                        } else {
                            showTip("状态修改失败");
                        }
                    }
                })
            } else {
                showTip("请选择上传文件");
            }
        }
    }

    function editPro(index) {
        projectmap = ttable.fnGetData()[index]
        console.log(projectmap.v_fields)
        $('#modal-edit-project').modal('show')
        $('#company-edit-name').val(projectmap["s_entname"])
        $('#rule-edit-name').val(projectmap["s_rulename"])
        $('#dpart-edit-name').val(projectmap["s_departname"])
        if (projectmap.v_fields !== undefined) {
            $('#markFieldSelect option').each(function(){
                let v = $(this).val()
                if (projectmap.v_fields[v] !== undefined) {
                    $(this)[0].selected = true
                }
            })
            $("#markFieldSelect").selectpicker("refresh");
        }
    }

    function saveData() {
        stype = "edit"
        let s1 = JSON.stringify(projectmap)
        let tmp = projectmap
        tmp["s_departname"] = $('#dpart-edit-name').val()
        tmp["s_rulename"] = $('#rule-edit-name').val()
        tmp["s_personname"] = $('#edit-person').val()

        let fieldArr = $('#markFieldSelect').val();
        let m = {}
        if (fieldArr.length > 0) {
            for (const i in fields) {
                if (fieldArr.indexOf(fields[i]["s_code"]) > -1) {
                    m[fields[i]["s_code"]] = fields[i]["s_name"]
                }
            }
        }
        tmp["v_fields"] = JSON.stringify(m)
        if (s1 === JSON.stringify(tmp)) {
            showTip("未做修改");
        } else {
            $.ajax({
                url: "/front/project/save",
                type: 'POST',
                data: {"s_type": stype, "s_departname": tmp["s_departname"], "s_personname": tmp["s_personname"],
                    "s_rulename": tmp["s_rulename"], "v_fields": tmp["v_fields"], "s_name": tmp["s_name"]},
                success: function (r) {
                    if (r.success) {
                        showTip("保存成功", 1000)
                        cancelModel()
                        ttable.api().ajax.reload();
                    } else {
                        showTip(r.msg);
                    }
                }
            })
        }
    }

    function clearPro(val) {
        let tmp = ttable.fnGetData()[val]
        if (tmp["v_fields"] === undefined) {
            showTip("请设置标注字段")
        }else {
            $.ajax({
                url: "/front/project/qualityAssessment",
                type: 'POST',
                data: {"pid": tmp["_id"]},
                success: function (r) {
                    if (r.success) {
                        window.location.href="/front/project/task/list?pid=" + tmp["_id"]
                    } else {
                        showTip(r.msg);
                    }
                }
            })
        }

    }
</script>
