/**
 * 宿舍信息管理
 */
$(function () {
  var service = $("#contextPath").val();
  var orgId = $('#orgId').attr('org__id');
  var param = {
    userName: '',
    mobile: '',
    orgId: orgId,
    build: '',
    floor: '',
    room: '',
    apartment: ''
  };
  // 数据获取
  var api = {
    get_user_info_list: function (param) {
      layui.use('table', function () {
        var table = layui.table;
        table.render({
          elem: '#table_user_info',
          url: service + '/admin/dorm/getDormRoomerList',
          method: 'post',
          where: {
            userName: param.userName,
            mobile: param.mobile,
            orgId: param.orgId,
            build: param.build,
            floor: param.floor,
            room: param.room,
            apartment: param.apartment
          },
          skin: 'nob', //行边框风格
          even: true, //开启隔行背景
          size: 'lg', //小尺寸的表格
          limit: 10,
          cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
          cols: [
            [{
            	field:'Index', 
	    		   title: '序号',
	    		   templet: '#indexTpl',
	    		   width: '6%'
            }, {
              field: 'apartment',
              width: "10%",
              title: '公寓'
            }, {
              field: 'build',
              width: "10%",
              title: '楼宇',
            }, {
              field: 'floor',
              width: "8%",
              title: '楼层'
            }, {
              field: 'room',
              title: '房号',
              width: "8%"
            }, {
              field: 'bedNo',
              title: '床号',
              width: "8%"
            }, {
              field: 'sex',
              title: '性别',
              width: "8%"
            }, {
              field: 'workNo',
              width: "10%",
              title: '工号'
            }, {
              field: 'userName',
              width: "8%",
              title: '姓名'
            }, {
              field: 'IDcard',
              width: "12%",
              title: '身份证号'
            }, {
              field: 'mobile',
              width: "12%",
              title: '手机号码'
            }]
          ],
          id: 'reloadData',
          page: true,
          response: {
            statusName: 'code', //数据状态的字段名称，默认：code             
            statusCode: 200, //成功的状 态码，默认：0        
            countName: "count", //数据总数的字段名称，默认：count            
            dataName: "data" //数据列表的字段名称，默认：data
          },
          done: function (res, curr, count) {

          }
        });
      });
    },
    mobile_judge: function (str) { //判断手机号
      var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!myreg.test(str)) {
        return false;
      } else {
        return true;
      }
    },
    exportFile: function () {
      var fileName = $("#excelFile").val();
      var extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length);
      if (extension == "xlsx") {
        if (fileName == null || fileName == "") {
          layer.alert("数据导入为空,请按照模板填写资料后再导入!");
          return;
        }
        $.ajaxFileUpload({
          url: $("#contextPath").val() + "/admin/dorm/importDormRoomer",
          secureuri: false, //是否需要安全协议，一般设置为false
          fileElementId: 'excelFile', //文件上传域的ID
          dataType: 'json', //返回值类型 一般设置为json
          data: {},
          success: function (data, status) //服务器成功响应处理函数
          {
            $('#excelFile').replaceWith('<input class="excelFile" type="file" id="excelFile" name="excelFile" >')
            if (data.code == '200') {
              layui.use('table', function () {
                var table = layui.table;
                table.reload('reloadData', {
                  where: {
                    userName: param.userName,
                    mobile: param.mobile,
                    orgId: param.orgId,
                    build: param.build,
                    floor: param.floor,
                    room: param.room,
                    apartment: param.apartment
                  },
                  page: {
                    curr: 1
                  }
                });
              });
            } else {
              layer.alert(data.msg);
            }
          }
        })
      } else {
        layer.alert("请上传xlsx格式的文件");
      }
    }
  }
  api.get_user_info_list(param);
  // 查询
  $('#searchUser').click(function () {
    param.userName = $('input[name="user"]').val();
    param.mobile = $('input[name="phone"]').val();
    param.build = $('input[name="build"]').val();
    param.floor = $('input[name="floor"]').val();
    param.room = $('input[name="room"]').val();
    param.apartment = $('input[name="apartment"]').val();
    param.orgId = $('#orgId').attr('org__id');
    console.log($('#orgId'))
    if (param.mobile!='') {
    	if(param.mobile.length>=4){
    		api.get_user_info_list(param);
    	}else{
    		layer.msg('请输入4位及以上位数手机号码');
    	}
//      if (!api.mobile_judge(param.mobile)) {
//        layer.msg('请输入正确的手机号')
//      } else {
//        
//      };
    } else {
      api.get_user_info_list(param);
    }
  })
  // 重置
  $('#resetSearch').click(function () {
    $('input[name="user"]').val('');
    $('input[name="phone"]').val('');
    $('input[name="apartment"]').val('');
    $('input[name="build"]').val('');
    $('input[name="floor"]').val('');
    $('input[name="room"]').val('');
  })
  // 下载模板
  $("#importTemplate").click(function () {
    location.href = $("#contextPath").val() + "/upload/DormRoomer.xlsx";
  })
  // 导入数据
  $("#excelFile").live("change", function () {
    api.exportFile();
  });
  search_fuzzy()
  // 模糊搜索事件
    function search_fuzzy(){
      $('body').on("input",'#orgId',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_input(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
      });
      $('body').on("focus",'#orgId',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',$("#contextPath").val(),'/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')
      });
    }

})