layui.extend({
    common  : 'js/common',
    accessDeviceSelect: 'js/device/access-device-select',
}).use(['jquery','accessDeviceSelect','laydate','common','table','form',],function() {
    var $ = layui.jquery;
    var pathParams = getLocationUrlParams();
    var id = pathParams.id
    //  引入公共模块
    var common = layui.common;

    var laydate = layui.laydate;

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

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

    //日期时间范围
    laydate.render({
        elem: '#push_date'
        ,type: 'datetime'
        ,range: true
        ,done: function(value, date, endDate){
            var dateArray = value.split(" - ");
            $("#begin_date").val(dateArray[0]);
            $("#end_date").val(dateArray[1]);
        }
    });

    //  提交表单
    layui.form.on('submit(push)', function (data) {
        common.timer($('button[lay-filter="push"]'));
        var selectedData = layui.accessDeviceSelect.getSelectedData();
        if (selectedData.list.length === 0) {
            layer.alert("请选择设备");
            return;
        }
        if (data.field.beginDate == "" || data.field.beginDate == null || data.field.endDate == "" || data.field.endDate == null){
            layer.alert("请选择下发有效期");
            return;
        }
        data.field.operType = 1;
        data.field.pushType = selectedData.type;
        data.field.pushMaterial = selectedData.pushMaterial;
        data.field.deviceIdList = selectedData.list.join(",");
        if(data.field.userType){
            if(data.field.userType == 0){
                common.sendAsyncRequest('/estate/access/accessDevicesManualPush','POST',data.field,callbackFn);
            }else if(data.field.userType == 4){
                common.sendAsyncRequest('/estate/serverUser/accessDevicesManualPush','POST',data.field,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;
    };
});
