{include file="public/head"}
<div class="layui-fluid layui-rg-bg">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-xs12 layui-col-sm3 layui-col-md3 layui-col-lg2">
            <div class="layui-card" id="category_div">
                <div class="layui-card-header"><i class="layui-icon layui-icon-template-1"> </i>模型选择</div>
                <div class="layui-card-body">
                    <div id="modellist" style="width:100%;height:auto; min-height:250px;overflow-y:auto;"></div>
                </div>
            </div>
        </div>
        <div class="layui-col-xs12 layui-col-sm9 layui-col-md9 layui-col-lg10">
            <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 class="layui-input-inline" id="delsbutton"></div>
                        <div class="layui-input-inline"></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" id="addbutton"></div>
                    </div>
                </div>
                <div class="layui-card-body">
                    <h3 id="tabletitle">字段管理</h3>
                    <table id="data_table" class="layui-table" lay-filter="data_table"></table>
                    {:pagedebug()}
                </div>
            </div>
        </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>
</script>
<script type="text/html" id="require">
    {{# if((d.typeinfo.config).indexOf("require") >= 0){ }}
    <input type="checkbox" name="require" value="{{d.id}}" lay-skin="primary" lay-filter="status" {{ d.require == 1 ? 'checked' : '' }}>
    {{#  } else { }}
    --
    {{#  } }}
</script>
<script type="text/html" id="edit">
    {{# if((d.typeinfo.config).indexOf("is_edit") >= 0){ }}
    <input type="checkbox" name="is_edit" value="{{d.id}}" lay-skin="primary" lay-filter="status" {{ d.is_edit == 1 ? 'checked' : '' }}>
    {{#  } else { }}
    --
    {{#  } }}
</script>
<script type="text/html" id="show">
    {{# if((d.typeinfo.config).indexOf("is_show") >= 0){ }}
    <input type="checkbox" name="is_show" value="{{d.id}}" lay-skin="primary" lay-filter="status" {{ d.is_show == 1 ? 'checked' : '' }}>
    {{#  } else { }}
    --
    {{#  } }}
</script>
<script type="text/html" id="sort">
    {{# if((d.typeinfo.config).indexOf("is_sort") >= 0){ }}
    <input type="checkbox" name="is_sort" value="{{d.id}}" lay-skin="primary" lay-filter="status" {{ d.is_sort == 1 ? 'checked' : '' }}>
    {{#  } else { }}
    --
    {{#  } }}
</script>
<script type="text/html" id="search">
    {{# if((d.typeinfo.config).indexOf("is_search") >= 0){ }}
        {{ d.is_search == 0 ? '停用' : (d.is_search == 1 ? '模糊' : '精确') }}
    {{#  } else { }}
    --
    {{#  } }}
</script>
<script type="text/html" id="thumb">
    {{# if((['img','imgs','ueditor','ueditors','layedit']).indexOf(d.type) >= 0){ }}
    <input type="checkbox" name="thumb" value="{{d.id}}" lay-skin="primary" lay-filter="status" {{ d.thumb == 1 ? 'checked' : '' }}>
    {{#  } else { }}
    --
    {{#  } }}
</script>
<script type="text/html" id="water">
    {{# if((['img','imgs','ueditor','ueditors','layedit']).indexOf(d.type) >= 0){ }}
    <input type="checkbox" name="water" value="{{d.id}}" lay-skin="primary" lay-filter="status" {{ d.water == 1 ? 'checked' : '' }}>
    {{#  } else { }}
    --
    {{#  } }}
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="primary" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/javascript">
    layui.use(['table','form','element','tree'],function(){
        var table = layui.table, form = layui.form, $ = layui.jquery;
        var tree = layui.tree;
        loading = layer.load(1, {shade: [0.1, '#fff']});
        table.render({
            elem: '#data_table',
            url: '{:url("fieldslist")}',
            method: 'POST',
            even: true,
            skin:'row',
            cols: [[
            {type:'checkbox'},
            {field:'title', title:'字段名称', edit:'text'},
            {field:'name', title:'字段名'},
            {title:'字段类型',templet:function(d){
                return d.typeinfo.title;
            }},
            {title:'所属模型',templet:function(d){
                return d.cmsmodels.title;
            }},
            {width: 80, align:'center', title:'必填',templet: '#require'},
            {width: 80, align:'center', title:'可修改',templet: '#edit'},
            {width: 80, align:'center', title:'列表列',templet: '#show'},
            {field:'width',width: 80, title:'列宽', edit:'text'},
            {width: 90, align:'center', title:'列表排序',templet: '#sort'},
            {width: 80, align:'center', title:'搜索',templet: '#search'},
            {width: 80, align:'center', title:'缩略图',templet: '#thumb'},
            {width: 80, align:'center', title:'水印',templet: '#water'},
            {width: 80, align:'center', title: '启用', templet: '#status'},
            {field:'torder', width: 80, align:'center', title:'{:lang("order")}', sort:true, edit:'text'},
            {toolbar: '#action', width: 90, 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('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}
            })
        });
        // 栏目树
        tree.render({
            elem: '#modellist'  //绑定元素
            ,id:'modellist'
            ,onlyIconControl:true
            ,data:{:app\\admin\\model\\Cmsmodels::getModeltree()}
            ,click: function(obj){
                var cmsmodels_id = obj.data.id;
                $("#modellist").find('.layui-tree-txt').attr('style','');
                $("#tabletitle").text(obj.data.title+'字段管理');
                $("#addbutton").html("<a class='layui-btn layui-btn-sm' onclick=openlayer('{:url('addfields')}?cmsmodels_id="+cmsmodels_id+"','{:lang('add')}','xs')>{:lang('add')}"+obj.data.title+'字段'+"</a>");
                $("#delsbutton").html("<button class='layui-btn layui-btn-sm layui-btn-danger data_act' data-value='delfields' cmsmodels_id="+cmsmodels_id+">{:lang('dels')}</button>");
                obj.elem.find('.layui-tree-txt').eq(0).attr('style','color:#FF5722');
                table.reload('data_table',{
                    page:{curr:1},
                    where: {cmsmodels_id:cmsmodels_id}
                })
            }
        });
        //监听行双击事件
        table.on('rowDouble(data_table)', function(obj){
            var url = "{:url('editfields')}?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 === 'edit'){
                // 编辑
                var url = "{:url('editfields')}?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; //得到字段
            if(field == 'torder') {
                table_ajax(data,'{:url("editfields")}',10);
            } else {
                table_ajax(data,'{:url("editfields")}',11);
            }
        });
        //监听开关操作
        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("editfields")}',11);
        });
        // 批量删除
        $(document).on('click','.data_act',function(){
            var checkStatus = table.checkStatus('data_table'), data = checkStatus.data;
            var ids = [];
            var data_act = $(this).attr("data-value");
            var text = $(this).text();
            if(data_act == 'delfields') {
                $(data).each(function(k,v){
                    ids.push(v.id);
                });
                if(ids == '') {
                    layer.msg('请选择记录！');
                    return false;
                };
                var cmsmodels_id = $(this).attr('cmsmodels_id');
                layer.confirm('确定要'+text+'吗？',{title:text},function(){
                    table_ajax({data:data,cmsmodels_id:cmsmodels_id},'{:url("'+ data_act +'")}',10);
                });
            }
            return false;
        });
    })
</script>