<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>同步周报</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <meta name="decorator" content="base" />
  <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css" />
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card-body" >
    <form class="layui-form" action="" id="searchForm">
      <div class="layui-form-item">
        <div class="layui-col-lg12 layui-col-sm12">
          <label class="layui-form-label"><i class="mark">*</i>同步日期：</label>
          <div class="layui-input-block">
            <div class="layui-col-xs8">
              <input type="text" name="creatTime" class="layui-input" id="creatTime" placeholder="统计日期" value="${time}" >
              <input type="hidden"  id="start" value="${startDate}" >
              <input type="hidden" id="end" value="${endDate}" >
            </div>
          </div>
        </div>
      </div>
    </form>
    <div class="operate-footer" style="background-color: #f2f2f2 !important">
      <button type="button" id="sync" class="layui-btn saveOrder">同步</button>
    </div>
  </div>
</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script src="${ctxPlugin}/lib/xm-select.js"></script>
<script>
  layui.config({
    base: '${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'tableExt', 'jquery', 'layer','laypage','laydate','form'], function(){
    $ = layui.$;
    tableExt = layui.tableExt;
    layer = layui.layer;
    laypage = layui.laypage;
    laydate = layui.laydate;
    form = layui.form;
    table = tableExt.getLayTable();

    laydate.render({
      elem: '#creatTime',
      type: 'date',
      max: genTime('day'),
      format: "yyyy-MM-dd~yyyy-MM-dd",
      showBottom: false,
      done: function (value, date, endDate) {
        if (value != "" && value.length > 0) {
          let today = new Date(value.substring(0, 10));
          let weekday = today.getDay();
          let monday;
          let sunday;
          if (weekday == 0) {
            monday = new Date(1000 * 60 * 60 * 24 * (weekday - 6) + today.getTime());
          } else {
            monday = new Date(1000 * 60 * 60 * 24 * (1 - weekday) + today.getTime());
          }
          if (weekday == 0) {
            sunday = today;
          } else {
            sunday = new Date(1000 * 60 * 60 * 24 * (7 - weekday) + today.getTime());
          }
          let month = monday.getMonth() + 1;
          if (month < 10) {
            month = "0" + month;
          }
          let day1 = monday.getDate();
          if (day1 < 10) {
            day1 = "0" + day1;
          }
          let start = "" + monday.getFullYear() + "-" + month + "-" + day1;
          let month2 = sunday.getMonth() + 1;
          if (month2 < 10) {
            month2 = "0" + month2;
          }
          let day2 = sunday.getDate();
          if (day2 < 10) {
            day2 = "0" + day2;
          }
          let end = "" + sunday.getFullYear() + "-" + month2 + "-" + day2;
          $("#creatTime").val(start + "~" + end);
          $("#start").val(start);
          $("#end").val(end);
        } else {
          $('#creatTime').val('');
        }
      }
    });


    var saveFlag = false;
    $(".saveOrder").click(function () {
      if (saveFlag) {
        return;
      }
      var creatTime = $("#creatTime").val();
      if (isBlank(creatTime)){
        layer.msg("请选择同步日期", {icon: 2});
        return false;
      }
      let startDate = $("#start").val();
      let endDate = $("#end").val();
      $("#sync").hide();
      var loadingIndex = layer.load(2, {shade: [0.5,'#fff']});
      $.ajax({
        type: "POST",
        url: "${ctx}/customerEntryWeeklyRecord/syncWeeklyRecord",
        data: {
          startDate:startDate,
          endDate:endDate,
        },
        success: function (result) {
          if (result.code === '200'){
            let index = parent.layer.getFrameIndex(window.name);
            layer.msg("操作成功,数据已同步", {icon: 1, time: 1000}, function () {
              parent.layer.close(index);
              // parent.searchOrder(); //刷新父页面
            })
          }else {
            if (isBlank(result.errMsg)) {
              layer.msg("操作失败！请联系管理员！", {icon: 2});
            } else {
              layer.msg("操作失败，" + result.errMsg, {icon: 2});
            }
          }
          layer.close(loadingIndex);

        },
        error:function (){
          layer.close(loadingIndex);
          layer.msg("同步失败", {icon: 2});
        }
      })


    });

    //监听搜索
    form.on('submit(LAY-user-back-search)', function(data){
      var field = data.field;
      //执行重载
      tableExt.reload('test', {
        where: field,
        page:{
          curr:1
        }
      });
    });

    $('input').on('keydown', function(event){
      if(event.keyCode == 13){
        //执行相关操作
        $("#searchBtn").click();
      }
    });
  })


  function genTime(opt) {
    let now = new Date();
    let year = now.getFullYear();
    let mth = now.getMonth();
    let day = now.getDate();
    let month = mth + 1;
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    let str;
    if (opt == 'day') {
      str = year + '-' + month + '-' + day;
    } else if (opt == 'month') {
      str = year + '-' + month;
    }
    return str;
  }
  function searchOrder() {
    //执行重载
    table.reload('test', {
      where: function (){
        return _serializeJson($("#searchForm"));
      }
    });
  }
  function isBlank(val) {
    if (val == null || val == '' || val == undefined) {
      return true;
    }
    return false;
  }

  function reset(){
    layui.form.render();
    $("#searchForm").find("select").val("");
    $("#searchForm").find("input").val("");
    $("#creatTime").val('${maxDate}');
  }
</script>
</body>
</html>


