
var permDelete = $("#perm-delete").val();//删除权限
var permEdit = $("#perm-edit").val();//编辑权限
/**
 * 用户信息
 */
$(function () {
  var service = $("#contextPath").val();
  var param = {};
  // 数据获取
  var api = {
    get_save_user_info: function (params) {
      $.ajax({
        type: "post",
        url: service + "/admin/washcar/saveCustomer",
        data: {
          id: params.id,
          userName: params.userName,
          mobile: params.mobile,
          dept: params.dept,
          position: params.position,
          carNumber: params.carNumber,
          carType: params.carType,
          parkSpace: params.parkSpace,
          serialNumber: params.serialNumber,
          orgId: params.orgId
        },
        success: function (data) {
          var data = data;
          if (data.code == '200') {
            layer.msg(data.msg);
            $('.alert').css('display', 'none');
            api.get_user_info_list();
            param = {};
            api.get_form_summit_val(param);
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_user_del: function (custId) {
      $.ajax({
        type: "post",
        url: service + "/admin/washcar/deleteCustomer",
        data: {
          custId: custId
        },
        success: function (data) {
          console.log(data, 222222222);
          if (data.code == '200') {
            layer.msg(data.msg)
            api.get_user_info_list();
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_user_info_list: function (orgId, userName, mobile, carNumber) {
      layui.use('table', function () {
        var table = layui.table;
        table.render({
          elem: '#table_user_info',
          url: service + '/admin/washcar/getCustmoerList',
          method: 'post',
          where: {
            orgId: orgId,
            userName: userName,
            mobile: mobile,
            carNumber: carNumber
          },
          skin: 'nob', //行边框风格
          even: true, //开启隔行背景
          size: 'lg', //小尺寸的表格
          limit: 5,
          cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
          cols: [
            [{
                field: 'serialNumber',
                width: "96",
                title: '序号',
              }, {
                field: 'userName',
                width: 164,
                title: '客户姓名'
              }, {
                field: 'mobile',
                width: 174,
                title: '电话',
              }, {
                field: 'dept',
                width: 174,
                title: '部门'
              }, {
                field: 'position',
                title: '职位',
                width: 174,
                minWidth: 100
              } //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
              , {
                field: 'carNumber',
                title: '车牌',
                width: 174
              }, {
                field: 'carType',
                title: '车型',
                width: 174
              }, {
                field: 'parkSpace',
                width: 174,
                title: '停车位'
              }, {
                field: 'manage',
                width: 181,
                fixed: 'right',
                title: '操作管理',
                templet: function (data) {
                  var str = "<div class='operate'>";
                  if (permEdit == 1){
                    str += "<span class='op_edit_btn' lay-event='edit'><i class='edit'></i><i class='op_hover'>编辑</i></span>";
                  }
                  if (permDelete == 1){
                      str += "<span class='op_del_btn' lay-event='del'><i class='delete'></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) {
            $(".edit,.delete").mouseover(function () {
              $(this).next().show();
            })
            $(".edit,.delete").mouseout(function () {
              $(this).next().hide();
            })
          }
        });
        table.on('tool(user)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
          console.log(obj.event); //点击事件
          if (obj.event === 'edit') {
            $('.alert').css('display', 'block');
            $('.alert .alert_con>h2').text('用户信息编辑');
            let params = {
              userName: obj.data.userName,
              mobile: obj.data.mobile,
              carNumber: obj.data.carNumber,
              dept: obj.data.dept,
              position: obj.data.position,
              carType: obj.data.carType,
              parkSpace: obj.data.parkSpace,
              id: obj.data.id,
              serialNumber: obj.data.serialNumber,
              orgId: obj.data.orgId
            };
            param = params;
            api.get_form_summit_val(params);
          } else if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
              api.get_user_del(obj.data.id);
              obj.del();
              layer.close(index);
            });
          }
          console.log(obj.data); //所在行的所有相关数据  
        });
      });
    },
    get_form_summit_val: function (params) {
      console.log(params);
      $("#username").val(params.userName);
      $("#mobile").val(params.mobile);
      $("#carNumber").val(params.carNumber);
      $("#dept").val(params.dept);
      $("#position").val(params.position);
      $("#carType").val(params.carType);
      $("#parkSpace").val(params.parkSpace);
      $("#serialNumber").val(params.serialNumber);
      $('#orgIds').val(params.orgId);
      layui.form.render('select');
    },
    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_must_props: function (mobile, userName, carNumber) { //判断必填项
      if (mobile != '' && userName != '' && carNumber != '') {
        return true
      } else if (mobile == '') {
        layer.msg('手机号必填');
        return false
      } else if (userName == '') {
        layer.msg('名字必填');
        return false
      } else if (carNumber == '') {
        layer.msg('车牌号必填');
        return false
      }
    },
    // 模糊搜索事件
    search_fuzzy: function(){
      $('body').on("input",'#orgId',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_input(_this,'org__id','org__span',service,'/admin/templ/getOrgListByName')
      });
      $('body').on("focus",'#orgId',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',service,'/admin/templ/getOrgListByName')
      });
      $('body').on('blur','#orgId',function(event) {
        var _this = $(this)
        cjqt.search_fuzzy.event_blur(_this,'org__id')
      });
      $('body').on('click','#orgId~.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')
      });

      $('body').on("input",'#orgIds',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_input(_this,'org__id','org__span',service,'/admin/templ/getOrgListByName')
      });
      $('body').on("focus",'#orgIds',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',service,'/admin/templ/getOrgListByName')
      });
      $('body').on('blur','#orgIds',function(event) {
        var _this = $(this)
        cjqt.search_fuzzy.event_blur(_this,'org__id')
      });
      $('body').on('click','#orgIds~.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','#orgIds')
      });
    }
  }
  api.get_user_info_list();
  // 模糊搜索
  api.search_fuzzy()
  // 事件处理
  $('.btn1').click(function () {
    $('.alert').css('display', 'block');
    $('.alert .alert_con>h2').text('新增用户');
  })
  // 保存
  $('.saveBtn').click(function () {
    let id = '';
    let serialNumber = null;
    if (param != {}) {
      id = param.id;
    }
    let data = {
      id: id,
      userName: $('input[name="username"]').val(),
      mobile: $('input[name="mobile"]').val(),
      dept: $('input[name="dept"]').val(),
      position: $('input[name="position"]').val(),
      carNumber: $('input[name="carNumber"]').val(),
      carType: $('input[name="carType"]').val(),
      parkSpace: $('input[name="parkSpace"]').val(),
      serialNumber: $('input[name="serialNumber"]').val(),
      orgId: $('#orgIds').attr('org__id')
    }
    if(!data.orgId){
      data.orgId = $('#orgId').attr('org__id');
    }
    if (api.get_must_props(data.mobile, data.userName, data.carNumber)) {
      if (!api.mobile_judge(data.mobile)) {
        layer.msg('请输入正确的手机号')
      } else {
        api.get_save_user_info(data);
      }
    }
  })
  // 取消
  $('.cancelBtn').click(function () {
    $('.alert').css('display', 'none');
    $('input[name="username"]').val('');
    $('input[name="mobile"]').val('');
    $('input[name="dept"]').val('');
    $('input[name="position"]').val('');
    $('input[name="carNumber"]').val('');
    $('input[name="carType"]').val('');
    $('input[name="parkSpace"]').val('');
    $('input[name="serialNumber"]').val('');
    $('#orgIds').get(0).selectedIndex = 0;
    layui.form.render('select');
  })
  // 查询
  $('#searchUser').click(function () {
    let userName = $('input[name="user"]').val();
    let mobile = $('input[name="phone"]').val();
    let carNumber = $('input[name="number"]').val();
    let orgId = $('#orgId').attr('org__id');
    console.log(userName, mobile, carNumber);
    if (mobile != '') {
      if (!api.mobile_judge(mobile)) {
        layer.msg('请输入正确的手机号')
      } else {
        api.get_user_info_list(orgId, userName, mobile, carNumber);
      };
    } else {
      api.get_user_info_list(orgId, userName, mobile, carNumber);
    }

  })
  // 重置
  $('#resetSearch').click(function () {
    $('input[name="user"]').val('');
    $('input[name="phone"]').val('');
    $('input[name="number"]').val('');
    $('#orgId').get(0).selectedIndex = 0;
    layui.form.render('select');
  })
})