layui.extend({
    common  : 'js/common',
    // accessUserDeviceSelect: 'js/device/access-user-device-select',
    pushType: 'layui/lay/mymodules/push-type',
}).use(['jquery','pushType','common','table','form'],function() {
    var $ = layui.jquery;
    var pathParams = getLocationUrlParams();
    var cId = pathParams.cId
    //  引入公共模块
    var common = layui.common;

    //  初始化发布类型
    layui.pushType.init({height: 'full-235'});

    //  点击取消事件
    $('button[type="reset"]').on('click', function () {
        //  关闭当前页面弹窗
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });

    //  提交表单
    layui.form.on('submit(entity_submit_event)', function (data) {
        var selectedData = layui.pushType.getSelectedData();
        if (selectedData.list.length === 0) {
            var targetName = "小区";
            if (selectedData.type == 1) {
                targetName = "小区";
            } else if (selectedData.type == 2) {
                targetName = "单元";
            } else if (selectedData.type == 3) {
                targetName = "设备";
            }
            layer.alert("请选择发布的" + targetName);
            return;
        }
        //var body = data.field
        var accessUserCardPushDevicesVO = {}
        accessUserCardPushDevicesVO.cardId = cId
        accessUserCardPushDevicesVO.enable = true
        accessUserCardPushDevicesVO.pushType = selectedData.type
        accessUserCardPushDevicesVO.busiIdList = selectedData.list
        $.ajax({
            url : '/estate/access/sendUserCard',
            type : 'POST',
            contentType: "application/json;charset=utf-8",
            data : JSON.stringify(accessUserCardPushDevicesVO)
        }).then(function (result) {
            callbackFn(result)
        });
        // common.sendAsyncRequest("/estate/access/sendUserFaceImage", 'POST', accessUserPushDeviceVO, callbackFn);
    });
    /**
     * 获取星期几
     * @param weekDay
     * @returns {string}
     */
    var getWeekDay = function (weekDay) {
        switch (weekDay) {
            case "1":
                return "星期一"
                break;
            case "2":
                return "星期二"
                break;
            case "3":
                return "星期三"
                break;
            case "4":
                return "星期四"
                break;
            case "5":
                return "星期五"
                break;
            case "6":
                return "星期六"
                break;
            case "7":
                return "星期七"
                break;
            default:
                return "";
                break;
        }
    };
    //回调函数
    function callbackFn(result) {
        if (result.data) {
            closeFn();
        } else {//添加失败
            layer.msg("设置失败");
        }
    };

//关闭事件
    function closeFn() {
        layer.msg("设置成功", {
            time: 500,
            shade: [0.1, '#000', true]
        }, function () {
            //获取当前弹窗出层的名字
            var index = parent.layer.getFrameIndex(window.name);
            //关闭弹窗层
            parent.layer.close(index);
            // 父页面刷新
            parent.location.reload();
        });
    };

    //从当前页面的url地址中获取参数数据
    function getLocationUrlParams () {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    };
});
