// 设备列表
$(function () {
  var ERROK = 200;
  var contextPath = $("#contextPath").val()
  var table;
  var orgList = [];
  var addressList = [];
  var departList = [];
  var companyList = [];
  template.config("escape", false);
  var orgId = "";
  var tpl = {
    "addSecondTpl": '<div class="dialog-wp detailOne">\
    <div class="mask"></div>\
    <div class="dialog dialog-account-con">\
      <div class="title-wp">\
        <span>新增设备</span>\
        <span class="close-btn"></button>\
      </div>\
      <div class="dialog-account-wp ">\
         <div class="account-list">\
         \
         <% if(orgList!=null&&orgList.length>1){ %>\
           <div class="account-item required clearfloat">\
            <div class="item-tip"><span class="red">*</span>校区：</div>\
            <div class="item-con">\
            <input class="item-select organ-id" name="orgId" id="orgId" autocomplete="off"/>\
            <span class="select_wrap _hide"></span>\
             </div>\
          </div>\
        <%}else if(orgList!=null&&orgList.length==1){%>\
           <div class="account-item required clearfloat" style="display: none">\
            <div class="item-tip"><span class="red">*</span>校区：</div>\
            <div class="item-con">\
              <input type="hidden" id="orgId" value="<%=orgList[0].id %>" org__id="<%=orgList[0].id %>"/>\
             </div>\
          </div>\
        <%}%>\
        \
           <ul class="list1">\
            <li class="li1"><label>名称：</label><span><%=data.deviceName%></span></li>\
            <li class="li1"><label>SN码：</label><span><%=data.deviceCode%></span></li>\
            <li class="li1"><label>设备ID：</label><span class="deviceId"><%=data.deviceId%></span></li>\
            <li class="li1">\
              <label><span class="red">*</span>类型：</label>\
              <select class="item-select device-type">\
              <option value="">请选择设备类型</option>\
              <option value="1">闸机</option>\
              <option value="2">梯控</option>\
              <option value="3">门禁</option>\
            </select>\
            </li>\
            <li class="li1 spans">\
              <label><span class="red">*</span>地址：</label>\
              <div class="address">\
                <textarea class="device-address" placeholder="请输入地址"></textarea>\
              </div>\
            </li>\
            <li class="li1 spans">\
            <label>备注：</label>\
            <div class="remark">\
              <textarea class="device-remark" placeholder="请填写备注"></textarea>\
            </div>\
          </li>\
           </ul>\
           <div class="dialog-btns">\
             <button class="sure-add-btn btn-red" style="margin-top: 25px">确定</button>\
            </div>\
      </div>\
    </div>\
  </div>	',
    "detailOne": '<div class="dialog-wp detailOne">\
					<div class="mask"></div>\
					<div class="dialog dialog-account-con">\
						<div class="title-wp">\
							<span>账号详情</span>\
							<span class="close-btn"></button>\
						</div>\
						<div class="dialog-account-wp ">\
							 <div class="account-list">\
								 <ul class="list1">\
									<li class="li1"><label>手机号码：</label><span><%=mobile%></span></li>\
									<li class="li1"><label>姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名：</label><span><%=userName%></span></li>\
									<li class="li1"><label>积分余额：</label><span><%=integrale%></span></li>\
									<li class="li1"><label>部&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;门：</label>\
									<%for (var i=0;i<departList.length;i++){ %>\
										<% if(departList[i].id == dept){ %>\
											<span><%=departList[i].itemname%></span>\
										<%}%>\
									<%}%>\
									</li>\
									<li class="li1"><label>送餐地址：</label>\
										<%for (var i=0;i<addressList.length;i++){ %>\
											<% if(addressList[i].id == address){ %>\
												<span><%=addressList[i].itemname%></span>\
											<%}%>\
										<%}%>\
									</li>\
									<li class="li1">\
										<label>账号类型：</label>\
										<% if(roleType == "1"){ %>\
											<span>管理员</span>\
										<%}%>\
										<% if(roleType == "2"){ %>\
											<span>个人账户</span>\
										<%}%>\
										<% if(roleType == "3"){ %>\
											<span>可被代订</span>\
										<%}%>\
									</li>\
									<li class="li1 spans"><label>积分明细：</label>\
										<div class="integral">\
										<% if(recodeList != null){ %>\
											<%for (var i=0;i<recodeList.length;i++){ %>\
												<div class="integral-list">\
													<div><span class="left" style="color: #333333; font-size:15px"><%=recodeList[i].opTypeStr%></span>\
													<% if(recodeList[i].exchangeIntegral.indexOf("-") != -1){ %><span class="right greenC"><%=recodeList[i].exchangeIntegral%></span><%}%>\
													<% if(recodeList[i].exchangeIntegral.indexOf("-") == -1){ %><span class="right yellowC">+<%=recodeList[i].exchangeIntegral%></span><%}%>\
													</div>\
													<div style="font-size:13px"><span class="left"><%=recodeList[i].createTime%></span><span class="right">余额:<%=recodeList[i].newIntgrale%></span></div>\
												</div>\
											<%}%>\
										<%}%>\
										</div>\
									</li>\
								 </ul>\
								 <div class="dialog-btns">\
								 	<button class="sure-detail-btn btn-red">确定</button>\
							 	 </div>\
						</div>\
					</div>\
				</div>	',

  };
  var api = {
    init: function () {
      this.render();
      this.bindEvent();
    },
    render: function () {
      this.rendertable();
    },
    bindEvent: function () {
      var _this = this;
      //重置
      $(".set-btn").on("click", function () {
        $("#conditionform input:not('#orgId'),#conditionform select").val("");
        _this.againRenderTable();
      });
      // 模糊搜索
      _this.search_fuzzy()
    },
    // 模糊搜索事件
    search_fuzzy: function(){
      $('body').on("input",'#orgId',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_input(_this,'org__id','org__span',contextPath,'/admin/templ/getOrgListByName')
      });
      $('body').on("focus",'#orgId',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',contextPath,'/admin/templ/getOrgListByName')
      });
      $('body').on('blur','#orgId',function(event) {
        var _this = $(this)
        cjqt.search_fuzzy.event_blur(_this,'org__id')
      });
      $('body').on('click','.select_wrap>.org__span', function(event) {
        event.preventDefault();
        event.stopPropagation();
        /* Act on the event */
        var _this = $(this)
        cjqt.search_fuzzy.event_click(_this,'org__id','#orgId')
      });
    },
    rendertable: function () {
      var _this = this;
      layui.use(['table', 'laydate', 'form'], function () {
        table = layui.table;
        var form = layui.form,
          layer = layui.layer,
          laydate = layui.laydate;
        //日期
        laydate.render({
          elem: '#date1'
        });
        laydate.render({
          elem: '#date2'
        });
        //方法级渲染
        table.render({
          elem: '#LAY_table_user',
          url: $("#contextPath").val() + "/lingling/getPassHistory",
          method: "post",
          where: {
            orgId: $("#orgId").attr('org__id')
          },
          skin: 'nob' //行边框风格
            ,
          even: true //开启隔行背景
            ,
          size: 'lg' //小尺寸的表格
            ,
          cols: [
            [{
              field: 'index',
              title: 'ID',
              width: '80',
              templet: '#indexTpl'
            }, {
              field: 'deviceType',
              title: '设备类型',
              width: 120,
              templet: function (data) {
                if (data.deviceType == 1) {
                  return '闸机'
                } else if (data.deviceType == 2) {
                  return '梯控'
                } else if (data.deviceType == 3) {
                  return '门禁'
                }
              }
            }, {
              field: 'deviceId',
              title: '设备ID',
              minWidth: 200
            }, {
              field: 'deviceName',
              title: '名称',
              minWidth: 200
            }, {
              field: 'mobile',
              title: '手机号码',
              minWidth: 200
            }, {
              field: 'userName',
              title: '姓名',
              minWidth: 200
            }, {
              field: 'type',
              title: '用户类型',
              width: 120,
              templet: function (data) {
                if (data.type == "1") {
                  return '员工'
                } else {
                  return '访客'
                }
              }
            }, {
              field: 'cardUid',
              title: '实体卡号',
              minWidth: 200
            }, {
              field: 'openTime',
              title: '通行时间',
              minWidth: 200
            }]
          ],
          id: 'testReload',
          page: true,
          response: {
            statusName: 'code' //数据状态的字段名称，默认：code
              ,
            statusCode: 200 //成功的状态码，默认：0
              ,
            countName: "count" //数据总数的字段名称，默认：count
              ,
            dataName: "data" //数据列表的字段名称，默认：data
          },
          request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
              ,
            limitName: 'pageSize' //每页数据量的参数名，默认：limit
          },
          done: function (res, curr, count) {

          }
        });
        //查询
        $('.search-btn').on('click', function () {
          _this.againRenderTable();
        });
      });
    },
    /*重新渲染table*/
    againRenderTable: function () {
      table.reload('testReload', {
        where: {
          deviceType: $("#conditionform .deviceType").val(),
          deviceId: $("#conditionform .deviceId").val(),
          cardCode: $("#conditionform .cardCode").val(),
          startTime: $("#date1").val(),
          endTime: $("#date2").val(),
          mobile: $("#conditionform .mobile").val(),
          userType: $("#conditionform .userType").val(),
          orgId: $("#conditionform  #orgId").attr('org__id')
        },
        page: {
          curr: 1 //重新从第 1 页开始
        }
      });
    },
    strlen: function (str) {
      var len = 0;
      for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i);
        //单字节加1   
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
          len++;
        } else {
          len += 2;
        }
      }
      return len;
    },
    // 两个数组去重
    array_diff: function (a, b) {
      for (var i = 0; i < b.length; i++) {
        for (var j = 0; j < a.length; j++) {
          if (a[j] == b[i]) {
            a.splice(j, 1);
            j = j - 1;
          }
        }
      }
      return a;
    }
  };
  api.init();
})