<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>信息抽查</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/page.css" rel="stylesheet">
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/util.js"></script>
    <script src="/js/vue.min.js"></script>
    <script src="/js/index.js"></script>
    <script src="/js/jquery-page.js"></script>
    <link rel="stylesheet" href="/css/index.css">
    <style>
        .form-horizontal {
            min-height: 40px;
        }

        .row-box label {
            white-space: nowrap;
        }

        .row-box select {
            width: 100px;
        }

        .row-box input {
            margin-left: 15px;
            width: 5em;
        }

        .row-box + .row-box {
            margin-left: 15px;
        }

        .row-box + button {
            margin-left: 15px;
        }

        .row-box {
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .checkbox-inline{
            margin-left: -11px;
        }

        .import-primary{
            margin-left: 9px;
        }
    </style>
</head>
 <body>
 <div>
  <div class="modal fade" id="loadingModal" backdrop="static" keyboard="false">
      <div style="width: 200px;height:100px; z-index: 20000; position: absolute; text-align: center; left: 50%; top: 50%;margin-left:-100px;margin-top:-10px">
          <div class="progress progress-striped active"
               style="margin-bottom: 0;height:50px; text-align:center;line-height: 50px;font-size:large;">
                数据加载中,请稍候......　　　　
          </div>
      </div>
  </div>
  <div class="col-md-12" style="border-right: 1px double #EEEEEE;">
         <div style="padding:30px 0px 0px 0px">
             <form action="/list" class="form-horizontal" role="form">
                 <div style="display: none">
                     <input type="input" name="pagenum" id="pagenum"  class="form-control" value="{{.T.pagenum}}"/>
                 </div>
                 <div class="col-md-3">
                     <input type="input" name="spidercode" class="form-control" value="{{.T.spidercode}}"
                            placeholder="请输入爬虫代码"/>
                 </div>
                 <div class="col-md-1" style="padding-left: 0;">
                     <input type="input" name="db" disabled class="form-control" value="{{.T.dbname}}"
                            placeholder="请输入库名"/>
                 </div>
                 <div class="col-md-1" style="padding-left: 0">
                     <input type="input" name="coll" id="fromtable" class="form-control" value="{{.T.fromtable}}" placeholder="请输入表名"/>
                 </div>
                 <div class="col-md-7 row-box" style="padding-left: 0">
                     <div class=" row-box" style="padding-left: 0">
                         <label class="checkbox-inline">类型:</label>
                         <select name="type" id="topsubtype" class="form-control">
                             <option value="-1">全部</option>
                             <!--<option value="trailer" {{if eq .T.type "notice"}}selected{{end}}>预告</option>
                             <option value="tender" {{if eq .T.type "tender"}}selected{{end}}>招标</option>
                             <option value="bid" {{if eq .T.type "bid"}}selected{{end}}>结果</option>-->
                         </select>
                     </div>
                     <div class="row-box">
                         <label class="checkbox-inline">字段:</label>
                         <select name="field" id="field" class="form-control">
                             <option value="-1">全部</option>
                             <!--<option value="trailer" {{if eq .T.type "notice"}}selected{{end}}>预告</option>
                             <option value="tender" {{if eq .T.type "tender"}}selected{{end}}>招标</option>
                             <option value="bid" {{if eq .T.type "bid"}}selected{{end}}>结果</option> -->
                         </select>
                         <input type="input" name="minval" id="minval" style="display:none" class="form-control"
                                value="{{.T.min}}" placeholder="最小值"/>
                         <input type="input" name="maxval" id="maxval" style="display:none" class="form-control"
                                value="{{.T.max}}" placeholder="最大值"/>
                         <div id="boexists" style="display:none;margin-left: -29px;">
<!--                             <label class="checkbox-inline">-->
<!--                                 <input type="checkbox" id="hasno" name="hasno"  value="1">是否存在-->
<!--                             </label>-->
                             <input id="hasno" type="checkbox" name="hasno" value="true"/>
                             <label style="margin-left: -28px;">包含不存在</label>
                         </div>
                     </div>
                     <!--<div class="col-md-2">
                           <label class="checkbox-inline">
                              <input type="checkbox" name="ispackage"  value="1">分包数据
                         </label>
                     </div>-->
                     <button type="submit" style="display: none" class="btn btn-primary" id="submitForm"></button>
                     <div  class="btn btn-primary" style="margin-left: 15px" onclick="alertsync()">查看</div>
                     <div  class="btn btn-primary" style="margin-left: 15px" onclick="syncmarked()">同步</div>
                 </div>
             </form>
         </div>
         <div class="col-md-12">
             <div class="form-group col-md-5" style="padding-left: 0">
                 <textarea class="form-control" name="descript" id="es"></textarea>
             </div>
             <div class="form-group col-md-6" style="margin-top: 10px;margin-left: -14px;">
                 <div class="col-md-3" style="padding-left: 0">
                     <input type="input" id="importcoll" class="form-control" value="" placeholder="库表名"/>
                 </div>
                 <button class="btn btn-primary import-primary" onclick='getescount()'>
                     Es导入数据
                 </button>
                 <button class="btn btn-primary import-primary" onclick='importbyexcel()'>
                     导入Excel
                 </button>
             </div>
             <iframe srcdoc="<form id='uploadform' method='post' enctype='multipart/form-data' action='/importbyexcel'><input type='file' name='xlsx' /><input type='input' id='excelcoll' name='excelcoll' /></form>"
                     height=0 scrolling=no class="hide" id="fileframe">
             </iframe>
         </div>
         <hr style="border:1 double" width="100%">
         <div  class="col-md-12">
             <div class="form-group col-md-5" style="margin-left: -15px;">
                 <h4>
                     已验/总数：{{.T.checkednum}}/{{.T.allnum}}
                 </h4>
             </div>
             <div>
                 <button class="btn btn-primary" style="margin-left: 17px;" onclick="updateinfo()">刷新</button>
             </div>
         </div>
         <hr style="border:1 double" width="100%">
         <div id="spcontent" class="column">
             {{range $k,$v:=.T.list}}
             <div>
                 <span style="font-size:18px"><script>document.write({{ $v }}["num"])</script>:</span>&nbsp;
                 <a href="/detail/{{$v._id}}.html" target="_blank" style="font-size:18px;color:{{if $v.checked}}green{{else}}#428bca{{end}}">{{$v.title}}</a>
             </div>
             {{end}}
         </div>
	</div>
  <div style="text-align: center;">
     <div id="pager" class="pager clearfix">
     </div>
  </div>
</div>

 </body>

<script>
var vueApp = new Vue()
$(function(){
  //是否不存在标记
  if( {{.T.hasno}} ){
    $("#hasno").attr("checked",true);
  }
  for(i in {{.T.topsubtype}}){
    var selected = ""
    if({{.T.type}} == {{.T.topsubtype}}[i]){
      selected = "selected"
    }
    $("#topsubtype").append("<option "+selected+" value='"+{{.T.topsubtype}}[i]+"'>"+{{.T.topsubtype}}[i]+"</option>")
  }
  var fk = "";
  for(i in {{.T.allfield}}){
    var selected = ""
      for(key in {{.T.allfield}}[i]){
        if({{.T.field}} == key){
          selected = "selected"
          fk = key
        }
        $("#field").append("<option " +selected+" value='"+key+"'>"+{{.T.allfield}}[i][key]+"</option>")
      }
  }
  if(fk=="budget"||fk=="bidamount"){
    $("#maxval").css("display","");
    $("#minval").css("display","");
    $("#boexists").css("display","");
  }

})
function getescount(){
  var importcoll = $("#importcoll").val();
  if(importcoll == ""){
    alert("请输入导入表！")
    return
  }
  var estext = $("#es").val()
  if(estext==""){
    alert("请输入es查询语句！")
    return
  }
  $.ajax({
  	url:"/getescount",
  	method:"post",
  	data: {"estext":estext},
  	success:function(res){
      importbyes(res.count,res.msg,estext,importcoll)
  	}
  });
}

function importbyes(count,msg,text,coll){
  vueApp.$confirm('es查询数据量：'+count, msg, {
    confirmButtonText: '确认导入',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    importes(text,coll)
  }).catch(() => {
  });
}

function importes(estext,coll){
  //showLoading()
    $.ajax({
        url: "/importbyes",
        method: "post",
        data: {"estext": estext, "coll": coll},
        success: function (r) {
            hideLoading()
            if(r){
                alert(r.msg)
            }else{
                alert("导入失败")
            }
            // if (res && res.success) {
            //     alert("数据导入成功")
            //     //setTimeout(alert("数据导入成功"),200)
            // } else {
            //     alert(res.msg)
            //     //setTimeout(alert(res.msg),200)
            // }
        }
    });
}

function importbyexcel(){
  var importcoll = $("#importcoll").val();
  if(importcoll == ""){
    alert("请输入导入表！")
    return
  }
  $("#excelcoll").val(importcoll);
  $("#fileframe").contents().find("#excelcoll").val(importcoll);
	var f=$("#fileframe").contents().find("input");
		f.get(0).click();
    f.change(function() {
    	var val = $(this).val() ? $(this).val() : "";
    	if (val.indexOf(".xlsx") < 0) {
    		showMsg("文件格式非法",
    		function() {});
    	} else {
            $(this).parent().submit();
            showLoading()
            var ret = setInterval(function () {
                var f = $(window.frames[0].document).find("form");
                if (f.length == 0) {
                    hideLoading()
                    var b = $(window.frames[0].document).find("body").text();
                    if (b) {
                        var r = JSON.parse(b)
                        if(r){
                            console.log(r.msg)
                            alert(r.msg)
                        }else{
                            alert("导入失败")
                        }
                        // if (r && r.success) {
                        //     alert("数据导入成功")
                        // } else {
                        //     alert(r.msg)
                        // }
                    }
                    $(window.frames[0].document).find("body").append("<form id='uploadform' method='post' enctype='multipart/form-data' action='/importbyexcel'><input type='file' name='xlsx' /><input type='input' id='excelcoll' name='excelcoll' /></form>");
                }
            },
            500)
    	}
    })
}

//展示loading框
showLoading = function (){
  $('#loadingModal').modal({backdrop: 'static', keyboard: false});
}
//隐藏掉loading框
hideLoading = function (){
  $('#loadingModal').modal('hide');
}

$("#field").change(function(){
  if($(this).val()=="bidamount"||$(this).val()=="budget"){
    $("#minval").css("display","");
    $("#maxval").css("display","");
    $("#boexists").css("display","");
  }else{
    $("#minval").css("display","none");
    $("#maxval").css("display","none");
    $("#boexists").css("display","none");
  }
});

//分页
$("#pager").zPager({
    totalData: {{.T.allnum}},//数据总条数
    current: {{.T.pagenum}},
    pageData: 50,//每页数据条数
    //htmlBox: $('#wraper'),
    btnShow: true,
    ajaxSetData: false
});
function currentPage(currentPage, type){
    /*
        触发页码数位置： Page/js/jquery.z-pager.js 64行
    */
    console.log("当前页码数：" + currentPage);
}

function alertsync() {
    var coll = $("#fromtable").val();
    if(confirm("请先同步"+coll+"表中数据。如需同步，请点确定；否则，请取消，直接查看数据。"+"\n"+"(通过ES/Excel导入数据，无需同步)")){
        if(coll == "marked"){
            alert("marked表无需同步");
            return
        }
        $.ajax({
            url:"/syncmarked",
            type:"post",
            data:{"coll":coll},
            success:function (r) {
                if(r){
                    console.log(r.msg);
                    alert(r.msg);
                    if(r.flag){
                        $("#submitForm").trigger('click');
                    }
                }
            }
        })
    }else{
        $("#submitForm").trigger('click');
    }
}

function submitform(number){
    $("#pagenum").val(number);
    $("#submitForm").trigger('click');
}

function updateinfo(){
    $("#submitForm").trigger('click');
}

//同步marked表中的数据
function syncmarked() {
    var coll = $("#fromtable").val();
    if(confirm("确定同步"+coll+"表中数据？")){
        if(coll == "marked"){
            alert("marked表无需同步");
            return
        }
        $.ajax({
            url:"/syncmarked",
            type:"post",
            data:{"coll":coll},
            success:function (r) {
                if(r){
                    console.log(r.msg)
                    alert(r.msg)
                }
            }
        })
    }

}
</script>

</html>
