var permApprove = $("#perm-approve").val();//审核权限

/**
 * 用户信息
 */ 
$(function () {
  var service = $("#contextPath").val();
  var param = {
    virName: "",
    applyName: "",
    status: "",
    applyTime: ""
  };
  var datalList = {
    enterNumber: '',
    status: '',
    virName: '',
    orgName: '',
    floorNames: '',
    roomNames: '',
    applyName: '',
    applyTel: '',
    createTime: '',
    contExpireTime: '',
    signingTime: '',
    address: '',
    email: ''
  };
  var detailList = {
    enterNumber: '',
    status: '',
    virName: '',
    orgName: '',
    floorNames: '',
    roomNames: '',
    applyName: '',
    applyTel: '',
    createTime: '',
    opinionUser: '',
    optionTme: '',
    status: "",
    opinion: "",
    contExpireTime: '',
    signingTime: '',
    address: '',
    email: ''
  };
  var orgId = "";
  var checkedList = [];
  var price = 0;
  var custId = '';
  var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
  var table_data = new Array();
  var ids = '';
  var enterId = '';

  // 数据获取
  var api = {
    set_detail_list: function (data) { //详情页面步骤
      $(".alert3 .orderEnter").text(data.enterNumber);
      if (data.status == '1') {
        $(".alert3 .order_status").text("待审核")
      } else if (data.status == '2') {
        $(".alert3 .order_status").text("已通过")
      } else if (data.status == '3') {
        $(".alert3 .order_status").text("已拒绝")
      }
      $(".alert3 .list1 li>label").eq(0).text(data.virName);
      $(".alert3 .list1 li>label").eq(1).text(data.orgName);
      $(".alert3 .list1 li>label").eq(2).text(data.floorNames);
      $(".alert3 .list1 li>label").eq(3).text(data.roomNames);
      data.address?$(".alert3 .list1 li>label").eq(4).text(data.address):$(".alert3 .list1 li>label").eq(4).text(' ');
      data.signingTime?$(".alert3 .list1 li>label").eq(5).text(data.signingTime):$(".alert3 .list1 li>label").eq(5).text(' ');
      data.contExpireTime?$(".alert3 .list1 li>label").eq(6).text(data.contExpireTime):$(".alert3 .list1 li>label").eq(6).text(' ');
      $(".alert3 .list1 li>label").eq(7).text(data.applyName);
      $(".alert3 .list1 li>label").eq(8).text(data.applyTel);
      data.email?$(".alert3 .list1 li>label").eq(9).text(data.email):$(".alert3 .list1 li>label").eq(9).text(' ');
      data.createTime? $(".alert3 .list1 li>label").eq(10).text(data.createTime): $(".alert3 .list1 li>label").eq(10).text(' ')

      data.opinionUser?$(".alert3 .list2 li>label").eq(0).text(data.opinionUser):$(".alert3 .list2 li>label").eq(0).text(' ')
      $(".alert3 .list2 li>label").eq(1).text(data.optionTme);
      if (data.status == '1') {
        $(".alert3 .list2 li>label").eq(2).text("待审核")
      } else if (data.status == '2') {
        $(".alert3 .list2 li>label").eq(2).text("已通过")
      } else if (data.status == '3') {
        $(".alert3 .list2 li>label").eq(2).text("已拒绝")
      }
      data.opinion?$(".alert3 .list2 li>label").eq(3).text(data.opinion):$(".alert3 .list2 li>label").eq(3).text(' ');
    },
    get_order_status: function (status, isEvaluate, comment) { //详情页面状态和评价判断
      if (status != '') {
        if (status == 1) {
          return "等待接单"
        } else if (status == 2) {
          return "待洗车"
        } else if (status == 3) {
          return "洗车中"
        } else if (status == 4) {
          return "已完成"
        } else if (status == 5) {
          return "已转单"
        } else if (status == 6) {
          return "已取消"
        }
      }
      if (isEvaluate == 1) {
        $(".alert3 .order_evaluate").text('待评价');
      } else if (isEvaluate == 2) {
        $(".alert3 .order_evaluate").text(comment);
      }
    },
    get_order_detail: function (id) { //详情数据获取
      $.ajax({
        type: "post",
        url: service + "/admin/enter/getEnterpriseInfo",
        data: {
          enterId: id,
          type: '2'
        },
        success: function (data) {
          console.log(data, 444);
          if (data.code == '200') {
            let detail = data.data;
            detailList = {
              enterNumber: detail.enterNumber,
              status: detail.status,
              virName: detail.virName,
              orgName: detail.buildName,
              floorNames: detail.floors,
              roomNames: detail.rooms,
              applyName: detail.applyName,
              applyTel: detail.applyTel,
              createTime: detail.createTime,
              opinionUser: detail.opinionUser,
              optionTme: detail.optionTme,
              status: detail.status,
              opinion: detail.opinion,
              signingTime: detail.signingTime,
              contExpireTime: detail.contExpireTime,
              address: detail.address,
              email: detail.email
            };
            api.set_detail_list(detailList);
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_enter_apply: function (data) { //企业入驻审核信息
      $(".orderEnter").text(data.enterNumber);
      if (data.status == '1') {
        $(".order_status").text("待审核")
      } else if (data.status == '2') {
        $(".order_status").text("已通过")
      } else if (data.status == '3') {
        $(".order_status").text("已拒绝")
      }
      $(".stepList li>label").eq(0).text(data.virName);
      $(".stepList li>label").eq(1).text(data.orgName);
      $(".stepList li>label").eq(2).text(data.floorNames);
      $(".stepList li>label").eq(3).text(data.roomNames);
      data.address?$(".stepList li>label").eq(4).text(data.address):$(".stepList li>label").eq(4).text(' ');
      data.signingTime?$(".stepList li>label").eq(5).text(data.signingTime):$(".stepList li>label").eq(5).text(' ');
      data.contExpireTime?$(".stepList li>label").eq(6).text(data.contExpireTime):$(".stepList li>label").eq(6).text(' ');
      $(".stepList li>label").eq(7).text(data.applyName);
      $(".stepList li>label").eq(8).text(data.applyTel);
      data.email?$(".stepList li>label").eq(9).text(data.email):$(".stepList li>label").eq(9).text(' ');
      $(".stepList li>label").eq(10).text(data.createTime);

      // $("#signingTime").val(' ');
      // $("#contExpireTime").val(' ');
    },
    get_summit_apply: function () { //企业入驻审核提交
      let status = $('input:radio:checked').val()
      let optionMsg = $(".layui-textarea").val()
      if(status == 3){
        if(!optionMsg){
          layer.msg("请填写审核意见")
          return false
        }
      }
      let enterApply = {
        enterId: enterId,
        optionMsg: optionMsg,
        status: status,
        type: '2'
      };
      console.log(enterApply, 333);
      $.ajax({
        type: "post",
        url: service + "/admin/enter/optionEnterpriseOrder",
        data: enterApply,
        success: function (data) {
          console.log(data, 222222222);
          if (data.code == '200') {
            api.get_enter_apply(datalList);

            detailList = {
              enterNumber: '',
              status: '',
              virName: '',
              orgName: '',
              floorNames: '',
              roomNames: '',
              applyName: '',
              applyTel: '',
              createTime: '',
              opinionUser: '',
              optionTme: '',
              status: "",
              opinion: "",
              contExpireTime: '',
              signingTime: '',
              address: '',
              email: ''
            };
            api.set_detail_list(detailList);
            $(".layui-textarea").val('');
            $('.alert').css('display', 'none');          
            api.get_entry_apply_list(param);
            layer.msg("审核成功")
          } else {
            layer.msg(data.msg)
          }
        },
        error: function (error) {
          layer.msg('error')
        }
      });
    },
    get_entry_apply_list: function (params) { //初次加载页面
      layui.use(['table', 'laydate', 'form'], function () {
        var table = layui.table;
        var form = layui.form,
          layer = layui.layer,
          laydate = layui.laydate;
        //日期
        laydate.render({
          elem: '#date1'
        });
        laydate.render({
          elem: '#date2'
        });
        table.render({
          elem: '#table_user_info',
          url: service + '/admin/enter/getEnterpriseList',
          method: 'post',
          request: {
            pageName: 'pageNo', //页码的参数名称，默认：page
            limitName: 'pageSize' //每页数据量的参数名，默认：limit
          },
          where: {
            virName: params.virName,
            applyName: params.applyName,
            status: params.status,
            applyTime: params.applyTime,
            type: '2',
            orgId: params.orgId
          },
          skin: 'nob', //行边框风格
          even: true, //开启隔行背景
          size: 'lg', //小尺寸的表格
          limit: 10,
          cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
          cols: [
            [{
              field: 'paixu',
              width: '5%',
              title: '序号',
              templet: '#paixu',
              }, {
                field: 'enterNumber',
                width: '10%',
                title: '入驻单编号'
              }, {
                field: 'virName',
                width: '10%',
                title: '企业名称',
              }, {
                field: 'orgName',
                width: '10%',
                title: '楼宇'
              }, {
                field: 'floorNames',
                title: '楼层',
                width: '10%'
              } //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
              , {
                field: 'roomNames',
                title: '房号',
                width: '10%'
              }, {
                field: 'applyName',
                title: '申请人',
                width: '8%'
              }, {
                field: 'applyTel',
                width: '8%',
                title: '联系电话'
              }, {
                field: 'createTime',
                width: '12%',
                title: '申请时间'
              }, {
                field: 'status',
                width: '7%',
                title: '状态',
                templet: function (data) {
                  if (data.status == 1) {
                    return "<span class='txtRed'>待审核</span>"
                  } else if (data.status == 2) {
                    return '已通过'
                  } else if (data.status == 3) {
                    return '已拒绝'
                  }
                }
              }, {
                field: 'manage',
                width: '10%',
                title: '操作',
                templet: function (data) {
                  if (data.status == 1 && permApprove == 1) {
                    return "<div class='operate op_btns'><span class='op_patch_btn' lay-event='patch'>审核</span><span class='op_detail_btn' lay-event='detail'>详情</span></div>"
                  } else {
                    return "<div class='operate op_btns'><span class='op_detail_btn' lay-event='detail'>详情</span></div>"
                  }

                }
              }
            ]
          ],
          id: '',
          page: true,
          response: {
            statusName: 'code', //数据状态的字段名称，默认：code             
            statusCode: 200, //成功的状 态码，默认：0        
            countName: "count", //数据总数的字段名称，默认：count            
            dataName: "data" //数据列表的字段名称，默认：data
          },
          done: function (res, curr, count) {

          }
        });
        table.on('tool(order)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
          console.log(obj.event); //点击事件
          console.log(obj.data, 'ddd');
          if (obj.event === 'patch') { //审核
            $('.alert2').css('display', 'block');
            console.log(obj.data, 222);
            enterId = obj.data.id;
            api.get_enter_apply(obj.data);
          } else if (obj.event === 'detail') { //详情
            api.get_order_detail(obj.data.id);
            $('.alert3').css('display', 'block');
          }
        });
      });
    },
    get_form_summit_val: function (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);
    },
    // 模糊搜索事件
    search_fuzzy: function(){
      $('body').on("input",'#slectOrgid',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_input(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
      });
      $('body').on("focus",'#slectOrgid',function(event){
        var _this = $(this)
        cjqt.search_fuzzy.event_focus(_this,'org__id','org__span',$("#contextPath").val(),'/admin/templ/getOrgListByName')
      });
      $('body').on('blur','#slectOrgid',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','#slectOrgid')
      });
    }
  }
  api.get_entry_apply_list(param);
  // 模糊搜索
  api.search_fuzzy()
  // 事件处理
  $('.certainBtn').click(function () {
    api.get_summit_apply();
  })
  // 关闭弹框按钮
  $('.cancelBtn').click(function () {
    detailList = {
      enterNumber: '',
      status: '',
      virName: '',
      orgName: '',
      floorNames: '',
      roomNames: '',
      applyName: '',
      applyTel: '',
      createTime: '',
      opinionUser: '',
      optionTme: '',
      status: "",
      opinion: "",
      contExpireTime: '',
      signingTime: '',
      address: '',
      email: ''
    };
    api.set_detail_list(detailList);
    $(".layui-textarea").val('');
    $('.alert').css('display', 'none');
  })
  // 查询
  $('#searchUser').click(function () {
    let data = {
      virName: $('input[name="virName"]').val(),
      applyName: $('input[name="applyName"]').val(),
      status: $('#status').val(),
      applyTime: $('input[name="date1"]').val(),
      orgId: $("#slectOrgid").attr('org__id')
    };
    console.log(data);
    api.get_entry_apply_list(data);
  })
  // 重置
  $('#resetSearch').click(function () {
    $('input[name="virName"]').val('');
    $('input[name="applyName"]').val('');
    $('#status').get(0).selectedIndex = 0;
    $('#status').val('');
    $('input[name="date1"]').val('');
    layui.form.render('select');
  })

})