// 以下为官方示例
$().ready(function () {
  validateRule();
  // $("#signupForm").validate();
  //省份
  loadProvince();
});

$.validator.setDefaults({
  submitHandler: function () {
    update();
  },
});

function update() {
  $("#roleIds").val(getCheckedRoles());
  $.ajax({
    cache: true,
    type: "POST",
    url: "/sys/user/update",
    data: $("#signupForm").serialize(), // 你的formid
    async: false,
    error: function (request) {
      alert("Connection error");
    },
    success: function (data) {
      if (data.code == 0) {
        parent.layer.msg(data.msg);
        parent.reLoad();
        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
        parent.layer.close(index);
      } else {
        parent.layer.msg(data.msg);
      }
    },
  });
}

function getCheckedRoles() {
  var adIds = "";
  $("input:checkbox[name=role]:checked").each(function (i) {
    if (0 == i) {
      adIds = $(this).val();
    } else {
      adIds += "," + $(this).val();
    }
  });
  return adIds;
}

function setCheckedRoles() {
  var roleIds = $("#roleIds").val();
  alert(roleIds);
  var adIds = "";
  $("input:checkbox[name=role]:checked").each(function (i) {
    if (0 == i) {
      adIds = $(this).val();
    } else {
      adIds += "," + $(this).val();
    }
  });
  return adIds;
}

function validateRule() {
  var icon = "<i class='fa fa-times-circle'></i> ";
  $("#signupForm").validate({
    rules: {
      name: {
        required: true,
      },
      username: {
        required: true,
        minlength: 2,
      },
      password: {
        required: true,
        minlength: 6,
      },
      deptName: {
        required: true,
      },
      province: {
        required: true,
      },
      confirm_password: {
        required: true,
        minlength: 6,
        equalTo: "#password",
      },
      email: {
        required: true,
        email: true,
      },
      topic: {
        required: "#newsletter:checked",
        minlength: 2,
      },
      messages: {
        name: {
          required: icon + "请输入姓名",
        },
        username: {
          required: icon + "请输入您的用户名",
          minlength: icon + "用户名必须两个字符以上",
        },
        password: {
          required: icon + "请输入您的密码",
          minlength: icon + "密码必须6个字符以上",
        },
        deptName: {
          required: icon + "请选择部门",
        },
        province: {
          required: icon + "请选择省份",
        },
        confirm_password: {
          required: icon + "请再次输入密码",
          minlength: icon + "密码必须6个字符以上",
          equalTo: icon + "两次输入的密码不一致",
        },
        email: icon + "请输入您的E-mail",
      },
    },
    ignore: ":hidden:not(select)",
  });
}

var openDept = function () {
  layer.open({
    type: 2,
    title: "选择部门",
    area: ["300px", "450px"],
    content: "/system/sysDept/treeView",
  });
};

function loadDept(deptId, deptName) {
  $("#deptId").val(deptId);
  $("#deptName").val(deptName);
}

function loadProvince() {
  var html = "";
  $.ajax({
    url: "/sys/user/listProvince",
    success: function (data) {
      //加载数据
      for (var i = 0; i < data.length; i++) {
        html +=
          '<option value="' + data[i].value + '">' + data[i].name + "</option>";
      }
      $(".chosen-select-pro").append(html);
      $(".chosen-select-pro").chosen({
        maxHeight: 200,
      });

      //赋值
      if ($("#provinces").val() != "") {
        $(".chosen-select-pro").val($("#provinces").val());
        loadCity($("#provinces").val());
      }
      $(".chosen-select-pro").trigger("chosen:updated");

      //点击事件
      $(".chosen-select-pro").on("change", function (e, params) {
        console.log(params.selected);
        loadCity(params.selected);
        var opt = {
          query: {
            type: params.selected,
          },
        };
      });
    },
  });
}

function loadCity(province) {
  $("#city").empty();
  $("#city").append('<option value="">--选择城市--</option>');
  var html = "";
  $.ajax({
    url: "/sys/user/listCityByProvince",
    data: {
      province: province,
    },
    success: function (data) {
      //加载数据
      for (var i = 0; i < data.length; i++) {
        html +=
          '<option value="' + data[i].value + '">' + data[i].name + "</option>";
      }
      $(".chosen-select-city").append(html);
      //赋值
      if ($("#citys").val() != "") {
        $(".chosen-select-city").val($("#citys").val());
      }
      $(".chosen-select-city").trigger("chosen:updated");
      $(".chosen-select-city").chosen({
        maxHeight: 200,
      });
      //点击事件
      $(".chosen-select-city").on("change", function (e, params) {
        $("#citys").val("");
      });
    },
  });
}

//返回
$("#go_back").click(function () {
  var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
  parent.layer.close(index);
});
