layui.use(["index", "api", "tpl", "dialog"], function () {
  var $ = layui.$,
    table = layui.table,
    element = layui.element,
    form = layui.form,
    laypage = layui.laypage,
    admin = layui.admin;
  $('#demo1').on('click', function (e) {
    e.stopPropagation();
    layui.dialog.setIframeDialog("iframe", {
      src: "https://www.layui.com/",
      otherconfigStyle:
        'scrolling="no" frameborder=0',
    });
  });
  $('#demo2').on('click', function (e) {
    e.stopPropagation();
    layui.dialog.showOkMsgDialog({ msg: "this is ok!" });
  });
  $('#demo3').on('click', function (e) {
    e.stopPropagation();
    layui.dialog.setQrCodeDialog("qrcode", "生成二维码", {
      value: 'https://www.layui.com/',
    });
  });
  $('#demo4').on('click', function (e) {
    e.stopPropagation();
    addDialog();
  });
  $('#demo5').on('click', function (e) {
    e.stopPropagation();
    var data = { name: '我是编辑好的名称', type: 1 };
    editDialog(data);
  });

  function addDialog() {
    var templet = $('#formDialogTemplate').html()
    layui.laytpl(templet).render({}, function (html) {
      layer.open({
        type: 1,
        title: '创建',
        content: templet,
        area: [px2rem(520), px2rem(600)],
        btn: ['取消', '确认'],
        btnAlign: 'r',
        yes: function (index, layero) {
          layer.close(index)
          return false
        },
        btn2: function (index, layero) {
          var submit = $('#LAY-dialog-submit')
          var submitID = 'lay-dialog-submit'
          form.on('submit(' + submitID + ')', function (data) {
            var field = data.field //获取提交的字段
            console.log('表单 field:', field);
            var ADD_URL = '';
            admin.req({
              url: ADD_URL,
              data: field,
              success: function (res) {
                layer.close(index);
                layer.msg('成功');
              },
              error: function (res) {
                layer.msg('失败');
                return false;
              }
            })
            layer.close(index); //关闭弹层
          })
          submit.trigger('click')
          return false
        },
        success: function success() {
          layui.form.render('')
        }
      })
    })
  }
  function editDialog(data) {
    var templet = $('#formDialogTemplate').html()
    layui.laytpl(templet).render({}, function (html) {
      layer.open({
        type: 1,
        title: '编辑',
        content: templet,
        area: [px2rem(520), px2rem(600)],
        btn: ['取消', '确认'],
        btnAlign: 'r',
        yes: function (index, layero) {
          layer.close(index)
          return false
        },
        btn2: function (index, layero) {
          var submit = $('#LAY-dialog-submit')
          var submitID = 'lay-dialog-submit'
          form.on('submit(' + submitID + ')', function (data) {
            var field = data.field //获取提交的字段
            console.log('表单 field:', field);
            var UPDATE_URL = "";
            admin.req({
              url: UPDATE_URL,
              data: field,
              success: function (res) {
                layer.close(index);
                layer.msg('成功');
              },
              error: function (res) {
                layer.msg('失败');
                return false;
              }
            })
            layer.close(index) //关闭弹层
          })
          submit.trigger('click')
          return false
        },
        success: function success() {
          form.val('dialogForm', data)
          layui.form.render('')
        }
      })
    })
  }
});
