
layui.use(['form', 'layedit', 'tpl', 'laydate'], function () {
  var form = layui.form
    , layer = layui.layer
    , layedit = layui.layedit
    , laydate = layui.laydate;
  var where = {};

  var FORM_QUERY_ID = "lay-form-submit"; // form seach ElementId
  init();
  form.on(`submit(${FORM_QUERY_ID})`, function (data) {
    console.log("查询：", data.field);
    table.reload(LIST_TABLE_ID, { where });
  });
  function init() {
    getSelectData();
  }
  function getSelectData() {
    var data = { list: [] };
    var res = [
      { name: "1111", value: "1111" },
      { name: "2222", value: "2222" },
    ];
    var list = [{ name: "请选择", value: "" }];
    for (var item in res) {
      var selected = false;
      list.push({
        name: res[item].name,
        value: res[item].value,
        selected: selected,
      });
    }
    data.list = list;
    layui.tpl.setSelectTpl("testIdSelect", data);
  }
  //日期
  laydate.render({
    elem: '#date'
  });
  laydate.render({
    elem: '#date1'
  });
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');

  //自定义验证规则
  form.verify({
    title: function (value) {
      if (value.length < 5) {
        return '标题至少得5个字符啊';
      }
    }
    , pass: [
      /^[\S]{6,12}$/
      , '密码必须6到12位，且不能出现空格'
    ]
    , content: function (value) {
      layedit.sync(editIndex);
    }
  });

  //监听指定开关
  form.on('switch(switchTest)', function (data) {
    layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
      offset: '6px'
    });
    layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
  });

  //监听提交
  form.on('submit(demo1)', function (data) {
    layer.alert(JSON.stringify(data.field), {
      title: '最终的提交信息'
    })
    return false;
  });

  //表单赋值
  layui.$('#LAY-component-form-setval').on('click', function () {
    form.val('example', {
      "username": "贤心" // "name": "value"
      , "password": "123456"
      , "interest": 1
      , "like[write]": true //复选框选中状态
      , "close": true //开关状态
      , "sex": "女"
      , "desc": "我爱 layui"
    });
  });

  //表单取值
  layui.$('#LAY-component-form-getval').on('click', function () {
    var data = form.val('example');
    alert(JSON.stringify(data));
  });

});