layui.extend({
    common    :   'js/common'
}).use(['jquery','form','common','laydate','layer','util'],function(){
    var $ = layui.jquery;
    //  页面元素
    var $node = {
        //  取消按钮
        resetBtn        :   $('button[type="reset"]'),
        strategyDate        :   $("input[name='strategyDate']"),
        workingDay        :   $("input[name='workingDay']"),
        laydateTimeItem        :    $(".layui-form .laydate-time-item"),
        timeRangeDiv        :   $("#time-range"),
        datetimeRangeDiv        :    $("#datetime-range-id"),
        weekdayDiv        :    $("#weekday-id"),
        typeRadio        :    $("input[name='type']"),
        name        :    $("input[name='name']"),
        remark        :    $("textarea[name='remark']"),
        timeRangeTemplate        :    $("#time-range-template")
    };
    //  获取时间策略信息
    $.ajax({
        url     :   '/device/time/strategy/get/' + $('#entity_id').val(),
        success :   function(result){
            var data = result.data;
            if (data) {
                $node.name.val(data.name);
                $node.remark.text(data.remark);
                //设置时间类型
                $("input[name='type'][value='1']").attr("checked", data.type == 1 ? true : false);
                $("input[name='type'][value='2']").attr("checked", data.type == 2 ? true : false);
                displayWorkingDay(data.type);
                if(data.type==2){
                    //范围
                    $node.strategyDate.val(layui.util.toDateString(data.beginDate,"yyyy-MM-dd")+" - "+layui.util.toDateString(data.endDate,"yyyy-MM-dd"));
                }else{
                    var workingDays=data.workingDay.split(",");
                    //星期
                    for(var i in workingDays){
                        $("input[name='workingDay'][value='"+workingDays[i]+"']").attr("checked", true);
                    }
                }
                //时间段信息
                var timeStrategyConfList=data.timeStrategyConfList;
                for(var i in timeStrategyConfList){
                    var $strategyTime,$soundVolume,$screenLuminance,$fillLightLuminance;
                    if(i==0){
                        //时间段
                        $strategyTime=$($node.laydateTimeItem[0]).find("input[name='strategyTime']");
                        //声音
                        $soundVolume=$($node.laydateTimeItem[0]).find("input[name='soundVolume']");
                        //屏幕亮度
                        $screenLuminance=$($node.laydateTimeItem[0]).find("input[name='screenLuminance']");
                        //补光灯亮度
                        $fillLightLuminance=$($node.laydateTimeItem[0]).find("input[name='fillLightLuminance']");
                    }else{
                        //添加时间段
                        $node.timeRangeDiv.append($node.timeRangeTemplate.html());
                        $strategyTime=$($node.timeRangeDiv.find("input[name='strategyTime']")[i-1]);
                        $soundVolume=$($node.timeRangeDiv.find("input[name='soundVolume']")[i-1]);
                        $screenLuminance=$($node.timeRangeDiv.find("input[name='screenLuminance']")[i-1]);
                        $fillLightLuminance=$($node.timeRangeDiv.find("input[name='fillLightLuminance']")[i-1]);

                    }
                    //赋值
                    $strategyTime.val(layui.util.toDateString(timeStrategyConfList[i].beginTime,"HH:mm:ss")+" - "+layui.util.toDateString(timeStrategyConfList[i].endTime,"HH:mm:ss"));
                    $soundVolume.val(timeStrategyConfList[i].soundVolume);
                    $screenLuminance.val(timeStrategyConfList[i].screenLuminance);
                    $fillLightLuminance.val(timeStrategyConfList[i].fillLightLuminance);
                }


                layui.form.render();
            }else {
                $('#entity_id').val('');
            }
        }
    });
    //  绑定表单提交事件
    layui.form.on("submit(entity_submit_event)",function(data){
        //类型
        var type=data.field.type;
        if(type==2){
            //日期范围
            var strategyDate=$node.strategyDate.val();
            var strategyDates=strategyDate.split(" - ");
            data.field.beginDate=strategyDates[0];
            data.field.endDate=strategyDates[1];
        }else if(type==1){
            //选中的工作日
            var workingDay="";
            $("input[name='workingDay']:checked").each(function (i) {
                var value=$(this).val();
                if(i==0){
                    workingDay=value;
                }else{
                    workingDay+=","+value;
                }
            });
            data.field.workingDay=workingDay;
        }

        var strategyTimeParams="";
        //获取时间段信息
        $(".layui-form .laydate-time-item").each(function (i) {
            //时间段
            var strategyTime=$(this).find("input[name='strategyTime']").val();
            //声音
            var soundVolume=$(this).find("input[name='soundVolume']").val();
            if(!soundVolume){
                soundVolume="0";
            }
            //屏幕亮度
            var screenLuminance=$(this).find("input[name='screenLuminance']").val();
            if(!screenLuminance){
                screenLuminance="0";
            }
            //补光灯亮度
            var fillLightLuminance=$(this).find("input[name='fillLightLuminance']").val();
            if(!fillLightLuminance){
                fillLightLuminance="0";
            }
            if(i==0){
                strategyTimeParams=strategyTime+";"+soundVolume+";"+screenLuminance+";"+fillLightLuminance;
            }else{
                strategyTimeParams+=","+strategyTime+";"+soundVolume+";"+screenLuminance+";"+fillLightLuminance;
            }
        });
        data.field.strategyTimeParams=strategyTimeParams;
        $.ajax({
            url     :   '/device/time/strategy/edit',
            data    :   data.field,
            type    :   'POST',
            success :   function () {
                $node.resetBtn.click();
                parent.location.reload();
            }
        });
    });
    //日期范围选择
    layui.laydate.render({
        elem: 'input[name="strategyDate"]'
        ,type: 'date'
        ,range:true
    });
    //时间范围选择
    layui.laydate.render({
        elem: '#laydate-type-time-head'
        ,type: 'time'
        ,range:true
    });
    //  点击取消事件
    $node.resetBtn.on('click',function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    });
    //增加时间段
    $(document).on('click','button[type="add-time-range"]',function () {
        var  item=$node.timeRangeDiv.find(".layui-form-item");
        if(item.length>9){
            layer.msg("最多10条时间段");
            return;
        }
        $node.timeRangeDiv.append($node.timeRangeTemplate.html());
        //重新渲染一次时间控件
        $node.timeRangeDiv.find("input[name='strategyTime']").each(function () {
            layui.laydate.render({
                elem:this
                ,type: 'time'
                ,range:true
            });
        });
    });
    //删除时间段
    $(document).on('click','button[type="delete-time-range"]',function (obj) {
        $(this).parents(".layui-form-item").remove();
    });
    layui.form.on('radio(type-filter)', function(data){
        var value=data.value;
        displayWorkingDay(value);
    });
    var displayWorkingDay=function(type){
        if(type==2){
            $node.datetimeRangeDiv.show();
            $node.weekdayDiv.hide();
            $node.workingDay.removeAttr("lay-verify");
            $node.strategyDate.attr("lay-verify","required");
        }else{
            $node.datetimeRangeDiv.hide();
            $node.weekdayDiv.show();
            $node.workingDay.attr("lay-verify","checkboxMustBe");
            $node.strategyDate.removeAttr("lay-verify");
        }
    }
    //自定义验证规则
    layui.form.verify({
        checkboxMustBe:function(value) {
            if (!$node.workingDay.is(":checked")) {
                return "星期必选";
            }
        },
        typeRequired:function(value) {
            if (!$node.typeRadio.is(":checked")) {
                return "策略类型必选";
            }
        }
    });
});