@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="export"><strong>匯出</strong></button>
                                    <button class="btn btn-darkblue btn-xs" id="jggclear"><strong>清空玩家九宮格</strong></button>
                                    <button class="btn btn-darkblue btn-xs" id="gpclear"><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">
                                <!-- 玩家 LINE ID -->
                                <div class="form-group col-xs-12 col-sm-12 col-lg-12">
                                    <label for="lineId">玩家 LINE ID</label>
                                    <input type="text" class="form-control input-sm" id="lineId" maxlength="50">
                                </div>
{{--                                <!-- 玩家名稱 -->--}}
{{--                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">--}}
{{--                                    <label for="lineUserId">玩家名稱</label>--}}
{{--                                    <input type="text" class="form-control input-sm" id="userName" maxlength="100">--}}
{{--                                </div>--}}
                                <!-- 建立時間範圍 from -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="createDateStart">建立時間範圍(起)</label>
                                    <input type="text" class="form-control input-sm" id="createDateStart"
                                           maxlength="10">
                                </div>
                                <!-- 建立時間範圍 to -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="createDateFinal">建立時間範圍(迄)</label>
                                    <input type="text" class="form-control input-sm" id="createDateFinal"
                                           maxlength="10">
                                </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>ID</th>
                                    <th>玩家 LINE ID</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": "tplayerManagement/grid",
                "paging": true,
                "ordering": true,
                "info": true,
                "order": [[0, "desc"]],
                "stateSave": true,
                "pagingType": "full",
                "bFilter": true,
                "aoColumnDefs": [{
                    'bSortable': false,
                    'aTargets': [] //不想參加排序的欄位，可指定多個，逗號分隔
                }]
            });
            // 從網址參觸發搜尋
            custom_search();
            $('#GridView1_filter').hide();
        });

        //客製化搜尋欄位
        function custom_search() {
            $('#GridView1').DataTable()
                .column(1).search($('#lineId').val())
                // .column(2).search($('#userName').val())
                .column(2).search($('#createDateStart').val() + "\n" + $('#createDateFinal').val())
            ;
            $('#GridView1').dataTable().fnDraw(true);
        }

        // 匯出報告
        $("#export").click(function () {
            //加個讀取中畫面
            $.blockUI({
                css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#000',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#FFF'
                }
            });
            var http = new XMLHttpRequest();
            http.responseType = 'blob';
            var header;
            var blob;
            var url = 'tplayerManagement/export';
            var params =
                ''
                + 'lineId=' + $('#lineId').val()
                // + '&userName=' + $('#userName').val()
                + '&createDateStart=' + $('#createDateStart').val()
                + '&createDateFinal=' + $('#createDateFinal').val()
            ;
            http.open("POST", url, true);
            http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            http.onreadystatechange = function () {//Call a function when the state changes.
                if (http.readyState == 4 && http.status == 200) {
                    var filename = "";
                    var disposition = http.getResponseHeader('Content-Disposition');
                    if (disposition && disposition.indexOf('attachment') !== -1) {
                        var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                        var matches = filenameRegex.exec(disposition);
                        if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                    }
                    var type = http.getResponseHeader('Content-Type');
                    blob = new Blob([http.response], {type: type, endings: 'native'});
                    var URL = window.URL || window.webkitURL;
                    var downloadUrl = URL.createObjectURL(blob);
                    var a = document.createElement("a");
                    a.href = downloadUrl;
                    a.download = filename;
                    document.body.appendChild(a);
                    a.click();
                }
                //解除鎖定
                $.unblockUI();
            }
            http.send(params);
            return false;
        });

        // 清空玩家吉點
        $("#jggclear").click(function () {
            var ask = confirm("此動作不可回復，確定嗎？");
            if (ask) {
                $.ajax({
                    url: 'tplayerManagement/jggclear',
                    method: 'GET',
                    success: function (response) {
                        alert('清除成功');
                    },
                    error: function () {
                        alert('清除成功，請稍後再試');
                    }
                });
            }
            $('#GridView1').dataTable().fnDraw(true);
            return false;
        });

        // 清空玩家吉點
        $("#gpclear").click(function () {
            var ask = confirm("此動作不可回復，確定嗎？");
            if (ask) {
                $.ajax({
                    url: 'tplayerManagement/gpclear',
                    method: 'GET',
                    success: function (response) {
                        alert('清除成功');
                    },
                    error: function () {
                        alert('清除成功，請稍後再試');
                    }
                });
            }
            $('#GridView1').dataTable().fnDraw(true);
            return false;
        });

        //日期選擇器
        $('#createDateStart').datepicker({
            dateFormat: 'yy-mm-dd'
        });
        $('#createDateFinal').datepicker({
            dateFormat: 'yy-mm-dd'
        });

        setInterval(function () {
            $('[data-toggle="popover"]').popover();
        }, 1000);

    </script>
@endsection
