layui.use('element', function () {
    var $ = layui.jquery
    , element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
    $('.site-demo-active').on('click', function () {
        var othis = $(this), type = othis.data('type');
        active[type] ? active[type].call(this, othis) : '';
    });

    //Hash地址的定位
    var layid = location.hash.replace(/^#test=/, '');
    element.tabChange('test', layid);

    element.on('tab(test)', function (elem) {
        location.hash = 'test=' + $(this).attr('lay-id');
    });

});
layui.use('laydate', function () {
    var laydate = layui.laydate;
    //日期时间范围
    laydate.render({
        elem: '#test10'
        // , type: 'datetime'
      , range: true

    });
});
layui.use(['table', 'jquery'], function () {
    var table = layui.table;
    var form = layui.form,
       layer = parent.layer === undefined ? layui.layer : parent.layer,
       $ = layui.jquery;
    //监听表格复选框选择
    table.on('checkbox(demo)', function (obj) {
        console.log(obj);
    });
    
     $ = layui.$, active = {
        //查询按钮
        reload: function () {
            //执行重载
            table.reload('recordtable', {
                where: {
                    dateParty: $("#test10").val()
                   , NowDateTime: new Date()

                }
            });
        }
        //本周
       , oneWeek: function () {

        var thisDateParty = getWeekStartDate() + ' - ' + formatDate(now);
        $("#test10").val(thisDateParty);
           //执行重载
           table.reload('recordtable', {
               where: {
                   dateParty: thisDateParty
                  , NowDateTime: new Date()

               }
           });
       }

        //本月
       , oneMonth: function () {

        var thisDateParty = getMonthStartDate() + ' - ' + formatDate(now);
        $("#test10").val(thisDateParty);
           //执行重载
           table.reload('recordtable', {
               where: {
                   dateParty: thisDateParty
                  , NowDateTime: new Date()

               }
           });
       }
        //本年
       , oneYear: function () {

        var thisDateParty = getYearStartDate() + ' - ' + formatDate(now);
        $("#test10").val(thisDateParty);

           //执行重载
           table.reload('recordtable', {
               where: {
                   dateParty: thisDateParty
                  , NowDateTime: new Date()

               }
           });
       }

    };

    $('.searchfilter .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';


    });
    function saveAs (blob, filename) {
        var type = blob.type;
        var force_saveable_type = 'application/octet-stream';
        if (type && type != force_saveable_type) { // 强制下载，而非在浏览器中打开
          var slice = blob.slice || blob.webkitSlice || blob.mozSlice;
          blob = slice.call(blob, 0, blob.size, force_saveable_type);
        }
    
        var url = URL.createObjectURL(blob);
        var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
        save_link.href = url;
        save_link.download = filename;
    
        var theevent = document.createEvent('MouseEvents');
        theevent.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        save_link.dispatchEvent(theevent);
        URL.revokeObjectURL(url);
      }
      //导出到excel
  $('#ExToExcel').on('click', function () {
    urlExcel = "GetDZDataExcel"
    ExToExcel()
  })

  function ExToExcel () {
    var img = $("#progressImgage");
    var mask = $("#maskOfProgressImage");
    var dataModel = {
      dateParty: $("#test10").val()
      
    }
    var para = JSON.stringify(dataModel);
    $.ajaxLoading({
      url: urlExcel,
      type: "post",
      async: true,
      contentType: "application/json;charset=utf-8",
      dataType: "json",
      cache: false,
      data: para,
      success: function (dataContent, result) {

        if (dataContent.Code == "0") {
          var datajson = dataContent.Data.replace(/&quot;/g, "\"");
          var ItemList = JSON.parse(datajson);
          // alert(ItemList);
          var bytes = window.atob(ItemList);      //转换为byte  
          //处理异常,将ascii码小于0的转换为大于0  
          var ab = new ArrayBuffer(bytes.length);
          var ia = new Uint8Array(ab);
          for (var i = 0; i < bytes.length; i++) {
            ia[i] = bytes.charCodeAt(i);
          }

          var blob = new Blob([ab], { type: 'application/vnd.ms-excel' })
          var fileName = '工单数据.xls';

          if (dataContent.Message != null && dataContent.Message != "") {
            fileName = dataContent.Message;
          }

          //获取浏览器信息
          var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
          var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
          var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
          var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
          if (isIE || isEdge || isIE11) {
            navigator.msSaveBlob(blob, fileName);
          }
          else {
            saveAs(blob, fileName);
          }
        }
        else {
          layer.msg('数据转换失败');
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        layer.msg(textStatus);
        layer.msg(errorThrown);
      }
    });

  }

    

});