@extends('admin.master')

@section('content')
    <div class="row">
        <div class="col-lg-12">
            <!-- 指定新增的端點 -->
            <form id="EditForm" class="form-horizontal" method="post"
                  action="{{ url('/backend/dataManagement/urlManagement/store') }}">
                {{ csrf_field() }}
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <!-- 更改文案 -->
                        <h4 class="panel-title">{{ ($operdata == "") ? "新增" : "修改" }}網址</h4>
                    </div>
                    <div class="panel-body">
                        <div>
                            <!-- 表格本體 -->
                            <table class="table" cellspacing="0" id="DetailsView1" style="border-collapse:collapse;">
                                <tbody>
                                <!-- 欄位：ID -->
                                @if ($operdata == "")
                                    <!-- Insert Mode -->
                                    <input type="hidden" name="mode" value="insert"/>
                                @else
                                    <!-- Edit Mode -->
                                    <input type="hidden" name="mode" value="edit"/>
                                    <tr>
                                        <td class="col-lg-2">ID</td>
                                        <td>
                                            <input name="serno" type="hidden" value="{{ $operdata['serno'] }}"
                                                   id="serno"/>
                                            {{ $operdata['serno'] }}
                                        </td>
                                    </tr>
                                @endif
                                <!-- 欄位：活動名稱 -->
                                <!-- ALL Mode -->
                                <tr>
                                    <td class="header-require col-lg-2">活動名稱</td>
                                    <td>
                                        <div class="col-lg-3 nopadding">
                                            @if ($operdata == "")
                                                <input name="title" type="text" value="" maxlength="100" id="title"
                                                       class="form-control">
                                            @else
                                                <input name="title" type="text" value="{{ $operdata['title'] }}"
                                                       maxlength="20" id="title" class="form-control">
                                            @endif
                                            <label class="error" for="title"></label>
                                        </div>
                                    </td>
                                </tr>
                                <!-- 欄位：網址 -->
                                <!-- ALL Mode -->
                                <tr>
                                    <td class="header-require col-lg-2">網址</td>
                                    <td>
                                        <div class="col-lg-3 nopadding">
                                            @if ($operdata == "")
                                                <input name="url" type="text" value="" maxlength="100" id="url"
                                                       class="form-control">
                                            @else
                                                <input name="url" type="text" value="{{ $operdata['url'] }}"
                                                       maxlength="1024" id="url" class="form-control">
                                            @endif
                                            <label class="error" for="url"></label>
                                        </div>
                                    </td>
                                </tr>
                                <!-- 欄位：短網址 -->
                                @if ($operdata == "")
                                    <!-- Insert Mode -->
                                @else
                                    <!-- Edit Mode -->
                                    <input type="hidden" name="mode" value="edit"/>
                                    <tr>
                                        <td class="col-lg-2">短網址</td>
                                        <td>
                                            {{ $operdata['code'] }}
                                        </td>
                                    </tr>
                                @endif
                                <!-- 欄位：備註 -->
                                <!-- ALL Mode -->
                                <tr>
                                    <td class="header-require col-lg-2">備註</td>
                                    <td>
                                        <div class="col-lg-3 nopadding">
                                            @if ($operdata == "")
                                                <input name="memo" type="text" value="" maxlength="100" id="memo"
                                                       class="form-control">
                                            @else
                                                <input name="memo" type="text" value="{{ $operdata['memo'] }}"
                                                       maxlength="20" id="memo" class="form-control">
                                            @endif
                                            <label class="error" for="memo"></label>
                                        </div>
                                    </td>
                                </tr>
                                <!-- 欄位：有效否 -->
                                <!-- ALL Mode -->
                                <tr>
                                    <td class="header-require col-lg-2">有效否</td>
                                    <td>
                                        <div class="col-lg-3 nopadding">
                                            @if ($operdata == "")
                                                <input type="checkbox" id="valid" name="valid" checked>
                                            @else
                                                <input type="checkbox" id="valid"
                                                       name="valid" {{ $operdata['valid'] == 1 ? "checked" : "" }}>
                                            @endif
                                        </div>
                                    </td>
                                </tr>
                                <!-- 下控制按鈕 -->
                                <tr>
                                    <td>&nbsp;</td>
                                    <td>
                                        <div style="text-align: right">
                                        @if ($operdata == "")
                                            <!-- Insert Mode -->
                                                <input type="submit" name="btnUpdate_foot" value="新增"
                                                       id="btnUpdate_foot" class="btn btn-primary btn-xs"
                                                       onclick="submitForm();">
                                        @else
                                            <!-- Edit Mode -->
                                                <input type="submit" name="btnUpdate_foot" value="修改"
                                                       id="btnUpdate_foot" class="btn btn-primary btn-xs"
                                                       onclick="submitForm();">
                                            @endif
                                            <input type="button" name="btnBackTo2_foot" value="返回" id="btnBackTo2_foot"
                                                   class="btn btn-default btn-xs">
                                        </div>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <!-- panel-body -->
                </div>
            </form>
        </div>
    </div>
@endsection

@section('extjs')
    <script>
        $(document).ready(function () {
            $("#btnBackTo2").click(function () {
                // 上方的返回列表按鈕觸發
                location.href = '{{ url('backend/dataManagement/urlManagement') }}';
            });
            $("#btnBackTo2_foot").click(function () {
                // 下方的返回列表按鈕觸發
                location.href = '{{ url('backend/dataManagement/urlManagement') }}';
            });
            // 初始化需要偵錯的表格
            $('#EditForm').validate();
            // 正規表達驗證初始化
            $.validator.addMethod(
                "regex",
                function (value, element, regexp) {
                    var re = new RegExp(regexp);
                    return this.optional(element) || re.test(value);
                }
            );
            // 各欄位
            $('#title').rules("add", {
                required: true,
                minlength: 1,
                maxlength: 20,
                messages: {
                    required: "活動名稱 長度請介於 1-20",
                    minlength: "活動名稱 長度請介於 1-20",
                    maxlength: "活動名稱 長度請介於 1-20"
                }
            });
            $('#url').rules("add", {
                required: true,
                url: true,
                minlength: 1,
                maxlength: 1024,
                messages: {
                    required: "網址 長度請介於 1-1024",
                    url: "網址 格式錯誤",
                    minlength: "網址 長度請介於 1-1024",
                    maxlength: "網址 長度請介於 1-1024"
                }
            });
            $('#memo').rules("add", {
                required: false,
                minlength: 0,
                maxlength: 20,
                messages: {
                    required: "備註 長度請介於 0-20",
                    minlength: "備註 長度請介於 0-20",
                    maxlength: "備註 長度請介於 0-20"
                }
            });
        });

        //提交與取消按鈕
        function submitForm() {
            if (!!($("#EditForm").valid()) === false) {
                return false;
            } else {
                $(document).ready(function () {
                    $.blockUI({
                        css: {
                            border: 'none',
                            padding: '15px',
                            backgroundColor: '#000',
                            '-webkit-border-radius': '10px',
                            '-moz-border-radius': '10px',
                            opacity: .5,
                            color: '#FFF'
                        }
                    });
                });
            }
            $(document).ready(function () {
                $("#EditForm").submit();
            });
        }

        // 活動名稱補完
        $(function () {
            var availableTags = [
                @foreach($titles as $data)
                    "{{ $data['title'] }}",
                @endforeach
                ""
            ];
            $("#title").autocomplete({
                source: availableTags
            });
        });

        function cancelValidate() {
            $("#EditForm").validate().cancelSubmit = true;
        }
    </script>
@endsection
