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

    //  初始化表单
    var layuiTable = common.layuiTable.render(layui.table,{
        elem: '#data_entity_table'
        ,url: '/device/time/strategy/pagingQuery'
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
            { field: 'name', title: '策略名称',align:'center',width:200,sort: true }
            , {field:'endDate',title: '使用时间',align:'center',width:300,sort:true,templet:function(d){
                        var type=d.type;
                        if(type==2){
                            //获取日期范围
                            var beginDate=layui.util.toDateString(d.beginDate, 'yyyy-MM-dd');
                            var endDate=layui.util.toDateString(d.endDate, 'yyyy-MM-dd');
                            return beginDate+" ~ "+endDate;
                        }else{
                            //获取星期
                            var weekDays="";
                            var workingDay=d.workingDay;
                            if(workingDay){
                                var dayArray=workingDay.split(",");
                                for( var i in dayArray){
                                    var week=dayArray[i];
                                    if(i==0){
                                        weekDays+=getWeekDay(week);
                                    }else{
                                        weekDays+=","+getWeekDay(week);
                                    }
                                }
                            }
                            return weekDays;
                        }
                }}
            , { field: 'remark',title: '备注',align:'center'}
            , { title: '操作',width:180, align: 'center',toolbar: '#table-operate'}
        ]]
    });

    //排序
    layui.table.on('sort(data_table_event)',function (obj) {
        reloadTable(obj);
    });
    
    //  重新加载表格
    var reloadTable = function(obj) {
        layuiTable.reload({
            //  设定异步数据接口的额外参数
            where: {
                name            :   $('#s_name').val(),
                //排序
                sortField   :   obj?obj.field:'',
                sortOrder   :   obj?obj.type:'',
            }
            //  重新从第1页开始
            ,page: {
                curr: 1
            },
            initSort:obj,
        });
    };

    //  操作栏事件绑定
    layui.table.on('tool(data_table_event)',function (obj) {
        switch (obj.event) {
            //  修改
            case 'edit':
                common.openDefaultIframe({
                    title   :   '修改时间策略',
                    content :   '/device/time/strategy/edit/' + obj.data.id
                });
                break;
            //  删除
            case 'delete':
                layer.confirm('是否确认删除?', {icon: 7, title:'提示'},function(index){
                    $.ajax({
                        url     :   '/device/time/strategy/delete/' + obj.data.id,
                        success :   function(result){
                            if (result.data) {
                                reloadTable();
                            }else {
                                layer.msg("删除失败");
                            }
                        }
                    });
                    layer.close(index);
                });
                break;
            default:
                break;
        }
    });
    /**
     * 获取星期几
     * @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;
        }
    }

    //监听查询
    form.on('submit(query)', function(){
        reloadTable();
    });

    //监听重置
    form.on('submit(reset)', function(){
        $('#s_name').val("");
        reloadTable();
    });

    //监听策略添加
    form.on('submit(add)', function(){
        common.openDefaultIframe({
            title   :   '添加时间策略',
            content :   '/device/time/strategy/add'
        });
    });
});