@extends('admin.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group ">
                {{--@can('message.activity.destroy')
                    <button class="layui-btn layui-btn-sm layui-btn-danger" id="listDelete">更改状态</button>
                @endcan--}}
                @can('message.activity.create')
                    <a class="layui-btn layui-btn-sm" href="{{ route('admin.activity.create') }}">添加</a>
                @endcan
                <button class="layui-btn layui-btn-sm" id="memberSearch">搜索</button>
                <button class="layui-btn layui-btn-sm" id="memberSearchOn">上线</button>
                <button class="layui-btn layui-btn-sm" id="memberSearchOff">下线</button>
            </div>
            <div class="layui-form">

                <div class="layui-input-inline">
                    <input type="text" name="activity_title" id="activity_title" placeholder="活动主题" class="layui-input">
                </div>

            </div>
        </div>

        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="activity_delete">
                @{{ d.activity_delete === 0 ? '<p style="color: lawngreen">上线</p>' : '<p style="color: red">下线</p>' }}
            </script>
            {{--<script type="text/html" id="info">
                <div>
                    <a style="cursor:pointer; color: #4141ef;" onclick="showInfo()">查看详情</a>
                    <div class="layui-layer layui-layer-page layui-layer-prompt" id="layui-layer1" type="page" times="1"
                         showtime="0" contype="string" style="display: none">
                        <div class="layui-layer-title" style="cursor: move;">活动详情：@{{ d.activity_title}}</div>
                        <div id="" class="layui-layer-content">
                            <p>
                                活动内容：
                            </p>
                            <p style="width: 100%; height: 70px; padding: 0 20px 0 20px;">
                                @{{ d.activity_content}}
                            </p>
                            <p>
                                活动数据：
                            </p>
                            <p style="height: 70px; padding: 0 20px 0 20px;">
                                @{{ d.activity_data}}
                            </p>
                        </div>
                        <span class="layui-layer-setwin">
                            <a class="layui-layer-ico layui-layer-close layui-layer-close1" href="javascript:;" onclick="hideInfo()">
                            </a>
                        </span>
                    </div>
                </div>
            </script>--}}
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('message.activity.edit')
                        <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    @endcan
                    @can('message.activity.destroy')
                        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">更改状态</a>
                    @endcan
                </div>
            </script>
            {{--<script type="text/html" id="avatar">
                <a href="@{{d.avatar}}" target="_blank" title="点击查看"><img src="@{{d.avatar}}" alt="" width="28" height="28"></a>
            </script>--}}
        </div>
    </div>
@endsection

@section('script')
    @can('message.activity')
        <script>
            /*function showInfo() {
                $("#layui-layer1").css({"z-index":"19891015", "top":"49px", "left":"378.5px", "display": "block", "width": "500px"});
            }
            function hideInfo() {
                $("#layui-layer1").css({"display": "none"});
            }*/
            layui.use(['layer','table','form'],function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                //用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable'
                    ,height: 500
                    ,url: "{{ route('admin.activity.data') }}" //数据接口
                    ,where:{model:"activity"}
                    ,page: true //开启分页
                    ,cols: [[ //表头
                        {checkbox: true,fixed: true}
                        ,{field: 'activity_title', title: '活动主题'}
                        ,{field: 'activity_ico', title: '活动图标', templet:function (d) { if (!d.activity_ico.length) {return ''}else{ return '<a href="'+d.activity_ico+'" target="_blank"><img src="'+d.activity_ico+'"></a>'}}}
                        ,{field: 'activity_content', title: '活动内容'}
                        ,{field: 'activity_data', title: '活动数据'}
                        ,{field: 'activity_beginTime', title: '活动开始'}
                        ,{field: 'activity_endTime', title: '活动结束'}
                        ,{field: 'activity_delete', title: '状态', width: 60, toolbar: '#activity_delete'}
                        ,{field: 'activity_money', title: '购买金额'}
                        ,{field: 'info', title: '详情', toolbar: '#info'}

                        ,{fixed: 'right', width: 120, align:'center', toolbar: '#options'}
                    ]]
                });

                //监听工具条
                table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        ,layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent === 'del'){
                        layer.confirm('确认更改状态吗？', function(index){
                            $.post("{{ route('admin.activity.destroy') }}",{_method:'delete',ids:[data.activity_identity],status:data.activity_delete},function (result) {
                                if (result.code==0){
                                    var activity_title = $("#activity_title").val();
                                    dataTable.reload({
                                        where:{activity_title:activity_title},
                                        page:{curr:1}
                                    })
                                }
                                layer.close(index);
                                layer.msg(result.msg)
                            });
                        });
                    } else if(layEvent === 'edit'){
                        location.href = '/admin/activity/'+data.activity_identity+'/edit';
                    }
                });

                //按钮批量删除
                $("#listDelete").click(function () {
                    var ids = []
                    var hasCheck = table.checkStatus('dataTable')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length>0){
                        $.each(hasCheckData,function (index,element) {
                            ids.push(element.activity_identity)
                        })
                    }
                    if (ids.length>0){
                        layer.confirm('确认更改状态吗？', function(index){
                            $.post("{{ route('admin.activity.destroy') }}",{_method:'delete',ids:ids},function (result) {
                                if (result.code==0){
                                    dataTable.reload()
                                }
                                layer.close(index);
                                layer.msg(result.msg)
                            });
                        })
                    }else {
                        layer.msg('请选择更改状态项')
                    }
                })
                //搜索
                $("#memberSearch").click(function () {
                    var activity_title = $("#activity_title").val();
                    dataTable.reload({
                        where:{activity_title:activity_title},
                        page:{curr:1}
                    })
                })
                //搜索
                $("#memberSearchOn").click(function () {
                    var activity_title = $("#activity_title").val();
                    dataTable.reload({
                        where:{activity_delete:0, activity_title:activity_title},
                        page:{curr:1}
                    })
                })
                //搜索
                $("#memberSearchOff").click(function () {
                    var activity_title = $("#activity_title").val();
                    dataTable.reload({
                        where:{activity_delete:1, activity_title:activity_title},
                        page:{curr:1}
                    })
                })
            })
        </script>
    @endcan
@endsection



