@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>
                                    <a class="btn btn-darkblue btn-xs" href="urlManagement/create">
                                        <strong>新增</strong>
                                    </a>
                                </li>
                                <li>
                                    <button class="btn btn-darkblue btn-xs" id="export"><strong>匯出</strong></button>
                                </li>
                                <li>
                                    <input type="text" id="copy_link" style="display: inline-block;">
                                </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">
                                <!-- 活動名稱 -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="title">活動名稱</label>
                                    <select class="form-control input-sm" id="title">
                                        @foreach($titles as $data)
                                            <option value="{{ $data['title'] }}">{{ $data['title'] }}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <!-- 備註 -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="memo">備註</label>
                                    <input type="text" class="form-control input-sm" id="memo" maxlength="20">
                                </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>
                                <!-- 有效否 -->
                                <div class="form-group col-xs-12 col-sm-6 col-lg-6">
                                    <label for="valid">有效否</label>
                                    <select class="form-control input-sm" id="valid">
                                        @foreach($valids as $data)
                                            <option value="{{ $data['id'] }}">{{ $data['text'] }}</option>
                                        @endforeach
                                    </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>ID</th>
                                    <th>活動名稱</th>
                                    <th>網址</th>
                                    <th>短代碼</th>
                                    <th>備註</th>
                                    <th>造訪次</th>
                                    <th>有效否</th>
                                    <th>編輯</th>
                                    <th>QRCode</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": "urlManagement/grid",
                "paging": true,
                "ordering": true,
                "info": true,
                "order": [[0, "desc"]],
                "stateSave": true,
                "pagingType": "full",
                "bFilter": true,
                "aoColumnDefs": [{
                    'bSortable': false,
                    'aTargets': [2, 3, 6, 7, 8] //不想參加排序的欄位，可指定多個，逗號分隔
                }]
            });
            // 從網址參觸發搜尋
            custom_search();
            $('#GridView1_filter').hide();
        });

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

        setInterval(function () {
            // 複製網址
            $("button[id^='url_copy_']").unbind("click");
            $("button[id^='url_copy_']").bind("click", function () {
                var copy_link = document.getElementById("copy_link");
                copy_link.value = $(this).attr('url');
                copy_link.select();
                document.execCommand("copy");
                return false;
            });
        }, 500);

        // 匯出報告
        $("#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 = 'urlManagement/export';
            var params =
                'title=' + $('#title').val()
                + '&memo=' + $('#memo').val()
                + '&createDateStart=' + $('#createDateStart').val()
                + '&createDateFinal=' + $('#createDateFinal').val()
                + '&valid=' + $('#valid').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;
        });

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