@extends('admin.master')

@section('content')
    <div class="row">
        <div class="col-lg-12">
            <div class="contentpanel">
                <form id="listForm" method="post">
                    <!-- 功能按鈕(新增/批量處理等等) -->
                    <div class="row" style="margin-bottom: 5px;">
                        <div class="col-lg-12">
                            <ol class="headermenu">
                                <li>
                                    <button class="btn btn-darkblue btn-xs" id="btnNew"><strong>新增</strong></button>
                                </li>
                                <li>
                                    <button class="btn btn-darkblue btn-xs" id="btnArchive"><strong>封存</strong></button>
                                </li>
                                <li>
                                    <button class="btn btn-darkblue btn-xs" id="btnUnarchive"><strong>解封</strong></button>
                                </li>
                                <li>
                                    <button class="btn btn-darkblue btn-xs" id="btnClear"><strong>清除收件個資</strong></button>
                                </li>
                            </ol>
                        </div>
                    </div>
                    <!-- 搜尋段 -->
                    <div class="panel panel-default">
                        <div class="panel-heading" data-toggle="collapse" data-target="#search_content">
                            <h3 class="panel-title">活動列表</h3>
                        </div>
                        <div id="search_content" class="collapse in">
                            <div class="panel-body">
                                <!-- 活動時間範圍 from -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="eventDateStart">活動時間範圍(起)</label>
                                    <input type="text" class="form-control input-sm" id="eventDateStart"
                                           maxlength="10">
                                </div>
                                <!-- 活動時間範圍 to -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="eventDateFinal">活動時間範圍(迄)</label>
                                    <input type="text" class="form-control input-sm" id="eventDateFinal"
                                           maxlength="10">
                                </div>
                                <!-- 活動名稱 -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="name">活動名稱</label>
                                    <input type="text" class="form-control input-sm" id="name" maxlength="16">
                                </div>
                                <!-- 狀態 -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="valid">狀態</label>
                                    <select class="form-control input-sm" id="archive">
                                        @foreach($archive as $k => $data)
                                            <option value="{{ $k }}">{{ $data }}</option>
                                        @endforeach
                                        <option value="">全部</option>
                                    </select>
                                </div>
                                <!-- Search -->
                                <div class="form-group col-xs-12 col-sm-12 col-lg-12">
                                    <a class="btn btn-success btn-xs" onclick="javascript: custom_search();">
                                        <div class="glyphicon glyphicon-search"></div>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <hr class="search-hr"/>
                    <!-- 列表段 -->
                    <div class="row">
                        <div class="col-lg-12">
                            <table id="GridView1" class="table table-striped table-bordered" cellspacing="0">
                                <thead>
                                <tr>
                                    <th></th>
                                    <th>ID</th>
                                    <th>活動名稱</th>
                                    <th>獎項數量</th>
                                    <th>起始日</th>
                                    <th>結束日</th>
                                    <th>建立時間</th>
                                    <th>修改時間</th>
                                    <th>修改人</th>
                                </tr>
                                </thead>
                            </table>
                        </div>
                    </div>
                </form>
                <!-- row -->
            </div>
        </div>
    </div>
@endsection

@section('extjs')
    <script>
        $(document).ready(function () {
            var table = $('#GridView1').dataTable({
                "scrollX": true,
                "processing": true,
                "serverSide": true,
                "ajax": "eventManagement/grid",
                "paging": true,
                "ordering": true,
                "info": true,
                "order": [[1, "desc"]],
                "stateSave": true,
                "pagingType": "full",
                "bFilter": true,
                "aoColumnDefs": [{
                    'bSortable': false,
                    'aTargets': [0] //不想參加排序的欄位，可指定多個，逗號分隔
                }]
            });
            // 從網址參觸發搜尋
            custom_search();
            $('#GridView1_filter').hide();
        });

        //客製化搜尋欄位
        function custom_search() {
            $('#GridView1').DataTable()
                .column(1).search($('#name').val())
                .column(2).search($('#eventDateStart').val() + "\n" + $('#eventDateFinal').val())
                .column(3).search($('#archive').val())
            ;
            $('#GridView1').dataTable().fnDraw(true);
        }

        // 新增
        $("#btnNew").click(function () {
            location.href = 'eventManagement/create';
            return false;
        });

        // 封存
        $("#btnArchive").click(function () {
            var ids = $("input[name^='grp']").map(function (idx, ele) {
                if ($(ele).is(":checked")) {
                    return $(ele).attr('name').replace(/\D/g, '');
                }
            }).get();
            $.ajax({
                url: 'eventManagement/archive',
                method: 'POST',
                dataType: 'JSON',
                data: {
                    grp: ids
                },
                success: function (response) {
                    alert('封存成功');
                },
                error: function () {
                    alert('封存失敗，請稍後再試');
                }
            });
            $('#GridView1').dataTable().fnDraw(true);
            return false;
        });

        // 解封
        $("#btnUnarchive").click(function () {
            var ids = $("input[name^='grp']").map(function (idx, ele) {
                if ($(ele).is(":checked")) {
                    return $(ele).attr('name').replace(/\D/g, '');
                }
            }).get();
            $.ajax({
                url: 'eventManagement/unarchive',
                method: 'POST',
                dataType: 'JSON',
                data: {
                    grp: ids
                },
                success: function (response) {
                    alert('解封成功');
                },
                error: function () {
                    alert('解封失敗，請稍後再試');
                }
            });
            $('#GridView1').dataTable().fnDraw(true);
            return false;
        });

        // 清除收件個資
        $("#btnClear").click(function () {
            var ids = $("input[name^='grp']").map(function (idx, ele) {
                if ($(ele).is(":checked")) {
                    return $(ele).attr('name').replace(/\D/g, '');
                }
            }).get();
            $.ajax({
                url: 'eventManagement/clear',
                method: 'POST',
                dataType: 'JSON',
                data: {
                    grp: ids
                },
                success: function (response) {
                    alert('清除成功');
                },
                error: function () {
                    alert('清除失敗，請稍後再試');
                }
            });
            $('#GridView1').dataTable().fnDraw(true);
            return false;
        });

        //日期選擇器
        $('#eventDateStart').datepicker({
            dateFormat: 'yy-mm-dd'
        });
        $('#eventDateFinal').datepicker({
            dateFormat: 'yy-mm-dd'
        });
    </script>
@endsection
