{include file="public/head"}
<div class="layui-fluid layui-rg-bg">
    <div class="layui-card">
        <div class="layui-card-header">
            <div class="layui-inline">
                <div class="layui-input-inline"><button class="layui-btn layui-btn-primary layui-btn-sm layui-btn-disabled countnum"></button></div>
            </div>
            <div class="layui-inline" style="float:right;">
                <div class="layui-input-inline"><a class="layui-btn layui-btn-primary layui-btn-sm refresh"><i class="layui-icon layui-icon-refresh-3"></i></a></div>
                <div class="layui-input-inline"><a class="layui-btn layui-btn-sm" onclick="openlayer('{:url("add")}','{:lang('add')}站点','xs')">{:lang('add')}站点</a></div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="data_table" class="layui-table" lay-filter="data_table"></table>
            {:pagedebug()}
        </div>
    </div>
</div>
{include file="public/foot"}
<script type="text/html" id="action">
    <div class="layui-input-inline"><a class="layui-btn layui-btn-xs" lay-event="edit"><i class='layui-icon layui-icon-edit'></i>{:lang("edit")}</a></div>
    {{# if(d.isdefault !== 1){ }}
    <div class="layui-input-inline"><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class='layui-icon layui-icon-delete'></i>{:lang("del")}</a></div>
    {{# } }}
    <div class="layui-input-inline"><a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="clear"><i class='layui-icon layui-icon-delete'></i>初始化</a></div>
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" lay-skin="primary" value="{{d.id}}" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="wapstatus">
    <input type="checkbox" name="wapstatus" lay-skin="primary" value="{{d.id}}" lay-filter="status" {{ d.wapstatus == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="isdefault">
    <input type="checkbox" name="isdefault" lay-skin="primary" value="{{d.id}}" lay-filter="status"  {{ d.isdefault == 1 ? 'checked disabled' : 'disabled' }}>
</script>
<script type="text/javascript">
    layui.use(['table','form','element'],function(){
        var table = layui.table, form = layui.form, $ = layui.jquery;
        loading = layer.load(1, {shade: [0.1, '#fff']});
        table.render({
            elem: '#data_table',
            url: '{:url("index")}',
            method: 'POST',
            cols: [[
            {field: 'id', align: 'center', title: 'ID',width:60},
            {field:'title', title:'站点名称', edit:'text'},
            {field:'alias', title:'别名', edit:'text'},
            {field:'isdefault', align: 'center', title: '主站', templet: '#isdefault'},
            {field:'status',align: 'center', title: 'PC站点', templet: '#status'},
            {field:'wapstatus',align: 'center', title: 'WAP站点', templet: '#wapstatus'},
            {field:'create_time', title:'创建时间',sort:true},
            {field:'end_time', title:'过期时间',sort:true},
            {field:'expired', align: 'center',width:80,title:'状态',templet:function(d){
                if(d.expired == 1) {
                    return '<font color="#009688"><i class="layui-icon layui-icon-triangle-r" style="float:left"></i><b>正常</b></font>';
                } else {
                    return '<font color="#FF5722"><i class="layui-icon layui-icon-close-fill" style="float:left"></i><b>过期</b></font>';
                }
            }},
            {field:'torder', width: 80, align: 'center', title:'{:lang("order")}', sort:true, edit:'text'},
            {toolbar: '#action', width: 200, align: 'center', title: '{:lang("act")}', fixed: 'right'}
            ]],
            limit: {:config('app.admin.pagelimit')},
            page: true,
            done: function (res) {
                $('.countnum').text('记录数：' + res.count + '条');
                layer.close(loading);
            }
        });
        //监听行双击事件
        table.on('rowDouble(data_table)', function(obj){
            var url = "{:url('edit')}?id="+obj.data.id;
            openlayer(url,'{:lang("edit")}','xs');
        });
        //点击事件
        table.on('tool(data_table)', function(obj) {
            var data = obj.data;
            var event = obj.event;
            if(event === 'del'){
                layer.confirm('确认{:lang("del")}站点('+ data.title +')吗？<br>将删除该站点所有内容！',{title:'{:lang("del")}'},function(){
                    table_ajax(data,'{:url("del")}',8);
                })
                return false;
            }else if(event === 'clear'){
                layer.confirm('初始化站点('+ data.title +')将清除所有站点内容包括栏目、动态字段，但不会删除模型字段。',{title:'确认初始化'},function(){
                    table_ajax({action:'clear',id:data.id},'{:url("del")}',8);
                })
                return false;
            }else if(event === 'edit'){
                var url = "{:url('edit')}?id="+data.id;
                openlayer(url,'{:lang("edit")}','xs');
            }
        });
        //监听单元格编辑
        table.on('edit(data_table)', function(obj){
            var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
            delete data.create_time;
            delete data.end_time;
            delete data.isdefault;
            if(field == 'torder') {
                table_ajax(data,'{:url("edit")}',10);
            } else {
                table_ajax(data,'{:url("edit")}',11);
            }
        });
        // 监听排序事件
        table.on('sort(data_table)', function(obj){
            var field = obj.field;
            var type = obj.type;
            table.reload('data_table',{
                initSort: obj
                ,where: {order:{field:field,type:type}}
                ,page:{curr:1}
            })
        });
        //监听开关操作
        form.on('checkbox(status)', function(obj){
            var data={};
            data[obj.elem.name] = obj.elem.checked ? 1 : 0;
            data['id'] = obj.value;
            data['list_switch'] = true;
            table_ajax(data,'{:url("edit")}',11);
        });
    })
</script>