/**
 * 退宿审核管理
 */
$(function () {
  var service = $("#contextPath").val();
  var permpcApprove = $("#perm-approve").val();

  var tpl = {
    "addApplay": ' <div class="alert alert1">\
    <div class="alert_con">\
      <h2>退宿审核 <i class="cancelBtn"></i></h2>\
      <div class="basic_info">\
        <ul class="list1">\
          <li class="li1"><label>申请单号：</label><span><%=data.dormRoomInfo.number%></span></li>\
          <li class="li1"><label>审核状态：</label>\
            <%if(data.dormRoomInfo.status==1){ %>\
              <span class="txt_red">待审核</span>\
            <%}else if(data.dormRoomInfo.status==2){ %>\
              <span class="txt_red">待放行</span>\
            <%}else if(data.dormRoomInfo.status==3){ %>\
              <span class="txt_red">已放行</span>\
            <%}else if(data.dormRoomInfo.status==4){ %>\
              <span class="txt_red">审核不通过</span>\
            <%}else if(data.dormRoomInfo.status==5){ %>\
              <span class="txt_red">放行不通过</span>\
            <%}%>\
          </li>\
          <li><label>申请时间：</label><span><%=data.dormRoomInfo.createTime%></span></li>\
          <li><label class="word_algin_l">申&nbsp;请&nbsp;人：</label><span><%=data.dormRoomInfo.userName%></span></li>\
          <li><label class="word_algin">工 号：</label><span><%=data.dormRoomInfo.workNo%></span></li>\
          <li><label>身份证号：</label><span><%=data.dormRoomInfo.idcard%></span></li>\
          <li><label class="word_algin">公 寓：</label><span><%=data.dormRoomInfo.apartment%></span></li>\
          <li><label>手机号码：</label><span><%=data.dormRoomInfo.mobile%></span></li>\
          <li><label class="word_algin">楼 宇：</label><span><%=data.dormRoomInfo.build%></span></li>\
          <li><label class="word_algin">楼 层：</label><span><%=data.dormRoomInfo.floor%></span></li>\
          <li><label class="word_algin">房 号：</label><span><%=data.dormRoomInfo.room%></span></li>\
          <li><label class="word_algin">床 号：</label><span><%=data.dormRoomInfo.bedNo%></span></li>\
          <li class="item_info lis"><label>物品信息：</label><span class="span-txt"><%=data.dormRoomInfo.goodsInfo%></span></li>\
          <li class="bed_head_empty lis"><label>床头置空照片：</label><div class="image-bo"><img src="<%=data.dormRoomInfo.bedEmptyImage%>"  onclick="boostImg(this)"/></div></li>\
          <li class="bed_head_sign lis"><label>床头标签照片：</label><div class="image-bo"><img src="<%=data.dormRoomInfo.bedLabelImage%>" onclick="boostImg(this)"/></div></li>\
          <li class="other_image lis">\
            <label>其他物品照片：</label>\
            <% if(data.dormRoomInfo.otherImage != null){ %>\
              <%for (var i=0;i<data.dormRoomInfo.otherImage.length;i++){ %>\
                <img class="mr8" src="<%=data.dormRoomInfo.otherImage[i]%>" onclick="boostImg(this)"/>\
              <% } %>\
            <%}%>\
          </li>\
          <li class="item_info lis">\
            <label>舍友工号：</label><span class="span-txt"><%=data.dormRoomInfo.roomerWorkNo%></span>\
          </li>\
          <li class="bed_head_empty lis other_image"><label>舍友照片：</label><div class="image-bo"><img src="<%=data.dormRoomInfo.roomerImage%>"  onclick="boostImg(this)"/></div></li>\
          <li class="audit_result lis">\
            <form class="layui-form" action="">\
              <div class="layui-form-item layui-form-text">\
                <label>审核结果：</label>\
                <div class="layui-input-block">\
                  <input type="radio" name="result" value="通过" title="通过" checked="">\
                  <input type="radio" name="result" value="拒绝" title="拒绝">\
                </div>\
              </div>\
            </form>\
          </li>\
          <li class="audit_view lis bt_no pt0">\
            <form class="layui-form" action="">\
              <div class="layui-form-item layui-form-text">\
                <label>审核意见：</label>\
                <div class="layui-input-block">\
                  <textarea placeholder="请输入审核意见" class="layui-textarea">通过</textarea>\
                </div>\
              </div>\
            </form>\
          </li>\
          <li class="btns lis">\
            <button class="cancelBtn bt_blue_middle mr28">取消</button>\
            <button class="saveBtn bl_yellow">确认</button>\
          </li>\
        </ul>\
      </div>\
    </div>\
  </div>',
    "addDetail": '<div class="alert alert2">\
  <div class="alert_con">\
    <h2>退宿详情 <i class="cancelBtn"></i></h2>\
    <div class="basic_info">\
      <ul class="list1">\
        <li class="li1"><label>申请单号：</label><span><%=data.dormRoomInfo.number%></span></li>\
        <li class="li1"><label>审核状态：</label>\
          <%if(data.dormRoomInfo.status==1){ %>\
            <span class="txt_red">待审核</span>\
          <%}else if(data.dormRoomInfo.status==2){ %>\
            <span class="txt_red">待放行</span>\
          <%}else if(data.dormRoomInfo.status==3){ %>\
            <span class="txt_red">已放行</span>\
          <%}else if(data.dormRoomInfo.status==4){ %>\
            <span class="txt_red">审核不通过</span>\
          <%}else if(data.dormRoomInfo.status==5){ %>\
            <span class="txt_red">放行不通过</span>\
          <%}%>\
        </li>\
        <li><label>申请时间：</label><span><%=data.dormRoomInfo.createTime%></span></li>\
        <li><label class="word_algin_l">申&nbsp;请&nbsp;人：</label><span><%=data.dormRoomInfo.userName%></span></li>\
        <li><label class="word_algin">工 号：</label><span><%=data.dormRoomInfo.workNo%></span></li>\
        <li><label>身份证号：</label><span><%=data.dormRoomInfo.idcard%></span></li>\
        <li><label class="word_algin">公 寓：</label><span><%=data.dormRoomInfo.apartment%></span></li>\
        <li><label>手机号码：</label><span><%=data.dormRoomInfo.mobile%></span></li>\
        <li><label class="word_algin">楼 宇：</label><span><%=data.dormRoomInfo.build%></span></li>\
        <li><label class="word_algin">楼 层：</label><span><%=data.dormRoomInfo.floor%></span></li>\
        <li><label class="word_algin">房 号：</label><span><%=data.dormRoomInfo.room%></span></li>\
        <li><label class="word_algin">床 号：</label><span><%=data.dormRoomInfo.bedNo%></span></li>\
        <li class="item_info lis"><label>物品信息：</label><span class="span-txt"><%=data.dormRoomInfo.goodsInfo%></span></li>\
        <li class="bed_head_empty lis"><label>床头置空照片：</label><div class="image-bo"><img onclick="boostImg(this)" src="<%=data.dormRoomInfo.bedEmptyImage%>" /></div></li>\
        <li class="bed_head_sign lis"><label>床头标签照片：</label><div class="image-bo"><img  onclick="boostImg(this)" src="<%=data.dormRoomInfo.bedLabelImage%>" /></div></li>\
        <li class="other_image lis">\
          <label>其他物品照片：</label>\
          <% if(data.dormRoomInfo.otherImage != null){ %>\
            <%for (var i=0;i<data.dormRoomInfo.otherImage.length;i++){ %>\
              <img onclick="boostImg(this)" class="mr8" src="<%=data.dormRoomInfo.otherImage[i]%>" />\
            <% } %>\
          <%}%>\
        </li>\
        <li class="item_info lis">\
        <label>舍友工号：</label><span class="span-txt"><%=data.dormRoomInfo.roomerWorkNo%></span>\
      </li>\
      <li class="bed_head_empty lis other_image"><label>舍友照片：</label><div class="image-bo"><img src="<%=data.dormRoomInfo.roomerImage%>"  onclick="boostImg(this)"/></div></li>\
        <% if(data.recodeInfo != null){ %>\
          <li class="li1 word_algin_l"><label>审&nbsp;核&nbsp;人：</label><span><%=data.recodeInfo.auditor%></span></li>\
          <li class="li1 word_algin_l"><label>放&nbsp;行&nbsp;人：</label><% if(data.releaseInfo != null){ %><span><%=data.releaseInfo.releaseMan%></span><%}%></li>\
          <li><label>审核时间：</label><span><%=data.recodeInfo.auditTime%></span></li>\
          <li><label>放行时间：</label><% if(data.releaseInfo != null){ %><span><%=data.releaseInfo.releaseTime%></span><%}%></li>\
          <li><label>审核结果：</label><span><%=data.recodeInfo.result%></span></li>\
          <li><label>放行结果：</label><% if(data.releaseInfo != null){ %><span><%=data.releaseInfo.releaseResult%></span><%}%></li>\
          <li class="apply-ideo"><label>审核意见：</label><span><%=data.recodeInfo.reason%></span></li>\
          <% if(data.releaseInfo != null){ %>\
            <li><label>放行意见：</label><span><%=data.releaseInfo.releaseReason%></span></li>\
            <li class="fr"><label>回收物品：</label><span><%=data.releaseInfo.releaseGoods%></span></li>\
            <% if(data.dormRoomInfo.status == 3 || data.dormRoomInfo.status == 5 ){ %>\
              <li class="lis other_image"><label>放行照片：</label>\
                <% if(data.releaseInfo.releaseImages != null){ %>\
                  <%for (var i=0;i<data.releaseInfo.releaseImages.length;i++){ %>\
                    <img onclick="boostImg(this)" class="mr8" src="<%=data.releaseInfo.releaseImages[i]%>" />\
                  <% } %>\
                <%}%>\
              </li>\
            <%}%>\
          <%}%>\
        <%}%>\
      </ul>\
    </div>\
  </div>\
</div>'
  };
  var orgId = $('#orgId').attr('org__id');
  var param = {
    userName: '',
    mobile: '',
    orgId: orgId,
    build: '',
    floor: '',
    room: '',
    apartment: '',
    status: ''
  };
  var layer;
  var table;
  layui.use('layer', function () {
    layer = layui.layer;
  });
  // 数据获取
  var api = {
    get_user_info_list: function (param) {
      layui.use('table', function () {
        var table = layui.table;
        table.render({
          elem: '#table_retreat_audit',
          url: service + '/admin/dorm/getDormCheckOutList',
          method: 'post',
          where: {
            userName: param.userName,
            mobile: param.mobile,
            orgId: param.orgId,
            build: param.build,
            floor: param.floor,
            room: param.room,
            apartment: param.apartment,
            status: param.status
          },
          skin: 'nob', //行边框风格
          even: true, //开启隔行背景
          size: 'lg', //小尺寸的表格
          limit: 10,
          cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
          cols: [
            [{
            	 field:'Index', 
	    		   title: '序号',
	    		   templet: '#indexTpl',
	    		   width: '6%'
            }, {
              field: 'number',
              width: "12%",
              title: '申请单号'
            }, {
              field: 'userName',
              width: "8%",
              title: '姓名',
            }, {
              field: 'workNo',
              width: "8%",
              title: '工号'
            }, {
              field: 'sex',
              title: '性别',
              width: "8%"
            }, {
              field: 'IDcard',
              title: '身份证号',
              width: "12%"
            }, {
              field: 'mobile',
              title: '手机号码',
              width: "8%"
            }, {
              field: 'apartment',
              width: "8%",
              title: '公寓'
            }, {
              field: 'build',
              width: "8%",
              title: '楼宇'
            }, {
              field: 'floor',
              width: "8%",
              title: '楼层'
            }, {
              field: 'room',
              width: "8%",
              title: '房号'
            }, {
              field: 'bedNo',
              width: "8%",
              title: '床号'
            }, {
              field: 'status',
              width: "8%",
              title: '状态',
              templet: function (data) {
                if (data.status == 1) {
                  return "<span class='txt_red'>待审核</span>"
                } else if (data.status == 2) {
                  return "<span class='txt_red'>待放行</span>"
                } else if (data.status == 3) {
                  return "<span class='txt_red'>已放行</span>"
                } else if (data.status == 4) {
                  return "<span class='txt_red'>审核不通过</span>"
                } else if (data.status == 5) {
                  return "<span class='txt_red'>放行不通过</span>"
                }
              }
            }, {
              field: 'createTime',
              width: "12%",
              title: '申请时间'
            }, {
              field: 'manage',
              width: '8%',
              title: '操作',
              fixed: 'right',
              templet: function (data) {
                var str = "<div class='operate op_btns'>";

                if (data.status == 1 && permpcApprove == 1) {
                  str += "<span class='op_patch_btn' lay-event='patch'><i class='audit'></i><i class='op_hover'>审核</i></span>"
                }

                str += "<span class='op_detail_btn' lay-event='detail'><i class='detail'></i><i class='op_hover'>详情</i></span>"
                str += "</div>";
                return str;
              }
            }]
          ],
          id: 'reloadData',
          page: true,
          response: {
            statusName: 'code', //数据状态的字段名称，默认：code             
            statusCode: 200, //成功的状 态码，默认：0        
            countName: "count", //数据总数的字段名称，默认：count            
            dataName: "data" //数据列表的字段名称，默认：data
          },
          done: function (res, curr, count) {
            $(".audit,.detail").mouseover(function(){
              $(this).next().show();
            })
            $(".audit,.detail").mouseout(function(){
              $(this).next().hide();
            })
          },
        });
        table.on('tool(retreat)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
          // console.log(obj.event); //点击事件
          // console.log(obj.data, 'ddd');
          if (obj.event === 'patch') {
            var id = obj.data.id;
            api.get_detail_val(id, function (data) {
              var data = data;
              //显示审核弹窗
              $("body").append(template.render(tpl.addApplay)({
                data: data
              }));
              var status = "1";
              layui.form.render('radio');
              layui.use('form', function () {
                var form = layui.form;
                form.on('radio', function (data) {
                  data.value == "通过" ? status = "1" : status = "2";
                  data.value == "通过" ? $(".layui-textarea").val("通过"):$(".layui-textarea").val("")
                });
              });
              // 保存
              $('.saveBtn').click(function () {
                var formData = new FormData();
                formData.append("id", id);
                formData.append("status", status);
                formData.append("reason", $(".layui-textarea").val());
                if($(".layui-textarea").val() == ""){
                  layer.msg("请输入审核意见")
                  return false
                }
                $.ajax({
                  type: "post",
                  url: service + "/admin/dorm/auditDormCheckOut",
                  data: formData,
                  cache: false,                      // 不缓存
                  processData: false,                // 告诉jQuery不要去处理发送的数据
                  contentType: false,                // 告诉jQuery不要去设置Content-Type请求头
                  dataType: 'text',
                  success: function (data) {
                    var data = JSON.parse(data);
                    if (data.code == '200') {
                      layer.msg(data.msg)
                      $(".alert").remove();
                      param = {
                        userName: '',
                        mobile: '',
                        orgId: $('#orgId').attr('org__id'),
                        build: '',
                        floor: '',
                        room: '',
                        apartment: '',
                        status: ''
                      };
                      api.get_user_info_list(param);
                    } else {
                      layer.msg(data.msg)
                    }
                  },
                  error: function (error) {
                    layer.msg('error')
                  }
                });
              })
              // 取消
              $('.cancelBtn').click(function () {
                $(".alert").remove();
              })
            })
          } else if (obj.event === 'detail') {
            var id = obj.data.id;
            api.get_detail_val(id, function (data) {
              $("body").append(template.render(tpl.addDetail)({
                data: data
              }));
              // 取消
              $('.cancelBtn').click(function () {
                $(".alert").remove();
              })
            });
          }

        });
      });
    },
    mobile_judge: function (str) { //判断手机号
      var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!myreg.test(str)) {
        return false;
      } else {
        return true;
      }
    },
    get_detail_val: function (id, callback) {
      $.ajax({
        type: "post",
        url: service + "/admin/dorm/getDormRoomerInfo",
        data: {
          id: id
        },
        success: function (data) {
          if (data.code == '200') {
            callback(data.data);
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    }
  }
  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');
    param.status = $('#status').val();
    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('');
    $('#status').get(0).selectedIndex = 0;
    layui.form.render('select');
  })
  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')
    });
  }

})
function boostImg(obj){
  console.log(obj.src);
  $(".alert-bo").css("display","block")
  $(".alert-bo img").attr("src",obj.src)
}
function closeAlert(){
  $(".alert-bo").css("display","none")
}
