layui.extend({
    common    :   'js/common'
}).use(['jquery','layer','common','form','laydate'],function () {
    //  Jquery插件
    var $ = layui.jquery;
    //  弹窗插件
    var layer = layui.layer;
    //  引入表单插件
    var form = layui.form;
    //  引入公共模块
    var common = layui.common;

    var laydate = layui.laydate;

    //日期时间范围
    laydate.render({
        elem: '#card_date'
        ,type: 'date'
        ,range: true
        ,min: 0 //最小日期为当天
        ,done: function(value, date, endDate){
            var dateArray = value.split(" - ");
            $("#begin_date").val(dateArray[0] + ' 00:00:00');
            $("#end_date").val(dateArray[1] + ' 23:59:59');
        }
    });

    //监听按钮
    form.on('submit(submit_button)', function(data){
        common.sendAsyncRequest("/estate/accessCard/updateAccessCard",'POST',data.field,callbackFn);
    });

    //回调函数
    function callbackFn(result) {
        if(result.data){
            layer.msg("设置成功",{
                time: 500,
                shade : [0.1 , '#000' , true]
            },function () {
                //获取当前弹窗出层的名字
                var index = parent.layer.getFrameIndex(window.name);
                //关闭弹窗层
                parent.layer.close(index);
                // 父页面刷新
                parent.location.reload();
            });
        }else {//添加失败
            layer.msg("设置失败");
        }
    };

    //取消按钮
    $('#cancel_btn').on("click",function(){
        //获取当前弹窗出层的名字
        var index = parent.layer.getFrameIndex(window.name);
        //关闭弹窗层
        parent.layer.close(index);
    });

    var initDateFn = function () {
        var beginDate = $("#begin_date").val();
        var endDate =  $("#end_date").val();
        if(beginDate && endDate){
            $("#card_date").val(beginDate.split(' ')[0] + " - " + endDate.split(' ')[0]);
            // $("#card_date").val(beginDate + " - " + endDate);
        }
    };


    //  初始化
    (function () {
        //  加载所属房间
        initDateFn();
    })();

});